// JavaScript Document
<!--

/*
	Version:	0.1
	Date:		3/9/2008
	Initials:	CS/ZJ

	Contains validation methods. These methods can be interfaced with jQuery later.
	Following validations are available:
	
	* DateMMDDYYYYSlashes
		If a date is of mm/dd/yyyy format, returns true or else returns false.	
	* FirstLastName
		If alphabets, numbers and spaces are provided, great. Or else returns false
	* EmailID
		Checks for username@domain.nnn type of email		
*/



	/*
		Function:	ValidateField
		Purpose:	Checks the field value. If value is required, validates length. Redirects
					validation to a function asked for and generates custom error (alert), if needed
		In:			fieldName 			name of the form field to analyze
					required			true/false, required or not required
					minLength			minimum length of the field's value
					maxLength			maximum length of the field's value
					validationFunction	specific function to analyze field's value
					customError			alert, if it needs to be displayed
		Out:		true or false
		In/Out:		none
		Notes:		none	
	*/
	function ValidateField(fieldName, required, minLength, maxLength, validationFunction, customError)
	{
	
		// evaluate the field
		with (fieldName) 
		{
		
			// check if the field is empty		
			if (value == null || value == "")
			{
				// was the field required? if yes, we have a problem
				if (required)
				{
					if (customError != undefined)
						alert (customError);
					fieldName.focus();
					return false;
				}
				else
				{
					value = "";
					return true;
				}
			}
			
			// check for trimmed length in individual methods
			// --none--
		
			
			// following this line, the required field is analyzed
			if (value.length < minLength || value.length > maxLength)
			{
				if (customError != undefined)
					alert (customError);
				fieldName.focus();
				return false;
			}
			
			
			
			// if validation function is specified, redirect information there
			if (validationFunction != undefined)
			{	
				var valid = eval(validationFunction(value));
				if (!valid)
				{
					if (customError != undefined)	
						alert (customError);
					fieldName.focus();
					return false;
				}				
			}
			
		} // with fieldName ends
		
		
		return true;
		
	} // isEmpty ends



	/*
		Function:	DateMMDDYYYYSlashes
		Purpose:	Fields with date mm/dd/yyyy are OK, others are not
		In:			val				incoming value
		Out:		true or false
		In/Out:		none
		Notes:		none	
	*/
	function DateMMDDYYYYSlashes(val)
	{
		//var regex = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
		var regex = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
		return regex.test(val)?true:false;		
	}
	
	
	
	

/*
		Function:	DateMMDDYYYYSlashes
		Purpose:	Fields with date dd/mm/yyyy are OK, others are not
		In:			val				incoming value
		Out:		true or false
		In/Out:		none
		Notes:		none	
	*/
	function DateDDMMYYYYSlashes(val)
	{
		//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})?$/;
		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})?$/;
		return regex.test(val)?true:false;		
	}	



	/*
		Function:	FirstLastName
		Purpose:	Fields with alphabets, numbers and spaces are OK, others are not
		In:			val				incoming value
		Out:		true or false
		In/Out:		none
		Notes:		none	
	*/
	function FirstLastName(val)
	{
		if (trim(val).length == 0) { return false; }
		var regex=/^([a-zA-Z0-9 ]+)$/;
		return regex.test(val)?true:false;
	}
	
	function OnlyAlphabets(val)
	{
		if (trim(val).length == 0) { return false; }
		var regex=/^([a-zA-Z]+)$/;
		return regex.test(val)?true:false;
	}

	function AlphabetsNumbersSpacesBracketsDashes(val)
	{
		if (trim(val).length == 0) { return false; }
		var regex=/^[A-Za-z0-9\(\)\- ]+$/;
		return regex.test(val)?true:false;	
	}
	
	/*
		Function:	SIN
		Purpose:	validates SIN - simple validation
		In:			val				incoming value
		Out:		true or false
		In/Out:		none
		Notes:		none	
	*/
	function SIN(val)
	{
		if (trim(val).length != 9) { return false; }
		var regex=/^([0-9]+)$/;
		return regex.test(val)?true:false;
	}
	
	
	/*
		Function:	AtLeastOneDigit
		Purpose:	validates number - should have at least one digit
		In:			val				incoming value
		Out:		true or false
		In/Out:		none
		Notes:		none	
	*/
	function AtLeastOneDigit(val)
	{
		if (trim(val).length == 0) { return false; }
		var regex=/^([0-9]+)$/;
		return regex.test(val)?true:false;
	}	
	
	
	
	/*
		Function:	EmailID
		Purpose:	emailid@domain.com.yy allowed, others are not
		In:			val				incoming value
		Out:		true or false
		In/Out:		none
		Notes:		none	
	*/
	function EmailID(val)
	{
		if (trim(val).length == 0) { return false; }
		//var regex=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var regex=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		return regex.test(val)?true:false;
	}
	
	
	
	/*
		Function:	trim
		Purpose:	trims a value
		In:			val				incoming value
		Out:		trimmed value
		In/Out:		none
		Notes:		none	
	*/
	function trim(val)
	{
		return val.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	}







	/*
		Function:	SIN1
		Purpose:	validate SIN (9 digits)
		In:			val				incoming value
		Out:		true or false
		In/Out:		none
		Notes:		none	
	*/
	function SIN1(val) 
	{                
		if(val == "") 
			return false;
	
		inStr = val;
		sin = val;
		inLen = inStr.length;		
		if (inLen > 11 || inLen < 11) 
			return false;
		
		for (var i = 0; i < val.length; i++) 
		{
			var ch = val.substring(i, i + 1)
			if ((ch < "0" || "9" < ch) && (ch != "-"))	
				return false;
				
			if ((i == 3 || i == 7) && (ch != "-")) 
				return false;
		}
		lastdigit = val.substring(10, 10 + 1);
		
		// add numbers in odd positions; IE 1, 3, 6, 8		
		var odd = ((val.substring(0,0 + 1)) * (1.0)  + (val.substring(2,2 + 1)) * (1.0) 
		+(val.substring(5, 5+1)) * (1.0) + (val.substring(8,8 + 1)) * (1.0));
		
		// form texting of numbers in even positions IE 2, 4, 6, 8
		var enumbers =  (val.substring(1,1 + 1)) + (val.substring(4,4 + 1))+
		(val.substring(6,6 + 1)) + (val.substring(9,9 + 1));
		
		// add together numbers in new text string
		// take numbers in even positions; IE 2, 4, 6, 8
		// and double them to form a new text string
		// EG if numbers are 2,5,1,9 new text string is 410218
		for (var i = 0; i < enumbers.length; i++) 
		{
			var ch = (enumbers.substring(i, i + 1) * 2);
			ch_sum = ch_sum + ch;
		}
		
		for (var i = 0; i < ch_sum.length; i++) 
		{
			var ch = (ch_sum.substring(i, i + 1));
			esum = ((esum * 1.0) + (ch * 1.0));
		}		
		
		checknum = (odd + esum);
		
		// subtextact checknum from next highest multiple of 10
		// to give check digit which is last digit in valid SIN
		if (checknum <= 10) 
			(checdigit = (10 - checknum));
			
		if (checknum > 10 && checknum <= 20)
			(checkdigit = (20 - checknum));
			
		if (checknum > 20 && checknum <= 30)
			(checkdigit = (30 - checknum));
			
		if (checknum > 30 && checknum <= 40)
			(checkdigit = (40 - checknum));
	
		if (checknum > 40 && checknum <= 50)
			(checkdigit = (50 - checknum));
	
		if (checknum > 50 && checknum <= 60)
			(checkdigit = (60 - checknum));
		
		if (checkdigit != lastdigit) 
			return false;
							  			
		return true;
	}



//-->
