function getObj(name){
 if (document.getElementById){
   return document.getElementById(name);
 }
 else if (document.all){
   return document.all[name];
 }
 else if (document.layers){
   return document.layers[name];
 }
 else return false;
}

function validChar(fVal) {
 var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@ "
 var ok = "yes";
 var temp;
 for (var i=0; i<fVal.length; i++) {
   temp = "" + fVal.substring(i, i+1);
   if (valid.indexOf(temp) == "-1") ok = "no";
 }
 if (ok == "no") {
   return false;
 }
 else return true;
}

function validNum(fVal) {
 var valid = "0123456789 ()."
 var ok = "yes";
 var temp;
 for (var i=0; i<fVal.length; i++) {
   temp = "" + fVal.substring(i, i+1);
   if (valid.indexOf(temp) == "-1") ok = "no";
 }
 if (ok == "no") {
   return false;
 }
 else return true;
}

function chkEmail(email) {
	atPos = email.indexOf("@");
	stopPos = email.lastIndexOf(".");
	
	if (email == "") {
		return false;
	}
	
	if (atPos == -1 || stopPos == -1) {
		return false;
	}
	
	if (stopPos < atPos) {
		return false;
	}
	
	if (stopPos - atPos == 1) {
		return false;
	}
	return true;
}

function chkQuoteFrm(frmName) {
	var errors = false;
	var x = getObj(frmName);
	var errorTxt = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if (!validNum(x.cWeight.value)) {
		errors = true;
		errorTxt += "Cargo Weight should be a number\n";
	}
	if (!validNum(x.cVol.value)) {
		errors = true;
		errorTxt += "Cargo Volume should be a number\n";
	}
	if (x.cWeight.value == "") {
		errors = true;
		errorTxt += "Please enter a Cargo Weight\n";
	}
	if (x.cVol.value == "") {
		errors = true;
		errorTxt += "Please enter a Cargo Volume\n";
	}
	if (x.id == "expQuote") {
		if ((x.town.value!="X")&&(x.depot.value!="X")) {
			errors = true;
			errorTxt += "Please select either a Collection Point or a Depot\n";
		}
		if ((x.town.value=="X")&&(x.depot.value=="X")) {
			errors = true;
			errorTxt += "Please select either a Collection Point or a Depot\n";
		}
		if (x.portID.value=="X") {
			errors = true;
			errorTxt += "Please select a Destination point\n";
		}
	}
	else if(x.id == "impQuote") {
		if ((x.itown.value=="X") && (x.delUKP.checked == false)) {
			errors = true;
			errorTxt += "Please select a Delivery Town or delivery to a UK Port\n";
		}
		if (x.portID.value=="X") {
			errors = true;
			errorTxt += "Please select an Origin point\n";
		}
	}
	if ((x.name.value=="")) {
		errors = true;
		errorTxt += "Please enter your name so we can send you a copy of your quote\n";
	}
	if (!chkEmail(x.email.value) || (x.email.value=="")) {
		errors = true;
		errorTxt += "Please enter your email address so we can send you a copy of your quote\n";
	}
	if (errors) {
		errorTxt += "Please change these details and click on the submit button again.";
		errors = false;
		alert(errorTxt);
		return false;
	}
	else return true;
}

// function to deselect one of the form fields in the import form
function deSel(toDeSel, origin) {
	var x = getObj(toDeSel);
	if(origin == "delUKP") {
		x.options[0].selected = true;
	}
	else if(origin == "town" || origin == "itown") {
		x.checked = false;
	}
}

// function to deselect one of the form fields in the export form
function deSelExp(toDeSel, origin) {
	var x = getObj(toDeSel);
	if(origin == "town") {
		x.options[0].selected = true;
	}
	else if(origin == "depot") {
		x.options[0].selected = true;
	}
}
