var g_iSuperCategoryIndex = 0;
var g_iCategory1Index = 0;
var g_sFamilyPageUrl = "/template/018s.jsp?view=s11c&Xcntry=USA&Xlang=en_US&category=FAMILYCATEGORY&override=en_US&last=true";
var g_sBVServerUrl = "";
var g_sSelectAllProductsMessage = "";
var g_sSelectAllProductsTaxonomy = "";
var xmlhttp;

function SuppliesHome_setAllProductsMessage( sMessage ) {
	g_sSelectAllProductsMessage = sMessage;
}
function SuppliesHome_setAllProductsTaxonomy( sTaxon ) {
	g_sSelectAllProductsTaxonomy = sTaxon;
}
function SuppliesHome_setBVServerUrl( sUrl ) {
	g_sBVServerUrl = sUrl;
}
function SuppliesHome_setServerUrl( sUrl ) {
	g_sServerUrl = sUrl;
}
function SuppliesHome_productCategorySubmit( sCategoryValue ) {
	var oControl = document.frmSuppliesSelector.category;
	var sCategory = oControl.options[oControl.selectedIndex].value;
	var oProdIdControl = document.frmSuppliesSelector.prodID;
	var sProdId = oProdIdControl.options[oProdIdControl.selectedIndex].value;
	//alert( sCategory + " " + sProdId );
	document.frmSuppliesSelector.submit();
}

function SuppliesHome_loadXMLDoc(url,funcHandler) {
	// code for Mozilla, etc.	
	//alert("url = " + url);
	if (window.XMLHttpRequest)  {
		xmlhttp=new XMLHttpRequest();
		xmlhttp.onreadystatechange=funcHandler;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	} else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	    if (xmlhttp) {
		    xmlhttp.onreadystatechange=funcHandler;
		    xmlhttp.open("GET",url,true);
		    xmlhttp.send();
	    }
	}	
}
function SuppliesHome_state_Change() {
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4) {
	  // if "OK"
	  if (xmlhttp.status==200) {
			//alert("XML data OK")
			var sResponse = xmlhttp.responseText;
			
			var aAllCategories = sResponse.split( "#" );
			document.frmSuppliesSelector.category.options.length = 0;
			for ( var index = 0; index < aAllCategories.length; index++ ) {
				var icategoryIndex = document.frmSuppliesSelector.category.options.length;
				var aCurrentCategory = aAllCategories[index].split( "|" );
				//alert( index + " " + aCurrentCategory.toString() );
				if ( aAllCategories[index].toString().length > 0 ) {
					document.frmSuppliesSelector.category.options[index] = 
						new Option( "* "+aCurrentCategory[0].replace( /Cleaning Supplies/g, "Maintenance Supplies"), aCurrentCategory[1] );
				}
			}

			if ( aAllCategories.length > 2 ) {
				if ( g_sSelectAllProductsMessage.length > 0 ) {		
					document.frmSuppliesSelector.category.options[index-1] = 
						new Option( "* "+g_sSelectAllProductsMessage, g_sSelectAllProductsTaxonomy+"/" );
				}
			}
			if ( aAllCategories.length == 0 ) {
				document.frmSuppliesSelector.category.options.length = 0;
				document.frmSuppliesSelector.category.options[0] =
					new Option( "No results found", "Loading.." );
			}
		} else {
			//alert("Problem retrieving XML data:" + xmlhttp.status)
		}
	} else {
		document.frmSuppliesSelector.category.options.length = 0;
		document.frmSuppliesSelector.category.options[0] =
			new Option( "Loading...", "Loading.." );
	}
}
function SuppliesHome_state_Changetaxon() {
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4) {
	  // if "OK"
	  if (xmlhttp.status==200) {
			//alert("XML data OK")
			var sResponse = xmlhttp.responseText;
			var aAllCategories = sResponse.split( "#" );
			document.frmSuppliesSelector.category1.options.length = 0;
			for ( var index = 0; index < aAllCategories.length-1; index++ ) {
				var icategoryIndex = document.frmSuppliesSelector.category1.options.length;
				var aCurrentCategory = aAllCategories[index].split( "|" );		
				var bResultIsProduct = (aCurrentCategory[1].indexOf( "/" ) != 0);
				var sLeadCharacter = ">";
				if ( bResultIsProduct ) {
					var sSelectedCategory = 
						document.frmSuppliesSelector.supercategory.options[document.frmSuppliesSelector.supercategory.selectedIndex].value;
					
					window.location.href = g_sBVServerUrl+g_sFamilyPageUrl.replace( /FAMILYCATEGORY/g, sSelectedCategory );
					sLeadCharacter = "*";	
					break;				
				} else {
					document.frmSuppliesSelector.category1.options[icategoryIndex] = 
						new Option( sLeadCharacter+" "+aCurrentCategory[0], aCurrentCategory[0]+"#"+aCurrentCategory[1] );
				}
			}
			document.frmSuppliesSelector.supercategory.selectedIndex = g_iSuperCategoryIndex;
			document.frmSuppliesSelector.category1.selectedIndex = -1;

		} else {
			//alert("Problem retrieving XML data:" + xmlhttp.status)
		}
	} else {
		document.frmSuppliesSelector.category1.options[0] = 
			new Option( "Loading...", "Loading.." );
	}
}
function SuppliesHome_state_MiscChangetaxon() {
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4) {
	  // if "OK"
	    if (xmlhttp.status==200) {
			//alert("XML data OK")
			var sResponse = xmlhttp.responseText;
			var aAllCategories = sResponse.split( "#" );
			document.frmSuppliesSelector.misccategory1.options.length = 0;
			for ( var index = 0; index < aAllCategories.length-1; index++ ) {
				var icategoryIndex = document.frmSuppliesSelector.misccategory1.options.length;
				var aCurrentCategory = aAllCategories[index].split( "|" );		
				var bResultIsProduct = (aCurrentCategory.toString().indexOf( "/" ) != 0);
				var sLeadCharacter = ">";
				if ( bResultIsProduct ) {
					var sSelectedCategory = 
						document.frmSuppliesSelector.miscsupercategory.options[document.frmSuppliesSelector.miscsupercategory.selectedIndex].value;
					window.location.href = g_sBVServerUrl+g_sFamilyPageUrl.replace( /FAMILYCATEGORY/g, sSelectedCategory );
					sLeadCharacter = "*";
					break;
				}
				document.frmSuppliesSelector.misccategory1.options[icategoryIndex] = 
					new Option( sLeadCharacter+" "+aCurrentCategory[0], aCurrentCategory[0]+"#"+aCurrentCategory[1] );
			}
			document.frmSuppliesSelector.miscsupercategory.selectedIndex = g_iSuperCategoryIndex;
			document.frmSuppliesSelector.misccategory1.selectedIndex = -1;
		} else {
			//alert("Problem retrieving XML data:" + xmlhttp.status)

		}
	} else {
		document.frmSuppliesSelector.misccategory1.options[0] = 
			new Option( "Loading...", "Loading.." );
	}
}
function SuppliesHome_handleProdIdEvent()
{
  var sFamily = document.frmSuppliesSelector.MFG.options[document.frmSuppliesSelector.MFG.selectedIndex].value;
  var sProdId = document.frmSuppliesSelector.prodID.value;
  var sUserType = document.frmSuppliesSelector.usertype.value;
  if (sFamily.indexOf("Phaser") > -1)
  { // if this is a Phaser product go right to the Phaser page, unless we're mys or xnet
    if (sUserType == "mys" || sUserType == "xnet")
      window.location = g_sBVServerUrl + "/template/018s.jsp?view=s13&prodID=" + sProdId;
    else
  		window.location = g_sBVServerUrl + "/template/redirect.jsp?id=LNK_T011_OPB_Product_Overview&Xcntry=USA&Xlang=en_US&page=supl&product=" + sProdId;
  }
  else
  {
		SuppliesHome_loadXMLDoc(g_sServerUrl + "/suppliesapp/supplieslookup?prodID=" + sProdId, SuppliesHome_state_Change);
		hideFamilyDiv();
		showCategoryDiv();
		document.frmSuppliesSelector.category.selectedIndex = -1;
	}
}
function SuppliesHome_handleFamilyEvent() {	

	document.frmSuppliesSelector.category.options.length = 0;
	document.frmSuppliesSelector.prodID.options.length = 0;
	hideCategoryDiv();		
} 
function SuppliesHome_handleMiscCategoryEvent() {
	var sSuperCategory = document.frmSuppliesSelector.miscsupercategory.options[document.frmSuppliesSelector.miscsupercategory.selectedIndex].value;
	sSuperCategory += "/";
	g_iSuperCategoryIndex = document.frmSuppliesSelector.miscsupercategory.selectedIndex;
	document.frmSuppliesSelector.misccategory1.options.length = 0;	
	SuppliesHome_loadXMLDoc( g_sServerUrl + "/suppliesapp/supplieslookup?category="+sSuperCategory,SuppliesHome_state_MiscChangetaxon );
}
function SuppliesHome_handleCategoryEvent() {
	var sSuperCategory = document.frmSuppliesSelector.supercategory.options[document.frmSuppliesSelector.supercategory.selectedIndex].value;
	sSuperCategory += "/";
	g_iSuperCategoryIndex = document.frmSuppliesSelector.supercategory.selectedIndex;
	document.frmSuppliesSelector.category1.options.length = 0;	
	SuppliesHome_loadXMLDoc( g_sServerUrl + "/suppliesapp/supplieslookup?category="+sSuperCategory,SuppliesHome_state_Changetaxon );
}
function SuppliesHome_handleSubCatEvent( sUrl ) {
	// the subcategory becomes the super category
	// first set the length
	var sSelected = document.frmSuppliesSelector.category1.options[document.frmSuppliesSelector.category1.selectedIndex].value;
	g_iCategory1Index = document.frmSuppliesSelector.category1.selectedIndex;
	var bIsCategory = sSelected.indexOf( "/" ) > -1;
	if ( bIsCategory ) {
		// the selected value is a sub category, so handle it and swap
		document.frmSuppliesSelector.supercategory.options.length = 0;
		for ( var index = 0; index < document.frmSuppliesSelector.category1.options.length; index++ ) {
			//alert( document.frmSuppliesSelector.category1.options[index].value );
			var aOption = document.frmSuppliesSelector.category1.options[index].value.split("#");
			if ( aOption.length > 0 ) {
				document.frmSuppliesSelector.supercategory.options[index] = new Option(
					"> "+ aOption[0], aOption[1]
				);	
			}
		}
		document.frmSuppliesSelector.category1.options.length = 0;	
		document.frmSuppliesSelector.supercategory.selectedIndex = g_iCategory1Index;
		SuppliesHome_handleCategoryEvent();
	} else {
		// the selected value is a product so go directly to the product page from here
		var aSelectedProduct = sSelected.split( "#" );
		var sProdId = aSelectedProduct[1];
		window.location.href = sUrl+"/template/018s.jsp?view=s13&prodID="+sProdId;
	}
}
function SuppliesHome_handleMiscSubCatEvent( sUrl ) {
	// the subcategory becomes the super category
	// first set the length
	var sSelected = document.frmSuppliesSelector.misccategory1.options[document.frmSuppliesSelector.misccategory1.selectedIndex].value;
	g_iCategory1Index = document.frmSuppliesSelector.misccategory1.selectedIndex;
	var bIsCategory = sSelected.indexOf( "/" ) > -1;
	if ( bIsCategory ) {
		// the selected value is a sub category, so handle it and swap
		document.frmSuppliesSelector.miscsupercategory.options.length = 0;
		for ( var index = 0; index < document.frmSuppliesSelector.misccategory1.options.length; index++ ) {
			//alert( document.frmSuppliesSelector.category1.options[index].value );
			var aOption = document.frmSuppliesSelector.misccategory1.options[index].value.split("#");
			if ( aOption.length > 0 ) {
				document.frmSuppliesSelector.miscsupercategory.options[index] = new Option(
					"> "+ aOption[0], aOption[1]
				);	
			}
		}
		document.frmSuppliesSelector.misccategory1.options.length = 0;	
		document.frmSuppliesSelector.miscsupercategory.selectedIndex = g_iCategory1Index;
		SuppliesHome_handleMiscCategoryEvent();
	} else {
		// the selected value is a product so go directly to the product page from here
		
		var aSelectedProduct = sSelected.split( "#" );
		var sProdId = aSelectedProduct[1];
		window.location.href = sUrl+"/template/018s.jsp?view=s13&prodID="+sProdId;
	}
}
function SuppliesHome_goBack() {
	hideCategoryDiv();
	document.getElementById("div_FamilySelection").style.display = "block";
	document.getElementById("startOverProdId").style.display = "block";
	resetPaperCategories();
	resetMiscCategories();
	document.frmSuppliesSelector.category.options.length = 0;
	document.frmSuppliesSelector.prodID.selectedIndex = -1;
	document.frmSuppliesSelector.prodID.options.length = 0;
	document.frmSuppliesSelector.MFG.selectedIndex = -1;
}
function hideCategoryDiv() {
	document.getElementById("div_categorySelection").style.display = "none";
}
function showCategoryDiv() {
	document.getElementById("div_categorySelection").style.display = "block";
}
function hideFamilyDiv() {
	document.getElementById("div_FamilySelection").style.display = "none";
	document.getElementById("startOverProdId").style.display = "none";
}
function showFamilyDiv() {
	document.getElementById("div_FamilySelection").style.display = "block";
}
function SuppliesHome_setActiveTab( sOnTab, sOffTab1, sOffTab2 ) {

	document.getElementById(sOnTab+"_on").style.display = "block";
	document.getElementById(sOffTab1+"_off").style.display = "block";
	document.getElementById(sOffTab2+"_off").style.display = "block";
	
	document.getElementById(sOnTab+"_off").style.display = "none";
	document.getElementById(sOffTab1+"_on").style.display = "none";
	document.getElementById(sOffTab2+"_on").style.display = "none";
}
function SuppliesHome_go_product_family(menu, href, country, language)
{
  var choice = menu.selectedIndex;
  var parm = menu.options[choice].value;
  if (parm != "")
  {
    parm = parm.replace(/ /g, "+");
    window.location.href = href + parm + "&Xcntry=" + country + "&Xlang=" + language;
  }
//  menu.selectedIndex = 0;
}
function divSwap( aShow, aHide ) {
	var bIsIe = (navigator.appVersion.indexOf("MSIE") > -1);
	for ( var index = 0; index < aShow.length; index++ ) {
		var sVisibility = "";
		// anything with a "cell" in the name is a table cell and has to
		// have table-cell visibility for Firefox (and IE, if it ever complies
		// to actual standards)
		var bIsTableCell = (aShow[index].indexOf( "cell" ) > -1);
		var sTableCellVisibility = "";
		if ( bIsTableCell && !bIsIe ) {
			// for Firefox and others the table cell visibility is table-cell
			sVisibility = "table-cell";		
		} else {
			// for IE it is block becuase IE doesn't understand table-cell visibility
			sVisibility = "block";
		}
		document.getElementById(aShow[index]).style.display = sVisibility;
	}
	for ( var index = 0; index < aHide.length; index++ ) {
		document.getElementById(aHide[index]).style.display = "none";
	}
}
