function isCreditCard(ccNum)
{
	if(trim(ccNum).length)
	{	  
		if (isNaN(ccNum)) 
	      	return false;
	   
	   	if (ccNum.length < 16) 
	     	return false;
	    else
			return true;		
	}
	else
		return true;
}

function trim(strText)
{
	while (strText.substring(strText.length-1,strText.length) == ' ')
    	strText = strText.substring(0, strText.length-1);
	while (strText.substring(0,1) == ' ')
    	strText = strText.substring(1, strText.length);
	return strText;
}

//checks syntax of zip code
function isZip(zipcode)
{
	if(zipcode.length)
	{
		if(zipcode.length!=5 && zipcode.length!=10)
			return false;
		else if(isNaN(zipcode.substring(0,5)))
			return false;
		else if(zipcode.length==10 && isNaN(zipcode.substring(6,zipcode.length-1)))
			return false;
		else
			return true;
	}
	else
		return true;
}

// Checks syntax of email address.
function isEmail(Email) { 
	// List of unacceptable characters.
	invalidChars = " /;,:#";
	
	// Return if email address is an empty string.
	if (trim(Email) == "") {
		return true;   // return true if not required
	}
	
	// Loop over list of unacceptable characters and make sure that it doesn't exist in the email address.
	for (var i=0; i < invalidChars.length; i++) { 
		badChar = invalidChars.charAt(i);
		
		if (Email.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	
	// Character position of at symbol in email address.
	atPos = Email.indexOf("@",1);
	
	// If no at symbol is found, return false.
	if (atPos == -1) {
		return false;
	}
	
	// If a second at symbol is found, return false.
	if (Email.indexOf("@", atPos + 1) != -1) {
		return false;
	}
	
	// Character position of period in email address.
	periodPos = Email.indexOf(".", atPos)
	
	// If no period is found, return false.
	if(periodPos == -1)	{
		return false;
	}
	
	// If no domain name (found between @ and .) is found, return false.
	if (atPos + 2 > periodPos) {
		return false;
	}
	
	// A period can not be the last or first character in the email address.
	if (Email.charAt(Email.length - 1) == "." || Email.charAt(0) == ".") {
		return false;
	}
	
	// An at symbol cannot be the last or first character in the email address.
	if (Email.charAt(Email.length - 1) == "@" || Email.charAt(0) == "@") {
		return false;
	}
	
	// If everything checks out, return true.
	return true;
}	


function doCheck(theForm)
{
	// required field validation
	for(i=0; i<theForm.elements.length; i++)
	{
		if(theForm.elements[i].name){
		if(theForm.elements[i].name.substring(theForm.elements[i].name.length-9,theForm.elements[i].name.length)=="_required")
		{
			var ffield=theForm.elements[i].name.substring(0,theForm.elements[i].name.indexOf("_required"));
			if(theForm.elements[ffield].type==null)
				fieldType=theForm.elements[ffield][0].type;
			else
				fieldType=theForm.elements[ffield].type;
			
			switch(fieldType)
			{
				case "text":
				{
					if(!trim(theForm.elements[ffield].value).length)
					{
						alert(theForm.elements[ffield+'_required'].value);
						theForm.elements[ffield].focus();
						return false;
					}
					break;
				}
				case "password":
				{
					if(!trim(theForm.elements[ffield].value).length)
					{
						alert(theForm.elements[ffield+'_required'].value);
						theForm.elements[ffield].focus();
						return false;
					}
					break;
				}
				case "textarea":
				{
					if(!trim(theForm.elements[ffield].value).length)
					{
						alert(theForm.elements[ffield+'_required'].value);
						theForm.elements[ffield].focus();
						return false;
					}
					break;
				}
				case "radio":
				{
					if(theForm.elements[ffield].length==null)
					{
						if(!theForm.elements[ffield].checked)
						{
							alert(theForm.elements[ffield+'_required'].value);
							theForm.elements[ffield].focus();
							return false;
						}
					}
					else
					{
						var isChecked=false;
						for(j=0; j<theForm.elements[ffield].length; j++)
						{
							if(theForm.elements[ffield][j].checked)
							{
								isChecked=true;
								break;
							}
						}
						if(isChecked==false)
						{
							
							alert(theForm.elements[ffield+'_required'].value);
							theForm.elements[ffield][0].focus();
							return false;
						}
					}
					break;
				}
				case "checkbox":
				{
					if(theForm.elements[ffield].length==null)
						{
							if(!theForm.elements[ffield].checked)
							{
								alert(theForm.elements[ffield+'_required'].value);
								theForm.elements[ffield].focus();
								return false;
							}
						}
					else
					{
						var isChecked=false;
						for(j=0; j<theForm.elements[ffield].length; j++)
						{
							if(theForm.elements[ffield][j].checked)
							{
								isChecked=true;
								break;
							}
						}
						if(isChecked==false)
						{
							
							alert(theForm.elements[ffield+'_required'].value);
							theForm.elements[ffield][0].focus();
							return false;
						}
					}
					break;
				}
				case "select-one":
				{
					if(!trim(theForm.elements[ffield].options[theForm.elements[ffield].options.selectedIndex].value).length)
					{
						alert(theForm.elements[ffield+'_required'].value);
						theForm.elements[ffield].focus();
						return false;
					}
				}
				case "select-multiple":
				{

					var isSelected=false;
					for(k=0; k<theForm.elements[ffield].options.length; k++)
					{
						if(theForm.elements[ffield].options[k].selected && trim(theForm.elements[ffield].options[k].value).length)
						{
							isSelected=true
							break;						
						}
					}
					
					if(!isSelected)
					{
						alert(theForm.elements[ffield+'_required'].value);
						theForm.elements[ffield].focus();
						return false;
					}
				}
			}
		}
		}
	}
	// numerical validation
	for(i=0; i<theForm.elements.length; i++)
	{
		if(theForm.elements[i].name){
		if(theForm.elements[i].name.substring(theForm.elements[i].name.length-8,theForm.elements[i].name.length)=="_numeric")
		{
			var ffield=theForm.elements[i].name.substring(0,theForm.elements[i].name.indexOf("_numeric"));
			if(theForm.elements[ffield].value.length && isNaN(theForm.elements[ffield].value))
			{
				alert(theForm.elements[i].value);									
				theForm.elements[ffield].focus();
				return false;
			}
		}
		}
	}
	// zipcode validation
	
	// zipcode validation
	for(i=0; i<theForm.elements.length; i++)
	{
		if(theForm.elements[i].name){
		if(theForm.elements[i].name.substring(theForm.elements[i].name.length-8,theForm.elements[i].name.length)=="_zipcode")
		{
			var ffield=theForm.elements[i].name.substring(0,theForm.elements[i].name.indexOf("_zipcode"));
			if(!isZip(trim(theForm.elements[ffield].value)))
			{
				alert("Invalid zip code entered.  Please enter 5 digit zip code like xxxxx or 9 digit zip code like xxxxx-xxxx.");
				theForm.elements[ffield].focus();
				return false;
			}
		}
		}
	}
	
	// date validation
	for(i=0; i<theForm.elements.length; i++)
	{
		if(theForm.elements[i].name){
		if(theForm.elements[i].name.substring(theForm.elements[i].name.length-5,theForm.elements[i].name.length)=="_date")
		{
			var ffield=theForm.elements[i].name.substring(0,theForm.elements[i].name.indexOf("_date"));
			if(trim(theForm.elements[ffield].value).length > 0 && !isDate(trim(theForm.elements[ffield].value)))
			{
				alert(theForm.elements[i].value);
				theForm.elements[ffield].focus();
				return false;
			}
		}
		}
	}
	
	// credit card validation
	for(i=0; i<theForm.elements.length; i++)
	{
		if(theForm.elements[i].name){
		if(theForm.elements[i].name.substring(theForm.elements[i].name.length-11,theForm.elements[i].name.length)=="_creditcard")
		{
			var ffield=theForm.elements[i].name.substring(0,theForm.elements[i].name.indexOf("_creditcard"));
			if(trim(theForm.elements[ffield].value).length > 0 && !isCreditCard(trim(theForm.elements[ffield].value)))
			{
				alert(theForm.elements[i].value);
				theForm.elements[ffield].focus();
				return false;
			}
		}
		}
	}

	// integer validation
	for(i=0; i<theForm.elements.length; i++)
	{
		if(theForm.elements[i].name){
		if(theForm.elements[i].name.substring(theForm.elements[i].name.length-8,theForm.elements[i].name.length)=="_integer")
		{
			var ffield=theForm.elements[i].name.substring(0,theForm.elements[i].name.indexOf("_integer"));
			if(trim(theForm.elements[ffield].value).length > 0 && !isInteger(trim(theForm.elements[ffield].value)))
			{							
				alert(theForm.elements[i].value);
				theForm.elements[ffield].focus();
				return false;
				//break;
			}
		}
		}
	}		
	
	// email validation
	for(i=0; i<theForm.elements.length; i++)
	{
		if(theForm.elements[i].name){
		if( ((theForm.elements[i].name.indexOf("email") >= 0)||(theForm.elements[i].name.indexOf("Email") >= 0)) && theForm.elements[i].name.indexOf("_required") < 0 && theForm.elements[i].name.indexOf("_verify") < 0)
		{
			emailField = theForm.elements[i]; //theForm.elements[theForm.elements[i].name.substring(0,theForm.elements[i].name.indexOf("_required"))];
			if(emailField.value.indexOf(";") >= 0)
				// Replace semi-colons with commas in the list of referred email addresses.
				emailField.value = emailField.value.replace(/;/g, ",");

			if(emailField.value.indexOf(",") >= 0)
			{
				var addArray=emailField.value.split(",");
				for(x=0; x<addArray.length; x++)
				{
					if( !isEmail(trim(addArray[x])) )
					{
						alert("Invalid email address: "+trim(addArray[x]));
						theForm.elements[i].focus();
						return false;
					}
				}
			}
			else
			{
				if( (theForm.elements[i].type != "hidden") && !isEmail(trim(emailField.value)) )
				{
					alert("Invalid email address: "+trim(emailField.value));
					//theForm.elements[theForm.elements[i].name.substring(0,theForm.elements[i].name.indexOf("_required"))].focus();
					theForm.elements[i].focus();
					return false;
				}
			}
		}
		}
	}
	// email verification
	for(i=0; i<theForm.elements.length; i++)
	{
		if(theForm.elements[i].name){
		if (theForm.elements[i].name.indexOf("_verify") >= 0)
		{
			emailField=theForm.elements[theForm.elements[i].name.substring(0,theForm.elements[i].name.indexOf("_verify"))];			
			if ( theForm.elements[i].value !=  emailField.value ) 
			{
				alert("Please verify that you have entered your e-mail address correctly in both fields.");
				theForm.elements[i].focus();
				return false;
			}
		}
		}
	}		
	// If everything checks out, return true.
	return true;
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

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){		
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}
