function SearchResults()
{
}

SearchResults.prototype.sortTypeChange = null;
SearchResults.prototype.pageSizeSelect = null;
SearchResults.prototype.prevPageButton = null;
SearchResults.prototype.nextPageButton = null;
SearchResults.prototype.compareButton = null;
SearchResults.prototype.shortlistButton = null;
SearchResults.prototype.selectedPageNumber = null;
SearchResults.prototype.selectedShortlistVehicle = null;
SearchResults.prototype.selectedCompareVehicle = null;
SearchResults.prototype.sortTypeSelect = null;


SearchResults.getClassInstance = function(controlId)
{
    var control = document.getElementById(controlId);
    
    if (control != null && control.classInstance == null)
    {
        var inst = new SearchResults();
        inst.sortTypeSelect = document.getElementById(control.getAttribute("sortTypeControlId"));        
        inst.pageSizeSelect = document.getElementById(control.getAttribute("pageSizeControlId"));
        inst.prevPageButton = document.getElementById(control.getAttribute("prevPageButtonControlId"));
        inst.nextPageButton = document.getElementById(control.getAttribute("nextPageButtonControlId"));
        inst.compareButton = document.getElementById(control.getAttribute("compareButtonControlId"));
        inst.shortlistButton = document.getElementById(control.getAttribute("shortlistButtonControlId"));        
        inst.selectedPageNumber = document.getElementById(control.getAttribute("selectedPageNumberControlId"));
        inst.shortlistSessionValues = document.getElementById(control.getAttribute("selectedPageNumberControlId"));
        inst.selectedShortlistVehicle = document.getElementById(control.getAttribute("selectedShortlistVehicleControlId"));
        inst.selectedCompareVehicle = document.getElementById(control.getAttribute("selectedVehicleControlId"));
        
        control.classInstance = inst;
    }
    
    return control == null ? null : control.classInstance;
}


SearchResults.sortTypeChange = function(controlId, controlName)
{
    var inst = SearchResults.getClassInstance(controlId);
       
    if (inst != null && inst.sortTypeSelect != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.sortTypeSelect.name + ",true");
        Utils.getForm().submit();
    }
}

SearchResults.pageSizeChange = function(controlId, controlName)
{
    var inst = SearchResults.getClassInstance(controlId);
    
    if (inst != null && inst.pageSizeSelect != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.pageSizeSelect.name + ",false");
        Utils.getForm().submit();
    }
}

SearchResults.prevPageButtonClick = function(controlId, controlName, pageNumber)
{
    var inst = SearchResults.getClassInstance(controlId);
        
    if (inst != null && inst.prevPageButton != null)
    {
        inst.selectedPageNumber.value = pageNumber;
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.prevPageButton.name);
        Utils.getForm().submit();
    }
}

SearchResults.nextPageButtonClick = function(controlId, controlName, pageNumber)
{
    var inst = SearchResults.getClassInstance(controlId);
        
    if (inst != null && inst.nextPageButton != null)
    {
        inst.selectedPageNumber.value = pageNumber;
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.nextPageButton.name);
        Utils.getForm().submit();
    }
}

SearchResults.pageNumberSelected = function(controlId, controlName, pageNumber)
{
    var inst = SearchResults.getClassInstance(controlId);
    
    if (inst != null && inst.selectedPageNumber != null)
    {
        inst.selectedPageNumber.value = pageNumber;
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.selectedPageNumber.name);
        Utils.getForm().submit();
    }
}

SearchResults.compareButtonClick = function(controlId, controlName)
{
    var inst = SearchResults.getClassInstance(controlId);
    
    if (inst != null && inst.compareButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.compareButton.name);
    }
}

SearchResults.shortlistButtonClick = function(controlId, controlName)
{
    var inst = SearchResults.getClassInstance(controlId);
    
    if (inst != null && inst.shortlistButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.shortlistButton.name);
    }
}





SearchResults.saveVehicleList=function(vehicleId,dealerId)
{
   // for example
        SearchResultsControl.UpdateShortlist(vehicleId, dealerId);
}




SearchResults.vehicleSelect = function(controlId, eventSource)
{
    var inst = SearchResults.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(eventSource.id == inst.selectedCompareVehicle.id)
    {
        if (checkedCount > 3)
        {
            var compareErrorMsg = document.getElementById("compareSelectError").value;
            alert(compareErrorMsg);
            eventSource.checked = false;
        }
        
        inst.compareButton.disabled = checkedCount < 2;    
    }
    else if(eventSource.id == inst.selectedShortlistVehicle.id)
    {
       
        var shortlistSessionValue = document.getElementById("shortlistSessionValueId").value;
        var totalCount = 0;
                
        if(shortlistSessionValue != "")
        {
            var sessionValueArray = shortlistSessionValue.split(',');
            totalCount = sessionValueArray.length;
            
            if(totalCount < 10)
            {
                for (var c = 0; c < vehicleCheckboxes.length; c++)
                {
                    var vehicleIdInShortlist = false;
                    
                    if(vehicleCheckboxes[c].checked)
                    {                
                        for(var p = 0; p < sessionValueArray.length; p++)
                        {
                            if(vehicleCheckboxes[c].value == sessionValueArray[p])
                            {
                                vehicleIdInShortlist = true;
                                break;
                            }
                        }
                        
                        if(!vehicleIdInShortlist)
                        {
                            totalCount++;
                        }  
                    }              
                }
            }
            else
            {
                totalCount++;
            }
        }
        else
        {
            totalCount = checkedCount;
        }
                
        if (totalCount > 10)
        {
            var shortlistErrorMsg = document.getElementById("shortlistSelectError").value;
            alert(shortlistErrorMsg);
            eventSource.checked = false;
        }
        
        inst.shortlistButton.disabled = totalCount < 1;
    }
        
}

SearchResults.showVehicleDetails = function(controlId, eventSource, params)
{
    var url = eventSource.getAttribute("friendlyName");
    url = url + params;
    
	var width = 745;
	var height = 500;
	
	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();
}

SearchResults.showVehiclePDF = function(controlId, eventSource, params)
{
    var url = eventSource.getAttribute("friendlyName");
    url = url + params;
    
	var width = 745;
	var height = 700;
	
	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=yes, status=no, width=' + width + ', height=' + height;

	var win = window.open(
	    url,
	    'popup',
	    winOpts);
	    
	win.focus();
}

function openLegacyEnquiry(pageurl, params, pagetitle, width, height, left, top)
{
    var pageurlcomplete = pageurl + params;
    var win = window.open(pageurlcomplete, pagetitle, 'left='+left+', top='+top+', width='+width+', height='+height+', scrollbars=yes, toolbar=no, resizable=no', true);
	win.focus();
}
