// JavaScript Document

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 Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

 		 return true					
	}

function validate()
{
 	 
   if (document.theForm.name.value == "")
  {
    alert("Please enter a value for the \"Name\" field.");
    document.theForm.name.focus();
    return (false);
  }
  
    var emailID=document.theForm.email;
	if(document.theForm.email.value=="")
	{
		alert("Please enter a value for the \"email\" field.");
		document.theForm.email.focus();
		return false;
	}
	else
	{
		if(echeck(emailID.value)==false)
		{
			emailID.focus();
			return false
		}
	}



  if (document.theForm.address.value == "")
  {
    alert("Please enter a value for the \"address\" field.");
    document.theForm.address.focus();
    return (false);
  }
 

   if (document.theForm.city.value == "")
  {
    alert("Please enter a value for the \"city\" field.");
    document.theForm.city.focus();
    return (false);
  }
  
   if (document.theForm.state.value == "")
  {
    alert("Please enter a value for the \"state\" field.");
    document.theForm.state.focus();
    return (false);
  }

   if (document.theForm.zip_code.value == "")
  {
    alert("Please enter a value for the \"zip code\" field.");
    document.theForm.zip_code.focus();
    return (false);
  }

   if (document.theForm.phone.value.length <6)
  {
    alert("Please enter valid value of  \"phone\" field.");
    document.theForm.phone.focus();
    return (false);
  }

   if (document.theForm.phone.value=="000000")
  {
    alert("Please enter valid value of \"phone\" field.");
    document.theForm.phone.focus();
    return (false);
  }


	var phone2 = document.getElementById('phone');
	phone3 = phone2.substring(1,100);
	
	

	if(isNumeric(phone3)==false){
		alert("please enter valid phone number");
	    document.theForm.phone.focus();
    	return (false);
	}
	

 
}


function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return true;
	}
	return false;
}

function isNumeric(elem){
	var numericExpression = /^[0-9\_\-\s]$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z\-\_\s]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isothers(elem, helperMsg){
	var alphaExp =  /^[0-9a-zA-Z\-\_\s]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length > min && uInput.length <= max){
		return true;
	}else{
		return false;
	}
	
	return true;
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
	
	return true;
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}