function trim (s) {
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) s = s.substring(1,s.length);
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) s = s.substring(0,s.length-1);
  return s;
}

function isEmail (str) {
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }

  if (!supported) return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");

  return (!r1.test(str) && r2.test(str));
}

function checkMailForm (email_error, email_error_sender, email_error_sender_no_email, email_error_subject, email_error_message) {
  var theForm = document.mailform;
  var error   = email_error+"\n";
  var allOK   = true;

  if (trim(theForm.sender.value) == "") {
    allOK  =  false;
    error  += "\n"+email_error_sender;
  } else if (!isEmail(trim(theForm.sender.value))) {
    allOK  =  false;
    error  += "\n"+email_error_sender_no_email;
  }
  if (trim(theForm.topic.value) == "") {
    allOK  =  false;
    error  += "\n"+email_error_subject;
  }
  if (trim(theForm.message.value) == "") {
    allOK  =  false;
    error  += "\n"+email_error_message;
  }

  if (!allOK) {
    alert(error);
    return false;
  } else {
    document.mailform.submit();
  }
}

function check_rec_form () {
  var theForm = document.inputform2;
  var allOK   = true;

  if (trim(theForm.email.value) == "" || !isEmail(trim(theForm.email.value))) {
   allOK  =  false;
   document.getElementById('cap_email').style.color = '#FF0000';
  } else
    document.getElementById('cap_email').style.color = '';

  if (!allOK) {
		document.getElementById('errortext').style.display = 'inline';
    return false;
  } else {
    document.inputform2.submit();
  }
}

function check_login_form () {
  var theForm = document.inputform;
  var allOK   = true;

  if (trim(theForm.email.value) == "" || !isEmail(trim(theForm.email.value))) {
   allOK  =  false;
   document.getElementById('cap_email').style.color = '#FF0000';
  } else
    document.getElementById('cap_email').style.color = '';

  if (trim(theForm.password.value) == "") {
    allOK  =  false;
    document.getElementById('cap_password').style.color = '#FF0000';
  } else
    document.getElementById('cap_password').style.color = '';

  if (!allOK) {
		document.getElementById('errortext').style.display = 'inline';
    return false;
  } else {
    document.inputform.submit();
  }
}

function check_edit_form () {
	  var theForm = document.inputform;
	  var allOK   = true;
	if (theForm.transaccepted){
    if (theForm.transaccepted.value == "0") {
      allOK  =  false;
      document.getElementById('cap_transaccepted').style.color = '#FF0000';
    } else
      document.getElementById('cap_transaccepted').style.color = '';
      }

    if (theForm.agbaccepted.value == "0") {
	    allOK  =  false;
	    document.getElementById('cap_agbaccepted').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_agbaccepted').style.color = '';

		if (theForm.operationalarea.options[theForm.operationalarea.selectedIndex].value == "") {
	    allOK  =  false;
	    document.getElementById('cap_operationalarea').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_operationalarea').style.color = '';

		if (theForm.references.value == "") {
	    allOK  =  false;
	    document.getElementById('cap_references').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_references').style.color = '';

		if (theForm.knowhow.value == "") {
	    allOK  =  false;
	    document.getElementById('cap_knowhow').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_knowhow').style.color = '';

		if (theForm.sparte_mode.value == 0 && theForm.sparte_architektur.value == 0 && theForm.sparte_onlocation.value == 0 &&
		   theForm.sparte_auto.value == 0 && theForm.sparte_stillife.value == 0 && theForm.sparte_locationscouting.value == 0 &&
		   theForm.sparte_landschaft.value == 0                                 && theForm.sparte_internaterfahrung.value == 0) {
	     allOK  =  false;
	     document.getElementById('cap_sparte').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_sparte').style.color = '';

	  if (trim(theForm.name.value) == "") {
	    allOK  =  false;
	    document.getElementById('cap_name').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_name').style.color = '';

	  if (trim(theForm.street.value) == "") {
	    allOK  =  false;
	    document.getElementById('cap_street').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_street').style.color = '';

	  if (trim(theForm.ziplocation.value) == "") {
	    allOK  =  false;
	    document.getElementById('cap_ziplocation').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_ziplocation').style.color = '';

	  if (trim(theForm.phone.value) == "") {
	    allOK  =  false;
	    document.getElementById('cap_phone').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_phone').style.color = '';

	  if (trim(theForm.email.value) == "" || !isEmail(trim(theForm.email.value))) {
	   allOK  =  false;
	   document.getElementById('cap_email').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_email').style.color = '';

	  if (trim(theForm.mailformemail.value) != "" && !isEmail(trim(theForm.mailformemail.value))) {
	   allOK  =  false;
	   document.getElementById('cap_mailformemail').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_mailformemail').style.color = '';


    if (trim(theForm.newpass.value) != "") {
  	  if (trim(theForm.conpass.value) == "" || trim(theForm.conpass.value) != trim(theForm.newpass.value)) {
  	    allOK  =  false;
  	    document.getElementById('cap_conpass').style.color = '#FF0000';
  	  } else
  	    document.getElementById('cap_conpass').style.color = '';
	  }

	  var checkFile = theForm.photo.value;
	  lastDot = checkFile.lastIndexOf('.');
	  fileext = checkFile.substr(lastDot + 1);
	  if (theForm.photo.value != '' && (document.testimage.width > 100 || document.testimage.height > 100 || (fileext != 'jpg' && fileext != 'jpeg' && fileext != 'gif'))) {
	    allOK  =  false;
	    document.getElementById('cap_photo').style.color = '#FF0000';
	  } else
	    document.getElementById('cap_photo').style.color = '';

	  if (!allOK) {
			document.getElementById('errortext').style.display = 'inline';
	    return false;
	  } else {
	    document.inputform.submit();
  }
}

function check_register_form (step) {
  var theForm = document.inputform;
  var allOK   = true;

  if (step == '3') {
  	if (theForm.transaccepted){
		if (theForm.transaccepted.value == "0") {
      allOK  =  false;
      document.getElementById('cap_transaccepted').style.color = '#FF0000';
    } else
      document.getElementById('cap_transaccepted').style.color = '';
	}
	
		if (theForm.agbaccepted.value == "0") {
      allOK  =  false;
      document.getElementById('cap_agbaccepted').style.color = '#FF0000';
    } else
      document.getElementById('cap_agbaccepted').style.color = '';
	} else if (step == '2') {
		if (theForm.operationalarea.options[theForm.operationalarea.selectedIndex].value == "") {
      allOK  =  false;
      document.getElementById('cap_operationalarea').style.color = '#FF0000';
    } else
      document.getElementById('cap_operationalarea').style.color = '';

		if (theForm.references.value == "" || theForm.references.value == theForm.references.defaultValue) {
      allOK  =  false;
      document.getElementById('cap_references').style.color = '#FF0000';
    } else
      document.getElementById('cap_references').style.color = '';

		if (theForm.knowhow.value == "" || theForm.knowhow.value == theForm.knowhow.defaultValue) {
      allOK  =  false;
      document.getElementById('cap_knowhow').style.color = '#FF0000';
    } else
      document.getElementById('cap_knowhow').style.color = '';

		if (theForm.sparte_mode.value == 0 && theForm.sparte_architektur.value == 0 && theForm.sparte_onlocation.value == 0 &&
		    theForm.sparte_auto.value == 0 && theForm.sparte_stillife.value == 0 && theForm.sparte_locationscouting.value == 0 &&
		    theForm.sparte_landschaft.value == 0                                 && theForm.sparte_internaterfahrung.value == 0) {
      allOK  =  false;
      document.getElementById('cap_sparte').style.color = '#FF0000';
    } else
      document.getElementById('cap_sparte').style.color = '';
	} else if (step == '1') {
    if (trim(theForm.name.value) == "") {
      allOK  =  false;
      document.getElementById('cap_name').style.color = '#FF0000';
    } else
      document.getElementById('cap_name').style.color = '';

    if (trim(theForm.street.value) == "") {
      allOK  =  false;
      document.getElementById('cap_street').style.color = '#FF0000';
    } else
      document.getElementById('cap_street').style.color = '';

    if (trim(theForm.ziplocation.value) == "") {
      allOK  =  false;
      document.getElementById('cap_ziplocation').style.color = '#FF0000';
    } else
      document.getElementById('cap_ziplocation').style.color = '';

    if (trim(theForm.phone.value) == "") {
      allOK  =  false;
      document.getElementById('cap_phone').style.color = '#FF0000';
    } else
      document.getElementById('cap_phone').style.color = '';

    if (trim(theForm.email.value) == "" || !isEmail(trim(theForm.email.value))) {
     allOK  =  false;
     document.getElementById('cap_email').style.color = '#FF0000';
    } else
      document.getElementById('cap_email').style.color = '';

    if (trim(theForm.mailformemail.value) != "" && !isEmail(trim(theForm.mailformemail.value))) {
     allOK  =  false;
     document.getElementById('cap_mailformemail').style.color = '#FF0000';
    } else
      document.getElementById('cap_mailformemail').style.color = '';


    if (trim(theForm.password.value) == "") {
      allOK  =  false;
      document.getElementById('cap_password').style.color = '#FF0000';
    } else
      document.getElementById('cap_password').style.color = '';

    if (trim(theForm.conpass.value) == "" || trim(theForm.conpass.value) != trim(theForm.password.value)) {
      allOK  =  false;
      document.getElementById('cap_conpass').style.color = '#FF0000';
    } else
      document.getElementById('cap_conpass').style.color = '';

    var checkFile = theForm.photo.value;
    lastDot = checkFile.lastIndexOf('.');
    fileext = checkFile.substr(lastDot + 1);
    if (theForm.photo.value != '' && (document.testimage.width > 100 || document.testimage.height > 100 || (fileext != 'jpg' && fileext != 'jpeg' && fileext != 'gif'))) {
      allOK  =  false;
      document.getElementById('cap_photo').style.color = '#FF0000';
    } else
      document.getElementById('cap_photo').style.color = '';
	}


  if (!allOK) {
		document.getElementById('errortext').style.display = 'inline';
    return false;
  } else {
    document.inputform.submit();
  }
}

plImages = [];
plImages['checkbox_on']      = new Image()
plImages['checkbox_on'].src  = 'images/checkbox_checked.gif';
plImages['checkbox_off']     = new Image()
plImages['checkbox_off'].src = 'images/checkbox_unchecked.gif';
plImages['radio_on']         = new Image()
plImages['radio_on'].src     = 'images/checkbox_checked.gif';
plImages['radio_off']        = new Image()
plImages['radio_off'].src    = 'images/checkbox_unchecked.gif';

function clickCheckbox (fieldname) {
  document.inputform[fieldname].value == '1' ? document.inputform[fieldname].value = '0': document.inputform[fieldname].value = '1';
  document.inputform[fieldname].value == '1' ? document.inputform[fieldname+'_image'].src = plImages['checkbox_on'].src: document.inputform[fieldname+'_image'].src = plImages['checkbox_off'].src;
}

function clickRadio (fieldname, number, value) {
  document.inputform[fieldname].value = value;

  for (i=0; document.inputform[fieldname+'_image_' + i] != null; i++) {
    document.inputform[fieldname+'_image_' + i].src = plImages['radio_off'].src;
  }

  document.inputform[fieldname+'_image_' + number].src = plImages['radio_on'].src;
}

var tempAreaValue = new Array();
var tempAreaText = new Array();
var tempSelectedValue;

function initOperationalAreas(){
	var operAreaSelectBox = document.getElementById('operAreasSelect');
	
	tempSelectedValue = operAreaSelectBox.options[operAreaSelectBox.selectedIndex].value;
	
	for (i=0; i<operAreaSelectBox.length; i++){
		tempAreaText[i] = operAreaSelectBox.options[i].text;
		tempAreaValue[i] = operAreaSelectBox.options[i].value;
	}
	
	for (i=operAreaSelectBox.length-1; i>=0; i--){
		operAreaSelectBox.options[i] = null;
	}
}

function changeOperationalAreas(){

	var operAreaSelectBox = document.getElementById('operAreasSelect');
	var countriesSelectBox = document.getElementById('countriesSelect');
	
	var countryId = countriesSelectBox.options[countriesSelectBox.selectedIndex].value;
	
	for (i=operAreaSelectBox.length-1; i>=0; i--){
		operAreaSelectBox.options[i] = null;
	}	
	
	for (i=0; i<tempAreaText.length; i++){
		var tempText = tempAreaText[i];	
		if (tempText.split("%")[1] == countryId || countryId == "" || !tempText.split("%")[1]){
			newOption = new Option(tempText.split("%")[0], tempAreaValue[i]);
			operAreaSelectBox.options[operAreaSelectBox.options.length] = newOption;
			if (tempAreaValue[i] == tempSelectedValue){
				newOption.selected = true;
			}			
		}
	}
}