/* ************************************************************************************ */
/* Change Log                                                                           */
/*                                                                                      */
/* 23-06-2010  - Richard Chapman - Deltasoft - Support revised 'contact' form           */
/* 22-11-2010  - Richard Chapman - Deltasoft - Support custom 'contact' form            */
/* ************************************************************************************ */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){

	var emailID = document.emailcontact.email;
	var cemailID = document.emailcontact.cemail;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	
	if (emailID.value.toLowerCase() !== cemailID.value.toLowerCase())
	    {
        alert("Your E-mail address and confirmation e-mail address must be the same!")
		cemailID.focus()
		return false
	    }
	
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
 
 function setfield(f,v)
 {
 jQuery('#'+f).val(v);
 return true;
 }
 
 var iscustomformvalid;
 var errmsgisRequired;
 var errmsgisNumeric;
 var errmsgisEmail;
 
 function validatecustomform(f,r,n,e)
 {
 errmsgisRequired = r;
 errmsgisNumeric = n;
 errmsgisEmail = e;
 iscustomformvalid = true;
 
 // Loop through each input field on the form
 jQuery("#"+f+" :input").each(function(i){return validateinput(i,this);});
 
 return iscustomformvalid;
 }
 
 function validateinput(i,f)
 {
 var errmsg;
 
 if (jQuery(f).attr("isRequired") == "true")
    {
    if (jQuery(f).val() == "")
       {
       iscustomformvalid = false;
       errmsg = errmsgisRequired;
       }
    }
 if (iscustomformvalid)
    {
    if (jQuery(f).attr("isNumeric") == "true")
       {
       if (isNaN(jQuery(f).val()))
          {
          iscustomformvalid = false;
          errmsg = errmsgisNumeric;
          }
       }
    else if (jQuery(f).attr("isEmail") == "true")
       {
       if (!isvalidemailaddress(jQuery(f).val()))
          {
          iscustomformvalid = false;
          errmsg = errmsgisEmail;
          }
       }       
    }
    
 if (!iscustomformvalid)
    {
    alert(errmsg);
    jQuery(f).focus();
    }
    
 return iscustomformvalid;
 }
  
function isvalidemailaddress(emailaddress) 
{
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailaddress);
}
