
	old_make_v = make_v;
	_old_model = _model;


	String.prototype.trim = function () {
    	return this.replace(/^\s*/, "").replace(/\s*$/, "");
	}

	function checkPostCode (toCheck) {

		// Permitted letters depend upon their position in the postcode.
		var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
		var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
		var alpha3 = "[abcdefghjkstuw]";                                // Character 3
		var alpha4 = "[abehmnprvwxy]";                                  // Character 4
		var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5


		// Array holds the regular expressions for the valid postcodes
		var pcexp = new Array ();

		// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
		pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

		// Expression for postcodes: ANA NAA
		pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

		// Expression for postcodes: AANA  NAA
		pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

		// Exception for the special postcode GIR 0AA
		pcexp.push (/^(GIR)(\s*)(0AA)$/i);

		// Standard BFPO numbers
		pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);

		// c/o BFPO numbers
		pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

		// Load up the string to check
		var postCode = toCheck;

		// Assume we're not going to find a valid postcode
		var valid = false;

		// Check the string against the types of post codes
		for ( var i=0; i<pcexp.length; i++) {
			if (pcexp[i].test(postCode)) {

				// The post code is valid - split the post code into component parts
				pcexp[i].exec(postCode);

				// Copy it back into the original string, converting it to uppercase and
				// inserting a space between the inward and outward codes
				postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();

				// If it is a BFPO c/o type postcode, tidy up the "c/o" part
				postCode = postCode.replace (/C\/O\s*/,"c/o ");

				// Load new postcode back into the form element
				valid = true;

				// Remember that we have found that the code is valid and break from loop
				break;
			}
		}

		// Return with either the reformatted valid postcode or the original invalid
		// postcode
		if (valid) {return postCode;} else return false;
	}


	function updateMakes()
	{
		var ddMake = document.createElement("select");
		ddMake.id = "make";
		ddMake.name = "make";

		selectedMake = 0;

		for (i = 0; i < make_v.length; i++)
		{
			var elMake = document.createElement("option");
			elMake.innerHTML = make_v[i];
			ddMake.appendChild(elMake);
			elMake.value = make_v[i];
			if (elMake.value.toUpperCase().trim() == "")
			{
				ddMake.options[i].selected = true;
				selectedMake = i;
			}
		}


		ddMake.onchange = function()
		{
			updateModels(this.selectedIndex);
		}

		var ddModel = document.createElement("select");
		ddModel.id = "model";
		ddModel.name = "model";

		ddMake.className = "small w120 mt4";
		ddModel.className = "small w120 mt4";

		var ddMakeContainer = document.createElement("span");
		var ddModelContainer = document.createElement("span");
				ddMakeContainer.className = "w130 mr5 fleft md_clear";
		ddModelContainer.className = "w130 mr5 fleft";

		ddMakeContainer.appendChild(ddMake);
		ddModelContainer.appendChild(ddModel);

		document.getElementById("make").parentNode.parentNode.removeChild(document.getElementById("make").parentNode);
		document.getElementById("at_search").insertBefore(ddModelContainer, document.getElementById("at_search").firstChild);
		document.getElementById("at_search").insertBefore(ddMakeContainer, document.getElementById("at_search").firstChild);

		updateModels(selectedMake);

	}

	function updateModels(make_id)
	{
		var dd = document.getElementById("model");
		removeOptions(dd);
		for (i = 0; i < _model[make_id].length; i++)
		{
			dd.options[dd.options.length] = new Option(_model[make_id][i],_model[make_id][i].replace("ANY MODEL", "ANY").replace("MODEL", "ANY"));

			if (_model[make_id][i].toUpperCase().trim() == "")
			{
				dd.options[i].selected = true;
			}
		}
	}

	function removeOptions(selectbox)
	{
		var i;
		for(i=selectbox.options.length-1;i>=0;i--)
		{
			selectbox.remove(i);
		}
	}

	function popupLinks()
	{
		if (document.getElementById("results"))
		{
			var x = document.getElementById("results").getElementsByTagName('a');
			for (var i=0;i<x.length;i++)
			{
				if (x[i].getAttribute('target') == '_blank')
				{
					x[i].onclick = function ()
					{
						return popitup(this.href);
					}
					x[i].title += ' (Popup)';
				}
			}
		}
	}

	function popitup(url)
	{
		newwindow=window.open(url,'name','menubar=1,resizable=1,height=630,width=714');
		if (window.focus)
		{
			newwindow.focus()
		}
		return false;
	}

	function validateForm()
	{
		if (document.getElementById("at_search"))
		{
			document.getElementById("at_search").onsubmit = function()
			{
				if (document.getElementById("postcode").value.trim() == "" && document.getElementById('postcode').style.visibility != "hidden")
				{
					showError("Please fill in your postcode");
					return false;
				}
				if (document.getElementById("make").selectedIndex < 1)
				{
					showError("Please select a make");
					return false;
				}

				if (!checkPostCode(document.getElementById("postcode").value.trim()))
				{
					if(document.getElementById('postcode').style.visibility != "hidden"){
						showError("Please check your postcode");
						return false;
					} else {
						document.getElementById("postcode").value = '';
					}
				}
			}
		}
	}

	function showError(theError)
	{
		var x;
		if (!document.getElementById("errors"))
		{
			x = document.createElement("span");
			x.id = "errors";
			x.className = "red md_clear mb10 fbold";
			document.getElementById("at_search").insertBefore(x, document.getElementById("at_search").firstChild);
		}
		else
		{
			x = document.getElementById("errors");
		}
		x.innerHTML = theError;
	}

	function monitorPostcode()
	{
		if (document.getElementById("postcode"))
		{

			var inPC = document.getElementById("postcode");

			if (inPC.value.trim() == '')
			{
				inPC.value = 'Postcode';
			}

			inPC.onfocus = function()
			{
				if (this.value == 'Postcode')
				{
					this.value = '';
				}
			}
		}
	}


	function updateSelects(isnew)
	{
		if (isnew)
		{
			make_v = new_make_v;
			_model = _new_model;
			document.getElementById("used").name = 'new';
			document.getElementById("atTab1").className = "artTab";
			document.getElementById("atTab2").className = "artTabSelected";
			document.getElementById("at-image").src = 'http://www.tiscali.co.uk/media/images/gfx/mot/at-top-bar-new.gif';
			document.getElementById("postcode").style.visibility = "hidden";

		}
		else
		{
			make_v = old_make_v;
			_model = _old_model;
			document.getElementById("used").name = 'used';
			document.getElementById("atTab1").className = "artTabSelected";
			document.getElementById("atTab2").className = "artTab";
			document.getElementById("at-image").src = 'http://www.tiscali.co.uk/media/images/gfx/mot/at-top-bar.gif';
			document.getElementById("postcode").style.visibility = "visible";			
		}

		var dd1 = document.getElementById("make");
		var dd2 = document.getElementById("model");
		removeOptions(dd1);
		removeOptions(dd2);

//		var elMake = document.createElement("option");
//		elMake.innerHTML = 'MAKE';
//		dd1.appendChild(elMake);

		var elMake = document.createElement("option");
		elMake.innerHTML = 'MODEL';
		dd2.appendChild(elMake);


		for (i = 0; i < make_v.length; i++)
		{
			var elMake = document.createElement("option");
			elMake.innerHTML = make_v[i];
			dd1.appendChild(elMake);
			elMake.value = make_v[i];
			if (elMake.value.toUpperCase().trim() == "")
			{
				dd1.options[i].selected = true;
				selectedMake = i;
			}
		}



		dd1.onchange = function()
		{
			updateModels(this.selectedIndex);
		}


	}