// Checks for Contact Form

function sendSollicitatieForm(){
	var formname = 'process_sollicitatie';
	var errormsg = '<img src="lib/img/checkform-error.gif"><br />';	
	var msg = '';
	
	// check a text field
	String(document.forms[formname].achternaam.value)=="" ? msg += "achternaam, " : "";
	String(document.forms[formname].straat.value)=="" ? msg += "straat, " : "";
	String(document.forms[formname].huisnr.value)=="" ? msg += "huisnr, " : "";	
	String(document.forms[formname].postcode.value)=="" ? msg += "postcode, " : "";	
	String(document.forms[formname].plaats.value)=="" ? msg += "plaats, " : "";
	String(document.forms[formname].telefoonnr.value)=="" ? msg += "telefoonnr, " : "";	
	String(document.forms[formname].geboortedatum.value)=="" ? msg += "geboortedatum, " : "";
	String(document.forms[formname].opleiding.value)=="" ? msg += "opleiding, " : "";
        selInd = document.forms[formname].via.selectedIndex;
	String(document.forms[formname].via.options[selInd].value)=="" ? msg += "wijze van contact, " : "";
	//String(document.forms[formname].email.value)=="" ? msg += "e-mail adres, " : "";

	// check a radio button
	checkSelected(document.forms[formname].geslacht) == false ? msg +="geslacht, " : "";
	checkSelected(document.forms[formname].diploma) == false ? msg +="diploma, " : "";	
	
	// check an e-mailadress
	checkEmailaddress(document.forms[formname].email.value)==false ? msg += "e-mail adres, " : "";	

	// check cv file upload only for Uniform functies cid==1
        if(document.forms[formname].cid_vacature.value !=1 ) {
	 String(document.forms[formname].file_cv.value)=="" ? msg += "CV, " : "";
        }
	//
	//
	// leave the rest alone :-)
	strLen = msg.length;
	msg = msg.substring(0,msg.length-2);
	if (msg != ""){
		document.getElementById('error').innerHTML = errormsg+msg; 
		document.getElementById('error').style.display = 'block';		
	}
	else{
		//document.getElementById('error-msg').style.display = 'none;';		
		document.forms[formname].submit();
	}
}

function sendDoorstuurForm(){
	var formname = 'sendform';
	var errormsg = '<img src="lib/img/checkform-error.gif"><br />';	
	var msg = '';
	
	// check a text field
	String(document.forms[formname].afzend_naam.value)=="" ? msg += "jouw naam, " : "";
	String(document.forms[formname].ontvang_naam.value)=="" ? msg += "naam ontvanger, " : "";

	// check a radio button
	//checkSelected(document.forms[formname].geslacht) == false ? msg +="geslacht, " : "";
	
	// check an e-mailadress
	checkEmailaddress(document.forms[formname].afzend_email.value)==false ? msg += "jouw e-mail adres, " : "";	
	checkEmailaddress(document.forms[formname].ontvang_email.value)==false ? msg += "e-mail adres ontvanger, " : "";		

	//
	//
	// leave the rest alone :-)
	strLen = msg.length;
	msg = msg.substring(0,msg.length-2);
	if (msg != ""){
		document.getElementById('error').innerHTML = errormsg+msg; 
		document.getElementById('error').style.display = 'block';		
	}
	else{
		//document.getElementById('error-msg').style.display = 'none;';		
		document.forms[formname].submit();
	}
}

function checkSelected(objectRadio)
{	
	found = 0;
	for(i=0;i<objectRadio.length && found==0;i++)
	{
		if(objectRadio[i].checked)
		{
			found = 1;
		}
	}

	return found;
}

function checkEmailaddress(ea){
	// assume address is valid
	valid = true;
	ea = ea.toLowerCase();

	// check email address syntax
	if(ea == null) valid = false;
	else if(ea.indexOf("@") < 2) valid = false;
	else if(ea.indexOf("@") != ea.lastIndexOf("@")) valid = false;
	else if(ea.length - ea.lastIndexOf(".") < 3) valid = false;
	else if(ea.substr(ea.indexOf("@")+1,ea.length-1).indexOf(".") < 2) valid = false;

	// make sure the last set set of chars only contain alpha chars
	s = ea.substr(ea.lastIndexOf(".")+1,ea.length-1);
	for(i = 0 ; i < s.length ; i++){
		if(s.charCodeAt(i) < 97 || s.charCodeAt(i) > 122) valid = false;
	}

	// make sure the chars before the @ are valid (charCodes 33-57,65-90,95,97-122)
	s = ea.substr(0,ea.indexOf("@"));
	for(i = 0 ; i < s.length ; i++){
		c = s.charCodeAt(i);
		if(c == 95 || (c >= 33 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) ;
		else {
			valid = false;
			break;
		}
	} 

	return valid;
}

function toggle_anders() {
 if (document.getElementById) {
  v=document.process_sollicitatie.via.options[document.process_sollicitatie.via.selectedIndex].value;
  value=v.substr(0,6).toLowerCase();
  if (value=='anders') {
   document.getElementById('div_anders').style.display = "";
   document.getElementById('via_anders').focus();
  }
  else {
   document.getElementById('div_anders').style.display = "none";
  }
 }
}
