// JavaScript Document
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@., \n"
 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 chkqFrm(frmName) {
	var x = getObj(frmName);
	var z = getObj('thedestination');
	
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";

	if((!validChar(x.name.value)) || (x.name.value == "")) {
		message += "The name field must not be blank and can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if((!chkEmail(x.email.value)) || (x.email.value == "")){
		message += "You have entered a blank or invalid email address\n";
		errors = true;
	}
	if(x.tel.value != ""){
		if(!validNum(x.tel.value)){
			message += "Your have entered invalid characters in your telephone number\n";
			errors = true;
		}
	}
	if(x.thedestination.options[z.selectedIndex].value == "UK"){
		if((!validNum(x.distance.value)) || (x.distance.value == "") || (x.distance.value == "0")){
			message += "You need to provide us with an estimate of the distance (greater than 0) of the move\n";
			errors = true;
		}
		if(x.vol.options[x.vol.selectedIndex].value == "x"){
			message += "You need to select your house size\n";
			errors = true;
		}
	}
	if(x.thedestination.options[z.selectedIndex].value == "Intl"){
		if((x.from.value =="From (town/city UK)") || (x.to.value =="To (town/city & country)") || (x.from.value == "") || (x.to.value == "")){
			message += "You need to provide us with the names of the areas/countries you are moving from and to\n";
			errors = true;
		}
	}
	if(x.thedestination.options[z.selectedIndex].value == "x"){
		message += "You need to select your destination type (within the UK/outside UK)\n";
		errors = true;
	}
	
	if(x.storage.value == "x"){
		message += "You need to select if you require storage or not\n";
		errors = true;
	}
	if(x.packing.value == "x"){
		message += "You need to select if you require a packing service or not\n";
		errors = true;
	}
	if(!validChar(x.addreq.value)) {
		message += "The Additional Requirements field can only contain the characters Aa-Zz, 0-9, ',' and '.'\n";
		errors = true;
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
	//alert ("destination: "+x.thedestination.options[z.selectedIndex].value +"\nvol: "+x.vol.options[x.vol.selectedIndex].value);
	//alert(z.selectedIndex);
	//alert("boo");
	return false;
}

function submitquote(){
	result = chkqFrm('quickquote');
	if (result) {
		document.quickquote.submit();
	}
	else return false;
	
}

function destswitch() {// for the destination switch on the quote form
	var x = getObj('inuk');
	var y = getObj('outuk');
	var z = getObj('thedestination');
	
	if (z.options[z.selectedIndex].value == "UK") {
		x.style.display='';
		y.style.display='none';
	}
	else {
		x.style.display='none';
		y.style.display='';
	}
}
/* -------- */
function chkCallback(frmName) {
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if(!validChar(x.name.value) || (x.name.value=="")) {
		message += "The Name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (!chkEmail(x.email.value) || (x.email.value=="")) {
		errors = true;
		message += "Please enter a valid email address\n";
		errors = true;
	}
	if(!validNum(x.tel.value) || (x.tel.value=="")) {
		message += "The tel field can only contain a valid number\n";
		errors = true;
	}
	if(x.calltime.value == 'X') {
		message += "You need to select a time for us to contact you\n";
		errors = true;
	}
	if(!validChar(x.msg.value) || (x.msg.value=="")) {
		message += "The message field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}
