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

function ChangePreReg(obj,id)
{
	var el = document.getElementById(id);

	if (obj.checked) {el.value='Y';}
	else {el.value='N';}
}

function validateForm()
{
	if (document.getElementById("at_form"))
	{
		document.getElementById("at_form").onsubmit = function()
		{
			var pc = document.getElementById("postcode");
			
			if (pc && pc.value.trim() == "" && pc.style.visibility != "hidden")
			{
				showError("Please fill in your postcode");
				return false;
			}
			
			if (document.getElementById("makes").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 DisplayExtras(aid,tid,exp)
{
	var exid = document.getElementById(tid); //table id
	var exa = document.getElementById(aid); // link id 
	var exp = document.getElementById(exp); // expanded or not

	if (exid.style.display == '' || exid.style.display == 'none')
	{
		if (navigator.userAgent.indexOf('MSIE') !=-1) exid.style.display = 'block';
		else exid.style.display = 'table';
	
		exa.innerHTML = '[-] Advanced search options';
		exp.value = '1';
	}
	else
	{
		exid.style.display = 'none';
		exa.innerHTML = '[+] Advanced search options';
		exp.value = '0';
	}

}

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 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_form").insertBefore(x, document.getElementById("at_form").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 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 ChangeTab2(type,id)
{
	for (var i=1;i<=3;i++)
	{
		var div = document.getElementById('atTab'+i);

		if (id == i) {div.style.backgroundColor = '#ffffff';}
		else {div.style.backgroundColor = '#e6e5ea';}
	}
	
	var banner = document.getElementById('banner');
	
	if (type=='new')
	{
		document.getElementById('used').name = 'new';
		document.getElementById('postcode').style.visibility = 'hidden';
		document.getElementById('postcode').value='';
		
		banner.src='http://media.tiscali.co.uk/images/gfx/mot/heading-tab-at.jpg';
		
		arr_makes = new_makes;
		arr_models = new_models;
	}
	else if(type=='used')
	{
		document.getElementById('used').name = 'used';
		document.getElementById('postcode').style.visibility = 'visible';
		document.getElementById('postcode').value='Postcode';
		
		banner.src='http://media.tiscali.co.uk/images/gfx/mot/heading-tab-at.jpg';
		
		arr_makes = used_makes;
		arr_models = used_models;
	}
	else if(type=='bikes')
	{
		document.getElementById('used').name = 'bikes';
		document.getElementById('postcode').style.visibility = 'visible';
		document.getElementById('postcode').value='Postcode';
		
		banner.src='http://media.tiscali.co.uk/images/gfx/mot/heading-tab-at-bike.jpg';
		
		arr_makes = bikes_makes;
		arr_models = bikes_models;
	}
	
	DrawMakes('makes');
	DrawModels('makes','models',true);
}

function ChangeTab(type)
{
	if (type=='new')
	{
		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';
		document.getElementById('postcode').value='';
		
		arr_makes = new_makes;
		arr_models = new_models;
	}
	else if(type=='used')
	{
		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';
		document.getElementById('postcode').value='Postcode';
		
		arr_makes = used_makes;
		arr_models = used_models;
	}
	
	DrawMakes('makes');
	DrawModels('makes','models',true);
}

function DrawMakes(makeid)
{
	var makeid = document.getElementById(makeid);
	var sel_make = makeid.options[makeid.selectedIndex].value;
	
	makeid.length=0;
	
	for (i=0;i<arr_makes.length;i++)
	{
		makeid.options[i] = new Option(arr_makes[i]);
		makeid.options[i].value = arr_makes[i];	
		
		if (arr_makes[i] == sel_make)
		{
			makeid.options[i].selected=true;
		}
	}
}

function DrawModels(makeid,modelid,init)
{
	var makeid = document.getElementById(makeid);
	var modelid = document.getElementById(modelid);
				
	var sel_make = makeid.options[makeid.selectedIndex].value;
	var sel_modl = modelid.selectedIndex;
	
	if (arr_makes.length==0 || arr_models.length == 0) {return false;}
	
	for (i=0;i<arr_makes.length;i++)
	{
		if (arr_makes[i]==sel_make)
		{
			var res = arr_models[i];
			
			if (res.length)
			{
				modelid.length = 0;
				
				for (j=0;j<res.length;j++)
				{
					modelid.options[j] = new Option(res[j]);
					
					if (res[j]=='MODEL') {res[j]='ANY';}
					modelid.options[j].value = res[j];
				}

				//if (init) {modelid.options[sel_modl].selected=true;}
				break;
			}
		}
	}
}