var TRACK_CSUSEDCARS3 = true;

/* This is for search button Basic 
DEPRECATED
Please do not use these anymore. It has been moved to CSSearchHelper*/
btnSearchBig = new Image();
btnSearchBig.src = "/carcontent/cse/search_big.jpg";	
	
btnSearchBig_Hover = new Image(); 
btnSearchBig_Hover.src = "/carcontent/cse/search_big_ov.jpg";	

/* This is for search button advanced */
btnSearchSmall = new Image();
btnSearchSmall.src = "/carcontent/cse/search.gif";	
	
btnSearchSmall_Hover = new Image(); 
btnSearchSmall_Hover.src = "/carcontent/cse/search_ov.gif";

function validatePostcode(txtPostcode, isStateValid)
{
	// if control is not specifed (null) 
	// just return true because nothing to validate
	if (txtPostcode == "")
		return true;
	
	var postcode = document.getElementById(txtPostcode).value;	
	if (postcode.length > 0)
	{	
		var regex = /^\d{3,4}$/;
		if (regex.test(postcode))
			return true;
		
		alert("Please enter a valid Postcode");	
	}	
	else 
	{
		// handle if state is valid and postcode is not provided (empty)
		if (!isStateValid)			
			// postcode always come together with state
			alert("You must select a State or enter a Postcode");
		else
			return true;
	}	
				
	return false;
}

function __doValidation(cboState, txtPostcode)
{	
	// if state exists, validate the postcode if provided
	if (document.getElementById(cboState).selectedIndex > 0)
		return validatePostcode(txtPostcode, true);
		
	// if no postcode control then validate state
	if (txtPostcode == "")
	{
		alert("You must select a State");
		return false;
	}
	
	return validatePostcode(txtPostcode, false);
}

function __doReset()
{
	if (typeof(AJAX_ENABLED) != "undefined")
	{
		if (AJAX_ENABLED != null && AJAX_ENABLED == true)
		{				
			document.forms[0].reset();
			document.location.hash = "Reset=1";
			
			// this var to keep category drop down if any
			var category = null;
								
			var ctrl = document.forms[0].elements; 					
			for (i=0; i<ctrl.length; i++)
			{							
				if (ctrl[i].id.indexOf("Category") > -1)
					category = ctrl[i];
					
				if (ctrl[i].type == "select-one")
				{	
					ctrl[i].selectedIndex = 0;
					if (ctrl[i].options[0].text.indexOf("model") > 0)
					{
						ctrl[i].options.length = 0;
						ctrl[i].options[0] = new Option("Any model", "");
					}					
					else if (ctrl[i].options[0].text.indexOf("version") > 0)
					{
						ctrl[i].options.length = 0;
						ctrl[i].options[0] = new Option("Any version", "");						
					}					
					else if (ctrl[i].options[0].text.indexOf("State Wide") > -1)
					{
						ctrl[i].options.length = 0;
						ctrl[i].options[0] = new Option("All Regions", "");
					}
					else if (ctrl[i].options[0].text.indexOf("10km") > -1)
					{
						ctrl[i].selectedIndex = 1;
					}
				}
				else if (ctrl[i].type == "select-multiple")
				{
					if (ctrl[i].options[0].text.indexOf("version") > 0)
					{
						ctrl[i].options.length = 0;
						ctrl[i].options[0] = new Option("Any version", "");
						ctrl[i].size = 1;
					}					
				}
				else if (ctrl[i].type == "text")
				{				
					ctrl[i].value = "";
				}
			}
			
			//Check whether category is set
			if (category != null)
			{
				var regexCategory = new RegExp("[?&]category=([0-4]{1})&*", "g");
				var url = location.href;	
				var match = regexCategory.exec(url);				
				if (match != null && match.length > 0)
				{	
					var index = match[1]
					if (index <= category.options.length-1)
						category.selectedIndex = index;
				}
			}
			return;
		}
	}	
	
	var url = location.href;	
	if (url.indexOf("?") > -1)
	{
		var baseUrl = url.substring(0, url.indexOf("?"));
		var regexCategory = new RegExp("[?&](category=[0-4]{1})&*", "g");		
		var match = regexCategory.exec(url);	
		if (match != null && match.length > 0)
		{
			url = baseUrl + "?" + match[1];
		}	
		else
			url = baseUrl;
	}
	location.href = url;
}

function __doSubmit(target, event)
{
	if (event.keyCode==13)
	{
		document.getElementById(target).click();
		if (window.event)
			window.event.returnValue = null; 
		else
			event.preventDefault();
	}
}

function __doLinkClick(target, event)
{
	if (event.keyCode==13)
	{
	    //if the target doesn't have onclick then proceed to href
	    //it the target do have onclick check if it passes the validation i.e doesn't return false
	    if ((!document.getElementById(target).onclick) || (document.getElementById(target).onclick() !== false))
		{
		    window.location.href=document.getElementById(target).href;
		} 
		
		if (window.event)
			window.event.returnValue = null; 
		else
			event.preventDefault();
	}
}


// This function is called if Ajax failed in refine postcode
function __doPostCodeValidation(txtPostCode, e)
{
	if (e.keyCode==13)
	{		
		var postCode = document.getElementById(txtPostCode).value;		
		var regex = /^\d{3,4}$/;
		if (regex.test(postCode) == false)
		{
			alert("You must provide a valid Postcode");	
			if (window.event)
				window.event.returnValue = null; 
			else
				e.preventDefault();
			return false;
		}			
		
		return true;
	}		
}

function select_all(cookie_name)
{
	var mylist = get_cookie(cookie_name);
	//set checkboxes
	var j = document.forms[0];
	for (i=0; i < j.length; i++)
	{
		if (j[i].name == "m" && j[i].checked == false)
		{
			j[i].checked = true;
			if (mylist == "")
				mylist = j[i].value;
			else
				mylist = mylist+","+j[i].value;
		}
	}
	set_cookie(cookie_name,mylist);
}

function deselect_all(cookie_name)
{
	//clear checkboxes
	var j = document.forms[0];
	for (i=0; i < j.length; i++)
	{
		if (j[i].name == "m")
			j[i].checked = false;
	}
	delete_cookie(cookie_name);
}

function reset_all(cookie_name)
{
	var mylist = get_cookie(cookie_name);
	var j = document.forms[0];
	for (i=0; i < j.length; i++)
	{
		if (j[i].name == "m" && mylist.indexOf(j[i].value) != -1)
			j[i].checked = true;
	}
}

function add_mylist(url, cookie_name, param_name)
{
	var mylist = get_cookie(cookie_name);
	if (mylist != null && mylist.length > 0)
		location.href = replaceUrlParams(url, param_name, mylist);
	else
		location.href = removeUrlParams(url, param_name);
	return false;
}

function add_mylist_legacy(url, cookie_name, param_name)
{
	var mylist = get_cookie(cookie_name);
	if (mylist != null && mylist.length > 0)
	{
		location.href = replaceUrlParams(url, param_name, mylist);
	}
	else
	{
		url = removeUrlParams(url, param_name);
		if(url.indexOf("?")<0)
			location.href = url+"?"+param_name+"=";
		else
			location.href = url+"&"+param_name+"=";	
	}
	return false;
}

function add_mylist_clip(url, cookie_name, param_name)
{
	var mylist = get_cookie(cookie_name);
	mylist = mylist.replace(",","+");
	if (mylist != null && mylist.length > 0)
		location.href = replaceUrlParams(url, param_name, mylist);
	else
		location.href = removeUrlParams(url, param_name);
	return false;
}


// functions for private mylist (legacy)
function add_pvmylist(url)
{
	return add_mylist_legacy(url, "csmylistprivate", "pvmylist");
}

//==================private listing=============
function reset_all_pvmylist()
{
	reset_all("csmylistprivate");
}

// functions for private mylist (real)
function add_pvmylist_R(url)
{
	return add_mylist_clip(url, "csmylistprivate", "R");
}

function select_all_pvmylist()
{
	select_all("csmylistprivate");
	return false;
}

function deselect_all_pvmylist()
{
	deselect_all("csmylistprivate");
	return false;
}
//======================================================

//======================dealer listing=========================
function reset_all_dlmylist()
{
	reset_all("csmylistdealerused");
}

//functions for dealer my list
function add_dlmylist_R(url)
{
	return add_mylist_clip(url, "csmylistdealerused", "R");
}

function select_all_dlmylist()
{
	select_all("csmylistdealerused");
	return false;
}

function deselect_all_dlmylist()
{
	deselect_all("csmylistdealerused");
	return false;
}
//===========================================================

