var vRoot = "/live/";
var vCgiBin = "/cgi-bin/livew";
var hostUrl = "http://www.statononline.com";
var sHostUrl = "https://www.statononline.com";


/* put in site.w
var vCookie = "sta97dev";
var vSponsor = "000001";
*/

var currentMenu;
var menuTimer;
var clearTimer;
var Hide = true;
var priID;
var xmlDoc;
var vStyleSheet;
var firstTime = false;

var v_sub_width = 175;
var v_sub_height = 16;

if (isMac) {
  if (isFirefox)
    v_sub_height = 17;

  if (isSafari)
    v_sub_height = 17;
} else {
  if (isSafari && !isChrome)
    v_sub_height = 16;
}
/*
alert("isMac: " + isMac + " isFirefox: " + isFirefox + " v_sub_height: " + v_sub_height);
*/

vCartItems = getCookie("cartItems");

if (vCartItems == "") vCartItems = 0;
if (vCartItems == 1) vCartItems = vCartItems + " item";
else vCartItems = vCartItems + " items";

vStyleSheet = "styles/header2.css";

/* If Mac */
if (navigator.platform.toLowerCase().indexOf('mac') != -1)
   vStyleSheet = "styles/header2-mac.css";

var user = getCookie("loggedIn" + vCookie + vSponsor);

if (user == "" || user == ";")
{
var vFrom = "' hostUrl appUrl '/site.w?sponsor=" + vSponsor + "&frames=no&target=main&static=yes&location=b2c/index.w?sponsor=" + vSponsor + "&location=olc/my-account.w?sponsor=" + vSponsor;
}

function validateSearchString(txt)
{
   var flgStringValid = true;
   var strSearchString = new String(txt);
   var index;
   var strErrorMsg, strErrorMsgInvalidChars;
   var strArrayOfInvalidChars =
    new Array("\?", "\/","\\","\@","\(","\)","$","%","!","&","#");
   index = 0;
   while (flgStringValid == true && index < strArrayOfInvalidChars.length)
   {
      if (strSearchString.indexOf(strArrayOfInvalidChars[index]) != -1)
      {
         strErrorMsg = "We're sorry, your search phrase cannot contain the following characters: ";
         strErrorMsgInvalidChars = "";
         for (index = 0; index < strArrayOfInvalidChars.length; index++)
         {
            strErrorMsgInvalidChars = strErrorMsgInvalidChars + "'" + strArrayOfInvalidChars[index] + "'";
            if (index + 1 < strArrayOfInvalidChars.length)
            {
               strErrorMsgInvalidChars = strErrorMsgInvalidChars + ", ";
            }
         }
         alert(strErrorMsg + strErrorMsgInvalidChars);
         flgStringValid = false;
      }
      index++;
   }
   return flgStringValid;
}

function goSearch() {
  if (document.getElementById("txtsearch").value == "") {
  alert("Please enter search criteria.");
  } else if (validateSearchString(document.getElementById("txtsearch").value)) {
    document.location.href = hostUrl + vCgiBin + '/site.w'
      + '?location=olc/catalog-browse.w'
      + '&action=search&staticsearch=yes'
      + '&sponsor=' + vSponsor
      + '&frames=no'
      + '&target=main'
      + '&textsearch=' + document.getElementById("txtsearch").value
      + '&result=' + document.getElementById("txtsearch").value;
 }
}

function goCart() {
 document.location.href = hostUrl + vCgiBin + "/site.w?sponsor=" + vSponsor + "&target=main&static=yes&form=no&location=b2c/index.w%3Fsponsor%3D" + vSponsor + "%26location%3Dolc/olc-cart-fm.w%3Fsponsor%3D" + vSponsor;
}

/*** vv Create Menu Object Functions vv ***/
function menuStart()
{  this.menu = new Array; }

function menuObject(p_name,p_path, pID)
{  this.name = p_name;
   this.path = p_path;
   this.id   = "menu-" + pID;
   this.subMenu = new Array; }

function subMenuObject(p_name, p_path, pID)
 { this.name = p_name;
   this.path = p_path;
   this.id   = "subMenu-" + pID;
   this.subMenu2 = new Array; }

function subMenu2Object(p_name, p_path, pID, sID)
 { this.name = p_name;
   this.path = p_path;
   this.id   = "subMenu2-" + pID + "-" + sID;
   this.subMenu3 = new Array;}

function subMenu3Object(p_name, p_path, pID, sID, tID)
 { this.name = p_name;
   this.path = p_path;
   this.id   = "subMenu3-" + pID + "-" + sID + "-" + tID; }

function createMenuObj(vxmlDoc)
{
 /* Use the exact same server side xml build as ecasuals, just change this
    client build to use the data differently so we don't have to change the
    build program. */

 if (!vxmlDoc) return false;

 mainMenu.menu[0] = new menuObject("Categories","","0");
 mainMenu.menu[1] = new menuObject("Brands","","1");

 for (var i=0; i<mainMenu.menu.length; i++) {
  var mainMenuIndex = i;
  var menuObj = vxmlDoc.getElementsByTagName("menu"+(i+1));

  for(x = 0; x < menuObj.length; x++)
  {
   mainMenu.menu[mainMenuIndex].subMenu[x] = new subMenuObject(menuObj[x].getElementsByTagName("name")[0].firstChild.data,menuObj[x].getElementsByTagName("path")[0].firstChild.data,menuObj[x].getElementsByTagName("ID")[0].firstChild.data);

   var subMenuObj = menuObj[x].getElementsByTagName("subMenu1");

   for(y = 0; y < subMenuObj.length; y++)
   {
    if ( subMenuObj[y].getElementsByTagName("name1")[0].firstChild &&
         subMenuObj[y].getElementsByTagName("path1")[0].firstChild &&
         menuObj[x].getElementsByTagName("ID")[0].firstChild &&
         subMenuObj[y].getElementsByTagName("priID")[0].firstChild )
    { mainMenu.menu[mainMenuIndex].subMenu[x].subMenu2[y] = new subMenu2Object(subMenuObj[y].getElementsByTagName("name1")[0].firstChild.data,subMenuObj[y].getElementsByTagName("path1")[0].firstChild.data,menuObj[x].getElementsByTagName("ID")[0].firstChild.data,subMenuObj[y].getElementsByTagName("priID")[0].firstChild.data); }
    else if ( menuObj[x].getElementsByTagName("ID")[0].firstChild &&
              subMenuObj[y].getElementsByTagName("priID")[0].firstChild )
    { mainMenu.menu[mainMenuIndex].subMenu[x].subMenu2[y] = new subMenu2Object("","","",""); }

    var subMenuObj2 = subMenuObj[y].getElementsByTagName("subMenu2");
    for(z = 0; z < subMenuObj2.length; z++)
    {
     if ( subMenuObj2[z].getElementsByTagName("name2")[0].firstChild &&
          subMenuObj2[z].getElementsByTagName("path2")[0].firstChild &&
          menuObj[x].getElementsByTagName("ID")[0].firstChild &&
          subMenuObj[y].getElementsByTagName("priID")[0].firstChild &&
          subMenuObj2[z].getElementsByTagName("secID")[0].firstChild )
     { mainMenu.menu[mainMenuIndex].subMenu[x].subMenu2[y].subMenu3[z] = new subMenu3Object(subMenuObj2[z].getElementsByTagName("name2")[0].firstChild.data,subMenuObj2[z].getElementsByTagName("path2")[0].firstChild.data,menuObj[x].getElementsByTagName("ID")[0].firstChild.data,subMenuObj[y].getElementsByTagName("priID")[0].firstChild.data,subMenuObj2[z].getElementsByTagName("secID")[0].firstChild.data);  }
     else if ( menuObj[x].getElementsByTagName("ID")[0].firstChild &&
               subMenuObj[y].getElementsByTagName("priID")[0].firstChild &&
               subMenuObj2[z].getElementsByTagName("secID")[0].firstChild )
     { mainMenu.menu[mainMenuIndex].subMenu[x].subMenu2[y].subMenu3[z] = new subMenu3Object("","","","","");  }
    }
   }
  }

 } /* for (var i=0; i<mainMenu.menu.length; i++) */

} /* function createMenuObj(vxmlDoc) */

/*** ^^ Create Menu Object Functions ^^ ***/

function changeClass(pID,hover)
{
  if (hover)
   { if(document.getElementById(pID) != undefined )
     { document.getElementById(pID).className = "headerMenuHoverTD";
       document.getElementById(pID + "2").className = "headerMenuLinkHover"; }
   }
  else
   { if(document.getElementById(pID) != undefined )
     { document.getElementById(pID).className = "headerMenuTD";
       document.getElementById(pID + "2").className = "headerMenuLink"; }
   }
}
/** Safari XML Load functions **/
var http_request = false;
var mimeType     = "";
var parseMode    = "";
var errorStatus  = "";
var errorMessage = "";
var textResponse = null;
var xmlResponse  = null;


function makeReq(url,vMethod,vMimeType,vMode) {
    http_request = false;
    mimeType = vMimeType;
    parseMode = vMode;
    var fields = new Array();
    var sendFields = "";
    var submitForm = null;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType(vMimeType);
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) return false;

    /*vvv XXX vvv*/
    try {
        http_request.onreadystatechange = getReq;
        http_request.open(vMethod, url, false);
        http_request.send(null);
    } catch (e) {}
    /*^^^ XXX ^^^*/

    createMenuObj(http_request.responseXML);
}

function getReq() {
   var vDoc = null;
   if (http_request.readyState == 4) {
       if (http_request.status == 200) {
           if (mimeType == "text/xml")
              vDoc = http_request.responseXML;
           else
              vDoc = http_request.responseText;

          processReq(vDoc);
       }
       else
          return false;
   }
}

/* XXX See below, this should act only as a forward declaration. */
function parseXML() {}

function processReq(vDoc) {
  if(mimeType == "text/xml") {
    xmlResponse = vDoc.documentElement;
    var topNode = vDoc.getElementsByTagName(~'ajaxml~').item(0);
    if (topNode)
      errorStatus = topNode.getAttribute(~'status~');
    errorMessage = "";
    if (errorStatus == "error")
      errorMessage = xmlResponse.getElementsByTagName("error")[0].firstChild.data;

    /* XXX This is not defined even when there is a function in the code later
        on.  I suspect it is too early in the load, perhaps?! XXX */
    parseXML();
  }
}
    /** END Safari XML Load functions **/


function loadXML() {
// code for IE
if (window.ActiveXObject) {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load(vRoot + "/xml/menu.xml");
  createMenuObj(xmlDoc);
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument) {
  if (isSafari) {
      makeReq(vRoot + "xml/menu.xml","GET","text/xml",""); /* XXX */
  }
  else { /* FireFox */
     xmlDoc=document.implementation.createDocument("","",null);
     xmlDoc.async=false;
     xmlDoc.load(vRoot + "xml/menu.xml");                          /* XXX */
     xmlDoc.onload=createMenuObj(xmlDoc);
  }
}
else alert('Your browser cannot handle this script');
}

function showHideCombo(action)
{
	var objCombo = document.getElementsByTagName("select");
	if(objCombo[0])
 {
		for(i=0;i<objCombo.length;i++)
  {
   		if(action=="hide")
      		objCombo[i].style.visibility = "hidden";
    	else
    				objCombo[i].style.visibility = "visible";
		}
	}
}

function showPasshint(pHint)
{
 if (pHint.length < 1)
 {
  alert("Warning: Username cannot be left blank. Please enter a value");
  document.leftMenu.struserid.focus();
  return;
 }
 var showHelp = window.open("" + vRoot + "/statichtml/new-password.htm?userid=" + pHint,"","toolbar=no,location=no,directories=no,scrollbars=no,status=no,menubar=no,resizable=no,height=300,width=780");
}

function getSavedLogin()
{
 if(getCookie("saveLogin" + vCookie)!="" && getCookie("saveLogin" + vCookie)!=";")
 {
  var tempString=getCookie("saveLogin" + vCookie);
  if(tempString!=null)
  {
   var login_password=tempString.split("|");
   document.leftMenu.struserid.value=login_password[0];
   document.leftMenu.strpassword.value=login_password[1];
   document.leftMenu.remember.checked=true;
  }
 }
}

function updateLogin(vChecked)
{
 if(vChecked)
 {
  var expirydate = new Date();
  expirydate.setTime( expirydate.getTime() + (3650 * 24 * 60 * 60 * 1000) );
  var tempString=getCookie("saveLogin" + vCookie);

  if(tempString!="")
   deleteCookie("saveLogin" + vCookie, "?", document.domain.substring(document.domain.lastIndexOf(".",document.domain.lastIndexOf(".") - 1), document.domain.length), null);

  setCookie("saveLogin" + vCookie, document.leftMenu.struserid.value + "|" + document.leftMenu.strpassword.value, expirydate, "/", null, null);
 }
 else
   deleteCookie("saveLogin" + vCookie, "/",null, null);


}

setCookie("cartItems",vNumCartItems, null, "/", null, null);

function set_cart_qty(vNumItems)
{
 var strQyt;
 strQyt = vNumItems;
 if(vNumItems == 1)
  strQyt = strQyt + " item";
 else
  strQyt = strQyt + " items";
 document.getElementById("numCartItems").innerHTML = strQyt;
}

var mainMenu = new menuStart();
loadXML();

document.write('<link rel="stylesheet" type="text/css" href="' + vRoot + vStyleSheet + '">');

document.write('<table width="100%" cellpadding="0" cellspacing="0" align="center" style="padding-top: 3px;"><tr><td width="100%" align="center">');
document.write('<table border="0" valign="top" width="780" cellpadding="0" cellspacing="0">');
document.write('<tr>');

/* Header */

document.write('<td valign="top"><a class="hdrLink" href="' + hostUrl + vCgiBin + '/site.w?sponsor=' + vSponsor + '"><img src="' + vRoot + '/images/Staton-Header-Logo.jpg"></a></td>');

/* Login Section */

document.write('<td align="left" valign="top" width="36%">');

document.write('<form name="leftMenu" action="' + sHostUrl + vCgiBin + '/login.w?location=' + escape(document.location.href) + '" onsubmit="" method="post">');
document.write('<TABLE border="0" align="left" valign="middle" cellpadding="0" cellspacing="0">');

if (user == "" || user == ";") {
 document.write('<tr>');
 document.write('<td width="80" class="login">');

 /*vvv XXX <input> moved to <td> from <tbody> vvv*/
 document.write('<input type="hidden" name="from" value="' + vFrom + '"/>');
 /*^^^ XXX ^^^*/

 document.write('<input class="login" type="text" name="struserid" size="10" maxlength="25" tabindex="1" value="USER NAME" onfocus="if (this.value == \'USER NAME\') this.value=\'\';"/></td>');
 document.write('<td><img src="' + vRoot + '/images/spacer.gif" width="10" border="0" /></td>');
 document.write('</tr>');
 document.write('<tr>');
 document.write('<td width="100" class="password"><input class="password" type="password"	name="strpassword" size="10" maxlength="25" tabindex="2" value="PASSWORD" onfocus="if (this.value == \'PASSWORD\') this.value=\'\';"/></TD>');
 document.write(                                                    /* 090526 */
    '<td valign="bottom" class="remember" nowrap>&nbsp;'
    + '<a href="javascript:void(0);"'
//  + 'onClick="updateLogin(document.leftMenu.remember.checked);'
    + 'document.leftMenu.submit();return false;">'
    + '<input type="image" src="' + vRoot + '/images/arrow-button-large.jpg" tabindex="3" border="0">'
    + '</a>'
 //   + '&nbsp;<a href="javascript:void(0);">Remember Me</a>'
 //   + '<input type="checkbox" name="remember" value="yes" tabindex="4">'
    + '</td>'
 );
 
 document.write('</tr>');
 document.write('<tr>');
 document.write('<td><a class="login" href="javascript:showPasshint(document.leftMenu.struserid.value);" tabindex="5"><font class="login">Forgot Password</font></a></td>');
 document.write('<td><img src="' + vRoot + '/images/1by1pix.gif" onload="getSavedLogin();"></td>');
 document.write('</tr>');
 document.write('<tr>');
 document.write('<td colspan="2" class="register">Not a registered user? <a class="register" href="javascript: golink(\'registration_form.w\',\'main\')" tabindex="5">Sign up TODAY!</a> <a href="javascript: golink(\'registration_form.w\',\'main\')"><img src="' + vRoot + '/images/arrow-button-small.jpg" tabindex="3" /></a></td>');
 document.write('</tr>');
} else {
 document.write('<TR><TD valign="top" class="login" nowrap>' + user + ', you are signed in.</td></TR>');
 document.write('<tr><td class="login"><a href="javascript:golink(\'logout-fm.w\',\'top\');" tabindex="1">Sign Out</a></TD></tr>');
}

document.write('</TABLE>');
document.write('</form>');
document.write('</td>');

/* Right side - links + shopping cart + search box */

document.write('<td align="right" width="34%">');
document.write('<table width="100%" border="0" cellpading="0" cellspacing="0">');
document.write('<tr>');
document.write('<td class="links" align="right"><a class="hdrLink" href="javascript:golink(\'olc/my-account.w\',\'main\');">My Account</a><span class="hdrBar">|</span><a class="hdrLink" href="javascript:golink(\'olc/order-status.w\',\'main\',\'yes\');">Order Status</a><span class="hdrBar">|</span><a class="hdrLink" href="' + hostUrl + vCgiBin + '/site.w?location=b2c/index.w%3Fsponsor%3D' + vSponsor + '%26location%3Dolc/quickorder.w%3Fsponsor%3D' + vSponsor + '&target=main&form=no&static=yes&sponsor=' + vSponsor + '">Quick Order</a></td>');
/*
/site.w?sponsor=000001&target=main&static=yes&form=no&location=b2c/index.w%3Fsponsor%3D000001%26location%3Dolc/quickorder.w%3Fstyle%3D1001%26color%3D%26sponsor%3D000001
*/
document.write('</tr>');
document.write('<tr>');
document.write('<td height="10"><img src="' + vRoot + '/images/1by1pix.gif" height="10" width="1" /></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td align="right">');
document.write('<table border="0" cellpadding="0" cellspacing="0">');
document.write('<tr>');
document.write('<td valign="bottom"><a class="hdrLink" href="javascript: goCart();"><img src="' + vRoot + '/images/Shopping-Cart-Image.jpg" />:</a> <a class="hdrLink" href="javascript: goCart();"><span class="numCartItems" id="numCartItems">' + vCartItems + '</span></a></td>');
document.write('</tr>');
document.write('<tr><td>');

/*vvv XXX form should be completly in <td>, def'n in <tr> disrupts table vvv*/
document.write('<form name="headerSearch" action="javascript:goSearch();">');
document.write('<input id="txtsearch" name="txtsearch" type="text" size="15" class="search" value=" SEARCH KEYWORD OR ITEM #" onfocus="if (this.value == \' SEARCH KEYWORD OR ITEM #\') this.value=\'\';"/>&nbsp;<a href="javascript:goSearch();"><img src="' + vRoot + '/images/arrow-button-large.jpg" alt="Search" tabindex="3" valign="bottom" /></a>');
document.write('</form>');
document.write('</td></tr>');
/*^^^ XXX ^^^*/

/*vvv XXX add advanced search link to hdr vvv*/
document.write('<tr><td align="left">');
document.write('<a class="hdrLink" href="'
    + hostUrl + vCgiBin + '/site.w'
    + '?location=olc/search.w'
    + '&frames=no&target=main&sponsor=' + vSponsor
    + '">advanced search</a>'
);
document.write('</td></tr>');
/*^^^ XXX ^^^*/

document.write('</table>');

document.write('</td>');
document.write('</tr>');
document.write('</table>');
document.write('</td>');
document.write('</tr>');

/*****v Drop Down menus v*****/

document.write(' <tr class="headermenuTR">');
document.write('  <td colspan="3" onmouseover="showHideCombo(\'hide\');" onmouseout="showHideCombo(\'show\');">');
document.write('   <table width="100%" cellspacing="0" cellpadding="0">');
document.write('    <tr height="34">');

/* Shop By Category */
document.write('       <td class="headerMenuTD" id="CATEGORYMain" width="80" nowrap onmouseover="changeClass(\'CATEGORYMain\',true); showMenu(0,0);" onmouseout="$(\'.downMenu\').hide(); changeClass(\'CATEGORYMain\',false);">');
document.write('         <a id="CATEGORYMain2" class="headerMenuLink" href="#">SHOP BY CATEGORY</a>');
if (isSafari)
   document.getElementById("CATEGORYMain").style.display = "none";
buildMenu(mainMenu.menu[0],0);
document.write('       </td>');

/* Shop By Brand */
document.write('       <td class="headerMenuTD" id="BRANDSMain" onmouseover="changeClass(\'BRANDSMain\',true); showMenu(1,0);" onmouseout="$(\'.downMenu\').hide(); changeClass(\'BRANDSMain\',false);">');
document.write('        <a id="BRANDSMain2" class="headerMenuLink" href="#">SHOP BY BRAND</a>');
if (isSafari)
   document.getElementById("BRANDSMain").style.display = "none";
buildMenu(mainMenu.menu[1],1);
document.write('       </td>');

/* Sales - SB 081222 removed &category=3 */
document.write('       <td class="headerMenuTD" id="SALESMain" width="80" onmouseover="changeClass(\'SALESMain\',true);" onmouseout="$(\'.downMenu\').hide(); changeClass(\'SALESMain\',false);">');
document.write('        <a id="SALESMain2" class="headerMenuLink" href="' + hostUrl + vCgiBin + '/site.w?location=olc/catalog-browse.w&action=special&target=main&frames=no&sponsor=' + vSponsor + '">SALES</a>');
if (isSafari)
   document.getElementById("SALESMain").style.display = "none";
buildMenu(mainMenu.menu[2],2);
document.write('       </td>');

/* Closeouts - SB 081222 removed &category=4 */
document.write('        <td class="headerMenuTD" id="CLOSEOUTSMain" onmouseover="changeClass(\'CLOSEOUTSMain\',true);" onmouseout="$(\'.downMenu\').hide(); changeClass(\'CLOSEOUTSMain\',false);">');
document.write('       <a id="CLOSEOUTSMain2" class="headerMenuLink" href="' + hostUrl + vCgiBin + '/site.w?location=olc/catalog-browse.w&action=closeout&target=main&frames=no&sponsor=' + vSponsor + '">CLOSEOUTS</a>');
if (isSafari)
   document.getElementById("CLOSEOUTSMain").style.display = "none";
buildMenu(mainMenu.menu[3],3);
document.write('       </td>');

/* New */
document.write(' <td class="headerMenuTD" id="NEWMain" onmouseover="changeClass(\'NEWMain\',true);" onmouseout="$(\'.downMenu\').hide(); changeClass(\'NEWMain\',false);">');

document.write(' <a id="NEWMain2" class="headerMenuLink" href="'
    + hostUrl + vCgiBin + '/site.w?location=olc/catalog-browse.w'
    + '&action=catalog&category=new'
    + '&target=main&frames=no&sponsor=' + vSponsor
    + '">NEW</a>'
);                                                                 /* XXX */

if (isSafari) document.getElementById("NEWMain").style.display = "none";

buildMenu(mainMenu.menu[4],4);
document.write('       </td>');
document.write('    </tr>');
document.write('  </td>');
document.write(' </table>');
document.write(' </td>');
document.write(' </tr>');
document.write('</table>');
document.write('</td></tr></table>');

/* XXX this is a mismatch ???
document.write('</form>');
*/

resizeMenu();
document.body.onload = resizeMenu;

// adjust for scrollbar in firefox
if (document.implementation &&
    document.implementation.createDocument)
{v_left_adjust = v_left_adjust - 9;}

if (isSafari)
{
   document.getElementById("CATEGORYMain").style.display = "block";
   document.getElementById("BRANDSMain").style.display = "block";
   document.getElementById("SALESMain").style.display = "block";
   document.getElementById("CLOSEOUTSMain").style.display = "block";
   document.getElementById("NEWMain").style.display = "block";
}


