isIE=document.all;
isNN=!document.all;
isHot=false;

function ddInit(e){
  //topDog=isIE ? "BODY" : "HTML";
  whichDog=isIE ? document.all.thePopupLayer : document.getElementById("thePopupLayer");  
  //hotDog=isIE ? event.srcElement : e.target;  
  //while (hotDog.id!="titleBar"&&hotDog.tagName!=topDog){
  //  hotDog=isIE ? hotDog.parentElement : hotDog.parentNode;
  //}  
  //if (hotDog.id=="titleBar"){
  //  offsetx=isIE ? event.clientX : e.clientX;
  //  offsety=isIE ? event.clientY : e.clientY;
  //  nowX=parseInt(whichDog.style.left);
  //  nowY=parseInt(whichDog.style.top);
  //  ddEnabled=true;
  //  document.onmousemove=dd;
  //}
}

function dd(e){
  if (!ddEnabled) return;
  whichDog.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx; 
  whichDog.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety;
  return false;  
}

function hideMe(){
  if (isIE||isNN) whichDog.style.visibility="hidden";
}

function showMe(){
  if (isIE||isNN) whichDog.style.visibility="visible";
}

document.onmousedown=ddInit;
document.onmouseup=Function("ddEnabled=false");

function checkTerms(ctl)
{
	var hidtermsOK = extractName("hidtermsOK");
	if (!ctl.checked) {
		alert("You must agree to the terms and conditions before placing an order.");
		hidtermsOK.value = "false";
		ctl.focus();
		return false
	}
	else {
		hidtermsOK.value = "true";
		return true;
	}
}

function switchCategory(vendorID, productLineID, subCategoryID, applicationPath, productListingPath, tabName)
{
	location.href=applicationPath + productListingPath + "?vendorID=" + vendorID + "&productLineID=" + productLineID + "&subCategoryID=" + subCategoryID + "&Tab=" + tabName;
}

function extractName(name)
{
	for(i=0; i<= document.forms[0].elements.length; i++)
	{
		var objField = document.forms[0].elements[i];
		if (objField.id.indexOf(name) > 0)
			return objField;
	}
	return "";
}
function AddToCartJS(button, prefix, qtyElName, productID)
{
	var qtyEl = document.getElementById(qtyElName);
	if (qtyEl)
	{		
		if (!VerifyTextField(qtyEl, "Please enter the quantity!", /^\d+$/, "Please enter a valid quantity!", true))
		return false;
		
		if (!VerifyTextFieldZeroValue(qtyEl, "Please enter a valid quantity!", true))
		return false;
			
		var hidProdID = extractName("hidProductID");
		var hidProdQty = extractName("hidProductQty");
		
		if (hidProdID && hidProdQty)
		{
			hidProdID.value = productID;
			hidProdQty.value = qtyEl.value;	
			button.form.submit();
		}
		else
		{
			if (!hidProdID) alert("Product ID field is missing");
			if (!hidProdQty) alert("Product Qty is missing");				
		}
	}
}

function ContinueShoppingJS(docID, sectionID, categoryID)
{
	window.location.href = "/display/router.aspx?DocID=" + docID + "&SiteSection=" + sectionID + "&catid=" + categoryID;
}

function ContinueEllsworthShoppingJS(docName)
{
	window.location.href = docName;
}

function RemoveShoppingItemJS(button, prefix, productID, productName)
{
	//var hidProdID = document.getElementById(prefix + "hidProductID");
	//var hidAction = document.getElementById(prefix + "hidAction");
	var hidProdID = extractName("hidProductID");
	var hidProdName = extractName("hidProductName");
	var hidAction = extractName("hidAction");
	if (hidProdID && hidAction)
	{
		hidProdName.value = productName;
		hidProdID.value = productID;
		hidAction.value = "remove";
		button.form.submit();
	}
	else
		alert("Action field is missing");
}

function UpdateShoppingCartJS(button, prefix, cntItems)
{
	//var hidProdID = document.getElementById(prefix + "hidProductID");
	//var hidAction = document.getElementById(prefix + "hidAction");
	var hidProdID = extractName("hidProductID");
	var hidAction = extractName("hidAction");
	if (hidProdID && hidAction)
	{
		hidProdID.value = CollectProductIDs(cntItems);
		if (hidProdID.value != "")
		{
			hidAction.value = "updateshoppingcart";
			button.form.submit();
		}
	}
	else
		alert("Action field is missing");	
}

function PlaceOrderJS(button, prefix)
{
	//var hidAction = document.getElementById(prefix + "hidAction");
	var hidCount = extractName("hidCount");
	var hidProdID = extractName("hidProductID");
	var hidAction = extractName("hidAction");
	if (hidAction)
	{
		hidProdID.value = CollectProductIDs(hidCount.value);
		hidAction.value = "placeorder";
		button.form.submit();
	}
	else
		alert("Action field is missing");
}

function AddPromoCodeJS(button, prefix, promoElName)
{
	if (!VerifyTextField(document.getElementById(prefix + promoElName), "Please enter the Promo Code!", null, "", false))
		return false;
	
	//var hidAction = document.getElementById(prefix + "hidAction");	
	var hidAction = extractName("hidAction");	
	if (hidAction)
	{
		hidAction.value = "addpromo";
		button.form.submit();
	}
	else
		alert("Action field is missing");
}

function UpdateShippingJS(button, prefix, promoElName)
{
	//var hidAction = document.getElementById(prefix + "hidAction");	
	var hidAction = extractName("hidAction");	
	if (hidAction)
	{
		hidAction.value = "updateshipping";
		button.form.submit();
	}
	else
		alert("Action field is missing");
}

function ShippingAndTaxCalculatorJS(button, prefix)
{
	if (!VerifyTextField(document.getElementById(prefix + "txbShipToZipCode"), "Please enter the Shipping Zip code!", /(^\d{5}$)|(^\d{5}-\d{4}$)/, "Please enter a valid 5 digit Shipping Zip code (99999).", false))
		return false;
		
	//var hidAction = document.getElementById(prefix + "hidAction");
	var hidAction = extractName("hidAction");
	if (hidAction)
	{
		hidAction.value = "getshipping";		
		button.form.submit();
	}
	else
		alert("Action field is missing");	
}

function ResetActionJS(rdo, prefix)
{
	//var hidAction = document.getElementById(prefix + "hidAction");
	var hidAction = extractName("hidAction");
	if (hidAction)
	{
		hidAction.value = "";
	}
}

function VerifyStep1JS(button, prefix)
{
	//if (document.getElementById(prefix + "rdoMasterCard").checked == false && document.getElementById(prefix + "rdoVisa").checked == false && 
	//	document.getElementById(prefix + "rdoAmex").checked == false && document.getElementById(prefix + "rdoDiscover").checked == false && 
	//	document.getElementById(prefix + "rdoPayPal").checked == false)
	var rdoMasterCard = extractName("rdoMasterCard");	
	var rdoVisa = extractName("rdoVisa");	
	var rdoAmex = extractName("rdoAmex");	
	var rdoDiscover = extractName("rdoDiscover");	
	var rdoPayPal = extractName("rdoPayPal");	
	if (rdoMasterCard.checked == false && rdoVisa.checked == false && 
		rdoAmex.checked == false && rdoDiscover.checked == false && 
		rdoPayPal.checked == false)
	{	
		alert("Please select card type!");
		return false;
	}
	
	//if (document.getElementById(prefix + "rdoPayPal").checked == true)
	if (rdoPayPal.checked == true)
	{
		var txbShippingFName = extractName("txbShippingFName");	
		var txbShippingLName = extractName("txbShippingLName");	
		var txbShippingAddress1 = extractName("txbShippingAddress1");	
		var txbShippingCity = extractName("txbShippingCity");	
		var ddlShippingState = extractName("ddlShippingState");	
		var txbShippingZip = extractName("txbShippingZip");	
		var txbShippingPhone = extractName("txbShippingPhone");	
//		if (!VerifyTextField(document.getElementById(prefix + "txbShippingFName"), "Please enter the Shipping First Name!", null, "", false))
//			return false;
//		if (!VerifyTextField(document.getElementById(prefix + "txbShippingLName"), "Please enter the Shipping Last Name!", null, "", false))
//			return false;
//		if (!VerifyTextField(document.getElementById(prefix + "txbShippingAddress1"), "Please enter the Shipping Address!", null, "", false))
//			return false;
//		if (!VerifyTextField(document.getElementById(prefix + "txbShippingCity"), "Please enter the Shipping City!", null, "", false))
//			return false;
//		if (!VerifyDropDown(document.getElementById(prefix + "ddlShippingState"), "Please select the Shipping State!"))
//			return false;
//		if (!VerifyTextField(document.getElementById(prefix + "txbShippingZip"), "Please enter the Shipping Zip code!", /(^\d{5}$)|(^\d{5}-\d{4}$)/, "Please enter valid Shipping Zip code (99999 or 99999-9999)!", false))
//			return false;
//		if (!VerifyTextField(document.getElementById(prefix + "txbShippingPhone"), "Please enter the Shipping Phone!", /(^\d{3}-\d{3}-\d{4}$)/, "Please enter valid Shipping Phone (999-999-9999)!", false))
//			return false;
		if (!VerifyTextField(txbShippingFName, "Please enter the Shipping First Name.", null, "", false))
			return false;
		if (!VerifyTextField(txbShippingLName, "Please enter the Shipping Last Name.", null, "", false))
			return false;
		if (!VerifyTextField(txbShippingAddress1, "Please enter the Shipping Address.", null, "", false))
			return false;
		if (!VerifyTextField(txbShippingCity, "Please enter the Shipping City.", null, "", false))
			return false;
		if (!VerifyDropDown(ddlShippingState, "Please select the Shipping State."))
			return false;
		if (!VerifyTextField(txbShippingZip, "Please enter the Shipping Zip code.", /(^\d{5})/, "Please enter valid Shipping Zip code (99999).", false))
			return false;
		if (!VerifyTextField(txbShippingPhone, "Please enter the Shipping Phone.", /(^\d{3}-\d{3}-\d{4}$)/, "Please enter valid Shipping Phone (555-555-5555).", false))
			return false;
	}
	else
	{
		//var txbCardHolderName = extractName("txbCardHolderName");	
		var txbCardNumber = extractName("txbCardNumber");	
		var ddlExpYear = extractName("ddlExpYear");	
		var ddlExpMonth = extractName("ddlExpMonth");	
		var txbCardID = extractName("txbCardID");	
		var txbBillingFName = extractName("txbBillingFName");	
		var txbBillingLName = extractName("txbBillingLName");	
		var txbBillingAddress1 = extractName("txbBillingAddress1");	
		var txbBillingCity = extractName("txbBillingCity");	
		var ddlBillingState = extractName("ddlBillingState");	
		var txbBillingZip = extractName("txbBillingZip");	
		var txbBillingPhone = extractName("txbBillingPhone");	
		var txbShippingFName = extractName("txbShippingFName");	
		var txbShippingLName = extractName("txbShippingLName");	
		var txbShippingAddress1 = extractName("txbShippingAddress1");	
		var txbShippingCity = extractName("txbShippingCity");	
		var ddlShippingState = extractName("ddlShippingState");	
		var txbShippingZip = extractName("txbShippingZip");	
		var txbShippingPhone = extractName("txbShippingPhone");	
		//if (!VerifyTextField(txbCardHolderName, "Please enter the Name (as it appears on card)!", null, "", false))
		//	return false;
		if (!VerifyTextField(txbCardNumber, "Please enter the Card Number.", /[0-9-]$/, "Please enter valid Card Number.", false))
			return false;
		if (!VerifyDropDown(ddlExpMonth, "Please select the Expiration Month."))
			return false;
		if (!VerifyDropDown(ddlExpYear, "Please select the Expiration Year."))
			return false;
		if (!VerifyTextField(txbCardID, "Please enter the Card ID!", /(^\d{3,4}$)/, "Please enter valid Card ID.", false))
			return false;
		if (!VerifyTextField(txbBillingFName, "Please enter the Billing First Name.", null, "", false))
			return false;
		if (!VerifyTextField(txbBillingLName, "Please enter the Billing Last Name.", null, "", false))
			return false;
		if (!VerifyTextField(txbBillingAddress1, "Please enter the Billing Address.", null, "", false))
			return false;
		if (!VerifyTextField(txbBillingCity, "Please enter the Billing City.", null, "", false))
			return false;
		if (!VerifyDropDown(ddlBillingState, "Please select the Billing State."))
			return false;
		if (!VerifyTextField(txbBillingZip, "Please enter the Billing Zip code.", /(^\d{5})/, "Please enter valid Billing Zip code (99999).", false))
			return false;
		if (!VerifyTextField(txbBillingPhone, "Please enter the Billing Phone.",  /(^\d{3}-\d{3}-\d{4}$)/, "Please enter valid Billing Phone (555-555-5555).", false))
			return false;
		//if (!VerifyTextField(document.getElementById(prefix + "txbCardHolderName"), "Please enter the Name (as it appears on card)!", null, "", false))
		//	return false;
		//if (!VerifyTextField(document.getElementById(prefix + "txbCardNumber"), "Please enter the Card Number!", /(^\d{13,16}$)/, "Please enter valid Card Number!", false))
		//	return false;
		//if (!VerifyDropDown(document.getElementById(prefix + "ddlExpMonth"), "Please select the Expiration Month!"))
		//	return false;
		//if (!VerifyDropDown(document.getElementById(prefix + "ddlExpYear"), "Please select the Expiration Year!"))
		//	return false;
		//if (!VerifyTextField(document.getElementById(prefix + "txbCardID"), "Please enter the Card ID!", /(^\d{3,4}$)/, "Please enter valid Card ID!", false))
		//	return false;
		//if (!VerifyTextField(document.getElementById(prefix + "txbBillingFName"), "Please enter the Billing First Name!", null, "", false))
		//	return false;
		//if (!VerifyTextField(document.getElementById(prefix + "txbBillingLName"), "Please enter the Billing Last Name!", null, "", false))
		//	return false;
		//if (!VerifyTextField(document.getElementById(prefix + "txbBillingAddress1"), "Please enter the Billing Address!", null, "", false))
		//	return false;
		//if (!VerifyTextField(document.getElementById(prefix + "txbBillingCity"), "Please enter the Billing City!", null, "", false))
		//	return false;
		//if (!VerifyDropDown(document.getElementById(prefix + "ddlBillingState"), "Please select the Billing State!"))
		//	return false;
		if (txbShippingAddress1.value != "" && txbShippingCity.value != "")
		{
			if (!VerifyTextField(txbShippingFName, "Please enter the Shipping First Name.", null, "", false))
				return false;
			if (!VerifyTextField(txbShippingLName, "Please enter the Shipping Last Name.", null, "", false))
				return false;
			if (!VerifyTextField(txbShippingZip, "Please enter the Shipping Zip code.", /(^\d{5})/, "Please enter valid Shipping Zip code (99999).", false))
				return false;
			if (!VerifyTextField(txbShippingPhone, "Please enter the Shipping Phone.",  /(^\d{3}-\d{3}-\d{4}$)/, "Please enter valid Billing Phone (999-999-9999)!", false))
				return false;

		}
		if (txbShippingFName.value != "" && txbShippingLName.value != "")
		{
			if (!VerifyTextField(txbShippingAddress1, "Please enter the Shipping Address.", null, "", false))
				return false;
			if (!VerifyTextField(txbShippingCity, "Please enter the Shipping City.", null, "", false))
				return false;
			if (!VerifyDropDown(ddlShippingState, "Please select the Shipping State."))
				return false;
		}
	}
	//var hidAction = document.getElementById(prefix + "hidAction");
	var hidAction = extractName("hidAction");
	if (hidAction)
	{
		hidAction.value = "next";
		button.form.submit();
	}
	else
		alert("Action field is missing");

}

function VerifyTextFieldZeroValue(el, msg, reset)
{
	if (el)
	{
		var value = TrimSpaces(el); 
		if (value == "0")
		{
			alert(msg);
			if (reset) el.value = "";
			el.focus();
			return false;
		}
	}
	return true;
}

function VerifyTextField(el, msg, regexp, regexpMsg, reset)
{
	if (el)
	{
		var value = TrimSpaces(el); 
		if (value == "")
		{
			alert(msg);
			el.focus();
			return false;
		}
		else if (regexp != null && !value.match( regexp ))
		{                   
			alert(regexpMsg);
			if (reset) el.value = "";
			el.focus();
			return false;
		}
	}
	return true;
}

function VerifyDropDown(el, msg)
{
	if (el && TrimString(el.options[el.selectedIndex].value) == "")
	{
		alert(msg);
		el.focus();
		return false;
	}
	return true;
}

function CollectProductIDs(cntItems)
{	
	var el, elName;
	var prodID_Qty = "";
	var qty = 0; 
	var innerCount = 0;
	
	var elements = document.getElementsByTagName("input");
    for (i=0; i<elements.length; i++)
	{
		el = elements[i];
		if (el.type == 'text')
		{
			elName = el.name;
			if (elName.indexOf("prodQty_") == 0)
			{
				if (!VerifyTextField(el, "Please enter the valid!", /^\d+$/, "Please enter a valid quantity!", true))
				return "";
				if (!VerifyTextFieldZeroValue(el, "Please enter a valid quantity!", true))
				return "";
		
				innerCount += 1;
				qty = el.value;
				if (cntItems == 1)
				{
					prodID_Qty = elName.replace("prodQty_", "") + "_" + qty;
					break;
				}
				else
				{
					if (innerCount == 1)
						prodID_Qty = elName.replace("prodQty_", "") + "_" + qty;
					else
						prodID_Qty += ("|" + (elName.replace("prodQty_", "") + "_" + qty));				
				}
			}
		}
	}
	return prodID_Qty;	
}

// Trim the spaces from an Element
function TrimSpaces(el)
{
	var s = el.value;
	s = s.replace(/^\s+|\s+$/g,'');				// Removes leading and trailling spaces
	s = s.replace(/\s+/g,'\x20');				// Replaces multiple spaces with one space
	el.value = s;
		
	return (s);
}

// Trim the spaces from a string
function TrimString(s)
{
	if (s.length > 0)
	{
		s = s.replace(/^\s+|\s+$/g,'');				// Removes leading and trailling spaces
		s = s.replace(/\s+/g,'\x20');				// Replaces multiple spaces with one space
	}	
	return (s);
}

function PopupPic(sPicURL)
{ 
     window.open("store_ProductPopup.htm?" + sPicURL, "", "resizable=1,HEIGHT=200,WIDTH=200"); 
} 
function editMiniCart_click(docID, siteSection, secureLink)
{
	window.location.href=(secureLink + "display/router.aspx?docid=" + docID + "&SiteSection=" + siteSection);
}
function myAccountMiniCart_click(docID, siteSection, secureLink)
{
	window.location.href=(secureLink + "display/router.aspx?docid=" + docID + "&MyAccount=1&SiteSection=" + siteSection);
}
function doLogoff()
{
	var currentUrl = window.location.href;
	window.location.href=(currentUrl + "&Logoff=1");
}
function clickButton(e, button){ 
      var bt = button; 
      if (typeof bt == 'object'){ 
            if(navigator.appName.indexOf("Netscape")>(-1)){ 
                  if (e.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
            if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){ 
                  if (event.keyCode == 13){ 
                        bt.click(); 
                        return false; 
                  } 
            } 
      } 
} 
function setSameBilling(oControl) {
	if (oControl.value != "") {
		var hidSameBilling = extractName("hidSameBilling");
		hidSameBilling.value="0";
	}
}
function clearStatus() {
	var statusLabel = extractName("statusLabel");
	statusLabel.visibility = 'hide';
	var messageLabel = extractName("messageLabel");
	messageLabel.visibility = 'hide';
}
function adjustPrice(qty, price1, price2, price3, rangeLow, rangeHigh) {
		var price = price1;
		if (parseInt(qty) >= parseInt(rangeLow) && parseInt(qty) <= parseInt(rangeHigh)) {
			price = price2;
		}
		else if(parseInt(qty) > parseInt(rangeHigh)){
			price = price3;
		}		
		document.getElementById("unitPrice").innerHTML = formatCurrency(price);
		document.getElementById("totalPrice").innerHTML = formatCurrency((qty * price));
}
	
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
function addToEllsworthCart(productID, qty, checkCofC)
{
	if (qty)
	{		
		if (!VerifyTextField(qty, "Please enter the quantity!", /^\d+$/, "Please enter a valid quantity!", true))
		return false;
		
		if (!VerifyTextFieldZeroValue(qty, "Please enter a valid quantity!", true))
		return false;
			
		var hidProdID = extractName("hidProductID");
		var hidProdQty = extractName("hidProductQty");
		var hidCofC = extractName("hidCofC");
		
		if (hidProdID && hidProdQty)
		{
			hidProdID.value = productID;
			hidProdQty.value = qty.value;
			if(checkCofC){
				if(checkCofC.checked) {
					hidCofC.value = "1";
				}
				else {
					hidCofC.value = "0";
				}	
			}
			document.routerForm.submit();
		}
		else
		{

			if (!hidProdID) alert("Product ID field is missing");
			if (!hidProdQty) alert("Product Qty is missing");				
		}
	}
}

function dispProductTab()
{
	if (document.getElementById('prodTabContainer') != null)
	{
		document.getElementById('prodTabContainer').style.display="block";
		document.getElementById('vendTabContainer').style.display="none";
		document.getElementById('prodheader').className="on";
		document.getElementById('vendheader').className="";
	}
}

function dispVendorTab()
{
	if (document.getElementById('vendTabContainer') != null)
	{
		document.getElementById('vendTabContainer').style.display="block";
		document.getElementById('prodTabContainer').style.display="none";
		document.getElementById('vendheader').className="on";
		document.getElementById('prodheader').className="";
	}
}

function getStringVar(st){ 
  var temp = self.document.location.search; 
  if(temp.indexOf(st) >= 0){ 
    temp = temp.substring((temp.indexOf(st)+(st.length+1)), temp.length); 
    temp = temp.substring(0, (((temp.indexOf('&') >=0)?temp.indexOf('&'):temp.length))); 
  }else{ 
    temp = ''; 
  } 
  return temp; 
} 

