// commonJS.js -- combo of chwJS.js, hwJS.js and phwJS.js (which were identical!!)
// plus the two menu functions below

// these top two f are the ones used for the side menu
// the third one needed includes speedscript and so
// remains on the pages

function getCombo(what){
// usage send in formname.selectlistName
//this works for single or multiple options

  for( var i = 0; i < what.options.length; i++ )
  { 

    if ( what.options[i] != null &&  what.options[i].selected == true  )

    {
        if (!comboVal){
        	var comboVal = what.options[i].value;
            } else {
        comboVal += "," + what.options[i].value;
        }
    }

  }
  return comboVal;
}

function setCombo(what,cVal){
// usage send in formname.selectlistName and value
// currently works only for a single value
  if(what.options){
      for( var i = 0; i < what.options.length; i++ )
      {

        if ( what.options[i] != null &&  what.options[i].value == cVal  ){

    	    what.options[i].selected = true;
        }

  } // var i
  }
}

function getRadio(what){
// usage -- send in formname.radiosetname
  j=what.length; 
	for (i=0; i<j; i++){
		if(what[i].checked) var radioVal = what[i].value;
	}
    return radioVal;
}

function getObject(objectId) {
	if (document.getElementById) {
		return document.getElementById(objectId);
	}
	else if (document.all) {
		return document.all[objectId];
	}
}

var objSelected = '';
function displaySubcat(objectId, objectIdLink) {
	var objDivToBeDisplayed = getObject(objectId);
	var objClassToBeChanged = getObject(objectIdLink);
	if (hideAllSubcats()) {
		if (objectIdLink == objSelected) {
			//objDivToBeDisplayed.style.display = 'none';
			//objClassToBeChanged.className = 'pageContentCatalogCatLink';
			objSelected = '';
		}
		else {
			objDivToBeDisplayed.style.display = 'block';
			objClassToBeChanged.className = 'pageContentCatalogCatLinkSelected';
			objSelected = objectIdLink;
		}
	}
}

// GPW NES proj.  function moved out of indiv pages
function validateExtension() {
    var temp = document.getElementById("FILE1").value;
//    var temp2 = temp.split('.');
//  var ext = temp2[1];
      var ext = temp.slice(-4); // path may include periods too
    ext = ext.toLowerCase();    
//   alert(ext);

    if(ext=='.gif' || ext=='jpeg' || ext=='.jpg' || ext=='.png'){
        passScreenFields(); 
        document.getElementById("buttonUploadFile").disabled = true; 
        document.getElementById("buttonUploadFile").className = 'pageContentFileUploadButtonDisabled';
        document.uploadFile.submit();
    } else {   

      alert('Please select a file of type gif, jpg, jpeg, or png.');
      return false; 
    }
}

function trHighlightingIsActive() {
   return true;
   //return false;
}

function trOnMouseOver(element) {
   if (trHighlightingIsActive()) {
      var oTR = getElement(element, 'TR');
      if (oTR != null && !eval(oTR.active)) {
         //oTR.style.backgroundColor = '#C9A9AF';
         //oTR.style.backgroundColor = '#CC99CC';
         oTR.style.backgroundColor = '#D5DAE7';
		 //oTR.style.color = '#990033';
      }
   }
}

function trOnMouseOut(element) {
   if (trHighlightingIsActive()) {
      var oTR = getElement(element, 'TR');
      if (oTR != null && !eval(oTR.active)) {
         oTR.style.backgroundColor = '';
		 oTR.style.color = '';
      }
   }
}

function getElement(src, tagName) {
   var obj = src;
   while (obj != null && obj.tagName != tagName) {
      obj = obj.parentElement;
   }
   return obj;
}


// only used in hw... programs so used that version
var screenFields = '';
function setUploadScreenField(fieldName, fieldValue, fieldSetDelimiter, fieldValueDelimiter) {
	//parse out any delimiters that might in field data, replace with a nothing
	var listDelimiters = new RegExp(fieldSetDelimiter + '|' + fieldValueDelimiter, 'g');
	fieldValue = fieldValue.replace(listDelimiters, '');
	
	//build up screenFields var to contain all fields to be passed to fileUpload
	screenFields += (screenFields != '' ? fieldSetDelimiter : '') + fieldName + fieldValueDelimiter + fieldValue;
}

var popupWindow;
var popupTypeRunning = '';
var popupMessageDefault = 'Loading popup window, please wait...';
var popupWidth = '600';
var popupHeight = '480';
var popUpWin=0;

centerPopupWindow();

function popUpWindow(URLStr, left, top, width, height, scrolls, resizes)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scrolls+',resizable='+resizes+',copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function setPopupWindowSize(popupWindowWidth,popupWindowHeight) {
	popupWidth = popupWindowWidth;
	popupHeight = popupWindowHeight;
	centerPopupWindow();
}

function centerPopupWindow() {
	lookupLeft = (popupWidth > screen.width ? 0 : (screen.width - popupWidth) / 2);
	lookupTop = (popupHeight > screen.height ? 0 : (screen.height - popupHeight) / 2);
}

function viewPopupWindow(popupType,popupUrl,popupMessage,popupForceClose) {
	if (!popupWindow || popupWindow.closed) {
		popupWindow = open('','popupWindow','width=' + popupWidth + ',height=' + popupHeight + ',directories=no,toolbar=no,menubar=no,scrollbars=yes,location=no,resizable=yes,left=' + lookupLeft + ',top=' + lookupTop);
		popupTypeRunning = '';
	}
	if (popupType != popupTypeRunning) {
		if (popupForceClose == true && popupTypeRunning != '') {
			closePopupWindow();
			viewPopupWindow(popupType,popupUrl,popupMessage,false);
			return;
		}
	
		popupTypeRunning = popupType;
		popupLoading(!popupMessage || isBlank(popupMessage) ? popupMessageDefault : popupMessage);
		popupWindow.location.href = popupUrl + '&noCache=' + noCache();
	}
	popupWindow.focus();
}

function closePopupWindow() {
	if (popupWindow && !popupWindow.closed) {
		popupWindow.close();
	}
}

function popupLoading(popupMessage) {
	popupWindow.document.close();
	popupWindow.document.open();
	popupWindow.document.writeln('<html><head><title>' + popupMessage + '</title></head><body bgcolor="#FFFFFF">');
	popupWindow.document.writeln('<div align="center" style="margin:20px; padding:16px; font-family:verdana; font-size:16pt; color:#0000FF;">' + popupMessage + '</div>');
	popupWindow.document.writeln('</body></html>');
	popupWindow.document.close();
}

function noCache() {
	var today = new Date;
	var noCache = today.getMonth() + "" + today.getDay() + "" + today.getFullYear() + "" + today.getTime();
	return escape(noCache);
}

var whiteSpace = /^\s+$/
function isBlank(fieldValue) {
   return ((fieldValue == null) || (fieldValue.length == 0) || whiteSpace.test(fieldValue))
}

/* my menu additions **************************/
function getSplit(cVar,no,cChar){
 var cTmp = cVar.split(cChar);
 return cTmp[no];
}

function brandUp(no){
    menuObj = document.getElementById("subMenu1");
    menuObj.innerHTML = brandSection[no-1];
}
function brandDown(no){
    menuObj = document.getElementById("subMenu1");
    menuObj.innerHTML = brandSection[no+1];
}
function catUp(no){
    menuObj = document.getElementById("subMenu2");
    menuObj.innerHTML = catSection[no-1];
}
function catDown(no){
    menuObj = document.getElementById("subMenu2");
    menuObj.innerHTML = catSection[no+1];
}


/* following all for the brand and category menu drop downs (from script.js) plus my additions **************************/
/* Internet Explorer Version Detection */

// convert all characters to lowercase to simplify testing
var agt=navigator.userAgent.toLowerCase();

// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var pgId;

var currentMenu;
var menuTimer;
var clearTimer;

//IE Browser Sniffing
var is_ie      = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie5     = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5   = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie6     = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie7     = (is_ie && (is_major == 4) && (agt.indexOf("msie 7.")!=-1) );
var is_ie6up   = (is_ie && !is_ie5 && !is_ie5_5);
var ie_7up     = (is_ie && is_ie6up && !is_ie6);
var safari = navigator.appVersion.search(/Safari/);
/* Array to display Colors */
var colorCode = new Array("#FFF","#FF23FF","#FF0000","#00FFFF","#0000FF","#000000","#22FFFF","#FF0000","#00FFFF","#0000FF","#000000","#FFF212","#FF0000","#00FFFF","#0000FF");
var colorVal = new Array("Select","Ash","Azalea","Azure","Bermuda","Black","Blue Dusk","Brick","Cardinal","Carolina Blue","Cedar","Charcoal","Chestnut","Coral","Daisy");

var popUpWin=0;
var flag=false;

function popUpWindow(URLStr, left, top, width, height, scrolls, resizes)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scrolls+',resizable='+resizes+',copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function hideProcessingMsg()
{
  showProcMsg(1);
  document.getElementById("processingMsg").style.display = "none";
  // document.getElementById("processingMsg").style.visibility = "hidden";
}

function displayProcessingMsg()
{
  //prevent ugly display in heading
  if(pgId=="advSearch"){
    document.getElementById("searchByBrand").style.display = "none";
    document.getElementById("searchByCategory").style.display = "none";
  }
  showProcMsg(0);
  document.getElementById("processingMsg").style.display = "block";
  document.getElementById("processingMsg").style.visibility = "visible";
}


/* Function to show hide Details */
function showHideDtls(imgObj,obj){
	if(document.getElementById(obj).style.display == '' || document.getElementById(obj).style.display == 'block'){
		document.getElementById(obj).style.display = "none";
		imgObj.className = 'hideDtls';
	} else {
		document.getElementById(obj).style.display = "block";
		imgObj.className = 'showDtls';
	}
}

function showPanel(imgObj,obj){
	if(document.getElementById(obj).style.display == '' || document.getElementById(obj).style.display == 'block'){
		document.getElementById(obj).style.display = "none";
		imgObj.className = 'hidePanel';
	} else {
		document.getElementById(obj).style.display = "block";
		imgObj.className = 'showPanel';
	}
}

/* Old Menu Function */
function changeClass2(obj,st){
	objTab = document.getElementById(obj);
	if(st == 0) {
		obj.className = 'menuTxtOvr';
	} else {
		obj.className = 'menuTxt';
	}
}


/* Menu Function */
/* changed from script.js to strip number off id -- simplifies progress string creation
    I am sending in item id instead of upper level menu  -- latest, I don't need
    all that looping stuff!  */

function changeClass(obj,no){
    document.getElementById(obj).className = 'menuTxtOvr';
    /*
    obj=obj.replace(no,"");
    if (document.getElementById(obj) != undefined){
	    objTab = document.getElementById(obj);
	    rows = objTab.getElementsByTagName('td')
	    for(i = 0; i < rows.length-1; i++){
		    if(no == i+1){
			    document.getElementById(obj+eval(i+1)).className = 'menuTxtOvr';
		    }
	    }
    } */
}


/* changed from script.js to strip number off id -- simplifies progress string creation
    I am sending in item id instead of upper level menu  -- latest, I don't need
    all that looping stuff!  */

function deactAll(obj,no){
    document.getElementById(obj).className = 'menuTxt';
    /*
    obj=obj.replace(no,"");
	objTab = document.getElementById(obj);
	rows = objTab.getElementsByTagName('tr')

	for(i = 0; i < rows.length-1; i++){
        if(document.getElementById(obj+eval(i+1)) != undefined){
		document.getElementById(obj+eval(i+1)).className = 'menuTxt';
        }
	} */
}

/* Show Sub Menu - Firefox 1.0 Fix*/
function showSubMenu(no,st){
	if(st==0){
		document.getElementById('subMenu'+no).style.display = 'block';
		document.getElementById('subMenu'+no).style.visibility = 'visible'
	} else {
		document.getElementById('subMenu'+no).style.display = 'none';
		document.getElementById('subMenu'+no).style.visibility = 'hidden';
	}
}
/* new 12.27.06 */
function showHideCombo(no,st){       

	menuHt1 = 500; //251
	menuHt2 = 500; //372
	menuHt3 = 500; //315
	menuHt4 = 500; //355
	menuHt5 = 500;
	menuHt6 = 500; //215
	menuHt1sm1 = 800;
	menuHt1sm2 = 800;
 var doStateSwitch = false;
 var comboX = 0;
 var menuX  = 0;
 var comboWidth;
 var menuEnd;

    if(pgId == 'hotBuys' || pgId == 'closeouts' || pgId == 'Index'){
        comboWidth = 207; } else {comboWidth = 143;}
 
	objCombo = document.getElementsByTagName("select");
	if(objCombo[0]!= undefined){
		for(i=0;i<objCombo.length;i++){
			if(objCombo[i].style.visibility != "hidden" && document.getElementById('subMenu'+no).style.visibility == 'visible') {

				if(findPosY(objCombo[i]) < eval("menuHt" + no)){

       comboX = findPosX(objCombo[i]);
       menuX  = findPosX(document.getElementById('subMenu'+no));
       menuEnd   = menuX +347;
       comboEnd   = comboX + comboWidth;

       if(
           ( (comboX > menuX) && (comboX < menuEnd) ) ||
           ( (comboEnd > menuX) && (comboEnd < menuEnd) )  ||
           ( (comboX < menuX) && (comboEnd > menuEnd) )
         ) {doStateSwitch = true;}


/*  removed gpw 2.12.07   ***************
       
       switch (pgId)
						 {
						    case "hotBuys":
										{
             if ( (comboX > menuX && comboX < menuX + 270) ||   
                  (comboX < menuX && comboX + 270 > menuX) ||
                  (comboX + 270 > menuX && comboX + 270 < menuX + 147) 
                )   
             {
													   doStateSwitch = true;
             }
													break;
										}

										case "closeouts":
										{
             if ( (comboX > menuX && comboX < menuX + 270) ||   
                  (comboX < menuX && comboX + 270 > menuX) ||
                  (comboX + 270 > menuX && comboX + 270 < menuX + 147) 
                )   
             {
													   doStateSwitch = true;
             }
													break;
										}

          case "Index":
										{
             if ( (comboX > menuX && comboX < menuX + 297) ||
                  (comboX + 200 > menuX)
                )
             {
													   doStateSwitch = true;
             }
													break;
										}

										default :
										{}
       } // switch
*******************/
       
       if (doStateSwitch)
       {
    						if(st==0)
    							objCombo[i].style.visibility = "hidden";
    						else
    							objCombo[i].style.visibility = "visible";
       }
/*
							if( ((pgId == 'hotBuys' || pgId == 'closeouts' || pgId == 'Index') &&
					  ((findPosX(objCombo[i]) > findPosX(document.getElementById('subMenu'+no)) && findPosX(objCombo[i]) < eval(findPosX(document.getElementById('subMenu'+no))+297)) || (eval(findPosX(objCombo[i]) + 270)  >  findPosX(document.getElementById('subMenu'+no)) && eval(findPosX(objCombo[i]) + 270)  < eval(findPosX(document.getElementById('subMenu'+no))+147))) )  JZ 02/05/07 
                   || ((findPosX(objCombo[i]) > findPosX(document.getElementById('subMenu'+no)) && findPosX(objCombo[i]) < eval(findPosX(document.getElementById('subMenu'+no))+147)) || (eval(findPosX(objCombo[i]) + 157)  >  findPosX(document.getElementById('subMenu'+no)) && eval(findPosX(objCombo[i]) + 157)  < eval(findPosX(document.getElementById('subMenu'+no))+147))) ){
						if(st==0){
							objCombo[i].style.visibility = "hidden";
						} else {
							objCombo[i].style.visibility = "visible";
						}
					}
*/
				}
			}
		}
	}
}

function showHideComboOut(){
	objCombo = document.getElementsByTagName("select");
	if(objCombo[0]!= undefined){
		for(i=0;i<objCombo.length;i++){
			objCombo[i].style.visibility = "visible";
		}
	}
}
/* end  new 12.27.06 */

totMenus = 2;

// if you are changing menus, set delay, but since every move within a menu
// also fires this you have to let it go immediately when WITHIN a menu, or the selection
// boxes don't stay hidden; so.. you have to reset currentMenu ONLY if you
// are really out and only way to detect that is to put a delay on that
// and then clear the delay immediately in mouseover!!

function showMenu(obj,no,st){
   if (st == 0){
     if(currentMenu != no){
       menuTimer=setTimeout(function() {showMenu2(obj,no,st)},500);
     } else {showMenu2(obj,no,st);}
   } else {
     if (st == 1){
         clearTimeout(menuTimer);
         showMenu2(obj,no,st);
     }
   }
}

function showMenu2(obj,no,st){
	document.getElementById("nav").style.zIndex = 2;
	if(st==0){
        clearTimeout(clearTimer);
        currentMenu = no;

		document.getElementById('subMenu'+no).style.visibility = 'visible'
		document.getElementById('subMenu'+no).style.display = 'block';
		obj.className = 'menu'+no+'on';

		for(i=1; i<=totMenus;i++){
			if(i!=no){
				document.getElementById('subMenu'+i).style.visibility = 'hidden'
				document.getElementById('subMenu'+i).style.display = 'none';
			}
		}
		
		if(is_ie && !ie_7up){
			showHideCombo(no,st);

			if(typeof(rowManager)!='undefined'){
				var rowID = rowManager.getFirstRowID();
				//alpha and nes's website dont have this menu overlay/IE<=6-specific problem
				if (SITE_NAME_FLAG == 'broder' && rowID != null && (no == 1 || no == 2)){
					$('colors'+rowID).style.display = "none";
					spacer = document.createElement('img');
					spacer.src = '/images/spacer.gif';
					spacer.id = 'spacerIMG';
					spacer.height = 10;
					spacer.width = 161;
					$('quantities'+rowID).cells[1].appendChild(spacer);
				}
			}
		}
	} else {
		for(i=1; i<=totMenus;i++){
			document.getElementById('subMenu'+i).style.visibility = 'hidden'
			document.getElementById('subMenu'+i).style.display = 'none';
		}
		obj.className = 'menu'+no;
		if(is_ie && !ie_7up){

            clearTimer=setTimeout("currentMenu = 0",500);
			showHideComboOut();

			if(typeof(rowManager)!='undefined'){
				var rowID = rowManager.getFirstRowID();
				//alpha and nes's website dont have this menu overlay/IE<=6-specific problem
				if (SITE_NAME_FLAG == 'broder' && rowID != null && (no == 1 || no == 2)){
					var spacerParent = spacer.parentNode;
					spacerParent.removeChild(spacer);
					$('quantities'+rowID).cells[2].height = "10";
					$('colors'+rowID).style.display = "";
				}
			}
		}
	}
}

function showSubMenu(obj,no,st)
{
	if(st==0)
	{
		document.getElementById('subMenu'+no).style.visibility = 'visible';
		document.getElementById('subMenu'+no).style.display = 'block';
		showHideCombo(no,st);
	} 
	else 
	{
		document.getElementById('subMenu'+no).style.visibility = 'hidden';
		document.getElementById('subMenu'+no).style.display = 'none';
		showHideComboOut();
	}
}

function showObj(id)
{
		var obj = document.getElementById(id);
		obj.style.visibility = 'visible';
		obj.style.display = 'block';
}

function hideObj(id)
{
		var obj = document.getElementById(id);
		obj.style.visibility = 'hidden';
		obj.style.display = 'none';
}

var brandNo = -1;
/* Browse brand all dropdown */
function findPosXY(obj, objNo){
	divObj = document.getElementById("brwsBrndDD");
	tdObj = document.getElementById("brand"+objNo);
	if(brandNo!= -1){
		prevObj = document.getElementById("brand"+brandNo);
	}
	if(objNo != brandNo){
		var curleft = 0;
		var curtop = 0;
		objX = obj;
		objY = obj;
		if(objX.offsetParent){
			while(1) 
			{
			  curleft += objX.offsetLeft;
			  if(!objX.offsetParent)
				break;
			  objX = objX.offsetParent;
			}
		}
		else if(objX.x)
			curleft += obj.x;
	
		if(objY.offsetParent)
			while(1)
			{
			  curtop += objY.offsetTop;
			  if(!objY.offsetParent)
				break;
			  objY = objY.offsetParent;
			}
		else if(objY.y)
		curtop += objY.y;
		divObj.style.left = curleft - 2 + "px";
		divObj.style.top = curtop + 16 + "px";
		divObj.style.visibility = "visible";
		if(safari != -1) {
			//outpuntObj=document.getElementById("output");
			divObj.style.left = curleft + 10 + "px";
			divObj.style.top = curtop + 208 + "px";
			//outpuntObj.style.top=-100 +"px";
		}
		tdObj.className = "brwsBrndLogoNameBgUp";
		if(brandNo!= -1){
			prevObj.className = "brwsBrndLogoNameBg";
		}
		brandNo = objNo;
	} else {
		divObj.style.visibility = "hidden";
		if(brandNo!= -1){
			prevObj.className = "brwsBrndLogoNameBg";
		}
		brandNo = -1;
    }
}

	function setVisible(visi){
		var x = document.getElementById("shadow");
		var t = document.getElementsByName("text")[0];
		x.style.position = 'absolute';
		x.style.top =  (findPosY(t)+3)+"px";
		x.style.left = (findPosX(t)+2)+"px";
		x.style.visibility = visi;
	}

	function init(){
		outp = document.getElementById("output");
		window.setInterval("lookAt()", 100);
		setVisible("hidden");
		document.onkeydown = keygetter; //needed for Opera...
		document.onkeyup = keyHandler;
	}

	function findPosX(obj)
	{
		var curleft = 0;
		if (obj && obj.offsetParent)
		{ 
			while (obj.offsetParent){
				curleft += obj.offsetLeft;
				obj = obj.offsetParent;
			}
		}
		else if (obj && obj.x)
			curleft += obj.x;
		return curleft;
	}

	function findPosY(obj)
	{
		var curtop = 0;
		if (obj.offsetParent){
			curtop += obj.offsetHeight;
			while (obj.offsetParent){
				curtop += obj.offsetTop;
				obj = obj.offsetParent;
			}
		}
		else if (obj.y){
			curtop += obj.y;
			curtop += obj.height;
		}
		return curtop;
	}

