// **********************************************************************************************
// Generic Form Validation Functions
//
// Created By: Aaron Walker 10/05/99
// Edited By:  Aaron Walker 10/05/99
//			   Aaron Walker 11/10/99
//				- added extended email validation
//             Dina Kozik 05/07/08
//              - added isValidExpDate function
//
// Usage:
//		Include to following tag in the header of the html file
//		<SCRIPT SRC="/aspw/validation.js">
//
//
// Functions:
//		function isZipcode(zipValue)				- determines if field is a valid US zip code
//		function isValidEmail(addr)					- determines if field is a valid email address
//		function isNumber(num)						- returns true if a field has a numeric value in it
//		function isDate(myDate)						- returns true if myDate is a valid date
//		function isSerialNumber(serNum, low, high)	- returns true if a field has a valid serial number in it
//		function isValidRegistration()				- returns whether form is valid or not
//		function setValidRegistration(bool)			- set valid form variable
//		function CheckField(field)					- validate field passed to function
//	    function isValidExpDate(expdate)			- determines if field is a valida credit card expiration date in //													  mm/yy format
//
// **********************************************************************************************
var bValidRegistration

function isZipcode(zipValue)
{
	var len
	var digits = "0123456789"

	// Check if length of zip is 5 or 10
	if (zipValue.length == 5)
	   len = 5
	else if (zipValue.length == 10)
	   len = 10
	else
	// Bad length, return immediately
	   return false

	// Check first 5 characters, must all be digits.
	for (var i = 0; i < 5; i++)  {
	    if (digits.indexOf(zipValue.charAt(i)) < 0)
		//Alphas found, return
	        return false
	}
	
	// If a 5 digit zip, all is OK
	if (len == 5)
	    return true

	// For extended zips, the sixth character must be "-"
	if (zipValue.charAt(5) != '-')
	    return false

	// Now the last 4 characters must be digits
	for (var i = 6; i< 10; i++) {
	    if (digits.indexOf(zipValue.charAt(i)) < 0)
		return false
	}
	
	// Good zip code at this point
	return true
}


function isValidExpDate(expdate)
{
	var len
	var digits = "0123456789"
	var i

	// Check if length of expirationg date is 5 characters long
	if (expdate.length == 5)
	   len = 5
	else
	// Bad length, return
	   return false

	// Check first 2 characters, must all be digits.
	for (var i = 0; i < 2; i++) 
	{
	    if (digits.indexOf(expdate.charAt(i)) < 0)
		//Alphas found, return
	        return false
	}
	
	// The 3rd character must be "/"
	if (expdate.charAt(2) != '/')
	    return false

	// The 2 characters after the "/" must be digits
	for (i = 3; i < 5; i++) 
	{
	    if (digits.indexOf(expdate.charAt(i)) < 0)
		return false
	}
	
    // Correct credit card expiration date in mm/yy format was entered
	return true
}


function isValidEmail(addr)
{
    var sign=addr.indexOf("@");
    var before=addr.substring(0,sign);
    var after=addr.substring(sign+1,addr.length);
    var lower=after.toLowerCase();
    var coma=before.indexOf(",");
    var period=after.indexOf(".");
	var spc=addr.indexOf(" ");
	var strdomain=after

    berrorFlag=false
	if (sign==0)
	{
		alert("EMAIL: User name is missing before the @.");
		berrorFlag=true;
	}
		
	else if (sign==-1)
	{
		alert("EMAIL: You need an @ somewhere in the address.");
		berrorFlag=true;
	}
	else if (sign==(addr.length-1))
	{
		alert("EMAIL: Missing host name after the @.");
		berrorFlag=true;
	}
	else if(addr.indexOf("@",sign+1) != -1)
	{
		alert("EMAIL: Only one @ for valid email address.");
		berrorFlag=true;
	}
	else if(addr.indexOf("@.") != -1)
	{
		alert("EMAIL: Invalid email address format, @.");
		berrorFlag=true;
	}
	else if(addr.indexOf("..") != -1)
	{
		alert("EMAIL: Invalid email address format, ..");
		berrorFlag=true;
	}
	else if(after.indexOf(",") != -1)
	{
		alert("EMAIL: Invalid email address format");
		berrorFlag=true;
	}
	else if(after.indexOf("/") != -1)
	{
		alert("EMAIL: Invalid email address format");
		berrorFlag=true;
	}
	else if(after.lastIndexOf(".") == after.length-1)
	{
		alert("EMAIL: Address can't end in dot");
		berrorFlag=true;
	}	
	else if (coma>=0)
	{
    	alert("EMAIL: You must change the comma to a period in the first part of the address.")
		berrorFlag=true;
	} 
	else if (period==-1)
	{
		alert("EMAIL: You need a period in the second part of the address");
		berrorFlag=true;
	}
	else if (spc != -1)
	{
		alert("EMAIL: Please Remove any spaces from your email address");
		berrorFlag=true;
	}
    return !berrorFlag;
}

function isNumber(num)
{
	var digits = "0123456789"
	
	// Check characters, must all be digits.
	for (var i = 0; i < num.length; i++)
	{
	    if (digits.indexOf(num.charAt(i)) < 0)
		//Alphas found, return
	        return false
	}
	return true;
}

function isUSDate(myDate)
{

	var dayOfMonth = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
	
	
	var x = myDate.length;
	if(!((x == 6) || (x == 7) || (x == 8) || (x == 9) || (x == 10)))
		return false;

	var monthPos = myDate.indexOf("/");
	var dayPos = myDate.indexOf("/",monthPos+1);
	
	if((monthPos == -1) || (dayPos == -1))
		return false;

	var monthVal = (myDate.substring(0,monthPos));
	var dayVal = (myDate.substring(monthPos+1,dayPos));
	var yearVal = (myDate.substring(dayPos+1,myDate.length));
	
	if(monthVal.length == 2)
	{
		if(monthVal.charAt(0) == "0")
			monthVal = monthVal.charAt(1);
	}
	
	if(dayVal.length == 2)
	{
		if(dayVal.charAt(0) == "0")
			dayVal = dayVal.charAt(1);
	}
	
	if(yearVal.length == 2)
	{
		if(yearVal.charAt(0) == "0")
			yearVal = yearVal.charAt(1);
	}
	else if(yearVal.length == 1)
		return false;
	
	if(isNaN(monthVal) || (parseInt(monthVal) > 12))
		return false;
	
	if(isNaN(dayVal) || (parseInt(dayVal) > dayOfMonth[parseInt(monthVal)]))
		return false;
	
	if(yearVal.length == 1 || yearVal.length == 2)
	{
		if(isNaN(yearVal) || (parseInt(yearVal) > 99) || parseInt(yearVal) < 0)
			return false;
	}
	else if(yearVal.length == 4)
	{
		if(isNaN(yearVal) || (parseInt(yearVal) > 9999) || (parseInt(yearVal) < 1900))
			return false;	
	}
	else
		return false;
		
	return true;
}

function isDate(myDate)
{
	if(isNaN(Date.parse(myDate)))
		return false;
	
	return true;
}

function isSerialNumber(serNum, low, high)
{
	if(isFinite(serNum))
	{
		if(serNum >= low && serNum <= high)
			return true;
		else
			return false;
	}
	return false;
}

function isValidRegistration()
{
	return bValidRegistration
}

function setValidRegistration(bool)
{
	bValidRegistration = bool
}

function CheckField(field,strMsg)
{
	var x,radioSelected;

	if(bValidRegistration)
	{
		if((field.type == "text") || (field.type == "password") || (field.type == "hidden") || (field.type == "textarea"))
		{
			if(field.value == "")
			{
				alert(strMsg);
				field.focus();
				bValidRegistration = false;
			}
		}
		else if((field.type == "select-one") || (field.type == "select-multiple"))
		{
			if(field.options[field.options.selectedIndex].value == "")
			{
				alert(strMsg);
				field.focus();
				bValidRegistration = false;
			}
		}
		else if(field[0].type == "radio")
		{
			radioSelected = false;
			x=0;
			while(field[x])
			{
				if(field[x].type == "radio")
				{
					if(field[x].checked)
						radioSelected = true;
				}
				x++;
			}
			if(!radioSelected)
			{
				alert(strMsg);
				field[0].focus();
				bValidRegistration = false;
			}
		}
	}
}

function CheckUSDateField(field,strMsg)
{
	CheckField(field,strMsg);
	if(bValidRegistration)
	{
		setValidRegistration(isUSDate(field.value));
		if(!isValidRegistration())
		{
			alert(strMsg);
			field.focus();
		}
	}
}

function CheckEmailField(field,strMsg)
{
	CheckField(field,strMsg);
	if(bValidRegistration)
	{
		bValidRegistration = isValidEmail(field.value);
		if(!bValidRegistration)
			field.focus();
	}
}