// JavaScript Document

function validatekForm() {
	
		var errors=0;
		if(!RegistrationErrorCheck('name','Please enter your name.')) errors=1;
		if(!RegistrationErrorCheck('email','Please enter a valid email address.')) errors=2;
		if(!RegistrationErrorCheck('message','Enter your message / comments.')) errors=4;
		

		if(document.getElementById('email').value) {
			if(!echeck(document.getElementById('email').value,document.getElementById('emailError'))) {
				errors=11;
			} else {
				document.getElementById('emailError').innerHTML="";
			}
		}
		
		
		
		if(errors) {
			//document.getElementById('message').innerHTML = "<h1>Fill the form</h1>";
			return false;
		}
		else {
			
			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=='Message *')) {
					
					
					document.getElementById(inputId+'Error').innerHTML=msg; 
					return false;
					
			} else 	{
					
				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					
	}
		
		
	
