function SearchResultsControl()
{
}

SearchResultsControl.prototype.sortTypeSelect = null;
SearchResultsControl.prototype.pageSizeSelect = null;
SearchResultsControl.prototype.firstPageButton = null;
SearchResultsControl.prototype.prevPageButton = null;
SearchResultsControl.prototype.nextPageButton = null;
SearchResultsControl.prototype.lastPageButton = null;
SearchResultsControl.prototype.compareButton = null;

SearchResultsControl.VehicleDetailsPDF = function(eventSource)
{    	
    var vehicleId = eventSource.getAttribute("vehicleid");
    var friendlyName = eventSource.getAttribute("friendlyName");
    
    var width = 850;
	var height = 650;
	
	xOffset = (window.screenLeft + (document.body.clientWidth / 2)) - width / 2;
	yOffset = (window.screenTop + (document.body.clientHeight / 2)) - height / 2 - 30;
    
    var url = friendlyName + '?vids=' + vehicleId + '&doPDF=true'
    
	var winOpts = 'toolbar=no, menubar=no, scrollbars=yes, location=no, resizable=yes, status=no, width=' + width + ', height=' + height;

	var win = window.open(url, 'PDF', winOpts);
	    
	win.focus();
}

SearchResultsControl.getClassInstance = function(controlId)
{
    var control = document.getElementById(controlId);
    
    if (control != null && control.classInstance == null)
    {
        var inst = new SearchResultsControl();

        inst.sortTypeSelect =
            document.getElementById(control.getAttribute("sortTypeControlId"));
         
         //our sort columns   
         inst.pictureSort =  
          document.getElementById(control.getAttribute("pictureSortControlId"));
         inst.vehicleModelSort =  
          document.getElementById(control.getAttribute("vehicleModelSortControlId"));
         inst.priceSort =  
          document.getElementById(control.getAttribute("priceSortControlId"));
         inst.mileageSort =  
          document.getElementById(control.getAttribute("mileageSortControlId"));
         inst.registrationDateSort =  
          document.getElementById(control.getAttribute("registrationDateSortControlId"));
         inst.locationSort =  
          document.getElementById(control.getAttribute("locationSortControlId"));                
        inst.pageSizeSelect =
            document.getElementById(control.getAttribute("pageSizeControlId"));
        inst.pageNumberSelect =     
             document.getElementById(control.getAttribute("pageNumberControlId"));
        inst.compareButton =
            document.getElementById(control.getAttribute("compareButtonControlId"));
        
        
        control.classInstance = inst;
    }
    
    return control == null ? null : control.classInstance;
}

//we now find what was column we are sorting on and wether it's asending / descneding
//store this in the sorttype hidden field for capture server side.
SearchResultsControl.sortResultsClick = function(controlId, parentControlId, controlName)
{
    var inst;
    var sortVal; 
    var returnData;
    var name;
        
    //0 = ascending
    //1 = descending
    
    //firstly get the control..
    inst = SearchResultsControl.getClassInstance(parentControlId);   
    
    //Picture
    if(controlId == inst.pictureSort)
    {
       name = inst.pictureSort.name;
       sort = Utils.getNode(inst.pictureSort, "sort");

       //check the sort type
       //if -1, it's a first load and NO IMAGE is displayed - set to sort type ASCENDING
       //vehicle
       if (sort == -1)
       {
            returnData = 3;
       }
       else
       {  //now invert the sort type - we have clicked the control after all..
          //4 = ascending, 11 = descending
          returnData = (sort == 3) ? 14: 3;
       }
       //store the value for a use server side.
       inst.sortTypeSelect.value = returnData;        
    }

    //Model;
    if(controlId == inst.vehicleModelSort)
    {
       name = inst.vehicleModelSort.name;
       sort = Utils.getNode(inst.vehicleModelSort, "sort");

       //check the sort type
       //if -1, it's a first load and NO IMAGE is displayed - set to sort type ASCENDING
       //vehicle
       if (sort == -1)
       {
            returnData = 4;
       }
       else
       {  //now invert the sort type - we have clicked the control after all..
          //4 = ascending, 11 = descending
          returnData = (sort == 4) ?11 : 4;
       }
       //store the value for a use server side.
       inst.sortTypeSelect.value = returnData;        
    }
    
    //location
    if(controlId == inst.locationSort)
    {
       name = inst.locationSort.name;
       sort = Utils.getNode(inst.locationSort, "sort");
       
       //check the sort type
       //if -1, it's a first load and NO IMAGE is displayed - set to sort type ASCENDING
       if (sort == -1)
       {
            returnData = 5;
       }
       else
       {  //now invert the sort type - we have clicked the control after all..
          //5 = ascending, 6 = descending
          returnData = (sort == 5) ? 6: 5;
       }
       //store the value for a use server side.
       inst.sortTypeSelect.value = returnData;    
    }

    
    //mileage
    if(controlId == inst.mileageSort)
    {
       name = inst.mileageSort.name;
       sort = Utils.getNode(inst.mileageSort, "sort");

       //check the sort type
       //if -1, it's a first load and NO IMAGE is displayed - set to sort type ASCENDING
       if (sort == -1)
       {
            returnData = 7;
       }
       else
       {  //now invert the sort type - we have clicked the control after all..
          //7 = ascending, 13 = descending
          returnData = (sort == 7) ? 13: 7;
       }
       //store the value for a use server side.
       inst.sortTypeSelect.value = returnData;        
    }

   
    //registration date note NOT model year.
    if(controlId == inst.registrationDateSort)
    {
       name = inst.registrationDateSort.name;
       sort = Utils.getNode(inst.registrationDateSort, "sort");

       //check the sort type
       //if -1, it's a first load and NO IMAGE is displayed - set to sort type ASCENDING
       if (sort == -1)
       {
            returnData = 8;
       }
       else
       {  //now invert the sort type - we have clicked the control after all..
          //8 = ascending, 16 = descending
          returnData = (sort == 8) ? 16: 8;
       }
       //store the value for a use server side.
       inst.sortTypeSelect.value = returnData;        
    }
      
    //price
    if(controlId == inst.priceSort)
    {
       name = inst.priceSort.name;
       sort = Utils.getNode(inst.priceSort, "sort");
       
       var x = inst.priceSort.sort
       //check the sort type
       //if -1, it's a first load and NO IMAGE is displayed - set to sort type DECENDING
       if (sort == -1)
       {
            returnData = 1;
       }
       else
       {  //now invert the sort type - we have clicked the control after all..
          //1 = ascending, 2 = descending
          returnData = (sort == 1) ? 2: 1;
       }
       
       //store the value for a use server side.
       inst.sortTypeSelect.value = returnData;        
    } 
    
    Utils.createSubmitHidden(Utils.getForm(), controlName, name);
    Utils.getForm().submit();
}

SearchResultsControl.getPageNumberClick = function (controlId, controlName, value)
{
    //firstly get the control..
    var inst = SearchResultsControl.getClassInstance(controlId);
    

    if (inst != null && inst.pageNumberSelect != null)
    {
        inst.pageNumberSelect.value = value;
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.pageNumberSelect.name);
    }
}


SearchResultsControl.pageSizeChange = function(controlId, controlName)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    if (inst != null && inst.pageSizeSelect != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.pageSizeSelect.name + ",true");
        Utils.getForm().submit();
    }
}


SearchResultsControl.compareButtonClick = function(controlId, controlName)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    if (inst != null && inst.compareButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.compareButton.name);
    }
}

SearchResultsControl.vehicleSelect = function(controlId, eventSource, message)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    var vehicleCheckboxes = document.getElementsByName(eventSource.name);
    
    var checkedCount = 0;
    
    for (var i = 0; i < vehicleCheckboxes.length; i++)
    {
        var checkbox = vehicleCheckboxes[i];
        
        if (checkbox.checked)
        {
            checkedCount++;
        }
    }
    
    if (checkedCount > 3)
    {
        eventSource.checked = false;
    }
    
    inst.compareButton.disabled = checkedCount < 2;
}

SearchResultsControl.showVehicleDetails = function(controlId, eventSource)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    var friendlyName = eventSource.getAttribute("friendlyName");
    var recordNumber = eventSource.getAttribute("recordNumber");
    var recordsCount = eventSource.getAttribute("recordsCount");
    var sortType = eventSource.getAttribute("sortType");
    
	var width = 666;
	var height = 750;
	
	xOffset = (window.screenLeft + (document.body.clientWidth / 2)) - width / 2;
	yOffset = (window.screenTop + (document.body.clientHeight / 2)) - height / 2 - 30;
	
	var url = friendlyName + '?recordNumber=' + recordNumber + '&recordsCount=' + recordsCount + '&sortType=' + sortType;	
	
	var winOpts = 'toolbar=no, menubar=no, scrollbars=yes, location=no, resizable=no, status=no, width=' + width + ', height=' + height;

	var win = window.open(
	    url,
	    'popup',
	    winOpts);
	     
	win.focus();
}

SearchResultsControl.printPage = function()
{
    var searchOptions = document.getElementById('searchOptionsPrint').value;
    var pageNumber = document.getElementById('pageNumberPrint').value;
    var pageSize = document.getElementById('pageSizePrint').value;
    
    if (pageNumber > -1)
    {
        var sortPrintCtrlID = document.getElementById('sortPrintCtrl').value;
        var sort = document.getElementById(sortPrintCtrlID).value;

        var width = 650;
        var height = 750;

        xOffset = (window.screenLeft + (document.body.clientWidth / 2)) - width / 2;
        yOffset = (window.screenTop + (document.body.clientHeight / 2)) - height / 2 - 30;

        var url = document.location.href + '/?searchOptions=' + searchOptions + '&doStockPDF=1' + '&sortPrint=' + sort + '&pageSizePrint=' + pageSize + '&pageNumberPrint=' + pageNumber;
        
        var winOpts = 'toolbar=no, menubar=no, scrollbars=yes, location=no, resizable=yes, status=no, width=' + width + ', height=' + height;

        var win = window.open(
            url,
            'popup',
            winOpts);

        win.focus();
    }
    else window.print();
}