// 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@., "
 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 domod(thedate){
	var x = getObj('mid');
	x.value = thedate;
}

function chkqFrm(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 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((!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(!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;
}

function chksFrm(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 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(!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;
}

function chkEnquiry(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 must not be blank and can only contain the characters Aa-Zz, 0-9, ',' and '.'\n";
		errors = true;
	}
	if((!validNum(x.tel.value)) && (!x.tel.value == "")) {
		message += "The telephone field must be a number and not blank\n";
		errors = true;
	}
	if((!validChar(x.msg.value)) || (x.msg.value == "")){
		message += "You need to type in your message and can only contain the characters Aa-Zz, 0-9, ',' and '.'\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 (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}

function checkstage1(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 must not be blank and can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if((!validNum(x.tel.value)) && (!x.tel.value == "")) { // if the user has entered and number, check it
		message += "The telephone field must be a number\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.mod.value != "dd/mm/yyyy") || (x.mid.value != "dd/mm/yyyy")){
		if((!isDate(x.mod.value)) || (x.mod.value == "")){
			message += "You have entered an invalid Move Out date. Please use the format: dd/mm/yyyy\n";
			errors = true;
		}
		if((!isDate(x.mid.value)) || (x.mid.value == "")){
			message += "You have entered an invalid Move In date. Please use the format: dd/mm/yyyy\n";
			errors = true;
		}
	}
	if((!validNum(x.distance.value)) || (x.distance.value == "")){
		message += "You need to provide us with an estimate of the distance of the move\n";
		errors = true;
	}
	if((!validChar(x.coladdress1.value)) && (x.coladdress1.value != "")) {
		message += "The Collection Address field must not be blank and can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(x.movetype.value == "dtr"){
		if((!validChar(x.deladdress1.value)) && (x.deladdress1.value != "")) {
		message += "The Delivery Address field must not be blank and can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
		}	
	}
	else if (x.movetype.value == "dta"){
		if((!validChar(x.airport.value)) || (x.airport.value == "")) {
			message += "'Where are you going' - The Airport / Port field must not be blank and 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;
}

function checkstage2(currentForm){
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	var errors = false;
	var elementsInputs;
	var x = getObj(currentForm);
	elementsInputs = x.getElementsByTagName("input");
	for (var intCounter = 0; intCounter < elementsInputs.length; intCounter++) {
		if (elementsInputs[intCounter].id != "submit"){
			if ((!validNum(elementsInputs[intCounter].value)) || (x.value == "")) {
				message += ("The value entered in the field '" + elementsInputs[intCounter].id + "' must be a valid number only.\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;
}

function checkstage3(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.comment.value)) && (!x.comment.value == "")) { // if the user has entered and number, check it
		message += "The Special Instructions field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(x.insreq.value == "yes"){
		if((!validNum(x.insval.value)) || (x.insval.value == "")) {
		message += "'The Insurance value field must be a number only. Do not use the '£' sign. E.g - 1234\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;
}

function deladdrswitch() {// delivery address on stage1
	var x = getObj('movetype');
	var y = getObj('del2res');
	var z = getObj('del2air');
	
	if (x.options[x.selectedIndex].value == "dta") {
		y.style.display='none';
		z.style.display='';
	}
	else {
		y.style.display='';
		z.style.display='none';
	}
}

function needIns() {// for the need insurance field on stage3
	var x = getObj('insreq');
	var y = getObj('insfield');
	
	if (x.options[x.selectedIndex].value == "yes") {
		y.style.display='';
	}
	else {
		y.style.display='none';
	}
}

function smlPop(target){
	window.open(target,'','width=280,height=280,resizable=0,status=0,toolbar=0,scrollbars=0');
}

function listelements(){
	var str = "";
	var x = getObj('fullquote3'); 
	var y = getObj('thelist');
	var elem = document.getElementById('fullquote3').elements;
	for(var i = 0; i < elem.length; i++){
		str += 	elem[i].id + "<br />";
	}
	y.innerHTML = str;
}

/* Date functions */
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}
/* -------- */