var whitespace = " \t\n\r";
var phoneNumberDelimiters = "()- ";
var digitsInUSPhoneNumber = 10;
var ZIPCodeDelimiters = "-";
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
decimalPointDelimiter = ".";

//parameter for date validation function
var dtCh= "/";

//to delete a cookie
function deleteCookie(name, path, domain)
	{
		if (getCookie(name))
			{
			    document.cookie = name + "=" + 
								  ((path) ? "; path=" + path : "") +
								  ((domain) ? "; domain=" + domain : "") +
								   "; expires=Thu, 01-Jan-70 00:00:01 GMT";
			}
	}
	
//to get a cookie
function getCookie(name) 
	{
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) 
			{
				begin = dc.indexOf(prefix);
				if (begin != 0) return null;
			}
		else
		  begin += 2;
		var end = document.cookie.indexOf(";", begin);
		if (end == -1)
		  end = dc.length;
		return unescape(dc.substring(begin + prefix.length, end));
	}	
	
//to set a cookie with one day expiration
function setCookie(name,value)
	{
		deleteCookie(name)
	    var expire = new Date();
	    var curCookie = name + "=" + escape(value) ;
		//+"; 
		//expires=" + expire.toGMTString() 
 	    document.cookie = curCookie;
	 	    
	}
	
// Check whether string s is empty.
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}
	
function isWhitespace (s)
{
	
	var i;

	// Is s empty?
	if (isEmpty(s)) return true;

	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.

	for (i = 0; i < s.length; i++)
	{   
	    // Check that current character isn't whitespace.
	    var c = s.charAt(i);

	    if (whitespace.indexOf(c) == -1) return false;
	}

	// All characters are whitespace.
	return true;
}


// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{  
	return ((c >= "0") && (c <= "9"))
}


function isInteger (s)

{   
	var i;
	// Search through string's characters one by one
	// until we find a non-numeric character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number.
   			var c = s.charAt(i);
	       if (!isDigit(c)) return false;
	}
	// All characters are numbers.
	return true;
}

function isFloat (s)
{   
	var i;
	var seenDecimalPoint = false;

		

	if (s == decimalPointDelimiter) return false;

	// Search through string's characters one by one
	// until we find a non-numeric character.
	// When we do, return false; if we don't, return true.

	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number.
	        var c = s.charAt(i);

	        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
	        else if (!isDigit(c)) return false;
	}
		
	// All characters are numbers.
	return true;
}

//date validation function

function daysInFebruary (year)
	{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}
	
function DaysArray(n)
	 {
		for (var i = 1; i <= n; i++) {
			this[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
			if (i==2) {this[i] = 29}
	 } 
	 return this
	}
	
function isDate(dtStr)
{

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) 
		{
			strDay=strDay.substring(1);
		}
	if (strMonth.charAt(0)=="0" && strMonth.length>1) 
		{
			strMonth=strMonth.substring(1);
		}
	for (var i = 1; i <= 3; i++) 
		{
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if (pos1==-1 || pos2==-1)
		{
			alert("The date format should be : mm/dd/yyyy")
			return false
		}
	if (strMonth.length<1 || month<1 || month>12)
		{
			alert("Please enter a valid month")
			return false
		}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
		{
			alert("Please enter a valid day")
			return false
		}
	if (strYear.length != 4 || year==0 )
		{
			alert("Please enter a valid 4 digit year")
			return false
		}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
		{
			alert("Please enter a valid date")
			return false
		}
	
	return true
}

function isDateYrTwoDigit(dtStr)
{

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) 
		{
			strDay=strDay.substring(1);
		}
	if (strMonth.charAt(0)=="0" && strMonth.length>1) 
		{
			strMonth=strMonth.substring(1);
		}
	for (var i = 1; i <= 3; i++) 
		{
			if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		
	month	=	parseInt(strMonth)
	day	=	parseInt(strDay)
	year	=	parseInt(strYr)
	
	if (pos1==-1 || pos2==-1)
		{
			alert("The date format should be : mm/dd/yy")
			return false
		}
	if (strMonth.length<1 || month<1 || month>12)
		{
			alert("Please enter a valid month")
			return false
		}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
		{
			alert("Please enter a valid day")
			return false
		}
	if (strYear.length != 2)
		{
			alert("Please enter a valid 2 digit year")
			return false
		}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
		{
			alert("Please enter a valid date")
			return false
		}
	
	return true
}
	
function isUSPhoneNumber (s)
{   
    
	return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

function reformat (s)

{
	var arg;
	var sPos = 0;
	var resultString = "";

	for (var i = 1; i < reformat.arguments.length; i++)
	{
		arg = reformat.arguments[i];
 		if (i % 2 == 1) resultString += arg;
    		else 
		{
    			resultString += s.substring(sPos, sPos + arg);
       			sPos += arg;
   			}
		}
	return resultString;
}

function reformatUSPhone (USPhone)
{  
	return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}

	
// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)
{   
	var i;
	var returnString = "";

	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.

	for (i = 0; i < s.length; i++)
	{   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}

	return returnString;
}


function checkUSPhone(theField)
{
		
	var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
	if (!isUSPhoneNumber(normalizedPhone, false)) 
	{
		alert("Not a valid Phone Number")
		theField.select()
		theField.focus()
		return false
	}
	else 
	{  
    	theField.value = reformatUSPhone(normalizedPhone)
		return true;
	}
		
}

function checkUSFax(theField)
{
		
	var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
	if (!isUSPhoneNumber(normalizedPhone, false)) 
	{
		alert("Not a valid Fax Number")
		theField.select()
		theField.focus()
		return false
	}
	else 
	{  
    	theField.value = reformatUSPhone(normalizedPhone)
		return true;
	}
		
}
	
function isEmail (s)
{  

	// there must be >= 1 character before @, so we
	// start looking at character position 1 
	// (i.e. second character)
	var i = 1;
	var sLength = s.length;

	// look for @
	while ((i < sLength) && (s.charAt(i) != "@"))
	{ 
		i++
	}

	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;

	// look for .
	while ((i < sLength) && (s.charAt(i) != "."))
	{ i++
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else return true;
}


function checkEmail (theField)
{   
	if (!isEmail(theField.value)) 
	{
	    alert("Not a valid Email")
		theField.select()
		theField.focus()
		return false
	}
	else return true;
}
	
	

function isZIPCode (s)
{  
	return (isInteger(s) && 
             ((s.length == digitsInZIPCode1) ||
    	     (s.length == digitsInZIPCode2)))
}

function reformatZIPCode (ZIPString)
{   
	if (ZIPString.length == 5) return ZIPString;
	else return (reformat(ZIPString, "", 5, "-", 4));
}
	
function checkUSZip(theField)
{
	var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)
	if (!isZIPCode(normalizedZIP)) 
	{
		alert("Not a valid Zip Code Format")
		theField.select()
		theField.focus()
		return false	
	}	
	else 
	{  
		// if you don't want to insert a hyphen, comment next line out
    		theField.value = reformatZIPCode(normalizedZIP)
			
    		return true;
    }
}