//function for details.page

// function to trim leading & trailing spaces
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

/*
1. PHONE NUMBER VALIDATION
Usage: 
Element  name of the control, like frm.phone
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

 if(!isValidPhone(frm.phone,'Phone Number','yes'))
 return;
*/
function isValidPhone(element, msg, required)
{	
	var VarPhone = element.value;
	if (VarPhone== "")
	{	
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if(VarPhone.length < 10)
	{
	   alert("Phone number must not be less than 10 digits.");
	   element.focus();
	   return false;
	}
	if (VarPhone != "")
	{
		var Phno;
		Phno=VarPhone;
		var valid = "+-0123456789() ";
		var hyphencount = 0;
		for (var i=0; i < Phno.length; i++) 
		{
			temp = "" + Phno.substring(i, i+1);
			if (valid.indexOf(temp) == "-1")
			{
				alert("Invalid characters in your "+msg+". Please try again.");
				element.focus();
				return false;
			}
		}
     } 
	 return true;      
}

/*
3. EMAIL ADDRESS VALIDATION

Usage: 
Element  name of the control, like frm.email
Required  Set this to yes if the field is mandatory, otherwise no.

	 if(!isValidEmail(frm.email,'yes'))
	 return;
*/

function isValidEmail(element, required)
{
	var VarEmail = element.value;
	if(VarEmail == "")
	{
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
			alert("Please enter Email Address");
			element.focus();
			return false;
		}
	}	
	if(VarEmail != "")
	{
		var emailStr = VarEmail;
		 
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var firstChars=validChars
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom="(" + firstChars + validChars + "*" + ")"
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) 
		{
			 alert("Email address seems to be incorrect (check @ and .'s)");
			 element.focus();
			 return false;
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		if (user.match(userPat)==null) 
		{
			alert("The Email Address doesn't seem to be valid.");
			element.focus();
			return false;
		}
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) 
		{
			for (var i=1;i<=4;i++) 
			{
				if (IPArray[i]>255) 
				{
					 alert("Destination IP address is invalid!");
					 element.focus();
					 return false;
				}
			}
		}
		var domainArray=domain.match(domainPat)
		if (domainArray==null) 
		{
			alert("The domain name doesn't seem to be valid.");
			element.focus();
			return false;
		}
		var atomPat=new RegExp(atom,"g");
		var domArr=domain.match(atomPat);
		var len=domArr.length;
		if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
		{
		   alert("The address must end in a three-letter domain, or two letter country.");
		   element.focus();
		   return false;
		}
		if (domArr[domArr.length-1].length==2 && len<3) 
		{
			var errStr = "This address ends in two characters, which is a country";
			errStr    += " code.  Country codes must be preceded by ";
			errStr	  += "a hostname and category (like com, co, pub, pu, etc.)";
			alert(errStr);
			element.focus();
			return false;
		}
		if (domArr[domArr.length-1].length==3 && len<2) 
		{
			 var errStr="This address is missing a hostname!";
			 alert(errStr);
			 element.focus();
			 return false;
		}
	}
	return true;
}
/* 
4. ZIPCODE/PINCODE  VALIDATION

Usage:
	Element  name of the control, like frm.zipcode
	Required  Set this to yes if the field is mandatory, otherwise no.

	if(!isValidZipcode(frm.zip,'yes'))
	return;
*/
function isValidZipcode(element,required) 
{
	var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789- ";
	var hyphencount = 0;
	var field = element.value;
	var str   = required;
	if(field == "")
	{
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
			alert("Please enter ZipCode");
			element.focus();
			return false;
		}
	}	
	if (field != "")
	{
		if (field.length!=6 && field.length!=5 && field.length!=10) 
		{
			alert("Postcode length should be at least 5 5 characters ");
			element.focus();
			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 post code.  Please try again.");
				element.focus();
				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.");
				element.focus();
				return false;
			}
		}
	}
	return true;
}
