//
// Miscellaneous Functions
//

function resizeOnBoardingFrame(height) {
	document.getElementById("onBoardingFrame").setAttribute("height", height);
}

/**
 * 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;

}

/**
 * Searches through all stylesheets and returns the definition of the specified class name.
 */
function getStyleClass (className) {
	for (var i = 0; i < document.styleSheets.length; i++) {
		if(document.styleSheets[i].rules) {
			for (var j = 0; j < document.styleSheets[i].rules.length; j++) {
				if (document.styleSheets[i].rules[j].selectorText == '.' + className) {
					return document.styleSheets[i].rules[j];
				}
			}
		}
		else if(document.styleSheets[i].cssRules) {
			for (var j = 0; j < document.styleSheets[i].cssRules.length; j++) {
				if (document.styleSheets[i].cssRules[j].selectorText == '.' + className) {
					return document.styleSheets[i].cssRules[j];
				}
			}
		}
	}
	
	return null;
}

function setIgnoreSubmit(val){
	ignoreSubmit = val;
}

/**
 * This function can be overridden in the local jsp file's javascript and be made
 * to ignore the enter key or have the enter key click any submit button you want.
 * 
 * DO NOT change this function here.  Copy it to the jsp file you want it to have an impact
 * on and then modify it in that jsp file!!
 *
 * You must have onsubmit="onlyOneClick(event);" in your form tag  and 
 * onkeydown="keyDownHandler(event);" in your body tag for any of this to work.
 */
function keyDownHandler(e) 
{ 

	//Process the Enter key
	if (e.keyCode == 13) 
	{ 			
		//To stop the use of the enter key to do any type of submit
		//then just uncomment the following line
		//setIgnoreSubmit(true);
		
		//Or do something like the following to make it act like the enter
		//key clicked some specific submit button of your choosing.
		//var button = document.getElementById("someSubmitButton");
		//button.click();
		//setIgnoreSubmit(true); //So that it ignores the submit resulting from this Enter Key hit.
		//
    }
} 

/**
 * Used by the tile file found in other/js/truncatetablecolumns/columnTile.jsp
 */
function addDots(fldId, len)
{
	var idFld = document.getElementById("ID"+fldId);
	var origFld = document.getElementById("ORIG"+fldId);
	var val = origFld.value;
	if (val.length > len){
    	idFld.title = val;
    	idFld.value = val.substring(0,len-2) + "...";
	}
}

/**
 * Opens a second window where the user will be allow to enter email related information
 */
function printWishListPopUp(){

  var obj = window.open("/corporate/externalweb/shopping/printlist/printList.jsp?printWindow=true",
	 			'PrintList',
	 			'width=640,height=600,left=20,top=10,status=no,scrollbars=yes,resizable=no,toolbar=no,menubar=no,scrollbars=1,resizable=0,copyhistory=0'); 
  if (obj != null)
    obj.focus();
  else
    alert("Your browser appears to have pop-ups block. Please unblock this site so you can view this printer-friendly version.");

}

/**
 * Opens a second window where the user will be allow to enter email related information
 */
function printOrderPopUp(){

  var obj = window.open("/corporate/externalweb/checkout/printorder/printOrder.jsp?printWindow=true",
	 			'PrintOrder',
	 			'width=640,height=600,left=20,top=10,status=no,scrollbars=yes,resizable=no,toolbar=no,menubar=no,scrollbars=1,resizable=0,copyhistory=0'); 
  if (obj != null)
    obj.focus();
  else
    alert("Your browser appears to have pop-ups block. Please unblock this site so you can view this printer-friendly version.");

}

function printRmaPopUp(){

  var obj = window.open("/corporate/externalweb/customer/printRma.jsp?printWindow=true",
	 			'PrintReturn',
	 			'width=640,height=400,left=20,top=10,status=no,scrollbars=yes,resizable=no,toolbar=no,menubar=no,scrollbars=1,resizable=0,copyhistory=0'); 
  if (obj != null)
    obj.focus();
  else
    alert("Your browser appears to have pop-ups block. Please unblock this site so you can view this printer-friendly version.");

}

/**
 * Opens a second window where the user will be allow to enter email related information
 */
function emailPopUp(subId, hasAttachment){
  var obj = window.open("/corporate/EmailUs.do?subHeadingId="+subId+"&hasAttachment="+hasAttachment,
	 			'EmailUs',
	 			'width=620,height=550,left=40,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 site so you can send an Email.");

}

/**
 * Opens a second window where the user will be allowed to view images
 */
function productImagePopUp(){

  var obj = window.open("/corporate/externalweb/shopping/productdetail/productImageViewer.jsp",
	 			'ProductViewer',
	 			'width=600,height=600,left=25,top=0,status=no,scrollbars=yes,resizable=yes,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 site so you can view these images.");

}

function storeMapHoursPopUp(storeNum) {

	width = 400;
    height = 200;
	var obj = window.open('/corporate/ListView.do?storeMapHours=' + storeNum,
	 		'MapAndHours',
	 		'width=' + width 
	 		 + ',height=' + height
	 		 + ',left=30,top=30,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 blocked. Please unblock this site to see our Store Map and Hours.");
}


/**
 * Opens a second window where the user can be given information.
 */
function genericHelpPopUpScroll(htmlUrl, width, height){

  var obj = window.open(htmlUrl,
	 			"GenericHelp"+height,
	 			"width=" + width + 
	 			",height=" + height +
	 			",left=50,top=0,status=no,scrollbars=yes,resizable=no,toolbar=no,menubar=no,scrollbars=1,resizable=0,copyhistory=0"); 
  if (obj != null)
    obj.focus();
  else
    alert("Your browser appears to have pop-ups block. Please unblock this site so you can view our Help window.");

}


/**
 * 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 site so you can view our Help window.");

}


/**
 * Displays or hides a specified table row
 */
function toggleField(name, flag) {
	if(flag == 1) {
		document.getElementById(name).style.visibility = "visible";
		if(navigator.appName.indexOf("Microsoft") > -1)
			document.getElementById(name).style.display = "block";
		else
			document.getElementById(name).style.display = "table-row";
	}
	else {
		document.getElementById(name).style.visibility = "hidden";
		document.getElementById(name).style.display = "none";
	}
}

/**
 * Close the browser window
 */
function closeWindow()
{
	window.opener=top;
	window.close();
}

function ew3CreateXMLHttp() {

    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"
      ];

      for (var i = 0; i < aVersions.length; i++) {
        try {
            var oXmlHttp = new ActiveXObject(aVersions[i]);
            return oXmlHttp;
        } catch (oError) {
            //Do nothing
        }
      }
    }
    return null;
}

/**
 * Causes a pop-up to appear if the page has not been submitted or refreshed in the
 * number of minutes passed into this method.  The pop-up will display the passed
 * in message.
 */
function setSessionTimeoutAlert(minutes, message){

	setTimeout('alert("'+message+'")', 1000 * 60 * minutes); 

}

/**
 * Used to stop any java script work that might be going on in the background.
 */
function unloading(){
	
	if (backgroundRequest != null){
	    try {
		    backgroundRequest.abort();
	    } catch (oError){
	        //Do Nothing
	    }
	}
}

function rejectPublishedPage(status, id, comment) {
	window.location = "/corporate/StatusChanger.do?status=" + status + "&pageId=" + id + "&rejectComment=" + comment;
}

/**
 * Turn on the background image
 */
function inNavSpan(span, className)
{
	span.parentNode.className = className;
}

/**
 * Turn off the background image
 */
function outNavSpan(span, className)
{
	span.parentNode.className = className;
}

/**
 * Turn on the background image
 */
function inNavSubSpan(tbCell, className)
{
	tbCell.parentNode.className = className;
}

/**
 * Turn off the background image
 */
function outNavSubSpan(tbCell, className)
{
	tbCell.parentNode.className = className;
}

/**
 * Go to the given href
 */
function hrefTo(href)
{
	window.location = href;
}
 