/* @author Soup */

function validation(MyForm)
{
	
	var dFName 		= document.getElementById("first_name");
	var dLName 		= document.getElementById("last_name");
	var dEmail 		= document.getElementById("email");
	var dhomephone 	= document.getElementById("phone");
	var position    =  document.getElementById("seljob");

	var Errors = 0;
	
	/************************ First Name ************************/
	var dfirstnamelength = dFName.value
	if(dFName.value =="")
	{
		document.getElementById("FName").style.display = "block";
		document.getElementById("FName").innerHTML = "Please provide your first name.<BR>";
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//		dFName.focus();
		Errors |= 1;
	}
	else if (!isAlpha(dFName.value)) 
	{ 
		 document.getElementById("FName").style.display = "block";
		 document.getElementById("FName").innerHTML = "You first name contains invalid characters.<BR>";
		 document.getElementById("error").style.display = "block";
		 document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//		 dFName.focus();
		 Errors |= 1;
	}
	else if(dfirstnamelength.length > 30 )
	{
		document.getElementById("FName").style.display = "block";
		document.getElementById("FName").innerHTML = "You've exceeded the maximum length for your first name.<BR>";
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//		dFName.focus();
		Errors |= 1;
	} else {
		document.getElementById("FName").style.display = "none";
		document.getElementById("error").style.display = "none"
		Errors |= 0;
	}
	
	
	/************************ Last Name ************************/
	var dlastnamelength = dLName.value
	if(dLName.value =="")
	{
		document.getElementById("LName").style.display = "block";
		document.getElementById("LName").innerHTML = "Please provide your last name.<BR>";
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//		dLName.focus();
		Errors |= 2
	}
	else if (!isAlpha(dLName.value)) 
	{ 
		 document.getElementById("LName").style.display = "block";
		 document.getElementById("LName").innerHTML = "Your last name contains invalid characters.<BR>";
		 document.getElementById("error").style.display = "block";
		 document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//		 dLName.focus();
		 Errors |= 2
	}
	else if(dlastnamelength.length > 30 )
	{
		document.getElementById("LName").style.display = "block";
		document.getElementById("LName").innerHTML = "You've exceeded the maximum length for your last name.<BR>";
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//		dLName.focus();
		Errors |= 2
	}else {
		document.getElementById("LName").style.display = "none";
		Errors |= 0;
	}

	
	
	
	/************************  Home Phone Number  ************************/
	if(dhomephone.value ==""  )
	{
		//document.getElementById("HomePhone").innerHTML = "Please provide your phone number.<BR>";	
		if (dhomephone.value =="")
		{
			document.getElementById("HomePhone").style.display = "block";
			document.getElementById("HomePhone").innerHTML = "Please provide your valid phone number.<BR>";	
			document.getElementById("error").style.display = "block";
			document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//			dhomephone.focus();
			Errors |= 64
		}			
		
		else {
			document.getElementById("HomePhone").style.display = "none";
			document.getElementById("error").style.display = "none";
			Errors |= 0
		}
	}
	else if (!isPhoneChars(dhomephone.value)) 
		{
			document.getElementById("HomePhone").style.display = "block";
			document.getElementById("HomePhone").innerHTML = "Please provide your valid Numeric phone number.<BR>";	
			document.getElementById("error").style.display = "block";
			document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//			dhomephone.focus();
			Errors |= 1
		}
	else
	{
		document.getElementById("HomePhone").style.display = "none";
		document.getElementById("error").style.display = "none";
		Errors |= 0
	}
	/************************ Email Address ************************/
	var demaillength = dEmail.value
	if(dEmail.value =="")
	{
		document.getElementById("EmailAddress").style.display = "block";
		document.getElementById("EmailAddress").innerHTML = "Please provide your E-mail address.<BR>";
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//		dEmail.focus();
		Errors |= 4
	}
	else if (!validateEmail(dEmail.value)) 
	{ 
		 document.getElementById("EmailAddress").style.display = "block";
		 document.getElementById("EmailAddress").innerHTML = "Please provide a valid E-mail address.<BR>";
		 document.getElementById("error").style.display = "block";
		 document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//		 dEmail.focus();
		 Errors |= 4
	}
	else if(demaillength.length > 30 )
	{
		document.getElementById("EmailAddress").style.display = "block";
		document.getElementById("EmailAddress").innerHTML = "You've exceeded the maximum length for an E-mail address.<BR>";
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML="PLEASE FIX THE FOLLOWING ERRORS AND RE-SEUBMIT";
//		dEmail.focus();
		Errors |= 4
	}
	else {
		document.getElementById("EmailAddress").style.display = "none";
		document.getElementById("error").style.display = "none"
		Errors |= 0
	}
	
	
	//if(position.value =="")
	//{
		//document.getElementById("Position").style.display = "block";
		//document.getElementById("Position").innerHTML = "Please provide your job opportunity.<BR>";
	//}
	//else 
	//{
		//document.getElementById("Position").style.display = "none";
		//Errors |= 0
	//}

		
	
	if(Errors != 0)
	{
	return false;
	}
	
	
}

function isPhoneChars(n) {
return /^[0-9 ()-]*$/.test(n);
}

// Returns true if the string only contains alpha characters (empty string = true)&nbsp;
function isAlpha(txt)
{
	return ValidString(txt,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-. ');
}

// Returns true if the string only contains numeric characters (empty string = true)
function isNumeric(txt)
{
	return ValidString(txt,'0123456789');
}

// Returns true if the string only contains alpha numeric characters (empty string = true)
function isAlphaNumeric(txt)
{
	return ValidString(txt,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
}
function isPostalcode(txt)
{
	return ValidString(txt,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-');
}
// Returns true if the CheckString only contains characters passed in ValidString (empty string = true)
function ValidString(ChkString,ValidString)
{
	for (i=0; i<ChkString.length; i++)
	{
		if (ValidString.indexOf(ChkString.substring(i,i+1)) == -1) return false;
	}
	return true;
}

function validateEmail(email)
{
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

