﻿function VehicleCompareControl()
{
}

VehicleCompareControl.prototype.backToSearchResultsButton = null;
VehicleCompareControl.prototype.shortListButton = null;
VehicleCompareControl.prototype.viewShortListLink = null;

VehicleCompareControl.getClassInstance = function(controlId)
{
   
    var control = document.getElementById(controlId);       
    if (control != null && control.classInstance == null)
    {
        var inst = new VehicleCompareControl();        
        inst.backToSearchResultsButton = document.getElementById(control.getAttribute("backToSearchResultsButtonControlId"));
        inst.shortListButton = document.getElementById(control.getAttribute("shortlistButtonControlId"));
        inst.viewShortListLink = document.getElementById(control.getAttribute("viewShortListLinkControlId"));
        inst.removeSelectedButton = document.getElementById(controlId);
        control.classInstance = inst;
    }
    
    return control == null ? null : control.classInstance;
}

VehicleCompareControl.backToSearchResultsButtonClick = function(controlId, controlName)
{
    var inst = VehicleCompareControl.getClassInstance(controlId);
    
    if (inst != null && inst.backToSearchResultsButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.backToSearchResultsButton.name);
        Utils.getForm().submit();
    }
}


VehicleCompareControl.removeSelectedButtonClick = function(controlId, controlName, simpleId)
{
    
    var inst = VehicleCompareControl.getClassInstance(controlId);
    
    if (inst != null && inst.removeSelectedButton != null)
    {
        
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.removeSelectedButton.name);
        Utils.getForm().submit();
    }    
}


VehicleCompareControl.shortListButtonClick = function(controlId, controlName)
{
    var inst = VehicleCompareControl.getClassInstance(controlId);
    
    if (inst != null && inst.shortListButton != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.shortListButton.name);
        Utils.getForm().submit();
    }
}

VehicleCompareControl.viewShortListLinkClick = function(controlId, controlName)
{
    var inst = VehicleCompareControl.getClassInstance(controlId);
    
    if (inst != null && inst.viewShortListLink != null)
    {
        Utils.createSubmitHidden(Utils.getForm(), controlName, inst.viewShortListLink.name);
        Utils.getForm().submit();
    }
}





VehicleCompareControl.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();
}


VehicleCompareControl.vehicleSelect = function(controlId, eventSource, currentShortlistCount)
{
    var shortlistErrorMsg = document.getElementById("shortlistSelectError").value;
            
    //If there are already 10 vehicles selected for the shortlist then do not allow 
    // anymore vehicles to be added to shortlist.
    if(currentShortlistCount == 10)
    {         
        eventSource.checked = false;
    }
    else
    {           
        var inst = VehicleCompareControl.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++;
            }
        }
        
        checkedCount = checkedCount + currentShortlistCount;
        
        if (checkedCount > 10)
        {        
            eventSource.checked = false;
        }
    }          
        
}

