//util.js

var state = 'none'; 

function showhide(layer_ref) { 
if (state == 'block') { 
state = 'none'; 
} 
else { 
state = 'block'; 
} 
if (document.all) { //IS IE 4 or 5 (or 6 beta) 
eval( "document.all." + layer_ref + ".style.display = state"); 
} 
if (document.layers) { //IS NETSCAPE 4 or below 
document.layers[layer_ref].display = state; 
} 
if (document.getElementById &&!document.all) { 
hza = document.getElementById(layer_ref); 
hza.style.display = state; 
} 
}

var tmpTitle="";
function tooltip(layer, elm, content,e){
   
    var title = jQuery(elm).attr('title');
    if (title != "") {tmpTitle= title;jQuery(elm).attr('title',''); }            
    if(elm){               
	var ref = jQuery('#' + layer);
        var state= (jQuery(ref).css('display') == 'none') ? 'block' : 'none';        
        var pos = jQuery(elm).offset();  
	pos.top -= (jQuery(ref).height()/2.5) - 26;
        pos.left -= jQuery(ref).width() + 23;//+10
        
	if (content) 
	{
	   var ttop = ref.find('#tooltip_top');
           if(ttop.length == 0)
           {
                var tooltipContentClone = jQuery('#' +content).clone();
                ref.append("<div id='tooltip_top'></div><div id='tooltip_arrow'></div><div id='tooltip_middle'></div><div id='tooltip_bottom'></div>");
                jQuery('#' +content).remove();
                jQuery("#tooltip_middle").append(tooltipContentClone);
           }
           jQuery('#' +content).html(tmpTitle);   
	}
        else
           jQuery(elm).attr('title',tmpTitle);     
        	
        jQuery(ref).css( { "left": (pos.left ) + "px", "top":(pos.top) + "px","display":state} );                           
        return false;    
    }else{
        return true;
    }
}
function showhideValidate(layer_ref, elem) { 
    if(elem.className =='login'){
        if (state == 'block') { 
        state = 'none'; 
        } 
        else { 
        state = 'block'; 
        } 
        if (document.all) { //IS IE 4 or 5 (or 6 beta) 
        eval( "document.all." + layer_ref + ".style.display = state"); 
        } 
        if (document.layers) { //IS NETSCAPE 4 or below 
        document.layers[layer_ref].display = state; 
        } 
        if (document.getElementById &&!document.all) { 
        hza = document.getElementById(layer_ref); 
        hza.style.display = state; 
        } 
        
        return false;    
    }else{
        return true;
    }
}

function ImageNotFound(img)
{
    if (img)
    {
            img.onerror= null; //prevent recursion if new img not found
            img.src = "http://www.autonationdirect.com/buyonline/imgs/ImageUnavailable.jpg";
    }                                   
    return true
}

    function showHide(whichLayer){  
    var imgName = "img_" + whichLayer;
    whichLayer = whichLayer + "Data";
    var elem, elemImg, vis;  
    if( document.getElementById ){ // this is the way the standards work    
    elem = document.getElementById( whichLayer );  
    elemImg = document.getElementById( imgName );  
    }else if( document.all ){ // this is the way old msie versions work      
    elem = document.all[whichLayer];  
    elemImg = document.all[imgName];  
    }
    else if( document.layers ){ // this is the way nn4 works    
    elem = document.layers[whichLayer];  
    elemImg = document.all[imgName];
    }
    vis = elem.style;  // if the style.display value is blank we try to figure it out here  
    if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)    
        vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';  
    
    //vis.display = (vis.display==''||vis.display=='block')?'none':'block';
    if(vis.display==''||vis.display=='block'){
        vis.display = 'none';
        elemImg.src='../images/img_plus.gif';
    }else{
        vis.display = 'block';
        elemImg.src='../images/img_minus.gif';
    }
    }
    
    function SetVehicleType(rbtVehicleType,vehId)
    {       
        var ctrlId = 'VehicleType';                
        if(vehId)
            ctrlId = vehId
        
        var cdeType = $find(vehId);
        if (cdeType)
        {
            cdeType.set_SelectedValue(rbtVehicleType.value);
            cdeType._onParentChange(null, true);      
        }
    }
    
    // Begin - AcevedoJ Consolidate Common JS into one.
       
    function ShowQuotationMessagePopup()
	{
	    var mpeQuotation = $find('Quotation');
	    if(mpeQuotation != null)
	    {
	        ShowDiv('divQuotation');
	        mpeQuotation.show();
	    }
	}
	
	function ShowConfirmPopup()
    {    
        //this shows a popup dlg
        var dlg = $find('Confirm');
        if(dlg != null)
        {
            ShowDiv('divConfirm');
            dlg.show();
        }
                
    }
    
    function ShowDiv(divID)
    {
        if(document.getElementById(divID) != null && document.getElementById(divID).style.display == 'none')
        {
            document.getElementById(divID).style.display = '';
        }
    }
    
    function CompareCheckedChangedOvLoad(imgCheckbox, btn_comp_ckd, btn_comp_unckd, firstCHNode, secondCHNode)
	{
	    if(Prototype.Browser.IE)
        {
	        if(imgCheckbox.src.indexOf(btn_comp_unckd) > -1)
	        {
	            if (selectedCount < 4)
	            {						        
	                imgCheckbox.src = imgCheckbox.src.replace(btn_comp_unckd, btn_comp_ckd);
	                imgCheckbox.parentNode.lastChild.firstChild.value = "1";
  	                selectedCount++;
	            }
	            else
	            {
	                alert('We can only compare up to four vehicles. Sorry for the inconvienence caused.');
	            }						            
	        }
	        else if(imgCheckbox.src.indexOf(btn_comp_ckd) > -1)
	        {
	            imgCheckbox.src = imgCheckbox.src.replace(btn_comp_ckd, btn_comp_unckd);
	            imgCheckbox.parentNode.lastChild.firstChild.value = "0";
	            selectedCount--;
            }
        }
        else
        {
            
            if(imgCheckbox.src.indexOf(btn_comp_unckd) > -1)
	        {
	            
	            if (selectedCount < 4)
	            {						        
	                imgCheckbox.src = imgCheckbox.src.replace(btn_comp_unckd, btn_comp_ckd);
	                imgCheckbox.parentNode.childNodes[firstCHNode].childNodes[secondCHNode].value = "1";
 	                selectedCount++;
  	                //alert(selectedCount)
	            }
	            else
	            {
	                alert('We can only compare up to four vehicles. Sorry for the inconvienence caused.');
	            }						            
	                
	        }
	        else if(imgCheckbox.src.indexOf(btn_comp_ckd) > -1)
	        {
	            imgCheckbox.src = imgCheckbox.src.replace(btn_comp_ckd, btn_comp_unckd);
	            imgCheckbox.parentNode.childNodes[firstCHNode].childNodes[secondCHNode].value = "0";
                selectedCount--;   						            
            }
        }
	}
    
    function TrimExpandMouseOver(imgExpand)
	{
	    if(imgExpand.src.indexOf("btn_plus.png") > -1)
	    {
	        imgExpand.src = imgExpand.src.replace("btn_plus.png", "btn_plus_orange.png");
	    }
	    else if(imgExpand.src.indexOf("btn_minus.png") > -1)
	    {
	        imgExpand.src = imgExpand.src.replace("btn_minus.png", "btn_minus_orange.png");
	    }
	}
	
	function TrimExpandMouseOut(imgExpand)
	{
	    if(imgExpand.src.indexOf("btn_plus_orange.png") > -1)
	    {
	        imgExpand.src = imgExpand.src.replace("btn_plus_orange.png", "btn_plus.png");
	    }
	    else if(imgExpand.src.indexOf("btn_minus_orange.png") > -1)
	    {
	        imgExpand.src = imgExpand.src.replace("btn_minus_orange.png", "btn_minus.png");
	    }
	}
	
	function OpenWindow(sUrl, title) 
    {    
       var xLeft = 150; //Will keep x position from Left.    
       var  yTop = 0;   //This will put your new window on           
                            //the Top, as Y co-ordinate given 
                            //'0'.                
       //var sUrl = "PrintDetails.aspx"; //New Window Name. 
       var sFeatures = "resizable=1,status=1,toolbar=1,location=1,menubar=1,scrollbars=1,left=" + xLeft + ",top=" + yTop; //This will give all these features to your 
                  //new opened window.             
       win = window.open(sUrl,title, sFeatures); 
       win.focus(); // This will give focus to your newly opened window. 
       
    }  
    
    function ShowUnhappyPathPopup()
	{
	    var mpeUnhappyPath = $find('UnhappyPath');
	    if(mpeUnhappyPath != null)
	    {
	        ShowDiv('divUnhappyPath');
	        mpeUnhappyPath.show();
	    }
	}
        
    function CancelSave()
	{
	    return false;
	}
	
	function DisableCompare()
	{		
	    if(document.getElementById("divCompareBot") != null)
	    {	    
	        document.getElementById("divCompareBot").style.display = 'none';
	    }
	    if(document.getElementById("divComparebotDisabled") != null)
	    {
	        document.getElementById("divComparebotDisabled").style.display = '';				
	    }
	}
	
	function ShowCompareBot()
	{
	    if(document.getElementById("divCompareBot") != null)
	    {
	        document.getElementById("divCompareBot").style.display = '';
	    }
	    if(document.getElementById("divComparebotDisabled") != null)
	    {
	        document.getElementById("divComparebotDisabled").style.display = 'none';
	    }
	}
	
	function TabHeaderClicked(imgTab)
	{
	    if(imgTab.src.indexOf("_active") > -1)
	    {
	        imgTab.src = imgTab.src.replace("_active","");
	    }
	    else
	    {
	        imgTab.src = imgTab.src.replace(".gif", "_active.gif");
        }
        
	}			
	function ActiveTabChanged(){if(typeof(UpdateImageDisclaimer)!='undefined')
	    {UpdateImageDisclaimer();}}
    
    function AutoTab(from,nsp,toId,maxLength, e){                    
        var key = (e.which == null) ? e.keyCode:e.which;                        
        if ((key >= 48 && key <=57) || (key >= 96 && key <=105))
        {
            if (from && (from.value.length >= maxLength)){                                  
                toId = String.isNullOrEmpty(nsp) ? toId : $get(nsp).value + '_' + toId;
                var ctrl = $get(toId);
                if (ctrl) {ctrl.focus();ctrl.select();}
            }
        }
    }

    function DownloadImage(src)
    {
       var img = new Image();    
       if (img && src && src != "")
       img.src= src;       
    }
    
    String.isNullOrEmpty = function(str) {return (str == null || str == "");}
    
   jQuery(document).ready(function(){
	DownloadImage("../Imgs/Arrow.png");
	DownloadImage("../Imgs/Middle.png");
	DownloadImage("../Imgs/Head.png");
	DownloadImage("../Imgs/Foot.png");
   });
    
