var name, address, postalcode, city, country, phone, email, comment, validated;

function processForm(thisForm) {
   name = thisForm.txtName.value;
   address = thisForm.txtAddress.value;
   postalcode = thisForm.txtPostalCode.value;
   city = thisForm.txtCity.value;
   country = thisForm.txtCountry.value;
   phone = thisForm.txtPhone.value;
   email = thisForm.txtEmail.value;
   comment = thisForm.txtComment.value;
   
   validated = true;

   if (isEmpty(name)) {
	   setAlertColor("lblName");
	   validated = false;
   } else {
	   setNormalColor("lblName");
   };
	   
   if (isEmpty(address)) {
	   setAlertColor("lblAddress");
	   validated = false;
   } else {
	   setNormalColor("lblAddress");
   };
	   
   if (isEmpty(postalcode)) {
	   setAlertColor("lblPostalCode");
	   validated = false;
   } else {
	   setNormalColor("lblPostalCode");
   };
	   
   if (isEmpty(city)) {
	   setAlertColor("lblCity");
	   validated = false;
   } else {
	   setNormalColor("lblCity");
   };
	   
   if (isEmpty(country)) {
	   setAlertColor("lblCountry");
	   validated = false;
   } else {
	   setNormalColor("lblCountry");};
	   
   if (isEmpty(phone)) {
	   setAlertColor("lblPhone");
	   validated = false;
   } else {
	   setNormalColor("lblPhone");
   };
	   
   if (isEmpty(email)||!emailFormatOK(email)) {
	   setAlertColor("lblEmail");
	   validated = false;
   } else {
	   setNormalColor("lblEmail");
   };
	   
   if (isEmpty(comment)) {
	   setAlertColor("lblComment");
	   validated = false;
   } else {
	   setNormalColor("lblComment");
   };
      
   if (!validated)
   {
      document.getElementById("lblRequired").innerHTML = "Please fill in required fields";
	  document.getElementById("lblRequired").style.fontWeight = "bold";
	  setAlertColor("lblRequired");
      return false;
   }
}

function setAlertColor(el) {
	document.getElementById(el).style.color = "#FF7800";
}

function setNormalColor(el) {
	document.getElementById(el).style.color = "#0986C9";
}
	
function isEmpty(textField) {
	return (textField == null || textField == "");	
}

function emailFormatOK(emailAddress) {
	var at="@";
	var dot=".";
	var lat=emailAddress.indexOf(at);
	var lstr=emailAddress.length;
	var ldot=emailAddress.indexOf(dot);
	
	if (emailAddress.indexOf(at)==-1){
	   alert("Invalid E-mail Address");
	   return false;
	}

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

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

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

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

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

	 return true;					
}


