﻿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.prototype.backToResultsButton = null;

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"));
        inst.pageSizeSelect =
            document.getElementById(control.getAttribute("pageSizeControlId"));
        inst.firstPageButton =
            document.getElementById(control.getAttribute("firstPageButtonControlId"));
        inst.prevPageButton =
            document.getElementById(control.getAttribute("prevPageButtonControlId"));
        inst.nextPageButton =
            document.getElementById(control.getAttribute("nextPageButtonControlId"));
        inst.lastPageButton =
            document.getElementById(control.getAttribute("lastPageButtonControlId"));
        inst.compareButton =
            document.getElementById(control.getAttribute("compareButtonControlId"));
        inst.backToResultsButton =
            document.getElementById(control.getAttribute("backToResultsButtonControlId"));            
        
        control.classInstance = inst;
    }
    
    return control == null ? null : control.classInstance;
}

SearchResultsControl.sortTypeChange = function(controlId, controlName)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    if (inst != null && inst.sortTypeSelect != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.sortTypeSelect.name + ",true");
        Utils.getForm().submit();
    }
}

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.firstPageButtonClick = function(controlId, controlName)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    if (inst != null && inst.firstPageButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.firstPageButton.name);
    }
}

SearchResultsControl.prevPageButtonClick = function(controlId, controlName)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    if (inst != null && inst.prevPageButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.prevPageButton.name);
    }
}

SearchResultsControl.nextPageButtonClick = function(controlId, controlName)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    if (inst != null && inst.nextPageButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.nextPageButton.name);
    }
}

SearchResultsControl.lastPageButtonClick = function(controlId, controlName)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    if (inst != null && inst.lastPageButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.lastPageButton.name);
    }
}

SearchResultsControl.compareButtonClick = function(controlId, eventSource)
{   
    var inst = SearchResultsControl.getClassInstance(controlId);     

    var removeVehicleId = eventSource.getAttribute("removeVehicleId");  
    var controlName = eventSource.getAttribute("controlName");
    
    if (inst != null && inst.compareButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.compareButton.name + "," + removeVehicleId);
    }
}

SearchResultsControl.backToResultsButtonClick = function(controlId, controlName)
{   
    var inst = SearchResultsControl.getClassInstance(controlId);     
    
    if (inst != null && inst.backToResultsButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.backToResultsButton.name);
        Utils.getForm().submit();
    }
}

SearchResultsControl.vehicleSelect = function(controlId, eventSource, errorMsg)
{
    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)
    {
        alert(errorMsg);
        eventSource.checked = false;
    }
    
    inst.compareButton.disabled = checkedCount < 2;
}

SearchResultsControl.showVehicleDetails = function(controlId, eventSource)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    var friendlyName = eventSource.getAttribute("friendlyName");
    
	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 = friendlyName;
	
	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();
}

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 position = document.location.href.indexOf("?");
        
        if(position > 1)
        {
            var url = document.location.href + '&amp;searchOptions=' + searchOptions + '&doStockPDF=1' + '&sortPrint=' + sort + '&pageSizePrint=' + pageSize + '&pageNumberPrint=' + pageNumber;
        }
        else
        {
            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();
}
