﻿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"));
        inst.selectedPageNumber = 
            document.getElementById(control.getAttribute("selectedPageNumberControlId"));
        inst.selectedCompareVehicle = 
            document.getElementById(control.getAttribute("selectedCompareVehicleControlId"));            
        
        control.classInstance = inst;
    }
    
    return control == null ? null : control.classInstance;
}

SearchResultsControl.OpenInPopUp = function(url, title, width, height, sizable)
{
	xOffset = (window.screenLeft + (document.body.clientWidth/2)) - width/2;
	yOffset = (window.screenTop + (document.body.clientHeight/2)) - height/2;
	
	var hwin = window.open(
		url,
		title,
		'width='+width+',height='+height+',top='+yOffset+',left='+xOffset+',status=yes,toolbar=no,menubar=no,scrollbars=yes,resizable='+sizable+'',
		false);
	
	hwin.focus();
}

SearchResultsControl.sortTypeChange = function(controlId, controlName, eventSource)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    if (inst != null && inst.sortTypeSelect != null)
    {
        inst.sortTypeSelect.value = eventSource.value;
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.sortTypeSelect.name + ",true");
        Utils.getForm().submit();
    }
}


//SearchResultsControl.sortTypeChange = function(controlId, controlName, eventSource)
//{
// 
//   // var sortTypeValue=eventSource.value;
//    var inst = SearchResultsControl.getClassInstance(controlId);  
//    if (inst != null && inst.sortTypeSelect != null)
//    {
//        inst.sortTypeSelect.value = eventSource.value;
//        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.sortTypeSelect.name + ",true");
//        Utils.getForm().submit();
//    }
//}


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.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.pageNumberSelected = function(controlId, controlName, pageNumber)
{
    var inst = SearchResultsControl.getClassInstance(controlId);
    
    if (inst != null && inst.selectedPageNumber != null)
    {
        inst.selectedPageNumber.value = pageNumber;
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.selectedPageNumber.name);
        Utils.getForm().submit();
    }
}

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.backToResultsButtonClick = function(controlId, controlName)
{   
    var inst = SearchResultsControl.getClassInstance(controlId);     
    
    if (inst != null && inst.backToResultsButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.backToResultsButton.name);
    }
}

SearchResultsControl.SelectVehicleForCompare = function(controlId, eventSource)
{
    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("A maximum of 3 vehicled can be selected for comparison.");
        eventSource.checked = false;
    }
    
    inst.compareButton.disabled = checkedCount < 2;
}

SearchResultsControl.showVehicleDetails = function(controlId, eventSource)
{
    var url = eventSource.getAttribute("friendlyName");
    
	var width = 600;
	var height = 770;
	
	xOffset = (window.screenLeft + (document.body.clientWidth / 2)) - width / 2;
	yOffset = (window.screenTop + (document.body.clientHeight / 2)) - height / 2 - 30;
	
	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.showVehiclePDF = function(controlId, eventSource, params)
{
    var url = eventSource.getAttribute("friendlyName");
    url = url + params;
    
	var width = 800;
	var height = 770;
	
	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();
}

