var key_code = GetKCQS();

if (key_code != "")
{
	//set cookie for kc
	setCookie('kc', key_code, 45)
}

function GetDefaultKeycode()
{
	return "SGA001";
}

function GetQS(s)
{
	var return_val = s;

	//check the querystring for kc
	var kc = GetKCQS();
	
	if (kc == "")
	{
		//check cookie
		kc = getCookie(kc);

		if (kc == null) kc = GetDefaultKeycode();
	}
	
	var qs_add = "?";
	if (return_val.indexOf('?') > 0) qs_add = "&";
	
	return (return_val + qs_add + "kc=" + kc);
}

function createJavaScriptLink(linkHref, anchorText, linkClass, newWindow)
{
	var targetString = "";
	if( newWindow )
		targetString = "target=\"_blank\"";	
	document.write("<a " + targetString + " class=\"" + linkClass + "\"" + " href=\"" + "http://www.autoanything.com/" + GetQS(linkHref) + "\">" + anchorText + "</a>");
}

function createJavaScriptImgLink(linkHref, anchorImg, altText, imgWidth, imgHeight, newWindow)
{
	var targetString = "";
	var imgWidthString = "";
	var imgHeightString = "";
	if( newWindow )
		targetString = "target=\"_blank\"";
	if( imgWidth != "" )
		imgWidthString = " width=\"" + imgWidth + "\"";
	if( imgHeight != "" )
		imgHeightString = " height=\"" + imgHeight + "\"";		
	document.write("<a " + targetString + " href=\"" + "http://www.autoanything.com/" + GetQS(linkHref) + "\">" + "<img src=\"" + anchorImg + "\"" + " alt=\"" + altText +"\"" + imgWidthString + imgHeightString + " border='0'></a>");
}

function GetKCQS()
{
	var query		= window.location.search.substring(1);
	var vars		= query.split("&");
	var return_val	= "";
	
	for (var i=0;i<vars.length;i++)
	{
		var pair = vars[i].split("=");
		if (pair[0] == "kc")
			return_val = pair[1];
	}
	
	return return_val;
}

function getCookie(NameOfCookie)
{
	if (document.cookie.length > 0)
	{
		begin = document.cookie.indexOf(NameOfCookie+"=");
		
		if (begin != -1) 
		{ 
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);

			if (end == -1)
				end = document.cookie.length;

			return unescape(document.cookie.substring(begin, end));
		} 
	}

	return null; 
}

function setCookie(NameOfCookie, value, expiredays) 
{
	var ExpireDate = new Date ();
	
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function delCookie (NameOfCookie) 
{
	if (getCookie(NameOfCookie))
	{
		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}