function makeRequest(url, vid, enabledInventory, enabledEcommerce, enabledQtyDiscount, callForPricing, ourPriceText, baseURL, popUpStandardWindowWidth, popUpStandardWindowHeight, enableSquareFeetPerPallet, squareFeetPerPallete) 
{

    var http_request = false;
	document.body.style.cursor = 'progress';

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
            // See note below about this line
        }
    } 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) {
    	//posting the form because they don't support AJAX
        //alert('Giving up :( Cannot create an XMLHTTP instance');
        //return false;
        document.forms['productForm'].submit();
    }
    http_request.onreadystatechange = function() { processContents(http_request, vid ,enabledInventory, enabledEcommerce, enabledQtyDiscount, callForPricing, ourPriceText, baseURL, popUpStandardWindowWidth, popUpStandardWindowHeight, enableSquareFeetPerPallet, squareFeetPerPallete); document.body.style.cursor = 'auto'; };
    http_request.open('GET', url, true);
    http_request.send(null);

}

function notAvailable() {
   	//perhaps also post the form instead here
	//*** alert('This color/size option is currently unavailable.');

	if (document.getElementById("stock_div"))
		document.getElementById("stock_div").innerHTML = '<p class="stock_text">Unavailable</p>';
	if (document.getElementById("add_to_cart_div"))
		//document.getElementById("add_to_cart_div").innerHTML = '';
		document.getElementById("add_to_cart_div").style.display = 'none';
	//document.forms['productForm'].reset();
}

function processContents(http_request, vid, enabledInventory, enabledEcommerce, enabledQtyDiscount, callForPricing, ourPriceText, baseURL, popUpStandardWindowWidth, popUpStandardWindowHeight, enableSquareFeetPerPallet, squareFeetPerPallete)
{

    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
        	//alert(http_request.responseText);
        	//alert(http_request.getAllResponseHeaders());
            var xmldoc = http_request.responseXML;

            if (!xmldoc.getElementsByTagName('displayName').item(0)) return notAvailable();

			var displayName = xmldoc.getElementsByTagName('displayName').item(0).firstChild.data;
			var longDescription = (xmldoc.getElementsByTagName('longDescription').item(0).firstChild) ? xmldoc.getElementsByTagName('longDescription').item(0).firstChild.data : "";
			var listPrice = xmldoc.getElementsByTagName('listPrice').item(0).firstChild.data;
			var salePrice = xmldoc.getElementsByTagName('salePrice').item(0).firstChild.data;
			var regularPrice = xmldoc.getElementsByTagName('regularPrice').item(0).firstChild.data;
			var regularPriceLabel = xmldoc.getElementsByTagName('regularPriceLabel').item(0).firstChild.data;
			var mainImage = xmldoc.getElementsByTagName('mainImage').item(0).firstChild.data;
			var attributes = xmldoc.getElementsByTagName('attribute');
			var idVariation = xmldoc.getElementsByTagName('idVariation').item(0).firstChild.data;
			var inventory = xmldoc.getElementsByTagName('inventory').item(0).firstChild.data;
			var sku = (xmldoc.getElementsByTagName('sku').item(0).firstChild) ? xmldoc.getElementsByTagName('sku').item(0).firstChild.data : "";
			var qtyDiscount = xmldoc.getElementsByTagName('discount');
			var availabilityTime = (xmldoc.getElementsByTagName('availability').item(0).firstChild) ? xmldoc.getElementsByTagName('availability').item(0).firstChild.data : "";
			var showSale=false; //show sale price
			var showList=true; // show list price
			inventory=Number(inventory);
			listPrice=Number(listPrice);
			salePrice=Number(salePrice);
			regularPrice=Number(regularPrice);
			var tryiton = 0;
			var svid = 0;
			if((xmldoc.getElementsByTagName('tryItOn').item(0) && xmldoc.getElementsByTagName('tryItOn').item(0).firstChild) && (xmldoc.getElementsByTagName('sidVariation').item(0) && xmldoc.getElementsByTagName('sidVariation').item(0).firstChild)) {
				tryiton = xmldoc.getElementsByTagName('tryItOn').item(0).firstChild.data;
				svid = xmldoc.getElementsByTagName('sidVariation').item(0).firstChild.data;
			}
			var isRxable = 0;
			if(xmldoc.getElementsByTagName('isRxable').item(0) && xmldoc.getElementsByTagName('isRxable').item(0).firstChild)
				isRxable = xmldoc.getElementsByTagName('isRxable').item(0).firstChild.data;

			var highestPrice=listPrice; //normally this is highest
			var lowestPrice=regularPrice; //normally this is lowest
			
			getDefaultImage = 0;
			
			// set the elements on the page
			document.forms['productForm'].vID.value = idVariation;
			// document.mainImage.src = "#request.catalogLiveRelPath#imageLibrary/" + mainImage;
			if (document.mainImage) { 
			document.mainImage.src = mainImage;
			}
			document.getElementById("displayName").innerHTML = displayName;
			if (document.getElementById("sku")) 
			{
				if(sku.length > 0)
					document.getElementById("sku").innerHTML = sku;
				else
					document.getElementById("sku").innerHTML = "N/A";
			}
			if(document.getElementById("longDescription"))
			document.getElementById("longDescription").innerHTML = longDescription;
			
			// added id to breadcrumbs (#12628)
			if(document.getElementById("finalcrumb1"))
			document.getElementById("finalcrumb1").innerHTML = displayName;
			
			//calulate the highest price / lowest price and you save percent - note that user custom prices is not implemented here.. 6/15/2006
			if (regularPrice >= highestPrice) { //if list price was zero or less than regular price
				highestPrice=regularPrice;
				showList=false; //then we dont show list price
			}

			if ((salePrice > 0) && (salePrice < regularPrice)) {  //  sale price > 0 and is less than regular - so it is selling price..
				//then lowest price should be
				lowestPrice=salePrice;
				showSale=true;
				// if sale price, don't show list
				highestPrice=regularPrice;
				showList = false;
			}

			var percentDiscount=0; //default to no discount
			//alert(highestPrice + ' low= ' + lowestPrice);

			if (highestPrice > lowestPrice) {
				percentDiscount = (highestPrice - lowestPrice)/highestPrice * 100;
			}
			//default label values
			var listlabel="";
			var listpricetxt="";
			var oldreglabel="";
			var oldregpricetxt="";
			var reglabel="";
			var regpricetxt="";
			var salepricelabel="";
			var salepricetxt="";
			var percentdiscountlabel="";
			var percentdiscounttxt="";
			var cartonPrice="";	
			var showInSF=false;		
			
			if(enableSquareFeetPerPallet && squareFeetPerPallete > 0)
			{
				cartonPrice = currency(lowestPrice) + "/carton";
				showInSF = true;
				
				if (document.getElementById("priceCartonLabel"))
					document.getElementById("priceCartonLabel").style.display = "block";
				if (document.getElementById("priceCarton"))
					document.getElementById("priceCarton").style.display = "block";
										
			}
			else
			{
				if (document.getElementById("priceCartonLabel"))
					document.getElementById("priceCartonLabel").style.display = "none";
				if (document.getElementById("priceCarton"))
					document.getElementById("priceCarton").style.display = "none";
			}
			
			if (percentDiscount > 0) 
			{
				percentdiscountlabel="You Save:";
				percentdiscounttxt=Math.round(percentDiscount) + "%";
				document.getElementById("percentDiscountLabel").style.display = "block";
				document.getElementById("percentDiscount").style.display = "block";
			}
			else
			{
				document.getElementById("percentDiscountLabel").style.display = "none";
				document.getElementById("percentDiscount").style.display = "none";
			}
			
			if (showList) 
			{
				listlabel="List Price:";
				if(showInSF)
					listpricetxt=currency(listPrice/squareFeetPerPallete)+"/sf";
				else
					listpricetxt=currency(listPrice);	
				document.getElementById("listPriceLabel").style.display = "block";
				document.getElementById("listPrice").style.display = "block";
			}
			else
			{
				document.getElementById("listPriceLabel").style.display = "none";
				document.getElementById("listPrice").style.display = "none";
			}
			
			if (showSale) 
			{

				salepricelabel="Sale Price:";
				if(showInSF)
					salepricetxt=currency(salePrice/squareFeetPerPallete)+"/sf";
				else
					salepricetxt=currency(salePrice);
					
				//also show regular price with line thru it.
				if(showInSF)
					oldregpricetxt=currency(regularPrice/squareFeetPerPallete)+"/sf";
				else
					oldregpricetxt=currency(regularPrice);
					
				oldreglabel="Our Price";
				if(ourPriceText.length > 0)
					oldreglabel=ourPriceText + ":";
				
				document.getElementById("salePriceLabelOut").style.display = "block";
				document.getElementById("salePrice").style.display = "block";
				document.getElementById("oldregularPriceLabel").style.display = "block";
				document.getElementById("oldregularPrice").style.display = "block";
				document.getElementById("regularPriceLabel").style.display = "none";
				document.getElementById("regularPrice").style.display = "none";
			}
			else 
			{				
				// there is no sale so don't put line
				if(showInSF)
					regpricetxt=currency(regularPrice/squareFeetPerPallete)+"/sf";
				else
					regpricetxt=currency(regularPrice);
				
				reglabel=regularPriceLabel;
				document.getElementById("salePriceLabelOut").style.display = "none";
				document.getElementById("salePrice").style.display = "none";
				document.getElementById("oldregularPriceLabel").style.display = "none";
				document.getElementById("oldregularPrice").style.display = "none";
				document.getElementById("regularPriceLabel").style.display = "block";
				document.getElementById("regularPrice").style.display = "block";
			}
						
			
			//alert('salprice txt='+ salepricetxt + ' saleprice= ' + salePrice + ' showsale = ' +  showSale + ' percentdiscounttxt = ' + percentdiscounttxt + ' percentDiscount = ' + percentDiscount);
			if (document.getElementById("listPriceLabel"))
				document.getElementById("listPriceLabel").innerHTML =listlabel;
			if (document.getElementById("listPrice"))
				document.getElementById("listPrice").innerHTML = listpricetxt;
			if (document.getElementById("regularPriceLabel"))
				document.getElementById("regularPriceLabel").innerHTML = reglabel;
			if (document.getElementById("regularPrice"))
				document.getElementById("regularPrice").innerHTML =regpricetxt ;
			if (document.getElementById("percentDiscount"))
				document.getElementById("percentDiscount").innerHTML = percentdiscounttxt;
			if (document.getElementById("percentDiscountLabel"))
				document.getElementById("percentDiscountLabel").innerHTML = percentdiscountlabel;

			if (document.getElementById("oldregularPriceLabel"))
				document.getElementById("oldregularPriceLabel").innerHTML = oldreglabel;
			if (document.getElementById("oldregularPrice"))
				document.getElementById("oldregularPrice").innerHTML =oldregpricetxt ;

			if (document.getElementById("salePriceLabelOut"))
				document.getElementById("salePriceLabelOut").innerHTML = salepricelabel;
			if (document.getElementById("salePrice"))
				document.getElementById("salePrice").innerHTML =salepricetxt ;
			
			//if (document.getElementById("priceCartonLabel"))
				//document.getElementById("priceCartonLabel").innerHTML = salepricelabel;
			if (document.getElementById("priceCarton"))
				document.getElementById("priceCarton").innerHTML = cartonPrice;

			//end calculation
			
			if (document.getElementById("try_it_on") && tryiton > 0 && svid > 0) {
				var link = document.getElementById("try_it_on_link").innerHTML;
				//alert(document.location);
				//alert(link);
				//alert(link.indexOf("vid"));
				//alert(link.slice(0,link.indexOf("vid")+4) + svid + link.slice(link.indexOf('">')));
				//document.getElementById("try_it_on").innerHTML = '<a href="#">Try it on</a>';
				document.getElementById("try_it_on_link").innerHTML = link.slice(0,link.indexOf("vid")+4) + svid + link.slice(link.indexOf('">'));
				document.getElementById("try_it_on").style.display = "block";
			}
			else if (document.getElementById("try_it_on")) {
				document.getElementById("try_it_on").style.display = "none";
			}
			
			if (document.getElementById("add_sub_product") && isRxable > 0) {
				document.getElementById("add_sub_product").style.display = "block";
			}
			else if (document.getElementById("add_sub_product")) {
				document.getElementById("add_sub_product").style.display = "none";
			}

			var myAttribute = "";
			var attributeHTML = "";
			var myAttributeValue = "";
			for (i = 0; i < attributes.length; i++) {
				myAttribute = attributes.item(i).getElementsByTagName('name').item(0).firstChild.data;
				myAttributeValue = attributes.item(i).getElementsByTagName('value').item(0).firstChild.data;
				myAttributeType = attributes.item(i).getElementsByTagName('type').item(0).firstChild.data;
				if (myAttribute == 'Style Number') {
					document.getElementById("styleNumber").innerHTML = myAttributeValue;
				}
				if (myAttributeType == 'image') {
					myAttributeValue = '<a class="size_chart" href="/imageViewer.cfm?i=/images/catalog/live/attribute/'+myAttributeValue+'" onclick="window.open(this.href,\'imager\',\'resizable=1,width=680,height=550\'); return false;" target="_blank">View</a>';
				}
				attributeHTML = attributeHTML + "<li><span><strong>" + myAttribute + ":</strong> " + myAttributeValue + "</span></li>";

			}
			if (document.getElementById("attributes"))
				document.getElementById("attributes").innerHTML = attributeHTML;
		
		if(enabledQtyDiscount)
		{
			var myQty = "";
			var myPrice = "";
			var qtyDiscountHTML = "";

			for (i = 0; i < qtyDiscount.length; i++) {
				myQty = qtyDiscount.item(i).getElementsByTagName('qty').item(0).firstChild.data;
				myPrice = qtyDiscount.item(i).getElementsByTagName('price').item(0).firstChild.data;
				qtyDiscountHTML = qtyDiscountHTML + "<tr><td><strong>Buy at least " + myQty + ":</strong></td><td>" + currency(myPrice) + "</td></tr>";
			}
			if (document.getElementById("qtyDiscount"))
				document.getElementById("qtyDiscount").innerHTML = '<table class="qtyDiscount">' + qtyDiscountHTML + '</table>';
		}
		
		if(enabledInventory && enabledEcommerce)
		{			
			if (document.getElementById("stock_div")) {
				if (inventory < 1) {
					//***alert('This color/size option is out of stock.');
					document.getElementById("stock_div").innerHTML = '<p class="stock_text">Out of Stock</p><p><a href="#" class="stock_link" onclick="openPopUp(\'' + baseURL + 'a/popup.notifyStock.show/vid/' + vid + '\',' + popUpStandardWindowWidth + ',' + popUpStandardWindowHeight + ')" onkeypress="openPopUp(\'' + baseURL + 'a/popup.notifyStock.show/vid/' + vid + '\',' + popUpStandardWindowWidth + ',' + popUpStandardWindowHeight + ')">Notify me when this product becomes available</a></p>';
					if(document.getElementById("qtyboxdiv"))
						document.getElementById("qtyboxdiv").style.display = 'none';

				} 
				else
				{
					document.getElementById("stock_div").innerHTML = '';
					if(document.getElementById("qtyboxdiv"))
						document.getElementById("qtyboxdiv").style.display = 'block';
				}
			}
			
			if(!callForPricing)
			{
				if (document.getElementById("add_to_cart_div")) {
					if (inventory < 1)
						document.getElementById("add_to_cart_div").style.display = 'none';
					else
						document.getElementById("add_to_cart_div").style.display = 'block';
				}
			}
			
			if(document.getElementById("availability"))
			{
				document.getElementById("availability").style.display = 'block';
				if(document.getElementById("avail_time"))
					document.getElementById("avail_time").innerHTML = availabilityTime;
			}
		}
		else
		{		
			if (inventory < 1) {
				//***alert('This product option is not available.');
				if (document.getElementById("stock_div"))
					document.getElementById("stock_div").innerHTML = '<p class="stock_text">Unavailable</p>';
				if (document.getElementById("add_to_cart_div"))
					document.getElementById("add_to_cart_div").style.display = 'none';
				if(document.getElementById("qtyboxdiv"))
					document.getElementById("qtyboxdiv").style.display = 'none';
			
			}
			else {
				if (document.getElementById("stock_div"))
					document.getElementById("stock_div").innerHTML = '';

				if (document.getElementById("add_to_cart_div"))
					document.getElementById("add_to_cart_div").style.display = 'block';
				
				if(document.getElementById("qtyboxdiv"))
					document.getElementById("qtyboxdiv").style.display = 'block';	
			}
			
			if(document.getElementById("availability"))
				document.getElementById("availability").style.display = 'none';
				
		}

        } else {
        	return notAvailable();
        }
    }
}

function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function updateVariation(vid, prodID, enabledInventory, enabledEcommerce, enabledQtyDiscount, callForPricing, ourPriceText, baseURL, popUpStandardWindowWidth, popUpStandardWindowHeight) 
{
	var myArray = document.forms['productForm'].attributeValueID;
	var myList = "";
	for (i = 0; i < myArray.length; i++) {
		myList = myList +  myArray[i].value + ",";
	}
		
	if (myList.search(/090909/) == -1) {
		makeRequest("/get.cfm?" + "vid=" + vid + "&attributeValueID=" + myList + "&ProdID=" + prodID, vid, enabledInventory, enabledEcommerce, enabledQtyDiscount, callForPricing, ourPriceText, baseURL, popUpStandardWindowWidth, popUpStandardWindowHeight);
	}
}

function updateVariationNew(attributeID, attributeIDValue, vid, prodID, enabledInventory, enabledEcommerce, enabledQtyDiscount, callForPricing, ourPriceText, baseURL, popUpStandardWindowWidth, popUpStandardWindowHeight, enableSquareFeetPerPallet, squareFeetPerPallete, reload) 
{	
   if (reload == 0)
	showLightbox();
	if (attributeIDValue == "")
		attributeIDValue = document.getElementById("idattr" + attributeID).value;
	
	if (typeof enableSquareFeetPerPallet == "undefined") 
		enableSquareFeetPerPallet = false;
  	
	if (typeof squareFeetPerPallete == "undefined")	
    	squareFeetPerPallete = 0;
		
	if (document.getElementById("hidden" + attributeID)) document.getElementById("hidden" + attributeID).value = attributeIDValue;
	if (document.getElementById("idattr" + attributeID)) document.getElementById("idattr" + attributeID).value = attributeIDValue;
	updateSwatches(attributeID,attributeIDValue);
	var myArray = document.forms['productForm'].attributeValueID;
	var myList = "";
	for (i = 0; i < myArray.length; i++) {
		myList = myList +  myArray[i].value + ",";
	}
	if (!myArray.length) {
		myList = myArray.value;
	}
	if (reload > 0) {
		if (document.getElementById("reload")) document.getElementById("reload").value = 1;
		document.forms['productForm'].submit();
	}
	else makeRequest("/get.cfm?" + "vid=" + vid + "&attributeValueID=" + myList + "&ProdID=" + prodID, vid, enabledInventory, enabledEcommerce, enabledQtyDiscount, callForPricing, ourPriceText, baseURL, popUpStandardWindowWidth, popUpStandardWindowHeight, enableSquareFeetPerPallet, squareFeetPerPallete);
	hideLightbox();
}

function updateNoneVariationOption(attributeID, attributeValueID)
{
	if (attributeValueID == "")
		attributeValueID = document.getElementById("idattr" + attributeID).value;
	if (document.getElementById("hidden" + attributeID)) document.getElementById("hidden" + attributeID).value = attributeValueID;
	if (document.getElementById("idattr" + attributeID)) document.getElementById("idattr" + attributeID).value = attributeValueID;
	updateSwatches(attributeID,attributeValueID);
}

function updateSwatches(attributeID, attributeIDValue)
{
	var swatchArray = document.getElementsByName("swatchImage" + attributeID);
	var swatchNameArray = document.getElementsByName("swatchName" + attributeID.replace(/-/g,'_'));
	var swatchInfix = "";
	if (swatchArray.length) {
		swatchInfix = "image";
	}
	else {
		swatchArray = document.getElementsByName("swatchHex" + attributeID);
		if (swatchArray.length) swatchInfix = "hex";
	}
	var swatchList = "";
	for (i = 0; i < swatchArray.length; i++) {
		swatchList = swatchList + swatchArray[i].id + ",";
		if (swatchArray[i].id == "swatch" + attributeIDValue) {
			document.getElementById(swatchArray[i].id).className = "swatch_" + swatchInfix + " swatch_" + swatchInfix + "_active";
		}
		else {
			document.getElementById(swatchArray[i].id).className = "swatch_" + swatchInfix;
		}
		if (swatchNameArray[i].value == "swatchName" + attributeIDValue) {
			document.getElementById(swatchNameArray[i].value).className = "swatch_name_display swatch_name_display_active";
		}
		else {
			document.getElementById(swatchNameArray[i].value).className = "swatch_name_display";
		}
	}
}

function currency( num )
{
   var prefix = "$";
   var suffix = "";
   if ( num < 0 )
   {
       prefix = "($";
       suffix = ")";
       num = - num;
   }
       var temp = Math.round( num * 100.0 ); // convert to pennies!
       if ( temp < 10 ) return prefix + "0.0" + temp + suffix;
       if ( temp < 100 ) return prefix + "0." + temp + suffix;
       temp = prefix + temp; // convert to string!
       return temp.substring(0,temp.length-2) + "." + temp.substring(temp.length-2) + suffix;
}





