// 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 Format")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Format")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Format")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Format")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Format")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Format")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Format")
		    return false
		 }

 		 return true					
	}

function validateForm(){
	
	//var emailID=document.frmSample.txtEmail
	var emailID = document.forms[0].Email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email")
		document.forms[0].Email.focus();
		return false;
		
	}
	
		if (echeck(emailID.value)==false){	
			return false;
	}
	if (validateFields()){
		//alert("1");
		//alert(document.forms[0].name);
		//alert(document.forms[0].action);
		document.forms[0].submit();
	}
}





function validateFields(){
//	alert('a');
	myForm = document.forms[0];
	var fieldList = new Array(6);
	var fieldLabelList = new Array(6);

	fieldList[0] = "FirstName"; 
	fieldList[1] = "LastName"
	fieldList[2] = "CompanyName"
	fieldList[3] = "City"
	fieldList[4] = "ZipCode"
	fieldList[5] = "Country"
	fieldList[6] = "Phone"	
	fieldList[7] = "Title"	

	fieldLabelList[0] = "First Name"; 
	fieldLabelList[1] = "Last Name"
	fieldLabelList[2] = "Company Name"
	fieldLabelList[3] = "City"
	fieldLabelList[4] = "Zip Code"
	fieldLabelList[5] = "Country"
	fieldLabelList[6] = "Phone"
	fieldLabelList[7] = "Title"


	for(z=0; z<fieldList.length; z++){
	//	alert(myForm.elements[fieldList[z]].value);
		if (!validText(myForm.elements[fieldList[z]])) {
	//	if ((myForm.elements[fieldList[z]].value==null) || (myForm.elements[fieldList[z]].value == "")){
			alert("Please fill out the " + fieldLabelList[z] + ".");
			myForm.elements[fieldList[z]].focus();
			return false;
		}
	}
	//alert('b');
	//only for Canada and US
	if ((myForm.Country.value=="US") || (myForm.Country.value=="CA")){
	//	if ((myForm.State.value==null) || (myForm.State.value=="")) {
		if (!validText(myForm.State)){
			alert("Please fill out the State.");
			document.forms[0].State.focus();
			return false;
		}
	}


	if (myForm.Product.value=="<choose>"){
		alert("Please select a product.");
		myForm.Product.focus();
		return false;
	}



	//alert('c');
	return true;
}

function validText(textObj) {
		if (textObj.value == null || textObj.value == "" || isblank(textObj.value))
			return false;
		return true;
	}


function isblank(s)
	{
	    for(var i = 0; i < s.length; i++) {
	        var c = s.charAt(i);
	        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	    }
	    return true;
	}
	
	
	
function prefillProduct() {	
	var qs = document.location.href;
	//var prod=;
	
	document.forms[0].Product.value=qs.substring(qs.lastIndexOf("=")+1);
}
