loaded=false;
var isInternetExplorer = (document.all) ? true : false;
var _divClicked=false;
function init() {
	try {
	menuArray=new Array();
	for (var i=0;i<4;i++) { //nav menus
		menuArray[i] = document.getElementById("navDrop"+i);
		cleanWhiteSpace(menuArray[i]);
		var n = menuArray[i].childNodes[1];//menu
		if (i==4) break;
		for (j=0;j<n.childNodes.length;j++) {
			n.childNodes[j].className ="navMenuItemOff";
			n.childNodes[j].childNodes[0].className ="navMenuItemLinkOff";
			n.childNodes[j].makeRollObj = makeRollObj;
            n.childNodes[j].makeRollObj(n.childNodes[j]);
		}
	}

	var imgArray = new Array('nav_view_cart','nav_checkout','btn_search');
	imgRollArray = new Array();
	for (var i=0;i<imgArray.length;i++){
		imgRollArray[i] = new Array();
		imgRollArray[i][0] = new Image();
		imgRollArray[i][0].src = "/images/common/nav/" + imgArray[i] + ".gif";
		imgRollArray[i][1] = new Image();
		imgRollArray[i][1].src = "/images/common/nav/" + imgArray[i] + "_on.gif";
		imgRollArray[i][2] = new Image();
		imgRollArray[i][2].src = "/images_es/common/nav/" + imgArray[i] + ".gif";
		imgRollArray[i][3] = new Image();
		imgRollArray[i][3].src = "/images_es/common/nav/" + imgArray[i] + "_on.gif";
	}

	loaded=true;
	} catch(e) {
		loaded=false;
	}
}

function makeRollObj(s) {
	this.onmouseover = function(){
		this.className = "navMenuItemOn";
		this.childNodes[0].className = "navMenuItemLinkOn";
	}
	this.onmouseout = function(){
		this.className = "navMenuItemOff";
		this.childNodes[0].className = "navMenuItemLinkOff";
	}
	this.onclick = function() {
		window.location.href = this.childNodes[0].attributes["href"].value;
	}
}

function showMenu(mn,st){
	if (loaded) {
		if (st==1) {
			for (var i=0;i<menuArray.length;i++) {
				menuArray[i].style.visibility  = "hidden";
			}
			if(isInternetExplorer) {
				hideObjects();
			}
		} else {
			if(isInternetExplorer) {
				showObjects();
			}
		}
		menuArray[mn].style.visibility  = (st==1) ? "visible" : "hidden";
	}
}  


function showRoll(s,w,st){
	if (loaded) {  
		s.src = imgRollArray[w][st].src;
	}
}  



function showAlphaItems(n) {
	var az = document.getElementById("alphaNumeric");
	cleanWhiteSpace(az);
	for (var i=0;i<az.childNodes.length;i++) {
		try {
			az.childNodes[i+1].style.border = "none";			
			document.getElementById("alpha"+i).style.visibility = "hidden";
		} catch(e) {}
	}
	 
	if (isInternetExplorer) {
		document.getElementById("allBrands").style.height = document.getElementById("alpha"+n).clientHeight;
	} else {
		document.getElementById("firefoxBug").innerHTML = '<p><img src="/images/spacer.gif" width=1 height='+document.getElementById("alpha"+n).clientHeight+'></p>';	
	}
	
	document.getElementById("alpha"+n).style.visibility = "visible";
	az.childNodes[(n+1)].style.border = "2px solid #CCCCCC";
	az.childNodes[(n+1)].style.borderBottom = "3px solid #ffffff";	
}

function showTab(n) {
	var tabs = document.getElementById("tabMenu");
	cleanWhiteSpace(tabs);
	for (var i=0;i<tabs.childNodes.length;i++) {
		try {
			tabs.childNodes[i].childNodes[0].src = replace(tabs.childNodes[i].childNodes[0].src,'_on.gif','.gif');
			document.getElementById("tab"+i).style.visibility = "hidden";
		} catch(e) {}
	}
	if (isInternetExplorer) {
		document.getElementById("allTabs").style.height = document.getElementById("tab"+n).clientHeight;
	} else {
		document.getElementById("firefoxBug").innerHTML = '<p><img src="/images/spacer.gif" width=1 height='+document.getElementById("tab"+n).clientHeight+'></p>';
	}	
	document.getElementById("tab"+n).style.visibility = "visible";
	tabs.childNodes[n].childNodes[0].src = replace(tabs.childNodes[n].childNodes[0].src,'.gif','_on.gif');
}

function toggleSubCat(o,n) {
	try {	
		var co = document.getElementById("manufacturer"+n);
		o.childNodes[0].src = (co.style.display=="block") ? toggleImg[0].src : toggleImg[1].src;
		co.style.display = (co.style.display=="block") ? "none" : "block";
	} catch(e) {
		alert(e.description);
	}
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
function cleanWhiteSpace(node) {  
	var notWhiteSpace = /\S/;
	
	for (var x = 0; x < node.childNodes.length; x++) {
		var childNode = node.childNodes[x]
		if ((childNode.nodeType==3)&&(!notWhiteSpace.test(childNode.nodeValue))) { //whitespace text node
			node.removeChild(node.childNodes[x]);
			x--;
	    }
		if (childNode.nodeType == 1) {// elements can have text child nodes of their own
			cleanWhiteSpace(childNode);
	    }
	} 
}

function hideObjects()
{
     var selCount = document.all.tags("select");
     for (i=0; i<selCount.length; i++)
          selCount[i].style.visibility = "hidden";

}

function showObjects() {
 selCount=document.all.tags("select")
     for (i=0;i<selCount.length;i++)
          selCount[i].style.visibility="visible"
}

if (window.attachEvent) {
	window.attachEvent("onload",init);
} else {
	window.addEventListener("load",init,false);
}

 function checkUncheckAll(theElement) {
     var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length;z++){
      if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
	  theForm[z].checked = theElement.checked;
	  }
     }
   }
  function checkbox_checker()
{
	// set var checkbox_choices to zero
	var checkbox_choices = 0;

	// Loop from zero to the one minus the number of checkbox button selections
	for (counter = 0;  counter < checkbox_form.hotlist_add.length; counter++)
	{
	// If a checkbox has been selected it will return true
	// (If not it will return false)
	if (checkbox_form.hotlist_add[counter].checked)
	{ checkbox_choices = checkbox_choices + 1; }

	}
	if (checkbox_choices < 1 )
	{
	// If there were less then selections made display an alert box
	alert("Please select any product to add in Hotlist. \n")
	return (false);
	}else{
	return (true);
	}
}

function checkbox_checker2()
{
	// set var checkbox_choices to zero
	var checkbox_choices = 0;

	// Loop from zero to the one minus the number of checkbox button selections
	for (counter = 0;  counter < cart_quantity.hotlist_add.length; counter++)
	{
	// If a checkbox has been selected it will return true
	// (If not it will return false)
	if (cart_quantity.hotlist_add[counter].checked)
	{ checkbox_choices = checkbox_choices + 1; }

	}
	if (checkbox_choices < 1 )
	{
	// If there were less then selections made display an alert box
	alert("Please select atleast one product to add in PO. \n")
	return (false);
	}else{
	return (true);
	}
}
function ajaxFunction(id,url){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	ajaxRequest.open("GET",url, true);
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
            document.getElementById(id).style.visibility="visible";
			var ajaxDisplay = document.getElementById(id);
			//alert(ajaxDisplay);
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
		else
		{
			var ajaxDisplay = document.getElementById(id);
			ajaxDisplay.innerHTML = '';
			}
	}
	ajaxRequest.send(null);
}
function ajaxFunction2(id,val){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	url="testunit.php?q="+val;
	ajaxRequest.open("GET",url, true);
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById(id);
			//alert(ajaxDisplay);
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
		else
		{
			var ajaxDisplay = document.getElementById(id);
			ajaxDisplay.innerHTML = '';
			}
	}
	ajaxRequest.send(null);
}

function onBlurHandler(event){
  if(!_divClicked){
	   hideCompleteDiv();
   }
 }
function onFocusHandler(event){
	showCompleteDiv();
}
function hideCompleteDiv(){
  document.getElementById("TopSearch").style.visibility="hidden";
}

//P_SUPPORT_E_22
function showCompleteDiv(){
  document.getElementById("TopSearch").style.visibility="visible";

}

function onmouseoverHandler(event){
	_divClicked = true;
	//alert(_divClicked);
}

 function onmouseoutHandler(event){

   _divClicked = false;
}

function getSuggest( field,div )
{    //div="TopSearch";
	 _inputField = field;
	 _inputField.onblur=onBlurHandler;
	 _inputField.onfocus=onFocusHandler;
	var url="ajax_products_search.php?key="+field.value;
	ajaxFunction(div,url);
}
function handleKeyUp( e,div )
{
    e = (!e) ? window.event : e;
    target = (!e.target) ? e.srcElement : e.target;

    if (e.type == "keyup")
    {
        // skip shift, alt, control keys

        if (e.keyCode == 16 || e.keyCode == 17 || e.keyCode == 18)
        {
            // do nothing
        } else
        {
            if(target.value.length >= 3)
            {
                //P_SUPPORT_E_11

                if (target.name == "keywords")
                {
                    if (target.value)
                    {
					//alert(target.value);
			        getSuggest(target,div);
                    } else
                    {
                        hidePopup();
                    }
                }
            }else
            {
                hidePopup();
            }
        }
    }
}
function hidePopup() {
     document.getElementById("TopSearch").style.visibility="hidden";
}

function encode( uri ) {
    if (encodeURIComponent) {
        return encodeURIComponent(uri);
    }

    if (escape) {
        return escape(uri);
    }
}
function paymentInfo(val){
   var url="payment_info.php?payment="+val;
   ajaxFunction("payment_info",url);

}
function makeBMark(title,url)
{
    if (window.sidebar)
    {
        window.sidebar.addPanel(title, url,"");
        alert("Firefox users: This link will open in your Sidebar\nrather than the main page!\nTo prevent this please right click the link and choose\n'Properties' and then uncheck the box marked:\n'Load This Bookmark in the Sidebar'\n\nSorry about this, but it is unavoidable with Firefox");

    } else if( window.opera && window.print )
    {
    alert("Opera users: By Default this link will open in your Sidebar\nrather than the main page!\nTo prevent this please uncheck the box marked\n'Show in Panel' that appears on the Add Bookmark window\n\nSorry about this, but it is currently unavoidable with Opera");
    var mbm = document.createElement('a');
    mbm.setAttribute('rel','sidebar');
    mbm.setAttribute('href',url);
    mbm.setAttribute('title',title);
    mbm.click();
    } else if( document.all )
    {
    window.external.AddFavorite( url, title);
    }
}