function openStoreHours(storeNum) {
	width = 600;
    height = 650;
	var obj = window.open('/site/fi/storemap.oap?storenum=' + storeNum,
	 		'Hours',
	 		'width=' + width 
	 		 + ',height=' + height
	 		 + ',left=30,top=30,status=no,scrollbars=yes,resizable=no'
	 		 + ',toolbar=no,menubar=no,scrollbars=0,resizable=1,copyhistory=0'); 
	if (obj != null)
	  obj.focus();
	else
	  alert("Your browser appears to have pop-ups blocked. Please unblock this Web site to see hours.");
}

function openNewWindow(url, width, height, x, y) {
	if(url.startsWith('http:')) {
		window.open(url, "mywindow", "height=" + height + ",width=" + width + ",status=1,toolbar=1,resizable=1,menubar=1,scrollbars=1,location=1,directories=1");
		mywindow.moveTo(x,y);
	} else if(url.startsWith('/')) {
		window.location = url;
	} else if(url.startsWith('javascript')) {
		eval(url);
	}
}

/**
 * Identify the browser we are working with.
 */
var ie = false; 
if (navigator.appName.indexOf("Microsoft") != -1)
{
    ie = true;
}

/**
 * Only allow a user to click the submit or a link button
 * one time on a page.  Kill all other clicks.
 */
var ignoreSubmit = false;
var alreadyClicked = false;
function onlyOneClick(e)
{
	
	if (alreadyClicked || ignoreSubmit)
	{
		//Ignore submit can be set to true by a method in the local jsp page
		//so that the first submit after it is set to true is ignored.
		ignoreSubmit = false;
		
		//alert("onlyOneClick(). alreadyClicked is TRUE");
		if (!ie)
		{
			e.stopPropagation();
			e.preventDefault();
		}
		else
		{
			e.returnValue = false; 
			e.cancel = true; 
		}
		return false;
	}
	
	//alert("onlyOneClick(). Setting alreadyClicked to true");
	alreadyClicked = true;
	return true;

}

function grayOut(vis, pTopDiv, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 5000;
  var opacity = options.opacity || 85;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#3D381E';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementById(pTopDiv);
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';    
    toggleAllSelectBoxes("hidden");
    if (forcedType != TYPE_LOGIN)
    	toggleVehicleSelect("true");
  } else {
     dark.style.display='none';
     toggleAllSelectBoxes("visible");  
  }
}

//  Maybe replace the code right above(starting at line 86) with this to add fading for the "gray out" 
//  Because IE sucks, this will not work...more testing and try to put in for release 2 
//    
//    dark.appear ({ duration: 1.0, to: .85 });
//    toggleAllSelectBoxes("hidden");
//    if (forcedType != TYPE_LOGIN)
//    	toggleVehicleSelect("true");
//  } else {
//     dark.fade({ duration: 1.0 });
//     toggleAllSelectBoxes("visible");  
//  }
//}


//
// Sets the visibility of all select boxes that do NOT have a class of "neverHide" to the specified visibility.
//

function toggleAllSelectBoxes(newVisibility) {
	var selectElements = document.getElementsByTagName("select");
	for(var i = 0; i < selectElements.length; i++) {
		var selectElement = selectElements[i];
		if(selectElement.className.match("neverHide") == null) {
			selectElement.style.visibility = newVisibility;
		}
	}
}

function MM_jumpMenuGo(objId,targ,restore){ //v9.0
  var selObj = null;  with (document) { 
  if (getElementById) selObj = getElementById(objId);
  if (selObj) eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0; }
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function toggleVehicleSelect(forceHide){
	try {
		if($("yearSelect").style.display == "" || forceHide == "true"){
			$("yearSelect").style.display = "none";
			$("makeSelect").style.display = "none";
			$("modelSelect").style.display = "none";
			$("engineSelect").style.display = "none";
			$("orSelect").style.display = "none";
			$("savedVehicleSelect").style.display = "none";
			Element.addClassName($("leftNavHP"),'leftNavExtend');
			$("yourVehicle").style.display = "";
			Element.addClassName($("vehicleSelect"),'vehicleSelected');
		}
		else{
			$("yearSelect").style.display = "";
			$("makeSelect").style.display = "";
			$("modelSelect").style.display = "";
			$("engineSelect").style.display = "";
			$("orSelect").style.display = "";
			$("savedVehicleSelect").style.display = "";
			Element.removeClassName($("leftNavHP"), 'leftNavExtend');
			$("yourVehicle").style.display = "none";
			Element.removeClassName($("vehicleSelect"),'vehicleSelected');		
		}
	} catch(err) {
		// do nothing
	}
}

function forceLeftNavExtend(){
	Element.addClassName($("leftNavHP"),'leftNavExtend');
}

function forceYourVehicleHide(){
	$("yourVehicle").style.display = "none";
}

function hideStorePanel(){
	var storePanelElement = document.getElementById("SCnoName");
	if(storePanelElement != null) {
		storePanelElement.style.display = "none";
	}
}

function showStorePanel(){
	var storePanelElement = document.getElementById("SCnoName");
	if(storePanelElement != null) {
		storePanelElement.style.display = "";
	}
}

// This hides flash, displays grayed out overlay 
// displays Forced Input from User & Detects Screen Resolution*/

function forcedVehicleSelect(makeVisible){
	toggleForcedPanel("forcedVehicleSelect", makeVisible);
}

function forcedZipCode(makeVisible){
	toggleForcedPanel("forcedZipCode", makeVisible);
	$("forcedZipCodeText").focus();
}

function forcedVehicleAndZip(makeVisible){
	toggleForcedPanel("forcedVehicleAndZip", makeVisible);
}

function forcedVehicleQuestions(makeVisible){
	toggleForcedPanel("forcedVehicleQuestions", makeVisible);
}

function displayVehicleQuestions(makeVisible){
	toggleForcedPanel("vehicleQuestions", makeVisible);
}

function forcedResetPassword(makeVisible){
	toggleForcedPanel("forcedResetPassword", makeVisible);
	$("forcedResetPassword").focus();
}

function forcedLogin(makeVisible, showGuestLogin){
	toggleForcedPanel("forcedLogin", makeVisible);
	if(showGuestLogin) {
		toggleForcedPanel("forcedLoginGuestFieldset", makeVisible);
	} else {
		toggleForcedPanel("forcedLoginGuestFieldset", false);
		$("forcedLogin").className = 'forcedUserInput';
		$("forcedLoginContents").className = 'forcedContents';
	}
	$("forcedLoginEmail").focus();
}

function toggleForcedPanel(panelId, makeVisible) {
	var forcedElement = document.getElementById(panelId);
	if (makeVisible){
		forcedElement.style.display = "block";
		hideFlash();
	}
	else{
		forcedElement.style.display = "none";
		showFlash();
	}
}

function hideFlash() {
	var hideFlashElement = document.getElementById("flashModule");
	if(hideFlashElement != null) {
		hideFlashElement.style.visibility = "hidden";
	}
}

function showFlash() {
	var hideFlashElement = document.getElementById("flashModule");
	if(hideFlashElement != null) {
		hideFlashElement.style.visibility = "visible";
	}
}

//Detect Screen Res & set class for smaller screens
function detectScreen(){
	if (screen.width < 1024) {
		document.getElementById("forcedVehicleSelect").className="forcedUserInputWideLowRes";
		document.getElementById("forcedZipCode").className="forcedUserInputLowRes";
		document.getElementById("forcedVehicleQuestions").className="forcedUserInputLowRes";
		document.getElementById("forcedVehicleAndZip").className="forcedUserInputLowRes";
	}
}

function toggleActive(id) {
	if(!Element.hasClassName(id+'H', 'active')) {
	
		var activeHArray = $$('h2.active');
		var activeLArray = $$('div.tabCornerLactive');
		var activeRArray = $$('div.tabCornerRactive');
		var activeWrapper = $$('div.activeTab');
		
		var activeH = activeHArray.first();
		var activeL = activeLArray.first();
		var activeR = activeRArray.first();
		var activeW = activeWrapper.first();
		
		Element.removeClassName(id + 'Wrapper', 'displayNone');
		Element.addClassName(id + 'Wrapper', 'activeTab');
		Element.removeClassName(activeW.id, 'activeTab');
		Element.addClassName(activeW.id, 'displayNone');
		
		Element.removeClassName(activeH.id, 'active');
		Element.removeClassName(activeH.id, 'over');
		Element.addClassName(activeH.id, 'up');
		Element.removeClassName(activeL.id, 'tabCornerLactive');
		Element.removeClassName(activeL.id, 'tabCornerLover');
		Element.addClassName(activeL.id, 'tabCornerLup');
		Element.removeClassName(activeR.id, 'tabCornerRactive');
		Element.removeClassName(activeR.id, 'tabCornerRover');
		Element.addClassName(activeR.id, 'tabCornerRup');

		Element.removeClassName(id+'H', 'up');
		Element.addClassName(id+'H', 'active');
		Element.removeClassName(id+'L', 'tabCornerLup');
		Element.addClassName(id+'L', 'tabCornerLactive');
		Element.removeClassName(id+'R', 'tabCornerRup');
		Element.addClassName(id+'R', 'tabCornerRactive');
	}
}

function toggleHover(id) {
	if(!Element.hasClassName(id+'H', 'active')) {
		if(Element.hasClassName(id+'H', 'over')) {
			Element.removeClassName(id+'H', 'over');
			Element.addClassName(id+'H', 'up');
			Element.removeClassName(id+'L', 'tabCornerLover');
			Element.addClassName(id+'L', 'tabCornerLup');
			Element.removeClassName(id+'R', 'tabCornerRover');
			Element.addClassName(id+'R', 'tabCornerRup');
		}
		else {
			Element.removeClassName(id+'H', 'up');
			Element.addClassName(id+'H', 'over');
			Element.removeClassName(id+'L', 'tabCornerLup');
			Element.addClassName(id+'L', 'tabCornerLover');
			Element.removeClassName(id+'R', 'tabCornerRup');
			Element.addClassName(id+'R', 'tabCornerRover');
		}
	}
}

//makes labels unselectable
function disableSelection(element) {
    element.onselectstart = function() {
        return false;
    };
    element.unselectable = "on";
    element.style.MozUserSelect = "none";
    element.style.cursor = "default";
}



//disableSelection(document.getElementById("label"));

 
 // Delay for :hovers



function clearDelay() {
	clearTimeout();
	Element.removeClassName("block");
}

function hoverDelay() {
	var t=setTimeout("displayEM()", 1000);
}

function displayEM(){
	Element.addClassName("block");
}

/**
 * Opens a second window where the user can be given information.
 */
function genericHelpPopUp(htmlUrl, width, height){

  var obj = window.open(htmlUrl,
	 			"GenericHelp"+height,
	 			"width=" + width + 
	 			",height=" + height +
	 			",left=50,top=0,status=no,scrollbars=no,resizable=no,toolbar=no,menubar=no,scrollbars=0,resizable=0,copyhistory=0"); 
  if (obj != null)
    obj.focus();
  else
    alert("Your browser appears to have pop-ups block. Please unblock this Web site so you can view our Help window.");

}

/**
 * Opens a second window where the user can be given information.
 */
function genericHelpPopUpWithScroll(htmlUrl, width, height){

  var obj = window.open(htmlUrl,
	 			"GenericHelp"+height,
	 			"width=" + width + 
	 			",height=" + height +
	 			",left=50,top=50,status=no,toolbar=no,menubar=no,scrollbars=1,resizable=1,copyhistory=0"); 
  if (obj != null)
    obj.focus();
  else
    alert("Your browser appears to have pop-ups block. Please unblock this Web site so you can view our Help window.");

}

function storeMapAndHoursPopUp(storeNumber) {
	genericHelpPopUp('/site/fi/storemap.oap?storenum=' + storeNumber, 600, 700);
}

function helpDeskPopUp(subtype, urlKey) {
	var url = '/site/helpdesk.oap';
	if(subtype != null) {
		url += '?subtype=' + subtype;
	}
	if(urlKey != null) {
		if(subtype == null) {
			url += '?';
		} else {
			url += '&';
		}
		url += 'urlKey=' + urlKey; 
	}
	genericHelpPopUpWithScroll(url,'571','510');
}

function helpDeskPopUpCustom(subtype, urlKey, width, height) {
	var url = '/site/helpdesk.oap';
	if(subtype != null) {
		url += '?subtype=' + subtype;
	}
	if(urlKey != null) {
		if(subtype == null) {
			url += '?';
		} else {
			url += '&';
		}
		url += 'urlKey=' + urlKey; 
	}
	genericHelpPopUpWithScroll(url,'571','510');
}


function initializeMap(canvas, address) {
	var map = null;
	var geocoder = null;
	
		if (GBrowserIsCompatible()) {
			map = new GMap2(canvas);
			geocoder = new GClientGeocoder();
			geocoder.getLatLng(address,
				function(point) {
					if (!point) {
						alert(address + " not found");
					} else {
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
					}
				});
		}
}

function enterKeyWasPressed(e) {
	var characterCode;
	if (e && e.which) {
		e = e;
		characterCode = e.which;
	} else {
		e = event;
		characterCode = e.keyCode;
	}
	return characterCode == 13;
}

function checkMaximumLength(textField, charLimit) {
	if (textField.value.length > charLimit)
		textField.value = textField.value.substring(0, charLimit);
}

function determineCharactersLeft(textField, charLimit, textDivID) {
	var charNum = textField.value.length;
	var textDiv = document.getElementById(textDivID);
	var charLimitMinusOne = charLimit-1;
	
	if(charNum<(charLimit-1)) {
		textDiv.innerHTML = (charLimit - charNum) + " characters left.";
	}
	else if(charNum==(charLimit-1)) {
		textDiv.innerHTML = "1 character left.";
	}
	else {
		textDiv.innerHTML = "No characters left.";
	}
}

function preloadImages() {
	if (document.images)
    {
		var imgs = new Array(1);
		imgs[0] = "/site/global/images/brandingNavImages/siteLogo_orly.gif";
		imgs[1] = "/site/global/images/brandingNavImages/DDtopBG.gif";
        imgs[2] = "/site/global/images/brandingNavImages/DDcornerTL.gif";
        imgs[3] = "/site/global/images/brandingNavImages/DDcornerTR.gif";
        imgs[4] = "/site/global/images/brandingNavImages/DDcornerBL.gif";
        imgs[5] = "/site/global/images/brandingNavImages/DDcornerBR.gif";
        imgs[6] = "/site/global/images/brandingNavImages/BrandingTop-BG.jpg"; 
        imgs[7] = "/site/global/images/brandingNavImages/BrandingBtm-Bg.jpg";
        imgs[8] = "/site/global/images/brandingNavImages/menuSeperator.gif";
        imgs[9] = "/site/global/images/brandingNavImages/menuSeperator.gif";
        imgs[10] = "/images/brandingNavImages/LeftPageEdge.jpg";
        imgs[11] = "/images/brandingNavImages/navCategoriesRoundEnd.png";
        imgs[12] = "/images/brandingNavImages/navCategories-BG.gif";
        imgs[13] = "/images/brandingNavImages/navCategories-BG-hover.gif";
        imgs[14] = "/images/brandingNavImages/navSite-BG.gif";
        imgs[15] = "/images/brandingNavImages/navSite-LftEnd.png";
        imgs[16] = "/images/brandingNavImages/navSite-RtEnd.png";
        imgs[17] = "/images/mainContentImages/NarrowSearchHeader-BG.jpg";
        imgs[18] = "/images/mainContentImages/removeX.gif";
        imgs[19] = "/images/mainContentImages/FooterOutline.gif";
        imgs[20] = "/images/mainContentImages/EmailPromo.gif";
        imgs[21] = "/images/mainContentImages/SignUpBtn-up.gif";
        imgs[22] = "/images/mainContentImages/SignUpBtn-over.gif";
        imgs[23] = "/images/mainContentImages/PickupInStore.jpg";
        imgs[24] = "/images/SCbackgrounds/SC193Wtan.gif";
        imgs[25] = "/images/SCbackgrounds/SC193Wgreen.gif";
        imgs[26] = "/images/SCbackgrounds/SC193Wbrown.gif";
        imgs[27] = "/images/SCbackgrounds/SC272Wtan.gif";
        imgs[28] = "/images/SCbackgrounds/SC272Wgreen.gif";
        imgs[29] = "/images/SCbackgrounds/SC272Wbrown.gif";
        imgs[30] = "/images/SCbackgrounds/SC557Wtan.gif";
        imgs[31] = "/images/SCbackgrounds/SC557Wgreen.gif";
        imgs[32] = "/images/SCbackgrounds/SC557Wbrown.gif";
        imgs[33] = "/images/SCbackgrounds/SC762Wtan.gif";
        imgs[34] = "/images/SCbackgrounds/SC762Wgreen.gif";
        imgs[35] = "/images/SCbackgrounds/SC762Wbrown.gif";
        imgs[36] = "/images/mainContentImages/OReillyRecommendsHeader.gif";
        imgs[37] = "/images/mainContentImages/scHelpCornerTop.gif";
        imgs[38] = "/images/mainContentImages/scHelpCornerBtm.gif";        
        
        var imgObjs = new Array();

		for ( var i = 0; i < imgs.length; i++ ){
		    var temp = new Image();
		    temp.src = imgs[i];
		    imgObjs[imgObjs.length] = temp;
		}               
    }
}

function focusElement(id) {
	document.getElementById(id).focus();
}

function imageZoomPopUp(url) {
	var obj = window.open(url, "PrintList", "width=580,height=600,left=100,top=100,status=no,scrollbars=no,resizable=no,toolbar=no,menubar=no,scrollbars=0,resizable=0,copyhistory=0");
	if (obj != null) {
		obj.focus();
	} else {
		alert("Your browser appears to have pop-ups block. Please unblock this Web site so you can view this printer-friendly version.");
	}
}
