
var selectedMoveDate; //page scope as the implementing page should implement the maximum move date.
var maxMoveDate = null; //page scope as implementing page should set this.
	
//take care of browsers without getElementById
//by creating a wrapper function.
if(document.all && !document.getElementById) {
    	document.getElementById = function(id) {
         	return document.all[id];
    	}
	}
		
	function clearIf(fieldIn, strCheck)
	{
	
		if (fieldIn.value == strCheck)
		{
			fieldIn.value="";
		}
	}
	
	function populateIfEmpty(fieldIn, strTxt)
	{
		if (fieldIn.value.length==0)
		{
			fieldIn.value = strTxt;
		}
	}

	function ValidatePostcode(pCodeIn)
	{
		return (/^[a-pr-uwyz]((\d{1,2})|([a-hk-y]\d{1,2})|(\d[a-hjks-uw])|([a-hk-y]\d[abehmnprv-y]))\s{0,1}\d[abd-hjlnp-uw-z]{2}$/i).test(pCodeIn);
	}
	
	
	
	function ValidateEmail()
	{
		var emailAddrStr = document.getElementById("emailAddr").value;
		return (/^([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z]{2,4})+/i).test(emailAddrStr);
	}
	
	function ValidateTelephone()
	{
		var telNum = document.getElementById("telNum").value;
		
		//telephone number is not compulsory, but it should be checked
		//if the user has entered it.
		if (telNum.length > 0)
		{
			return (/^((0[1|2|7]{1}[0-9\s]{9,12})|(Telephone))/i).test(telNum);
		}
		else
		{
			return true;
		}
	}
	
	function CleanNumber(numberIn)
	{
		//there is a bug in parseInt that seems not to like the value 08 and so parses it as 0.
		if (numberIn.substring(0,1) == "0") numberIn = numberIn.substring(1,2);
		return parseInt(numberIn);
	}
	
	function ValidateMoveDate()
	{
		var dateOK = true;
		var moveDayObj = document.getElementById("day");
		var moveMonthObj = document.getElementById("month");
		var moveYear = parseInt(moveMonthObj.options[moveMonthObj.selectedIndex].value.substring(2,6));
		var moveMonth = moveMonthObj.options[moveMonthObj.selectedIndex].value.substring(0,2);
		var moveDay = moveDayObj.options[moveDayObj.selectedIndex].value;
		
		//set the hidden moveDate field...
		document.getElementById("moveDate").value = (moveYear.toString() + "-" + moveMonth.toString() + "-" + moveDay.toString());
		
		moveMonth = CleanNumber(moveMonth);
		moveDay = CleanNumber(moveDay);
				
		
		selectedMoveDate = new Date(moveYear, (moveMonth-1), moveDay);
		
		
		if (moveDay > 28)
		{
			//check february, plus leap years...
			if (moveMonth == 2)
			{
				//leap years
				if (moveYear%4 != 0)
				{
					dateOK = (moveDay < 29); //up to 28 days is fine
				}
				else if (moveYear%400 == 0)
				{
					dateOK = (moveDay < 30); //up to 29 days is fine
				}
				else if (moveYear%100 == 0)
				{
					dateOK = (moveDay < 29); //up to 28 days is fine
				}
				else
				{
					dateOK = (moveDay < 30);
				}
			}
			//check April, June, September, November
			else if ((moveMonth == 4) || (moveMonth == 6) || (moveMonth == 9) || (moveMonth == 11))
			{
				dateOK = (moveDay < 31); //only 30 days in these months.
			}
			
		}
		
		return dateOK;
	}

	
	function ValidateForm()
	{
		var startPCode = document.getElementById("opc").value;
		var endPCode = document.getElementById("dpc").value;
		var hasValidMileageData = false;
		if ( ((startPCode == "Origin") == false) && ((startPCode == "") == false)
			&& ((endPCode == "Destination") == false) && ((endPCode == "") == false)
			)
		{
			if (ValidatePostcode(document.getElementById("opc").value) == false)
			{
				alert("Please enter a valid origin postcode");
				document.getElementById("opc").focus();
				return false;
			}
		
			if (ValidatePostcode(document.getElementById("dpc").value) == false)
			{
				alert("Please enter a valid destination postcode");
				document.getElementById("dpc").focus();
				return false;
			}
			
			hasValidMileageData = true;
		}
		
		var mileageSelect = document.getElementById("mileage");
		if (hasValidMileageData == false)
		{
			hasValidMileageData = (mileageSelect.options[mileageSelect.selectedIndex].value != "-1");
		}
		
		if (hasValidMileageData == false)
		{
			alert("You must enter either an origin and destination postcode,\nor estimate your mileage using the pull-down list");
			return false;
		}
		
		if (ValidateMoveDate() == false)
		{
			alert("The date you have selected is invalid.");
			return false;
		}
		
		var dateNow = new Date();
		if (selectedMoveDate.getTime() < dateNow.getTime())
		{
			alert("You must select a date in the future.");
			return false;
		}
		
		if (maxMoveDate != null)
		{
			if (selectedMoveDate.getTime() > maxMoveDate.getTime())
			{
				alert("Currently, we can only quote up until " + maxMoveDate.getDate() + "/" + (maxMoveDate.getMonth()+1) + "/" + maxMoveDate.getYear());
				return false;
			}
		}
		
		if (ValidateEmail() == false)
		{
			alert("Please enter a valid email address");
			document.getElementById("emailAddr").focus();
			return false;
		}
		
		if (ValidateTelephone() == false)
		{
			alert("Your telephone number is invalid.\n\nPlease enter a telephone number to a UK landline number beginning with 01 or 02\n or a mobile number beginning with 07");
			return false;
		}
		
		
		
		//get to here and all is ok.
		//return true;
		
		return true; //debug
		
	
	
	}
	
	
	function formSubmit()
	{
		if (ValidateForm())
		{
			var objForm = document.getElementById("quote");
			objForm.submit();
		}
	
	}


 			
			
			