function Init()
{
	LoadLocation();
	
	if (DoSelChange)
		DoSelChange(null);
}

function LoadLocation()
{
	try {
		document.menuFindCentre.searchHome.value = GetCookie("homelocation");
	}
	catch (e)
	{
	}
}

function GetCookie(name)
{
	var res = "";
	var index = 0;

	if (document.cookie)
		index = document.cookie.indexOf(name + "=");
	else
		index = -1;

	if (index < 0)
		res = "";
	else
	{
		var countbegin = (document.cookie.indexOf("=", index) + 1);
		if (0 < countbegin)
		{
			var countend = document.cookie.indexOf(";", countbegin);
			if (countend < 0)
				countend = document.cookie.length;
			res = document.cookie.substring(countbegin, countend);
		}
		else
			res = "";
	}
	
	return res;
}

function ShowNotBuyingOther()
{
	var sel = document.getElementById("notBuyingSelect");
	var otherReason = document.getElementById("notBuyingOtherReason");
	var otherReasonDiv = document.getElementById("notBuyingOtherReasonDiv");
	
	if (sel == null || otherReason == null || otherReasonDiv == null)
		return;
		
	if (sel.options[sel.selectedIndex].value == "Other")
	{
		otherReasonDiv.style.display = "";
		otherReason.focus();
	}
	else
		otherReasonDiv.style.display = "none";
}

////////////////////////////////////////////////////////////////////////////////
//
// Make/Model lookup support
//
var REQUEST = null;
var CONTROL = null;

function CreateRequestObject()
{
	if (typeof XMLHttpRequest != 'undefined') 
	{
		try 
		{
			REQUEST = new XMLHttpRequest();
		} 
		catch (e) 
		{
			REQUEST = null;
		}
	}
	else
	{
		REQUEST = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function SubmitQuery(query)
{
	CreateRequestObject();

	if (query == "")
		REQUEST.open("GET", "/includes/makes.xml", false);
	else
		REQUEST.open("GET", "/ts/search/makemodellookup.aspx" + query, false);
	
	REQUEST.send(null);
	Extract();
}

function SubmitMotorbikeQuery(query)
{
	CreateRequestObject();
	REQUEST.open("GET", "/ts/search/motorbikelookup.aspx" + query, false);
	REQUEST.send(null);
	Extract();
}

function Extract()
{
	if (CONTROL == null)
		return;
		
	var tags = REQUEST.responseXML.getElementsByTagName(CONTROL.id);

	if (CONTROL.id == "MVRIS")
	{
		CONTROL.value = tags[0].firstChild.nodeValue;
		var searchButton = document.getElementById("searchButtonRow");
		if (searchButton != null)
			searchButton.style.display = "";
	}
	else
	{
		if (CONTROL.id.indexOf("Motorbike") != -1)
		{
			if (CONTROL.id == "MotorbikeSpeedRating")
			{
				CONTROL.options[0] = new Option("---", "");
				CONTROL.options[1] = new Option("Any", "Any");
			}
			else if (CONTROL.id == "MotorbikeType")
			{
				CONTROL.options[0] = new Option("Any", "");
			}
			else
			{
				CONTROL.options[0] = new Option("---", "");
			}
		}
		else
		{
			CONTROL.options[0] = new Option("Select...", "");
		}
		
		for (var i = 0; i < tags.length; i++)
		{
			var item = tags[i].firstChild.nodeValue;
			
			if (CONTROL.id == "MotorbikeSpeedRating") // Allow for '---' and 'Any'
			{
				CONTROL.options[i + 2] = new Option(GetMotorbikeSpeedRatingName(item), item);
			}
			else
				CONTROL.options[i + 1] = new Option(item, item);
		}
		
		CONTROL.options[0].selected = true;
		CONTROL.disabled = false;
		
		if (tags.length == 0)
		{
			//nothing for this column, move onto next;
			if (CONTROL.id == "MotorbikeHeight")
			{
				//alert(CONTROL.id);
				//DoMotorBikeChange(CONTROL); //here
			}
		}
	}
}

function GetSelectValue(control)
{
	if (control == null || control.selectedIndex < 0)
		return "";
	else
		return control.options[control.selectedIndex].value;
}

function ClearSel(ctrl1, ctrl2, ctrl3, ctrl4, ctrl5)
{
	if (ctrl1)
	{
		ctrl1.options.length = 0;
		ctrl1.disabled = true;
	}

	if (ctrl2)
	{
		ctrl2.options.length = 0;
		ctrl2.disabled = true;
	}

	if (ctrl3)
	{
		ctrl3.options.length = 0;
		ctrl3.disabled = true;
	}

	if (ctrl4)
	{
		ctrl4.options.length = 0;
		ctrl4.disabled = true;
	}

	if (ctrl5)
	{
		ctrl5.options.length = 0;
		ctrl5.disabled = true;
	}
}

function DoSelChange()
{
	// Should be overriden in pages that need it
}

function DoMotorbikeChange()
{
	// Should be overriden in pages that need it
}

function GetMotorbikeSpeedRatingName(speedRating)
{
	var speedRatingOut = speedRating;
	
	if (speedRating == "B")
		speedRatingOut = "B - 31 mph";
	if (speedRating == "F")
		speedRatingOut = "F - 50 mph";
	if (speedRating == "J")
		speedRatingOut = "J - 62 mph";
	if (speedRating == "L")
		speedRatingOut = "L - 74 mph";
	if (speedRating == "M")
		speedRatingOut = "M - 81 mph";
	if (speedRating == "P")
		speedRatingOut = "P - 93 mph";
	if (speedRating == "Q")
		speedRatingOut = "Q - 100 mph";
	if (speedRating == "R")
		speedRatingOut = "R - 106 mph";
	if (speedRating == "S")
		speedRatingOut = "S - 112 mph";
	if (speedRating == "T")
		speedRatingOut = "T - 118 mph";
	if (speedRating == "H")
		speedRatingOut = "H - 130 mph";
	if (speedRating == "V")
		speedRatingOut = "V - up to 149 mph";
	if (speedRating == "(V)")
		speedRatingOut = "(V) - over 149 mph";
	if (speedRating == "W")
		speedRatingOut = "W - up to 168 mph";
	if (speedRating == "(W)")
		speedRatingOut = "(W) - over 168 mph";
	if (speedRating == "Z")
		speedRatingOut = "Z - over 149 mph";
	
	return speedRatingOut;
}