// PAULCAAH: DON'T MAKE CHANGES TO THIS FILE!
// DO THIS IN /super/ IF NECESSARY, AND THEN COPY THE FILE TO HERE.

//Created By: Chris Campbell
//www.particletree.com

function attachFormHandlers(formname)
{
	if (document.getElementsByTagName)//make sure were on a newer browser
	{
		var objInput = formname.getElementsByTagName('input'); // store all input fields
		var objSelect = formname.getElementsByTagName('select'); // store all select fields
		var objTextarea = formname.getElementsByTagName('textarea'); // store all textareas

		for (var iCounter=0; iCounter<objInput.length; iCounter++){
			if(objInput[iCounter].className.indexOf('validate')!=-1 && objInput[iCounter].type != 'hidden' && objInput[iCounter].className.indexOf('keyup')==-1){
				objInput[iCounter].onfocus = function(){return showhelp(this);}
				objInput[iCounter].onblur = function(){return attach(this);} //attach the onchange to each input field
				attach(objInput[iCounter]);
			}else if(objInput[iCounter].className.indexOf('keyup')!=-1){
				objInput[iCounter].onkeyup = function(){return attach(this);} //attach the onchange to each input field
				objInput[iCounter].onblur = function(){return attach(this);} //attach the onchange to each input field
			}else if(objInput[iCounter].className.indexOf('optioneel')!=-1){
				objInput[iCounter].onfocus = function(){return showhelp(this);}
				objInput[iCounter].onblur = function(){return hidehelp(this);}
			}else{
			}
		}

		for (var xCounter=0; xCounter<objSelect.length; xCounter++){
			if(objSelect[xCounter].className.indexOf('validate')!=-1 && objSelect[xCounter].type != 'hidden'){
				objSelect[xCounter].onfocus = function(){return showhelp(this);}
				objSelect[xCounter].onblur = function(){return attach(this);} //attach the onchange to each input field
				attach(objSelect[xCounter]);
			}else if(objSelect[xCounter].className.indexOf('optioneel')!=-1){
				objSelect[xCounter].onfocus = function(){return showhelp(this);}
				objSelect[xCounter].onblur = function(){return hidehelp(this);}
			}else{
			}
		}

		for (var yCounter=0; yCounter<objTextarea.length; yCounter++){
			if(objTextarea[yCounter].className.indexOf('validate')!=-1 && objTextarea[yCounter].type != 'hidden' && objTextarea[yCounter].className.indexOf('keyup')==-1){
				objTextarea[yCounter].onfocus = function(){return showhelp(this);}
				objTextarea[yCounter].onblur = function(){return attach(this);} //attach the onchange to each Textarea
				attach(objTextarea[yCounter]);
			}else if(objTextarea[yCounter].className.indexOf('keyup')!=-1){
				objTextarea[yCounter].onkeyup = function(){return attach(this);} //attach the onchange to each input field
				objTextarea[yCounter].onblur = function(){return attach(this);} //attach the onchange to each input field
			}else if(objTextarea[yCounter].className.indexOf('optioneel')!=-1){
				objTextarea[yCounter].onfocus = function(){return showhelp(this);}
				objTextarea[yCounter].onblur = function(){return hidehelp(this);}
			}else{
			}
		}
	}

	formname.onsubmit = function(){return validate(formname);} //attach validate() to the form
	
	// A very very dirty hack to validate the 'persoonsgegevens' at stap-voor-stap solliciteren when clicked on a link instead of a button
	if(window.location.search.search(/fuseaction=2/) != -1 && typeof(validateOnLinks) != "undefined" && validateOnLinks == 1)
	{
		document.persoonsgegevens.urlredirect.value = "";
		var validationLinks = Dom.getElementsByClassName("stapvoorstaplink");
		Event.on(validationLinks,"click",function(e){ 
			Event.preventDefault(e);
			if(validate(document.persoonsgegevens)){
				document.persoonsgegevens.urlredirect.value = this.href.split("fuseaction=")[1].substr(0,1);
				document.persoonsgegevens.submit();
			}else
			{
				document.persoonsgegevens.urlredirect.value = "";
			}
		});
	};
}

function test(){
	alert('test');	
}
function showhelp(obj){
	if(document.getElementById(obj.id+'help')){
		document.getElementById(obj.id+'help').style.display='block';
	}
}

function hidehelp(obj){
	if(document.getElementById(obj.id+'help')){
		document.getElementById(obj.id+'help').style.display='none';
	}
}

var gContinue = true;
function attach(objInput)
{
	sVal = objInput.value; //get value inside of input field
	var sFeedBack; //feedback is the feedback message sent back to the user
	gContinue = true;
	sRules = objInput.className.split(' '); // get all the rules from the input box classname
	sValidate = sRules[0]; //validate means we will validate the field
	sRequired = sRules[1]; // required means field is required
	sTypeCheck = sRules[2]; //typecheck are additional validation rules (ie. email, phone, date)
	sFeedbackLoc = sRules[3]; //feedbackLoc is the td id where feedback is sent to.
	if(document.getElementById(objInput.id+'label')){
		sLabelName = objInput.id+'label';
	}else{
		sLabelName = '';
	}
	sFeedback = validateRequired (sRequired, sVal, sTypeCheck,sLabelName); //validateRequired() checks if it is required and then sends back feedback
		if (gContinue) //if it is required and blank gContinue is false and we don't validate anymore.  // this is done because if it is blank
		//it will also fail other tests.  We don't want to spam the user with INVALID EMAIL!! if the field is still blank.
		{
			// check the different validation cases (ie: email, phone, etc.)
			switch (sTypeCheck)
			
			{
				case "date":
					sFeedback = validateDate(sVal,sLabelName);
					break;
				case "email":
					sFeedback = validateEmail(sVal,sLabelName);
					break;
				case "phone":
					sFeedback = validatePhone(sVal,sLabelName);
					break;
				case "zip":
					sFeedback = validateZip(sVal,sLabelName);
					break;
				case "password":
					sFeedback = validatePassword(sVal,sLabelName);
					break;
				case "name":
					sFeedback = validateName(sVal,sLabelName);
					break;
				case "numeric":
					sFeedback = validateNumeric(sVal,sLabelName);
					break;
				case "select":
					sFeedback = validateSelect(sVal,sLabelName,objInput);
					break;
				case "string":
					sFeedback = validateString(sVal,sLabelName);
					break;
			}
		}
			// after validation is complete return the feedback 
			document.getElementById(sFeedbackLoc).innerHTML = sFeedback;	
			hidehelp(objInput);
}


function validateRequired(sRequired, sVal, sTypecheck,sLabelName)
{
	if (sRequired == "required")  //check if required if not, continue validation script
	{
   		if (sVal == "") //if it is rquired and blank then it is an error and continues to be required
		{
			gContinue = false;
			return  "<img src='/images/form_invalid.gif' />";
			//<div style='position:absolute;z-index:100;margin-top:-16px;margin-left:15px;'>"+document.getElementById(sLabelName).innerHTML+"</div>
	 	}
  		else if (sTypecheck == "none")  //if its not blank and has no other validation requirements the field passes
		{
			return "<img src='/images/form_valid.gif' />";
		}
	}
}


function validateDate(sVal)
{
	// our date regular expression (http://www.regexlib.com)
 //var regex=/(((0[13578]|10|12)([-.\/])(0[1-9]|[12][0-9]|3[01])([-.\/])(\d{4}))|((0[469]|11)([-.\/])([0][1-9]|[12][0-9]|30)([-.\/])(\d{4}))|((2)([-.\/])(0[1-9]|1[0-9]|2[0-8])([-.\/])(\d{4}))|((2)(\.|-|\/)(29)([-.\/])([02468][048]00))|((2)([-.\/])(29)([-.\/])([13579][26]00))|((2)([-.\/])(29)([-.\/])([0-9][0-9][0][48]))|((2)([-.\/])(29)([-.\/])([0-9][0-9][2468][048]))|((2)([-.\/])(29)([-.\/])([0-9][0-9][13579][26])))/;
 var regex=/^(?=\d)(?:(?!(?:(?:0?[5-9]|1[0-4])(?:\.|-|\/)10(?:\.|-|\/)(?:1582))|(?:(?:0?[3-9]|1[0-3])(?:\.|-|\/)0?9(?:\.|-|\/)(?:1752)))(31(?!(?:\.|-|\/)(?:0?[2469]|11))|30(?!(?:\.|-|\/)0?2)|(?:29(?:(?!(?:\.|-|\/)0?2(?:\.|-|\/))|(?=\D0?2\D(?:(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|2[0-8]|1\d|0?[1-9])([-.\/])(1[012]|(?:0?[1-9]))\2((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?:$|(?=\x20\d)\x20)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/



	// do the comparison, if we have a match write <img src='/images/form_valid.gif' /> or else the date is invalid
	if (regex.test(sVal))
	{
      return "<img src='/images/form_valid.gif' />";
	}
	else 
	{
      return "<img src='/images/form_invalid.gif' />";
	}

}

function validateEmail(sVal)
{
	
// our email regular expression (http://www.regexlib.com)
var regex = /^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i;

 
	// do the comparison, if we have a match write <img src='/images/form_valid.gif' /> or else the email is invalid
	if (regex.test(sVal))
	{
      return "<img src='/images/form_valid.gif' />";
	}
	else if(sVal=="")
	{
      return "";
	}
	else
	{
      return "<img src='/images/form_invalid.gif' />";
	}
}

function validatePhone(sVal)
{
	
// our phone regular expression
// This expression is a very simplex expression that allows null values or 3 digits, dash, 
//3 digits, dash, 4 digits. It validates a basic US phone number. Written by Jason N. Gaylord.(http://www.regexlib.com)
// Matches:  	 [555-555-1212], [123-456-7890]
//var regex=/^(\d{3}-\d{3}-\d{4})*$/;
 	
	var regex=/([\+]{1}|[0]{1}[6]{1}[-\s]*[1-9]{1}[\s]*([0-9]{1}[\s]*){7})|([0]{1}[1-9]{1}[0-9]{1}[0-9]{1}[-\s]*[1-9]{1}[\s]*([0-9]{1}[\s]*){5})|([0]{1}[1-9]{1}[0-9]{1}[-\s]*[1-9]{1}[\s]*([0-9]{1}[\s]*){6})/;
  	
 
	// do the comparison, if we have a match write <img src='/images/form_valid.gif' /> or else the email is invalid
	if (regex.test(sVal))
	{
      return "<img src='/images/form_valid.gif' />";
	}
	else
	{
      return "<img src='/images/form_invalid.gif' />";
	}
}

function validateZip(sVal)
{
	
// our email regular expression
//Javascript matches US zipcodes not allowing all zeros in first 5 or +4 (http://www.regexlib.com)
// Matches:  	 [12345], [12345-6789], [123456789]
 var regex=/^[0-9]{4}\s*[a-zA-Z]{2}$/;

 
	// do the comparison, if we have a match write <img src='/images/form_valid.gif' /> or else the email is invalid
	if (regex.test(sVal))
	{
      return "<img src='/images/form_valid.gif' />";
	}
	else if(sVal=="")
	{
      return "";
	}
	else
	{
      return "<img src='/images/form_invalid.gif' />";
	}
}

function validatePassword(sVal)
{ 
//Description: The password's first character must be a letter, it must contain at least 4 characters
//and no more than 15 characters and no characters other than letters, numbers and the underscore may be used
//Matches: 	[abcd], [aBc45DSD_sdf], [password] (http://www.regexlib.com)
 var regex=/^[a-zA-Z]\w{3,14}$/;
 
	// do the comparison, if we have a match write <img src='/images/form_valid.gif' /> or else the email is invalid
	if (regex.test(sVal))
	{
      return "<img src='/images/form_valid.gif' />";
	}
	else
	{
      return "<img src='/images/form_invalid.gif' />";
	}
}

function validateName(sVal)
{ 
//This is the simplest RegEx for validating someone's name. The name can contain only alphabets(in either case) & 
//should be of minimum length 4 & maximum length 32. Only white spaces are allowed apart from alphabets.
//Jurgen/Sanne addendum: added special characters for Jan-Jaap, J.J.
//Matches: 	[some body], [hey there], [hello] (http://www.regexlib.com)
//var regex=/^([a-zA-z\s]{4,32})$/;
 var regex=/^([a-zA-z\s\-\.]{4,32})$/;
 
	// do the comparison, if we have a match write <img src='/images/form_valid.gif' /> or else the email is invalid
	if (regex.test(sVal))
	{
      return "<img src='/images/form_valid.gif' />";
	}
	else
	{
      return "<img src='/images/form_invalid.gif' />";
	}
}

function validateString(sVal)
{ 
//This is the simplest RegEx for validating someone's name. The name can contain only alphabets(in either case) & 
//should be of minimum length 4 & maximum length 32. Only white spaces are allowed apart from alphabets.
//Matches: 	[some body], [hey there], [hello] (http://www.regexlib.com)
 
	// do the comparison, if we have a match write <img src='/images/form_valid.gif' /> or else the email is invalid
	if (sVal!='')
	{
      return "<img src='/images/form_valid.gif' />";
	}
	else
	{
      return "<img src='/images/form_invalid.gif' />";
	}
}


function validateNumeric(sVal)
{ 
//Input for Numeric values. Handles negatives, and comma formatted values. Also handles a single decimal point
//Matches: 	[5,000], [-5,000], [100.044] (http://www.regexlib.com)
 var regex=/^(\d|-)?(\d|,)*\.?\d*$/;
 
	// do the comparison, if we have a match write <img src='/images/form_valid.gif' /> or else the email is invalid
	if (regex.test(sVal))
	{
      return "<img src='/images/form_valid.gif' />";
	}
	else
	{
      return "<img src='/images/form_invalid.gif' />";
	}
}

function validateSelect(sVal,lbl,obj)
{ 
	if (obj.selectedIndex!=0)
	{
      return "<img src='/images/form_valid.gif' />";
	}
	else
	{
      return "<img src='/images/form_invalid.gif' />";
	}
}