var qqreq;

function viewNotice(pnoticeId)
{
	if(document.searchForm.NI!=null) document.searchForm.NI.value = pnoticeId;
	document.searchForm.action="/car_details";
	document.searchForm.method.value="view";
	document.searchForm.submit();
}

function ReturnToList()
{
	//If the feedback form is showing then we need to hide it before we submit
	//because the notice reference is on that form
	if(feedbackHTML=="") ShowHideFeedback();
	if(emailafriendHTML=="") ShowHideEmailAFriend();

	document.searchForm.action="/cars";
	document.searchForm.method.value="search";
	document.searchForm.NI.value="";
	document.searchForm.NR.value="";
	document.searchForm.submit();
}

function GetQuickQuote()
{
	var amount = document.searchForm.qqamount.value;
	if(!isNaN(parseInt(amount)))
	{
		var qqageCookieVal = readCookie("quickQuoteAge");
		var qqperiodCookieVal = readCookie("quickQuotePeriod");
		var qqhouseownerCookieVal = readCookie("quickQuoteHomeOwner"); 
		var url = "/servlet/QuickQuote?AGE=" + qqageCookieVal + "&AMT=" + amount + "&TM=" + qqperiodCookieVal + "&HO=" + qqhouseownerCookieVal;
		qqreq = createObject();
		qqreq.open("GET", url, true);
		qqreq.onreadystatechange = quickquotecallback;
		qqreq.send(null);
	}
}
function quickquotecallback()
{
	if (qqreq.readyState == 4)
	{
		if (qqreq.status == 200)
		{
			quickquoteparseMessage();
		}
	}
}
function quickquoteparseMessage()
{
	if(qqreq.responseXML!=null)
	{
		var message = qqreq.responseXML.getElementsByTagName("message");
		if(message!=null && message.length>0)
		{
			var amount = document.searchForm.qqamount.value;
			var qqageCookieVal = readCookie("quickQuoteAge");
			var qqperiodCookieVal = readCookie("quickQuotePeriod");
			var qqhouseownerCookieVal = readCookie("quickQuoteHomeOwner"); 
			var vals = message[0].childNodes;
			document.getElementById("quickquotelinksection").innerHTML = "<a class=\"skupe_link\" target=\"_\" href=\"http://www2.smart-quotes.com/landing/generic/loans.asp?amount=" + amount + "&term=" + qqperiodCookieVal + "&owner=" + qqhouseownerCookieVal + "&age=" + qqageCookieVal + "&ctc=1024&cmpg=&web=&src=1380&brn=497\">best financing option <b>&pound;" + vals[0].firstChild.nodeValue + " per month</b></a> [<a id=\"quickquotelink\" class=\"skupe_link\" href=\"javascript:ShowHideQuickQuote();\">change settings</a>]";
		}
	}
}
function SubmitQuickQuote()
{
	if(ValidateQuickQuote())
	{
		createCookie("quickQuoteAge", document.searchForm.qqage.value,1000);
		createCookie("quickQuotePeriod", document.searchForm.qqperiod.value,1000);
		createCookie("quickQuoteHomeOwner", document.searchForm.qqhouseowner.value,1000);
		alert("Details saved.");
		ShowHideQuickQuote();
		GetQuickQuote(); 
	}
}
var quickQuoteHTML = "";
function ShowHideQuickQuote()
{
	var elem1 = document.getElementById("quickquoteimg");
	var elem2 = document.getElementById("quickquotelink");
	var elem3 = document.getElementById("quickquotesection");
	if(quickQuoteHTML=="")
	{
		quickQuoteHTML = elem3.innerHTML;
		elem3.innerHTML = "";
		elem2.className = "skupe_link";
		elem1.src="/images/list_icon.gif";
		elem2.blur();
	}
	else
	{
		elem2.className = "activelink";
		elem1.src="/images/list_icon_d.gif";
		elem3.innerHTML = quickQuoteHTML;
		quickQuoteHTML = "";
		var qqageCookieVal = readCookie("quickQuoteAge");
		var qqperiodCookieVal = readCookie("quickQuotePeriod");
		var qqhouseownerCookieVal = readCookie("quickQuoteHomeOwner");
		if(qqageCookieVal.length>0)
		{
			document.searchForm.qqage.value = qqageCookieVal;
		}
		if(qqperiodCookieVal.length>0)
		{
			document.searchForm.qqperiod.value = qqperiodCookieVal;
		}
		if(qqhouseownerCookieVal.length>0)
		{
			document.searchForm.qqhouseowner.value = qqhouseownerCookieVal;
		}
		document.searchForm.qqage.focus();
	}
}
function ValidateQuickQuote()
{
	var sError;
	sError = "";
	if(document.searchForm.qqage.value==null||document.searchForm.qqage.value.length==0)
	{
		sError += "Please enter your age\n";
	}
	else if(isNaN(parseInt(document.searchForm.qqage.value)))
	{
		sError += "Age must be an integer value\n";
	}
	else
	{
		document.searchForm.qqage.value = parseInt(document.searchForm.qqage.value);
	}
	if(sError.length==0)
	{
		return true;
	}
	else
	{
		alert(sError);
		return false;
	}
}
function initQuickQuote()
{
	if(readCookie("quickQuoteAge")==null) createCookie("quickQuoteAge", "30", 1000);
	if(readCookie("quickQuotePeriod")==null) createCookie("quickQuotePeriod", "60",1000);
	if(readCookie("quickQuoteHomeOwner")==null) createCookie("quickQuoteHomeOwner", "Yes", 1000);
	ShowHideQuickQuote();
	GetQuickQuote();
}