// JavaScript Document
function CheckData() {
	var msg = "";
	pass=true;
	var f = document.contactform;

	//Validate Name
	if  ((pass) && (! ValidateTextBox(f.Name))) {
		msg = "Please enter your Name";
		pass = errorMsg(msg, f.Name);
	}

	//Email
	if ((pass) && (!ValidateEMail(f.Email.value))) {
		msg = "Please enter a valid Email Address";
		pass = errorMsg(msg, f.Email);
	}
	
	if (f.NoOfPieces.value != "" || f.AverageWeight.value != "" || f.FiringTemperature.value != "" || f.EnergyCost.value != "" || f.Currency.value != "" || f.ProductType.selectedIndex > 0){
		if  ((pass) && (! ValidateTextBox(f.NoOfPieces))) {
			msg = "Please enter the number of pieces";
			pass = errorMsg(msg, f.NoOfPieces);
		}
		if  ((pass) && (! ValidateTextBox(f.AverageWeight))) {
			msg = "Please enter the average weight per piece";
			pass = errorMsg(msg, f.AverageWeight);
		}
		if  ((pass) && (! ValidateTextBox(f.FiringTemperature))) {
			msg = "Please enter the current firing temperature";
			pass = errorMsg(msg, f.FiringTemperature);
		}
		if  ((pass) && (! ValidateTextBox(f.EnergyCost))) {
			msg = "Please enter the Energy cost £s per KWh or Therm";
			pass = errorMsg(msg, f.EnergyCost);
		}
		if  ((pass) && (! ValidateTextBox(f.Currency))) {
			msg = "Please enter the currency";
			pass = errorMsg(msg, f.Currency);
		}
		if  ((pass) && f.ProductType.selectedIndex == 0){
			msg = "Please select the product type";
			pass = errorMsg(msg, f.ProductType);
		}
		if  ((pass) && f.ProductType.selectedIndex == 3){
			if  (! ValidateTextBox(f.ProductTypeOther)) {
				msg = "Please enter the product type";
				pass = errorMsg(msg, f.ProductTypeOther);
			}
		}
		
	}

	if(!pass){
		return false;
	}
	else {
		f.submit();
		return true;
	}
}

function ValidateTextBox(form_object) {
	if (form_object.value == "") {
		return(false);
	}
	else {
		return(true);
	}
}

function errorMsg(msg, form_object) {
	alert(msg);
	form_object.focus();
	if(form_object.type == "text") {
		form_object.select();
	}
}

function ValidateEMail(i_strEMailAddress) {
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) {
			supported = 1;
		}
	}

	if (!supported) {
    		return (i_strEMailAddress.indexOf(".") > 2) && (i_strEMailAddress.indexOf("@") > 0);
    	}

	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(i_strEMailAddress) && r2.test(i_strEMailAddress));
}
