/* ************************************************************************************ */
/* Change Log                                                                           */
/*                                                                                      */
/* 23-06-2010  - Richard Chapman - Deltasoft - Support revised 'contact' form           */
/* 22-11-2010  - Richard Chapman - Deltasoft - Support custom 'contact' form            */
/* 24-11-2010  - Richard Chapman - Deltasoft - Enhance custom 'contact' form with       */
/*                                             Checkboxes & linked Textboxes            */
/* ************************************************************************************ */

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 isattributeset(f, attrib)
 {
 if (jQuery(f).is('[' + attrib + ']'))
    return (jQuery(f).attr(attrib).toLowerCase() == "true");
 else
    return false;
 }
 
 function isattributepresent(f, attrib)
 {
 return (jQuery(f).is('[' + attrib + ']'))
 }
 
 function validateinput(i,f)
 {
 var errmsg;
 
 if (isattributeset(f,"isRequired"))
    {
    if (jQuery(f).val() == "")
       {
       iscustomformvalid = false;
       errmsg = errmsgisRequired;
       }
    }

 if (iscustomformvalid)
    {
    // Ensure that any linked textboxes have their corresponding checkbox ticked if they have a value
    // Just in case the visitor has side stepped the live javascript on field change
    if (isattributeset(f,"isLinked"))
        {
        if (jQuery(f).val() !== "")
            linkedtextboxchanged(f);
        }
 
    if (isattributeset(f,"isLinkSetRequired"))
        {
        if (jQuery(f).val() == "")
            {
            // Empty field only invalid if the linked checkbox/radio field is checked
            if (isattributepresent(f, "LinkID"))
                {
                var linkobj = '#' + jQuery(f).attr("LinkID");
                if (jQuery(linkobj).is("input[type='checkbox']") || jQuery(linkobj).is("input[type='radio']")) 
                    {
                    if (jQuery(linkobj).is(':checked'))
                        {
                        iscustomformvalid = false;
                        errmsg = errmsgisRequired;
                        }            
                    }
                }
            }            
        }   
    }
        
 if (iscustomformvalid)
    {
    if (isattributeset(f,"isNumeric"))
       {
       if (isNaN(jQuery(f).val()))
          {
          iscustomformvalid = false;
          errmsg = errmsgisNumeric;
          }
       }
    else if (isattributeset(f,"isEmail"))
       {
       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);
}

function linkedtextboxchanged(textbox)
{
if (isattributeset(textbox,"isLinked")) {
    // Fetch linked control
    if (isattributepresent(textbox,"LinkID"))
        {    
        var linkobj = '#' + jQuery(textbox).attr("LinkID");
        if (jQuery(linkobj).is("input[type='checkbox']") || jQuery(linkobj).is("input[type='radio']"))
            {
            if (!jQuery(linkobj).is(':checked'))
                jQuery(linkobj).attr("checked",true);
            }
        }
    }
}
