
//Check for empty field
function isEmpty(testChar,FieldName)
{
	if(testChar.value == "")
	{
		alert(FieldName + " is required");
		testChar.focus();
		return false;
	}
	else
		return true;
}

//Check for Numeric value
function checkDigit(testChar,FieldName)
{
	var validChar="0123456789";
	var field=testChar.value;
	for (i=0;i < field.length; i++)
	{
		temp = "" + field.substring(i, i+1);
		if (validChar.indexOf(temp) == "-1")
		{
			alert("Number required for "+FieldName);
			testChar.focus();
			testChar.select();			   
			return false;
		}
	}
	return true;
}
// Check for Special Character
function checkSpecialChar(testChar,theElementName)
{
	var iChars = "0123456789!@#$%^&*()+=-_`~[]\\\';,./{}|\":<>?";

	for (var i = 0; i < testChar.value.length; i++)
	{
  		if (iChars.indexOf(testChar.value.charAt(i)) != -1) 
  		{
  			alert ("Only alphabets needed for "+theElementName);
  			testChar.focus();
  			testChar.select();
  			return false;
  		}
	}
	return true;
}
// Validation for Street
function checkStreet(testChar)
{
	var iChars = "!@$%^&*()+=_`~[]\\\';{}|\"<>?";

	for (var i = 0; i < testChar.value.length; i++)
	{
  		if (iChars.indexOf(testChar.value.charAt(i)) != -1) 
  		{
  			alert ("Your field has special characters. \n Please remove them and try again.");
  			testChar.focus();
  			return false;
  		}
	}
	return true;
}
// Validating name
function checkName(testChar)
{
	var iChars = "0123456789!@#$%^&*()+=-_`~[]\\\';,./{}|\":<>? ";

	for (var i = 0; i < testChar.value.length; i++)
	{
  		if (iChars.indexOf(testChar.value.charAt(i)) != -1) 
  		{
  			alert ("Your field has special characters or space. \n Please remove them and try again.");
  			testChar.focus();
  			return false;
  		}
	}
	return true;
}
//Validating Phone Numbers With Area Code
function isPhoneNumber(theElement, theElementName) 
{
var str,c,i,len;
str = theElement.value;
if (str.length != 12) 
{ 
	alert(theElementName + " must be of format (***) - *** - **** .");
	return false;
}
len = str.length;
for (i = 0; i < len; i++) 
{
	c = str.charAt(i);
	if ((i == 3) || (i == 7)) 
	{
		if (c != "-") 
		{ 
			alert( theElementName + " must be of format(***) - *** - ****. ");
			return false;
		}
	} 
	else if ((c < "0") || (c > "9")) 
	{ 
		alert( theElementName + " must be of format(***) - *** - ****. ");
		return false;
	}
}
}

//Validating Phone Numbers (Only phone numbers not area code(i.e the initial 3 digit area code))
function isPhone(theElement, theElementName) 
{
	var str,c,i,len;
	str = theElement.value;
	if (str.length != 8) 
	{ 
		alert(theElementName + " must be of format 555-1232");
		theElement.focus();
		theElement.select();
		return false;
	}
	len = str.length;
	for (i = 0; i < len; i++) 
	{
		c = str.charAt(i);
		
		if (i == 3) 
		{
			if (c != "-") 
			{ 
				alert(theElementName + " must be of format 555-1232");
				theElement.focus();
				theElement.select();
				return false;
			}
		} 
		else if ((c < "0") || (c > "9")) 
		{ 
			alert(theElementName + " must be of format 555-1232");
			theElement.focus();
			theElement.select();
			return false;
		}
	}
	return true;
}
//Email ID Validation
function checkEmail(testChar)
{
	var str=testChar.value;   
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var temp;
	var slen;
	var specialChar="`~!@#$%^&*()-+=\|{}[];':<>?/,"
	for (i=0;i<lat;i++)
	{
		temp=str.charAt(i);
		slen=specialChar.length;
		for (j=0;j<slen;j++)
		{
			if(temp==specialChar.charAt(j))
			{
				alert("Please Enter valid e-mail address.");
				testChar.focus();   
				return false;
			}
		}
	}
	if (str.substring(lat,lat+1)==dot)
	{
	alert("Please Enter valid e-mail address.");
	testChar.focus();   
	return false;
	}

	if (str.indexOf(at)==-1)
	{
	alert("Please Enter valid e-mail address.");
	testChar.focus();   
	return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
	alert("Please Enter valid e-mail address.");
	testChar.focus();   
	return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		alert("Please Enter valid e-mail address.");
		testChar.focus();   
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		alert("Please Enter valid e-mail address.");
		testChar.focus();   
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		alert("Please Enter valid e-mail address.");
		testChar.focus();   
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		alert("Please Enter valid e-mail address.");
		testChar.focus();   
		return false;
	}

	if (str.indexOf(" ")!=-1)
	{
		alert("Please Enter valid e-mail address.");
		testChar.focus();   
		return false;
	}
	return true;
}  
// Validation Password:- space bar and single quotes r not allowes

function checkPass(testChar)
{
	var iChars = " '";

	for (var i = 0; i < testChar.value.length; i++)
	{
  		if (iChars.indexOf(testChar.value.charAt(i)) != -1) 
  		{
  			alert ("Your field has special characters. \n Please remove them and try again.");
  			testChar.focus();
  			return false;
  		}
	}
	return true;
}

//Validate phone numbers
function isPhoneNumber(theElement, theElementName) 
{
	var str,c,i,len;
	str = theElement.value;
	if (str.length != 12) 
	{ 
		alert(theElementName + " must be of format (***) - *** - **** .");
		return false;
	}
	len = str.length;
	for (i = 0; i < len; i++) 
	{
		c = str.charAt(i);
		if ((i == 3) || (i == 7)) 
		{
			if (c != "-") 
			{ 
				alert( theElementName + " must be of format(***) - *** - ****. ");
				return false;
			}
		} 
		else if ((c < "0") || (c > "9")) 
		{ 
			alert( theElementName + " must be of format(***) - *** - ****. ");
			return false;
		}
	}
}

//Validate zip field
function validateZIP(field) 
{
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length!=5 && field.length!=10) 
	{
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		return false;
	}
	
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			alert("Invalid characters in your zip code. Please try again.");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
		{
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
			return false;
	   }
	}
}

//Validate Data field
function checkDate(datevar,monthvar,yearvar)
{
       var monthsarr =  new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
       if(monthvar==4 || monthvar==6 || monthvar==9 || monthvar==11)
          {
            if(datevar>30)
             {
               alert("Date cannot be greater than 30 in the month of " + monthsarr[monthvar-1]);
               return false;
             }
          }
       if(monthvar==2)
          {
            if(datevar>29)
             {
               alert("Date cannot be greater than 29 in the month of " + monthsarr[monthvar-1]);
               return false;
             }
            else
             {
               if(datevar==29 && !isLeap(yearvar))
                {
                   alert("29 FEB is an invalid Date since year " + yearvar + " is not a leap year");
                   return false;
                }
             }
          }
  		return true;
}
//Check for Leap Year
function isLeap(year)
{
      return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
}


