
//==========TRIM=================
function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}
function CheckTrim(str)
{  
	while(str.charAt(0) == (" ") )
	{  str = str.substring(1);
	}
	while(str.charAt(str.length-1) == " " )
	{  str = str.substring(0,str.length-1);
	}
	return str;
}
//==========TRIM=================
function IsChar(sText)
{
	var ValidChars = "0123456789 -+";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = "false";
			//alert ("please enter numeric only.");						
		}
	}
	
	return IsNumber;   
}//number validation
function emailvalid(sText)
{
	var IsNumber=true;
	var pattern1=/[._]$/;
	var pattern2=/\._|_\./;
    if(sText.indexOf("__")!==-1)
    {4
	IsNumber = "false";  
    }else if(sText.indexOf("..")!==-1)
    {
    IsNumber = "false";  
    }else if(sText.match(pattern1)!==null)
    {
    IsNumber = "false";  
    }else if(sText.match(pattern2)!==null)
    {
    IsNumber = "false";  
    }else if(sText.indexOf("--")!==-1)
    {
    IsNumber = "false";  
    }
        
    return IsNumber;
}
function isValidEmail(email)
{ 
    var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/ 
    if(RegExp.test(email))
	{ 
        return true; 
    }
	else
	{ 
        return false; 
    } 
}
function checkField(){ 
    var enquiryFrm = document.enquiryFrm, error = ""; 
    if(!isValidEmail(enquiryFrm.email.value)){ 
        error += 'Please enter a valid email address\n'; 
    } 
    if(error != ""){ 
        alert(error); 
        return false; 
    }else{ 
        return true; 
    } 
} 
function counter(sText,charTest)
{
	var aposcnt;
	var cnt=0;
	var ch1;
	aposcnt="true";
	ch1=sText.charAt(0);
	if(ch1==charTest)
	{
		aposcnt="false";
	}
	for(i=0;i<sText.length;i++)
	{
		ch1=sText.charAt(i);
		if(ch1==charTest)
		{
			cnt ++;
		}
	}
	if(cnt>2)
	{
		aposcnt="false";
	}
	return aposcnt;
}//apostrophe and hypen validation for text and contact field

function counter1(sText,charTest,validCnt)
{
	var aposcnt;
	var cnt=0;
	var ch1;
	aposcnt="true";
	ch1=sText.charAt(0);
	if(charTest=="+" && ch1=="+" && ch1!=charTest)
	{
		aposcnt="false";
	}
	for(i=0;i<sText.length;i++)
	{
		ch1=sText.charAt(i);
		if(ch1==charTest)
		{
			cnt++;
		}
	}
	if(parseInt(cnt) > parseInt(validCnt))
	{
		aposcnt="false";
	}
	return aposcnt;
}
//apostrophe and hypen validation for text and contact field

///////////////----------OnKeyPress Event code to restrinct Kayboard input starts----------//////////
var KEY_NULL = null;
var KEY_NONE = 0;
var KEY_BCKSPC = 8;
var KEY_TAB = 9;
var KEY_ENTER = 13;
var KEY_ESC = 27;



function validData(e,field) {
	var key;
	var keychar;
	
	if (window.event) {
		key = window.event.keyCode;
	}
	else if (e) {
		key = e.which;
	}
	else {
		return true;
	}
	keychar = String.fromCharCode(key);
 //characters which are allowed 
	switch(field)
	{
		case "name":
			chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'. "
			break;	
		case "text":
			chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'. "
			break;	
		case "email":
			chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@_."
			break;	
		case "design":
			chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ()'.:[]-&,;{}\@#%^*?/_"
			break;	
		case "city":
			chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&. "
			break;	
		case "num":
			chars = "0123456789";
			break;
		case "nameSpl":
			chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\"abcdefghijklmnopqrstuvwxyz1234567890()'.:[]-&,;{}@#%^*?/_ "
			break;	
		case "phone":
			chars = "1234567890-+ "
			break;	
			 case "bcity":
			chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz& "
			break;	
		case "num":
			chars = "0123456789"
			break;
		case "web":
			chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@_./\-%:?=&#"
			break;
		case "mobile":
			chars = "1234567890-+()"
			break;
	}
	// Control keys (no @#$% "magic numbers")
	if (
	(key == KEY_NULL) || 
	(key == KEY_NONE) || 
	(key == KEY_BCKSPC) || 
	(key == KEY_TAB) || 
	(key == KEY_ENTER) || 
	(key == KEY_ESC) || 
	((chars).indexOf(keychar) > -1)
	 ) 
     {
		return true;
	}
	/*else if (e1 == 'dec') 
    { // Decimal point allowed
		if (keychar == '.') 
        {
			if (this.value.indexOf('.') == -1)
            { // Can only have a single decimal point
				return true;
			}
		}
	} */
	return false;
}

//////////////----------OnKeyPress Event code to restrinct Kayboard input ends----------//////////


function go()
	{
        if(document.frm.cname.value==""){
			alert("Please enter Company Name");
			document.frm.cname.focus();
			return false;
		}
		else if(document.frm.cname.value!="")
		{
			var mytext=trim(document.frm.cname.value);
			pop=document.frm.cname.value;
				//validation for space
				var ValidChars = " ";
				Char = pop.charAt(0);
				if (ValidChars.indexOf(Char) != -1) 
				{
					
					alert("Please enter valid Company Name");
					document.frm.cname.focus();
					document.frm.cname.select();
					return false;
				}//validchars.indexof
				//validation for space
			
			var char1=counter(mytext,"'");
			if (char1=="false")
			{
				
	            alert("Please enter valid Company Name");
			    document.frm.cname.focus();
				document.frm.cname.select();
		        return false;
			}
			var mytext=trim(document.frm.cname.value);		
			if(mytext=="")
	    	{
	             alert("Please enter Company Name");
	    		document.frm.cname.focus()
	    		return false;
	    	}
		}//company name validation
	
	
		if(document.frm.vname.value==""){
			alert("Please enter Name");
			document.frm.vname.focus();
			return false;
		}
		else if(document.frm.vname.value!="")
		{
			var mytext=trim(document.frm.vname.value);
			pop=document.frm.vname.value;
				//validation for space
				var ValidChars = " ";
				Char = pop.charAt(0);
				if (ValidChars.indexOf(Char) != -1) 
				{
					
					alert("Please enter valid Name");
					document.frm.vname.focus();
					document.frm.vname.select();
					return false;
				}//validchars.indexof
				//validation for space
			
			var char1=counter(mytext,"'");
			if (char1=="false")
			{
				
	            alert("Please enter valid Name");
			    document.frm.vname.focus();
				document.frm.vname.select();
		        return false;
			}
			var mytext=trim(document.frm.vname.value);		
			if(mytext=="")
	    	{
	             alert("Please enter Name");
	    		document.frm.vname.focus()
	    		return false;
	    	}
		}// name validation
		
		if(document.frm.designation.value!="")
		{
			var mytext=trim(document.frm.designation.value);
			pop=document.frm.designation.value;
				//validation for space
				var ValidChars = " ";
				Char = pop.charAt(0);
				if (ValidChars.indexOf(Char) != -1) 
				{
					
					alert("Please enter valid Designation");
					document.frm.designation.focus();
					document.frm.designation.select();
					return false;
				}//validchars.indexof
				//validation for space
			
			var char4=counter(mytext,"'");
			if (char4=="false")
			{
				
	            alert("Please enter valid Designation");
			    document.frm.designation.focus();
				document.frm.designation.select();
		        return false;
			}
			var mytext=trim(document.frm.designation.value);		
			if(mytext=="")
	    	{
	             alert("Please enter Designation");
	    		document.frm.designation.focus()
	    		return false;
	    	}
		}// name validation
		/*if(document.frm.email.value==""){
			alert("Please enter your Email Address");
			document.frm.email.focus();
			return false;
		}
		var str =/[-a-zA-Z0-9_\.]+@[-a-zA-Z0-9]+\.[-a-zA-Z0-9\.]+/;
		var eflag = document.frm.email.value.match(str);
		if(eflag!=document.frm.email.value)
		{
			alert("Please enter a valid Email Address")
			document.frm.email.focus();
			document.frm.email.select();
			return false;
		}*/
		if(document.frm.email.value==""){
			alert("Please enter your Email Address");
			document.frm.email.focus();
			return false;
		}
		else if(document.frm.email.value!="")
		{
			var str =/[-a-zA-Z0-9_\.]+@[-a-zA-Z0-9]+\.[-a-zA-Z\.]+/;
			var eflag = document.frm.email.value.match(str);
			if(eflag!=document.frm.email.value)
			{
				alert("Please enter a valid Email Address")
	            document.frm.email.value="";
				document.frm.email.select();
				document.frm.email.focus();
				return false;
			}
	        var string1=document.frm.email.value;
	        if(string1!="")
	        {
	            var number=emailvalid(string1);
	            if (number=="false")
	            {
	                alert("Please enter a valid Email Address")
		            document.frm.email.select();
		            document.frm.email.focus();
		            return false;
	            }
	        }
	        var mytext = trim(document.frm.email.value);
	        if(mytext=="")
	        {
	            alert("Please enter a Email Address")
		        document.frm.email.focus();
	            document.frm.email.value="";
		        return false;
	        }        
		}//email validation
		
		if(document.frm.country.value=="0"){
			alert("Please select the Country");
			document.frm.country.focus();
			return false;
		}
		
		if(document.frm.address.value==""){
			alert("Please enter the Address");
			document.frm.address.focus();
			return false;
		}		
		else if(document.frm.address.value!="")
		{
			var mytext=trim(document.frm.address.value);
			pop=document.frm.address.value;
			//validation for space
			var ValidChars = " ";
			Char = pop.charAt(0);
			if (ValidChars.indexOf(Char) != -1) 
			{
				alert("Please enter the Address");		
				document.frm.address.focus();
				document.frm.address.select();
				return false;
			}//validchars.indexof
			//validation for space
	        if(mytext=="")
	    	{
	    		alert("Please enter the Address");
	    		document.frm.address.focus()
	    		return false;
	    	}
	        var strcnt2=mytext.length;
	        if(strcnt2<2 || strcnt2>500)
	    	{
	            alert("Address accept 2-500 characters only");
				document.frm.address.value="";
		        document.frm.address.focus();
				document.frm.address.select();
		        return false;
	        }
		}//address validation
        str9 = document.frm.contactno.value;
	if (document.frm.contactno.value=="" )
	{
		alert("Please enter Phone Number");
		document.frm.contactno.focus();
		return false;
	}
	
	else if (document.frm.contactno.value!="")
	    {
	       var mytext=trim(document.frm.contactno.value);
	        var number=IsChar(mytext);
	        if (number=="false")
	        {
               alert("Phone Number accept digits & hyphen only");
	       		document.frm.contactno.focus();
	            document.frm.contactno.select();
	            return false;
	        }
			 var char3=counter1(mytext,"-",2);
		        if (char3=="false")
		        {
		            alert("Please enter valid Phone Number");
		            document.frm.contactno.focus();
		            document.frm.contactno.select();
		            return false;
		        }
		        var char4=counter1(mytext,"+",1);
		        if (char4=="false")
		        {
		            alert("Please enter valid Phone Number");
		            document.frm.contactno.focus();
		            document.frm.contactno.select();
		            return false;
		        }
	        if (mytext=="")
	          {
	              alert("Please enter Phone Number");
	              document.frm.contactno.focus();
	              return false;
	            }
	        if (document.frm.contactno.value=="")
	          {
	            alert("Please enter Phone Number");
	 			 document.frm.contactno.focus();
	             return false;
	          }
	        var strcnt2=mytext.length;
	        if(strcnt2<8 || strcnt2>16)
	          {
	            alert("Phone Number accept 8-16 digits only");
	            document.frm.contactno.focus();
	            document.frm.contactno.select();
	             return false;
	        }
	
	    }//mobile validation
	
	 if (document.frm.faxno.value!="")
	    {
	       var mytext=trim(document.frm.faxno.value);
	        var number=IsChar(mytext);
	        if (number=="false")
	        {
                alert("Fax Number accept digits & hyphen only");
	       		document.frm.faxno.focus();
	            document.frm.faxno.select();
	            return false;
	        }
			 var char3=counter1(mytext,"-",2);
			        if (char3=="false")
			        {
			            alert("Please enter valid Fax Number");
			            document.frm.faxno.focus();
			            document.frm.faxno.select();
			            return false;
			        }
			        var char4=counter1(mytext,"+",1);
			        if (char4=="false")
			        {
			            alert("Please enter valid Fax Number");
			            document.frm.faxno.focus();
			            document.frm.faxno.select();
			            return false;
			        }
	        if (mytext=="")
	          {
	              alert("Please enter Fax Number");
	              document.frm.faxno.focus();
	              return false;
	            }
	        if (document.frm.faxno.value=="")
	          {
	             alert("Please enter Fax Number");
	 			 document.frm.faxno.focus();
	             return false;
	          }
	         var strcnt2=mytext.length;
	         if(strcnt2<8 || strcnt2>16)
	          {
	            alert("Fax Number accept 8-16 digits only");
	            document.frm.faxno.focus();
	            document.frm.faxno.select();
	             return false;
	        }
	
	    }//faxno validation
		
		if(document.frm.enquiry.value!="")
		{
			var mytext=trim(document.frm.enquiry.value);
			pop3=document.frm.enquiry.value;
			//validation for space
			var ValidChars = " ";
			Char = pop3.charAt(0);
			if (ValidChars.indexOf(Char) != -1) 
			{
				alert("Please enter the Remarks / Suggestions");		
				document.frm.enquiry.focus();
				document.frm.enquiry.select();
				return false;
			}//validchars.indexof
			//validation for space
	        if(mytext=="")
	    	{
	    		alert("Please enter the Remarks / Suggestions");
	    		document.frm.enquiry.focus()
				document.frm.enquiry.select();
	    		return false;
	    	}
	        var strcnt2=mytext.length;
	        if(strcnt2<2 || strcnt2>500)
	    	{
	            alert("Remarks / Suggestions accept 2-500 characters only");
				document.frm.enquiry.value="";
		        document.frm.enquiry.focus();
				document.frm.enquiry.select();
		        return false;
	        }
		}//enquiry validation
		
		
		//document.frm.action="../enquiry/submit.php";
		//document.frm.submit();
	}
