hex=255

function doSubmit(action)
{
  var qs = new Querystring()
  var v1 = qs.get("user")
  var action = "search.asp";
  
  if (document.getElementById("brandID").selectedIndex > 0)
    action = "brands.asp";
  else if (document.getElementById("familyID").selectedIndex > 0)
    action = "product.asp";
  else if (document.getElementById("mspID").selectedIndex > 0)
    action = "markets.asp";
          
	var theForm = document.frmSrchSlct;
	if (action.length > 0)	  
		{
		    
		    if (v1 != '' )
		        action += "?user=" + v1;
		        
		    theForm.action = action;
		}
	
	theForm.submit();
}
function doSubmitHdr(action)
{
  var qs = new Querystring()
  var v1 = qs.get("user")
  var action = "search.asp";
  
  if (document.getElementById("brandIDHdr").selectedIndex > 0)
    action = "brands.asp";
  else if (document.getElementById("familyIDHdr").selectedIndex > 0)
    action = "product.asp";
  else if (document.getElementById("mspIDHdr").selectedIndex > 0)
    action = "markets.asp";

	var theForm = document.frmSrchSlctHdr;
	if (action.length > 0)	  
		{
		    
		    if (v1 != '' )
		        action += "?user=" + v1;
		        
		    theForm.action = action;
		}
	
	theForm.submit();
}
function resetDropDowns(srchType)
{
    if (srchType == 'brands')
    {
        document.getElementById("familyID").selectedIndex = 0;
        document.getElementById("mspID").selectedIndex = 0;
    }
    if (srchType == 'product')
    {
        document.getElementById("brandID").selectedIndex = 0;
        document.getElementById("mspID").selectedIndex = 0;
    } 
    if (srchType == 'markets')
    {
        document.getElementById("brandID").selectedIndex = 0;
        document.getElementById("familyID").selectedIndex = 0;
    }          
}
function resetDropDownsHdr(srchType)
{
    if (srchType == 'brands')
    {
        document.getElementById("familyIDHdr").selectedIndex = 0;
        document.getElementById("mspIDHdr").selectedIndex = 0;
    }
    if (srchType == 'product')
    {
        document.getElementById("brandIDHdr").selectedIndex = 0;
        document.getElementById("mspIDHdr").selectedIndex = 0;
    } 
    if (srchType == 'markets')
    {
        document.getElementById("brandIDHdr").selectedIndex = 0;
        document.getElementById("familyIDHdr").selectedIndex = 0;
    }          
}
function onStart()
{

	widgets_string = getWidgetString()
	var widget_array = widgets_string.split("||")
	var total = 0 
	var q
  var total =  getTotal();

	var tmpItms = total;
	document.getElementById("ViewCart").innerHTML= "View Cart ( " + tmpItms + " )";
}


function doSubmitLit()
{
	widgets_string = getWidgetString()
	document.frmLit.cartString.value=widgets_string; 
	var isInt = getQueryVariable("user")
	//var oldSubmit = document.frmLit.action
	if ((isInt) && (isInt != "undefined"))
	{
	    if (document.frmLit.action.indexOf('.asp?') > 0)
	        document.frmLit.action = document.frmLit.action + "&user=" + isInt
	    else
	        document.frmLit.action = document.frmLit.action + "?user=" + isInt
	}
	    
	document.frmLit.submit();
}

function getWidgetString(){
	if(document.cookie && document.cookie != ""){
		var whole_cookie = unescape(document.cookie);
		var each_cookie = whole_cookie.split(";");
		var tmpStrng = ""
		var i
		for (i=0; i < each_cookie.length; i++){
		if (each_cookie[i].indexOf("widgets") > -1){
			var tmpStrng = tmpStrng + each_cookie[i].replace("widgets=","")
			}
		}
		return tmpStrng.replace("||||","||")
	}
	else
	{
		return "";
	}
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}    
function CheckItem(obj)
{

	var strCatalogNo;
	var intCatalogQty;
	if (obj.checked == true)
	{
		strCatalogNo = new String("txtQty" + obj.value);
		intCatalogQty = document.frmLit[strCatalogNo].value;
		if (isNaN(intCatalogQty) || intCatalogQty == "")
		{
		  document.frmLit[strCatalogNo].value = 1;
		}
	}
	else if (obj.checked == false)
	{
	    strCatalogNo = new String("txtQty" + obj.value);
		document.frmLit[strCatalogNo].value = "";
	}
}
function checkQty(uid){
	if (document.getElementById("txtQty" + uid).value != '')
	{
		document.getElementById("chkAdd" + uid).checked = true;
	}
	else
	{
	document.getElementById("chkAdd" + uid).checked = false;
	}
}
function addToCart(new_item,qty, lang,uniqueKey){
  if(qty == 'undefined')
    qty = 1
  if (!lang)
    lang = getLangName(uniqueKey)

  var widgets_string = getWidgetString()
  if (widgets_string.length > 0)
    widgets_string += "||" + new_item + "=" + qty + "=" + lang;
  else
    widgets_string += new_item + "=" + qty + "=" + lang;
  
  widgets_string = widgets_string.replace("||||","||")
  var widget_array = widgets_string.split("||")
  widget_array = widget_array.sort()
  /*for (q=0; q < widget_array.length; q++){   
    alert(widget_array[q])
  }*/
  var new_widget_array = widget_array
  var tmpItem = "";
  var tmpItemLang = "";
  var tmpItemPos = 0
  var total = 0;
  var q;
  var item_array;
  var old_item_array;
  var tmpQty;
  for (q=0; q < widget_array.length; q++){   
  	if (widget_array[q] != "widgets")
	  {
        item_array =  widget_array[q].split("=")  
        if ((tmpItem == item_array[0]) && (tmpItemLang == item_array[2]))  
          {       
            old_item_array = new_widget_array[tmpItemPos].split("=");
            tmpQty = parseFloat(item_array[1]) + parseFloat(old_item_array[1]);
            new_widget_array[tmpItemPos] = old_item_array[0] + "=" + tmpQty + "=" + old_item_array[2];           
            new_widget_array.splice(q,1);
          }
         else
         {
          tmpItem = item_array[0]
          tmpItemPos = q
          tmpItemLang = item_array[2]
         }
     }
  }		
  widgets_string = new_widget_array.join("||")

  document.cookie = "widgets=" + escape(widgets_string);
  document.frmLit.cartString.value=widgets_string;
  total = getTotal();
  fadetext(total);
}


function alphabetical(a, b)
{
     var A = a.toLowerCase();
     var B = b.toLowerCase();
     if (A < B){
        return -1;
     }else if (A > B){
       return  1;
     }else{
       return 0;
     }
}


function getLangName(uniqueKey)
{
   var langVal
   if (document.getElementById("lang" + uniqueKey) == null)
    {
        langVal = "en";
    }
    else
    {
        var ddlist=document.getElementById("lang" + uniqueKey);
        var strSelected = ddlist.options[ddlist.selectedIndex].value;
        var arrSelected = strSelected.split(',');
        langVal = arrSelected[0];    
    } 
    return langVal;
}

function getTotal()
{
  var widgets_string = getWidgetString();
  widgets_string = widgets_string.replace("||||","||")
  var widget_array = widgets_string.split("||")    
  var total = 0 
  var q;
  for (q=0; q < widget_array.length; q++){   
  	if (widget_array[q] != "widgets")
	  {
	      var item_array =  widget_array[q].split("=")
	      total += parseFloat(item_array[1])
	  }
	}  
	
	if (isNaN(total))
	  total = 0
	  	
	return total;
}

function removeItem(litID,lang)
{

	var uSure= confirm("Are you sure you wish to remove this item from your cart?");
	if (uSure == true)
	{
		widgets_string = getWidgetString()
		var widget_array = widgets_string.split("||")
		var i;
		for (i=0; i < widget_array.length; i++){
		  var item_array =  widget_array[i].split("=")  
      if ((litID == item_array[0]) && (lang == item_array[2]))  
        {		  
          widget_array.splice(i,1)
        }
		}
		widgets_string = widget_array.join("||")
		if (widgets_string == "")
		{
        var iframe = document.createElement("iframe");
        iframe.src = "cleanup.asp";
        iframe.style.display = "none";
        document.body.appendChild(iframe);				
		}
		
		document.cookie = "widgets=" + escape(widgets_string);
		document.frmLit.cartString.value=widgets_string; 
		document.frmLit.submit();
	}
}

function addSelectedToCart(){
var slctItems = ""
var tmpLitID

		var i;
	for(i=0; i<document.frmLit.elements.length; i++)
		{		

      if(document.frmLit.elements[i].type=="checkbox")
			{
				if (document.frmLit.elements[i].checked)
				{
				    tmpLitID = document.frmLit.elements[i].value

				    if ((!isNaN(tmpLitID)) || (tmpLitID.indexOf("pkg") >= 0))
				    {
				    		lang = getLangName(tmpLitID)
				        addToCart(eval('document.frmLit.hidID' + tmpLitID + '.value'),eval('document.frmLit.txtQty' + tmpLitID + '.value'),lang)
							  slctItems += eval('document.frmLit.hidID' + tmpLitID + '.value') + "=" + eval('document.frmLit.txtQty' + tmpLitID + '.value') + "||"
						}
			  }
			}
		}

		if (slctItems.length > 0)	
			{
			setTimeout("alert('The literature items you have selected have been added to your cart. To view your cart or to submit your order please click the View Cart button.')",500);
			}
		//document.frmLit.submit();
}

function clear_cart(){
var kill_date = new Date("January 1, 1970");
document.cookie = "widgets=stub;expires=" + kill_date.toGMTString();
var iframe = document.createElement("iframe");
iframe.src = "cleanup.asp";
iframe.style.display = "none";
document.body.appendChild(iframe);
        
fadetext("0");
}

function inPrintChk(uniqueKey,strVal){
    var strValArr = strVal.split(',')
    isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
    var isPrint = document.getElementById("isPrint" + uniqueKey).value
    
    if (isPrint == 'True')
    {
        if (strValArr[2] == 'True')
        {    
            if (isIE6)
            {
            document.getElementById("inPrint" + uniqueKey).style.display = 'table-row';
            document.getElementById("inPrint1" + uniqueKey).style.display = 'table-row';        
            }
            else
            {
            document.getElementById("inPrint" + uniqueKey).style.display = 'block';
            document.getElementById("inPrint1" + uniqueKey).style.display = 'block';           
            }
            document.forms[1].elements["txtQty" + uniqueKey].disabled = false;
        }
        else if (strValArr[2] == 'False')
        {
            document.getElementById("inPrint" + uniqueKey).style.display = 'none';
            document.getElementById("inPrint1" + uniqueKey).style.display = 'none';        
            document.forms[1].elements["txtQty" + uniqueKey].disabled = true;        
        }    
    }
}
function fadetext(num_itms){ 
if(hex>0) { //If color is not black yet
	hex-=11; // increase color darkness
	document.getElementById("ViewCart").style.color="rgb("+hex+","+hex+","+hex+")";
	document.getElementById("ViewCart").innerHTML= "View Cart ( " + num_itms + " )";
	setTimeout("fadetext(" + num_itms + ")",20); 
}
else
	hex=255 //reset hex value
}

//utility to invoke the info into the different page
function doPDFDownLoad(fileName, itemNo, brandName, pdfName)
{
  var url = "redir.asp?fileName=" + escape(fileName) + "&itemNo=" + escape(itemNo) + "&brandName=" + escape(brandName) + "&pdfName=" + escape(pdfName);
  window.open (url,"_blank","menubar=1,resizable=1,scrollbars=1,width=500,height=400"); 

}

//utility to invoke the info into the different page
function doLangDownLoad(fileName, itemID, itemNo, brandName,uniqueKey)
{
  var e = document.forms[1].elements["lang" + uniqueKey];
  var strSelected = e.options[e.selectedIndex].value;
  var arrSelected = strSelected.split(',');
  var lang_code = arrSelected[0];
  var pdfName = arrSelected[1];
  
  var url = "redir.asp?fileName=" + escape(fileName) + "&itemNo=" + escape(itemNo) + "&brandName=" + escape(brandName) + "&pdfName=" + escape(pdfName);
  window.open (url,"_blank","menubar=1,resizable=1,scrollbars=1,width=500,height=400"); 

}

//utility to invoke the info into the different page
function doSection(itemID, brandName, usrString,uniqueKey)
{
  if (document.forms[1].elements["lang" + uniqueKey] != null)
  {
  var e = document.forms[1].elements["lang" + uniqueKey];
  var strSelected = e.options[e.selectedIndex].value;
  var arrSelected = strSelected.split(',');
  var lang_code = arrSelected[0];
  var pdfName = arrSelected[1];
  }
  else
    lang_code = "en"
    
  var url = "section.asp?litID=" + escape(itemID) + "&brandName=" + escape(brandName) + "&lang=" + escape(lang_code) + usrString;
  document.location.href = url;

}

// Get QueryString Variables
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}


//===================================
function showLoadLayer() {
layer = document.getElementById("loadingLayer")
layer.style.visibility = "visible";
document.body.style.overflow="auto";
}

function hideLoadLayer()
{
layer = document.getElementById("loadingLayer")
layer.style.visibility = "hidden";
document.body.style.overflow="auto";
}  



function checkKeyHdr()
{
    if (window.event.keyCode == 13)
    {
        doSubmitHdr('search.asp')
    }
}

function checkKey()
{
    if (window.event.keyCode == 13)
    {
        doSubmit('search.asp')
    }
}


