// JavaScript Document

function validatekForm() {
		var errors=0;
		if(!RegistrationErrorCheck('name','Enter your name.')) errors=1;
		if(!RegistrationErrorCheck('email','Enter a valid e-mail address')) errors=2;
		//if(!RegistrationErrorCheck('query','Enter your question / comments.')) errors=3;
		if(!RegistrationErrorCheck('phone','Enter your phone number.')) errors=6;
		//if(!RegistrationErrorCheck('location','Enter your location')) errors=5;
		
		/*check email address */
		if(document.getElementById('email').value) {
			if(!echeck(document.getElementById('email').value,document.getElementById('emailError'))) {
				errors=4;
			} else {
				document.getElementById('emailError').innerHTML="";
			}
		}
		/*check contact number */
		if(document.getElementById('phone').value) {
			document.getElementById('phone').value = trim(document.getElementById('phone').value);
			if(!checkInternationalPhone(document.getElementById('phone').value)) {
				document.getElementById('phone').style.border = "0px";
				document.getElementById('phoneError').innerHTML="Please enter a valid phone number";
				errors=5;
			} else {
				document.getElementById('phone').style.border = "";
				document.getElementById('phoneError').innerHTML="";
			}
		}
		
		if(errors) {
			//document.getElementById('message').innerHTML = "<h1>Fill the form</h1>";
			return false;
		}
		else {
			document.getElementById('message').innerHTML = "<img src='images/loader.gif' alt='' />Please wait...";
			return true;
		}
	}
	function RegistrationErrorCheck(inputId,msg) {
		try {
			document.getElementById(inputId).value=trim(document.getElementById(inputId).value); 
			if((document.getElementById(inputId).value=='')||(document.getElementById(inputId).value=='0')||(document.getElementById(inputId).value=='Name*')||(document.getElementById(inputId).value=='E-mail*')||(document.getElementById(inputId).value=='Location*')||(document.getElementById(inputId).value=='Query*')) {
					
					document.getElementById(inputId).style.border = "0px";
					document.getElementById(inputId+'Error').innerHTML="<span>"+msg+"</span>"; 
					return false;
			} else 	{
					document.getElementById(inputId).style.border = "";
				document.getElementById(inputId+'Error').innerHTML=""; 
				return true;
			}
		}
		catch(e){alert(e);}
	}
	function trim(string) {
		var a = string.replace(/^\s+/, '');
		return a.replace(/\s+$/, '')
	}
	function echeck(str,div_object) {
		var member_email_div=div_object;
		//alert(div_object)
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   member_email_div.innerHTML="Please enter a valid email address";
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   member_email_div.innerHTML="Please enter a valid email address";
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   member_email_div.innerHTML="Please enter a valid email address";
		    return false
		}
		if (str.indexOf(at,(lat+1))!=-1){
		   member_email_div.innerHTML="Please enter a valid email address";
		    return false
		 }
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   member_email_div.innerHTML="Please enter a valid email address";
		    return false
		 }
		if (str.indexOf(dot,(lat+2))==-1){
		   member_email_div.innerHTML="Please enter a valid email address";
		    return false
		 }
		if (str.indexOf(" ")!=-1){
		   member_email_div.innerHTML="Please enter a valid email address";
		    return false
		 }
 		return true					
	}
	function checkInternationalPhone(strPhone){
		var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;  
		return phoneNumberPattern.test(strPhone);  
		
		
	}
