	var qDealer, qCategory, qMake, qModel, qGrade, qMinPrice, qMaxPrice, qTransmission, qBodyType, qFuelType, qMinRegDate, qMinMileage, qMaxMileage;  //search panel vars
	var qOfferType, qId, qPage, qUrl, panelId, supergroup; //page specific vars
	var qParams = {}; //query params
	var panelSet = false; //for initial load - to handle direct links and page back events 
	var nhpc = "n"; //set false initially and reset to true for nhpc
	
    $(window).hashchange( function(){
        //get hash values
        getHash ();		
        ajaxVehicles(qUrl,qParams);
    })
	
	function ajaxVehicles(strUrl, searchParams)
	//strUrl = path of php file
	{

        $.ajaxSetup({
            timeout: 5000
        });

        $.ajax({
            url: strUrl,
            context: document.body,
            data: searchParams,
            async: false,
            success: function(response){
                //check for results
                if ($(response).filter("div#results")[0]) 
                {
                    $("#initCarHolder").html($(response).filter("div#results")[0].innerHTML);
                    
                    $('a.zoom').lightBox({
                        imageLoading: 'http://dealerassets.nmsdigital.co.uk/js/lightbox/images/lightbox-ico-loading.gif',
                        imageBtnClose: 'http://dealerassets.nmsdigital.co.uk/js/lightbox/images/lightbox-btn-close.gif',
                        imageBtnPrev: 'http://dealerassets.nmsdigital.co.uk/js/lightbox/images/lightbox-btn-prev.gif',  
                        imageBtnNext: 'http://dealerassets.nmsdigital.co.uk/js/lightbox/images/lightbox-btn-next.gif'        
                    });
                }
                else
                {
                    $("#initCarHolder").html("");
                }
                //model results
            
                if ($(response).filter("div#modelresults")[0]) 
                {
                    $("#modelHolder").html($(response).filter("div#modelresults")[0].innerHTML);
                }
                else
                {
                    $("#modelHolder").html("");
                }
                //check if pagination returned
                if ($(response).filter("div#pagination")[0])
                {
                    //reshow pagination if it was hidden
                    $("#vehPaginationHolder").show();
                    $("#vehPaginationHolder").html($(response).filter("div#pagination")[0].innerHTML);
                }
                else {
                    //hide pagination if not returned
                    $("#vehPaginationHolder").hide();
                }
                //check if an updated title is returned.
                if ($(response).filter("div#titleresults")[0])
                {
                    document.title = $(response).filter("div#titleresults")[0].innerHTML;  
                }
                //check if breadcrumbs were updated 
                if ($(response).filter("div#breadcrumbresults")[0])
                {
                    $(".breadcrumb").html($(response).filter("div#breadcrumbresults")[0].innerHTML);
                }
                //check if subtitle returned
                if ($(response).filter("div#subtitleresults")[0])
                {
                    $(".subPageTitle").html($(response).filter("div#subtitleresults")[0].innerHTML);
                }
				//dealer address
				if ($(response).filter("div#addressresults")[0]) 
                {
                    $("div.contactDetails").html($(response).filter("div#addressresults")[0].innerHTML);
                }
				
                //if panel1 and id is not set and page is page 1 or panel hasn't been set (for direct access and page back)
                if (searchParams["page"]=="1" || panelSet == false) {
                    if(panelId==1 && !searchParams["id"]){
                        setPanelCounts();
                        //bindPanel();
                    }
                }
				

            },
            error: function(xhr, status, error){
                //$(this).addClass("error");
                var strCode = xhr.status;
                var strMessage = "";
                if (strCode == "200") 
                {
                    strMessage = "200 - Error";
                }
                else if (strCode == "404") 
                {
                    strMessage = "404 - Page Not Found";
                }
                else if (strCode == "0") 
                {
                    this.tryCount++;
                    if(this.tryCount <= this.retryLimit)
                    {
                        //try again
                        $.ajax(this);
                        return;
                    }
                }
                else 
                {
                    strMessage = strCode + " - Error";                    
                }
                //$("#initCarHolder").html(strMessage);
            }
        });
	}
	
	
	function getNextPage(strPage)
	{
		//get current search params
		getHash ()
		//set the page
		qParams["page"] = strPage;
		window.location.hash = $.param(qParams);

	}
	
	function getHash() 
    {
		var hash = location.hash;
		qParams = $.deparam.fragment();
		if (nhpc=="y")
		{
			qParams["nhpc"] = nhpc;
		}
	}
	
	function clearParams() 
    {
		for(var index in qParams) 
        {
			delete qParams[index];
		}
	
	}
		
	function getQString() 
    {
		var qs = location.search;
		qParams = $.deparam.querystring();
		if (qParams["minPrice"] != undefined)
		{
			qParams["min"] = qParams["minPrice"];
			delete qParams["minPrice"];
		}
		if (qParams["maxPrice"] != undefined)
		{
			qParams["max"] = qParams["maxPrice"];
			delete qParams["maxPrice"];
		}
	}
	
	function FilterChanged()
	{

		//clear previous values
		clearParams();
		//panelId: 1=full, 2=new, 3=used
		if (panelId == 1) 
		{
			qParams["transmission"] = transmission;
			qParams["body"] = bodyType;
			qParams["fueltype"] = fuelType;
			qParams["minreg"] = minRegDate;
			qParams["minmileage"] = minMileage;
			qParams["maxmileage"] = maxMileage;
		}
		else if (panelId == 2) 
		{
			qParams["category"] = category;
		}
		
		if (panelId == 1 || panelId == 3)
		{
			qParams["make"] = make;
		}

		if (supergroup) {
			qParams["dealer"] = dealer;
		}

		qParams["model"] = model;
		qParams["min"] = minPrice;
		qParams["max"] = maxPrice;
		qParams["page"] = 1; 
		window.location.hash = $.param(qParams);

	}
	
	function setInitValues()
    {
		//qdealer is set from the server side code 
		if (panelId == 1) 
        {
			qMake = qParams["make"];
			qModel = qParams["model"];
			qMinPrice = qParams["min"];
			qMaxPrice = qParams["max"];
			qTransmission = qParams["transmission"]; 
			qBodyType = qParams["body"]; 
			qFuelType = qParams["fueltype"];
			qMinRegDate = qParams["minreg"];
			qMinMileage = qParams["minmileage"];
			qMaxMileage = qParams["maxmileage"];
		}
		else if (panelId == 2) 
        {
			qCategory = qParams["category"];
			qModel = qParams["model"];
			qMinPrice = qParams["min"];
			qMaxPrice = qParams["max"];
		}
		else if (panelId == 3) 
        {
			qMake = qParams["make"];
			qModel = qParams["model"];
			qMinPrice = qParams["min"];
			qMaxPrice = qParams["max"];
		}
	
	}
	
	function panelInit()
	{
		//panelId: 1=full, 2=new, 3=used
		//boolSG: T/F if super group
		if (supergroup) 
        {
			if (panelId == 1) 
            {                
				SearchPanelVarsInit(qDealer, qMake, qModel, qMinPrice != "" && qMinPrice != undefined ? qMinPrice : 0, qMaxPrice != "" && qMaxPrice != undefined ? qMaxPrice : 80000, qTransmission, qBodyType, qFuelType, qMinRegDate, qMinMileage != "" && qMinMileage != undefined ? qMinMileage : 0, qMaxMileage != "" && qMaxMileage != undefined ? qMaxMileage : 150000);
			}
			else if (panelId == 2) 
            {
				SearchPanelVarsInit(qDealer, qCategory, qModel, qMinPrice != "" && qMinPrice != undefined ? qMinPrice : 0, qMaxPrice != "" && qMaxPrice != undefined ? qMaxPrice : 80000)
			}
			else if (panelId == 3) 
            {			
			}
		
		}
		else 
        {
			if (panelId == 1) 
            {
				SearchPanelVarsInit(qMake, qModel, qMinPrice != "" && qMinPrice != undefined ? qMinPrice : 0, qMaxPrice != "" && qMaxPrice != undefined ? qMaxPrice : 80000, qTransmission, qBodyType, qFuelType, qMinRegDate, qMinMileage != "" && qMinMileage != undefined ? qMinMileage : 0, qMaxMileage != "" && qMaxMileage != undefined ? qMaxMileage : 150000);
			}
			else if (panelId == 2) 
            {
				SearchPanelVarsInit(qCategory, qModel, qMinPrice != "" && qMinPrice != undefined ? qMinPrice : 0, qMaxPrice != "" && qMaxPrice != undefined ? qMaxPrice : 80000)
			}
			else if (panelId == 3) 
            {				
			}		
		}		
	}
	
	function unbindPanel() {
		document.getElementById("ddlMakeList").onchange = null;
		document.getElementById("ddlModelList").onchange = null;
	}
	
	function bindPanel(){
		document.getElementById("ddlMakeList").onchange = function(){MakeChanged()};
		document.getElementById("ddlModelList").onchange = function(){SetFilter()};
	}
	
	
	function setPanelCounts()
	{
		//panelId: 1=full, 2=new, 3=used
		if (panelId==1)
        {
			panelSet = false; 
			
			unbindPanel();			
			$.ajax({
                url: pUrl,
                dataType: "script",
                data: qParams,
                async: false,
                success: function(response){
			  
                    $('#ddlMakeList').children().remove();
                    $('#ddlModelList').children().remove();
			  
                    var opt="<option value=''>Select a make</option>"+"\n";
                    $.each(list2, function(key, value) {
                        opt += "<option value='"+key+"'>"+key+" ("+value+")</option>"+"\n"; 
                    });
                    $('#ddlMakeList').html(opt);
				

				
                    if (curMake != 'ALL' && curMake != 'DEFAULT' && curMake != '') 
                    {
                        //alert("Changing Make In SetPanelCounts()");
                        $('#ddlMakeList').val(curMake);
                    }
                    else 
                    {
                        //alert("Changing Make In SetPanelCounts()");
                        $('#ddlMakeList').val("Select a make");
                    }
				
                    
                    var opt2 = "<option value=\"\">Select a model</option>"+"\n";
                    var opt2make = $('#ddlMakeList').val();
                    if(opt2make == '')
                    {
                        opt2make = 'DEFAULT';
                    }
                    for(var i in list3[opt2make])
                    {
                        var valueText = list3[opt2make][i].split("|");
                        opt2 += "<option value=\"" + valueText[0] + "\">" + valueText[1] + "</option>" + "\n"; 
                    }                    
                    

                    
                    //alert("appending" + opt2);
                    $('#ddlModelList').html(opt2);
                                    
                    if (curModel != 'ALL' && curModel != 'DEFAULT' && curModel != '') 
                    {
                        //alert("Changing Model To " + curModel + "In SetPanelCounts()");
                        $('#ddlModelList').val(curModel);
                    }
                    else 
                    {
                        //alert("Changing Model To All In SetPanelCounts()");
                        $('#ddlModelList').val("All");
                    }
                    
													
                    panelSet = true;				
			  
                },
                error: function(xhr, status, error){

                }
			});	
			bindPanel();
		} //panel 1
	}
	
