﻿var ignoreRadioID;
var triggerRadioLastID;
var triggerRadioLastID2;
var accessoryNumericInputLoadingPanelID;
var accessoryNumericInputUpdatedControlID;

function carCompare_SwitchLock(leftRadioID, rightRadioID)
{
	var leftRadio = document.getElementById(leftRadioID);
	var rightRadio = document.getElementById(rightRadioID);
	
	var radioToClick;
	if (leftRadio.checked)
	{
		radioToClick = rightRadio;
	}
	else
	{
		radioToClick = leftRadio;
	}
	
	radioToClick.checked = true;
	eval(radioToClick.attributes['onclick'].value);
}

// ROW OVER method
function SetRowOver(row)
{
	if (row.className != "selected")
		row.className = "hover";
}
// ROW OUT method
function SetRowNormal(row)
{
	if (row.className != "selected")
		row.className = "";
}
// ROW SELECTED method for radio buttons
function SetRowSelected(radio, mainObj)
{          
    ignoreRadioID = radio.id;
	ClearRowsClass(document.getElementById(mainObj));

	if (radio.checked == true)
	{
		var tr = radio.parentNode.parentNode;
		while (tr.nodeName != "TR" && tr != null)		
		    tr = tr.parentNode;		
		
		if (tr.nodeName == "TR")		
			tr.className = "selected";		
	}
}

// ROW SELECTED method for checkbox
function SetRowSelectedCheckBox(checkbox)
{              		
	var tr = checkbox.parentNode.parentNode;
	while (tr.nodeName != "TR" && tr != null)		
	    tr = tr.parentNode;		
			
	if (tr.nodeName == "TR")	
	{
	    if (checkbox.checked == true)	
		    tr.className = "selected";		
		else    
		    tr.className = "";		
    }	
}

// CLEAR ROWS CLASS method
function ClearRowsClass(obj)
{
	var objNodes = obj.childNodes;
	for (var i = 0; i < objNodes.length; i++)
	{
		var objNode = objNodes[i];
		if (objNode.nodeName == "TR")
		{
			objNode.className = "";
			ClearRowsClass(objNode);
		}	
		else if (objNode.nodeName == "INPUT" && objNode.type == 'radio' && objNode.id != ignoreRadioID)		
			objNode.checked = false;					
		else
		{
			ClearRowsClass(objNode);
		}
	}
}

function SelectRadio(radioID,mainObj)
{            
    UnselectAllRadios(mainObj);
    var radio = document.getElementById(radioID);         
    radio.checked = true;  
    SetRowSelected(radio, mainObj)      
}

function UnselectAllRadios(mainObj)
{    
    var container = document.getElementById(mainObj);
    var inputs = container.getElementsByTagName('input');
            
    for (var i = 0;i < inputs.length;i++)    
        inputs[i].checked = false;                        
}

function SetCheckboxState(elementID,parentID)
{
        
    var checkBox = document.getElementById(elementID);
    var parent = document.getElementById(parentID);
    checkBox.checked = !checkBox.checked;
    
    
    if (checkBox.checked == true)
        parent.className = "selected";
    else    
        parent.className = "";
}

function SetCheckboxParentClass(elementID,parentID)
{
        
    var checkBox = document.getElementById(elementID);
    var parent = document.getElementById(parentID);
            
    if (checkBox.checked == true)
        parent.className = "selected";
    else    
        parent.className = "";
}

function ToggleAreaVisibility(areaID,titleID,titleLess,titleMore)
{    
    var area = document.getElementById(areaID);
    var title = document.getElementById(titleID);

    area.style.display = (area.style.display == 'block' ? 'none' : 'block');
    title.innerText = (area.style.display == 'block' ? titleLess : titleMore);
    title.className = (area.style.display == 'block' ? 'bmclgHeaderInnerRightClose' : 'bmclgHeaderInnerRightOpen');
}

function SaveAreaVisibilityToHidden(areaID,hiddenID)
{
    document.getElementById(hiddenID).value = document.getElementById(areaID).style.display == 'block' ? 'true' : 'false';    
}

function InitiateAjaxRequestAsynch(ajaxManagerID,controlID)
{
	window.setTimeout(function() { InitiateAjaxRequest(ajaxManagerID, controlID) }, 1000);
}

function InitiateAjaxRequest(ajaxManagerID,controlID)
{
	ajaxManagerID.AjaxRequest(controlID);
}

function ShowConflictModalPopup(url, conflictLoadingID,
                    radAjaxManagerClientID, updatedControlClientID)
{
	var oWnd = window.radopen(url, null);
	if (oWnd == null) return;

	if (oWnd != null)
	{
		oWnd._iframe.allowTransparency = true;

		if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1)
		{
			SetShowWrapperChangesForIE6(oWnd);
		}

		oWnd.OnClientClose = "OnConflictClientClose('" + conflictLoadingID + "'," + radAjaxManagerClientID + ",'" + updatedControlClientID + "');";
	}
}


function ShowModalPopupWithClientClose(url,radAjaxManagerClientID,updatedControlClientID)
{
	var oWnd = window.radopen(url, null);
	if (oWnd == null) return;

    oWnd.OnClientClose = "OnClose(" + radAjaxManagerClientID + ",'" + updatedControlClientID + "');";
    
    if (oWnd != null)
    {
    	oWnd._iframe.allowTransparency = true;

    	if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1)
    	{
    		SetShowWrapperChangesForIE6(oWnd);
    	}
    }
}

function ShowModalPopupNew(url)
{
	var oWnd = window.radopen(url, null);
	if (oWnd == null) return;

	if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1 && oWnd == null) window.setTimeout(function() { ShowModalPopupNew(url) }, 1000);

	if (oWnd != null)
	{
		oWnd._iframe.allowTransparency = true;

		if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1)
		{
			SetShowWrapperChangesForIE6(oWnd);
		}
	}
}

function ShowModalPopup(windowID)
{
	var oWnd = window.radopen(null, windowID);
	if (oWnd == null) return;

	if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1 && oWnd == null)
	{
		window.setTimeout(function() { ShowModalPopup(windowID) }, 1000);
	}

	if (oWnd != null && oWnd._iframe != null && oWnd.WrapperElement != null)
	{
		oWnd._iframe.allowTransparency = true;

		if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1)
		{
			SetShowWrapperChangesForIE6(oWnd);
		}
	}
}

function GetRadWindowDelayed(windowID)
{
	var oWnd = window.radopen(null, windowID);
	return oWnd;
}

function ShowModalPopupWithUrl(windowID, url)
{
	window.radopen(url, windowID);
	var oWnd = GetWindow(windowID);
	if (oWnd == null) return;

	if (oWnd != null)
	{
		oWnd._iframe.allowTransparency = true;

		if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1)
		{
			SetShowWrapperChangesForIE6(oWnd);
		}
	}
}

function HideModalPopup(windowID)
{
	var oWnd = GetWindow(windowID);
	if (oWnd == null) return;

	if (oWnd != null)
	{
		if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1)
		{
			SetHideWrapperChangesForIE6(oWnd);
		}

		oWnd.Close();
	}
} 

function SetShowWrapperChangesForIE6(oWnd)
{
	if (oWnd != null)
	{
		if (oWnd._iframe != null)
		{
			oWnd._iframe.width = "100%";
			oWnd._iframe.height = "100%";
			oWnd._iframe.style.top = "0px";
			oWnd._iframe.style.left = "0px";
		}

		if (oWnd.WrapperElement != null)
		{
			oWnd.WrapperElement.style.width = "100%";
			oWnd.WrapperElement.style.height = "100%";
			oWnd.WrapperElement.style.top = "0px";
			oWnd.WrapperElement.style.left = "0px";
			oWnd.WrapperElement.style.position = "absolute";
			oWnd.WrapperElement.style.display = "block";
		}
	}
}

function SetHideWrapperChangesForIE6(oWnd)
{
	if (oWnd != null)
	{
		if (oWnd._iframe != null)
		{
			oWnd._iframe.width = "0px";
			oWnd._iframe.height = "0px";
			oWnd._iframe.style.top = "0px";
			oWnd._iframe.style.left = "0px";
			oWnd._iframe.display = "none";
		}

		if (oWnd.WrapperElement != null)
		{
			oWnd.WrapperElement.style.width = "0px";
			oWnd.WrapperElement.style.height = "0px";
			oWnd.WrapperElement.style.top = "0px";
			oWnd.WrapperElement.style.left = "0px";
			oWnd.WrapperElement.style.position = "static";
			oWnd.WrapperElement.style.display = "none";
		}
	}
}

function ShowPopupCarCompare(windowID,url)
{    
	var iframe = document.getElementById(windowID);
	
	iframe.src = url + '&' + GetCurrentDate();
	iframe.style.display = "block";
	return;
}

function HidePopupCarCompare(windowID)
{    
	var iframe = window.top.document.getElementById(windowID);
	iframe.style.display = "none";
}

function GetWindow(windowID)
{
    //bGetting RadWindow manager      
    var oManager = GetRadWindowManager();       
          
    //Success. Getting existing window DialogWindow using GetWindowByName      
    return oManager.GetWindowByName(windowID);
}

function GetWindowFromItself()       
{
	var oWindow = null;
    if (window.RadWindow)
    {
    	oWindow = window.RadWindow; //Will work in Moz in all cases, including clasic dialog       
    }
    else if (window.frameElement.radWindow)
    {
    	oWindow = window.frameElement.radWindow; //IE (and Moz as well)
    }

    if (oWindow != null)
    {
    	SetHideWrapperChangesForIE6(oWindow);
    }

    return oWindow;                 
}       

function CloseFromItself()       
{             
    GetWindowFromItself().Close();            
}   

function OnClose(ajaxManagerID,updatedControlID)
{                
    InitiateAjaxRequest(ajaxManagerID,updatedControlID);
}

function OnConflictClientClose(modalPopupID,ajaxManagerID,updatedControlID)
{
    ShowModalPopup(modalPopupID);
    InitiateAjaxRequest(ajaxManagerID, updatedControlID);
   } 

// check that at least one item is checked
function IsAtLeastOneItemCheck(itemIds)
{
    for (var i = 0;i < itemIds.length; i++)
    {
        var item = document.getElementById(itemIds[i]);
        if (item.checked)
            return true;    
    }
    
    return false;
}

function DisableItems(id,itemIds)
{
    var isChecked = document.getElementById(id).checked;

    for (var i = 0;i < itemIds.length; i++)
    {
        var item = document.getElementById(itemIds[i]);
        if (item != null)
            item.disabled = isChecked;
    }
}

function SetStateAllOpositeItems(containerID,oppositeContainerId,itemIDs,oppositeItemIDs,buttonID,buttonOppositeID)
{   
    var currentContainer = document.getElementById(containerID); 
    var opositeContainer = document.getElementById(oppositeContainerId);
    var button = document.getElementById(buttonID);
    var oppositeButton = document.getElementById(buttonOppositeID);
    var isAtLeastItemCheckedInCurrentContainer = IsAtLeastOneItemCheck(itemIDs);
    var isAtLeastItemCheckedInOppositeContainer = IsAtLeastOneItemCheck(oppositeItemIDs);

    currentContainer.disabled = !isAtLeastItemCheckedInCurrentContainer && isAtLeastItemCheckedInOppositeContainer;            
    opositeContainer.disabled = !isAtLeastItemCheckedInOppositeContainer && isAtLeastItemCheckedInCurrentContainer;            
    button.disabled = !isAtLeastItemCheckedInCurrentContainer;
    oppositeButton.disabled = !isAtLeastItemCheckedInOppositeContainer;
}

function ShowLoadingPanel(panelID,updatedControlID)
{
    var panel = RadAjaxNamespace.LoadingPanels[panelID];
    if (panel != null)
        panel.Show(updatedControlID);
}

function HideLoadingPanel(panelID,updatedControlID)
{    
    var panel = RadAjaxNamespace.LoadingPanels[panelID];
    if (panel != null)
        panel.Hide(updatedControlID);
}

function ShowLoadingPanelCenterWhenChecked(checkBoxID,panelID,updatedControlID)
{
    var checkBox = document.getElementById(checkBoxID);
    if (checkBox != null && checkBox.checked)
    {
         var panel = RadAjaxNamespace.LoadingPanels[panelID];
         if (panel != null)             
            panel.Show(updatedControlID);                        
    }
}

function AccessoryNumericUpDownBlur(sender, eventArgs)
{        
    var textBoxValue = sender.GetValue();
    if (textBoxValue == null || textBoxValue == '')
        sender.SetValue('0');            
}

function AccessoryNumericValueChanged(sender, eventArgs)
{            
    if ((eventArgs.NewValue == 1 && eventArgs.OldValue == 0) ||
       (eventArgs.NewValue == 0 && eventArgs.OldValue == 1))
       {
            ShowLoadingPanel(accessoryNumericInputLoadingPanelID,accessoryNumericInputUpdatedControlID);
       }
}

function InitAccessoryNumericLoadingPanel(loadingPanelID,updatedControlID)
{    
    accessoryNumericInputLoadingPanelID = loadingPanelID;
    accessoryNumericInputUpdatedControlID = updatedControlID;
}


function SetVisibilityStyleForClientElement(id,visibility)
{    
    var item = document.getElementById(id);
    if (item != null)    
      item.style.display = visibility;
}

function ShowElement(id)
{
    var item = document.getElementById(id);
    if (item != null)    
      item.style.display = 'block';
}

function HideElement(id)
{
    var item = document.getElementById(id);
    if (item != null)    
      item.style.display = 'none';
}

function GetCurrentDate()
{
    var date = new Date();
    return date.getDay().toString() + date.getMonth().toString() + date.getFullYear().toString() + date.getHours().toString() + date.getMinutes().toString() + date.getSeconds().toString() + date.getMilliseconds().toString();
    
}

function ConfirmGarageCarRemove(message)
{
    if (confirm(message))
        return true;
        
    return false;    
}

function ReloadConflictsWithClearSelection(url)
{
    window.top.location.href = url;
}

function FocusItem(id)
{    
    var object = document.getElementById(id);
    if (object != null)    
        object.focus();            
}

function SetValueToInput(inputId,inputValue)
{
    var input = document.getElementById(inputId);
    if (input != null)
        input.value = inputValue;
}

function createCookie(name, value, days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++)
	{
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name, "", -1);
}

function FlipStandardEquipmentVisibility(equipmentListContainerID,imgTriggerID,
    imgTriggerOpenImagePath,imgTriggerCloseImagePath)
{

    var equipmentListContainer = document.getElementById(equipmentListContainerID);
    var imgTrigger = document.getElementById(imgTriggerID);
    
    // flip visibility
    equipmentListContainer.style.display = equipmentListContainer.style.display == "block" ? "none" : "block";
    imgTrigger.src = equipmentListContainer.style.display == "block" ? imgTriggerCloseImagePath : imgTriggerOpenImagePath;        
}

function FlipPricesVisibility(pricesTableRowID, imgTriggerID, imgTriggerOpenImagePath, imgTriggerCloseImagePath)
{
	var pricesTableRow = document.getElementById(pricesTableRowID);
	var imgTrigger = document.getElementById(imgTriggerID);

	// flip visibility
	if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1 ||
		navigator.userAgent.toUpperCase().indexOf("MSIE 7.") != -1)
	{
		pricesTableRow.style.display = pricesTableRow.style.display == "block" ? "none" : "block";
		imgTrigger.src = pricesTableRow.style.display == "block" ? imgTriggerCloseImagePath : imgTriggerOpenImagePath;
	}
	else
	{
		pricesTableRow.style.display = pricesTableRow.style.display == "table-row" ? "none" : "table-row";
		imgTrigger.src = pricesTableRow.style.display == "table-row" ? imgTriggerCloseImagePath : imgTriggerOpenImagePath;
	}
}

function IsTriggerRadioButton(ID)
{                
    if (ID != triggerRadioLastID)
    {
        triggerRadioLastID = ID;
        return true;
    }
            
    return false;                    
}

function IsTriggerRadioButton2(ID)
{            
    if (ID != triggerRadioLastID2)
    {
        triggerRadioLastID2 = ID;
        return true;
    }
            
    return false;                    
}

function ShowInteriorCollissionDIV(containerID,allInteriorsContainerID)
{    
    ProcessInteriorCollissionDIVs('none',allInteriorsContainerID);
    ProcessInteriorCollissionDIVs('inline',containerID);
}

function ProcessInteriorCollissionDIVs(displayStyle,parentContainerID)
{
    var container = document.getElementById(parentContainerID);
    if (container != null)
    {
        var collisionDIVs = container.getElementsByTagName('span');
        for (var i = 0;i < collisionDIVs.length;i++)
        {
          if (collisionDIVs[i].attributes['isCollissionDiv'] != null)
              collisionDIVs[i].style.display = displayStyle;  
        }    
    }
   }

   function ShowPricesRow(pricesRowID, allInteriorsContainerID)
   {
   	ProcessPricesRows('none', allInteriorsContainerID);
   	var pricesRow = document.getElementById(pricesRowID);
   	if (pricesRow != null)
   	{
   		if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1 ||
   			navigator.userAgent.toUpperCase().indexOf("MSIE 7.") != -1)
   		{
   			pricesRow.style.display = "block";
   		}
   		else
   		{
   			pricesRow.style.display = "table-row";
   		}
   	}
   }

   function ProcessPricesRows(displayStyle, parentContainerID)
   {
   	var container = document.getElementById(parentContainerID);
   	if (container != null)
   	{
   		var pricesRows = container.getElementsByTagName('tr');
   		for (var i = 0; i < pricesRows.length; i++)
   		{
   			if (pricesRows[i].attributes['isPricesRow'] != null)
   				pricesRows[i].style.display = displayStyle;
   		}
   	}
   }

   function ShowPricesRowExtra(pricesRowID, status)
   {
   	var pricesRow = document.getElementById(pricesRowID);
   	if (pricesRow != null)
   	{
   		if (status == 'checked')
   		{
   			if (navigator.userAgent.toUpperCase().indexOf("MSIE 6.") != -1 ||
   			navigator.userAgent.toUpperCase().indexOf("MSIE 7.") != -1)
   			{
   				pricesRow.style.display = "block";
   			}
   			else
   			{
   				pricesRow.style.display = "table-row";
   			}
   		}
   		else
   		{
   			pricesRow.style.display = "none";
   		}
   	}
   }
