function makeRequest(url, vid, enabledInventory, enabledEcommerce, enabledQtyDiscount, callForPricing, ourPriceText, baseURL, popUpStandardWindowWidth, popUpStandardWindowHeight) 
{

    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); 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 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)
{

    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 showSale=false; //show sale price
			var showList=true; // show list price
			inventory=Number(inventory);
			listPrice=Number(listPrice);
			salePrice=Number(salePrice);
			regularPrice=Number(regularPrice);

			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;
			document.mainImage.src = mainImage;
			document.getElementById("displayName").innerHTML = displayName;
			if (document.getElementById("sku")) {document.getElementById("sku").innerHTML = sku;}
			document.getElementById("longDescription").innerHTML = longDescription;

			//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;
				//alert('ehello');
			}
			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="";			
			
			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";
			}
			// changed this because showList defaults to true, and we want to turn it off
			if(listPrice == 0 || showSale)
				showList = false;
			
			if (showList) 
			{
				listlabel="List Price:";
				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:";
				salepricetxt=currency(salePrice);
				//also show regular price with line thru it.
				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
				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 ;


			//end calculation

			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="javascript:void(0)" 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>';
				} else
					document.getElementById("stock_div").innerHTML = '';
			}
			
			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';
				}
			}
		}
		else
		{		
			//alert(inventory);
			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';
			}
			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';
			}

		//</cfif>
		}



        } 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 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;
}
