
function updateVariationProductList(prodID, vid, enabledInventory, callForPricing, currentID, suffix, ourPriceText) 
{
	var myArray = document.forms['ProductForm' + suffix]['attributeValueID' + suffix + '_' + currentID];
	var myList = "";
	for (i = 0; i < myArray.length; i++) {
		myList = myList +  myArray[i].value + ",";
	}
		
	makeRequestProductList("/get.cfm?vid=" + vid + "&attributeValueID=" + myList + "&ProdID=" + prodID, vid, currentID, enabledInventory, callForPricing, currentID, suffix, ourPriceText);
}


// UPDATE VARIATION INFO FOR PRODUCT LISTING PAGES
function updateVarProductListing(prodID, vid, enabledInventory, callForPricing, idAttribute, idAttributeValue, ourPriceText, prodItem, catID,selAction) 
{
	makeRequestVarProductList("/get.cfm?vid=" + vid + "&ProdID=" + prodID, vid, enabledInventory, callForPricing, ourPriceText, prodItem, catID,selAction);
}

function makeRequestVarProductList(url, vid, enabledInventory, callForPricing, ourPriceText,prodItem, catID) 
{
	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) {}
        }
    }

	http_request.onreadystatechange = function() { processProductListContents(http_request, vid, enabledInventory, callForPricing, ourPriceText,prodItem, catID); document.body.style.cursor = 'auto'; };
    http_request.open('GET', url, true);
    http_request.send(null);
}


function processProductListContents(http_request, vid, enabledInventory, callForPricing, ourPriceText,prodItem, catID)
{
	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 notAvailableUpSell(currentID);	
        	
        	var displayName = xmldoc.getElementsByTagName('displayName').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 mainImage = xmldoc.getElementsByTagName('mainImage').item(0).firstChild.data;
			var idVariation = xmldoc.getElementsByTagName('idVariation').item(0).firstChild.data;
			var inventory = xmldoc.getElementsByTagName('inventory').item(0).firstChild.data;
			var regularPriceLabel = xmldoc.getElementsByTagName('regularPriceLabel').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 highestPrice=listPrice; //normally this is highest
			var lowestPrice=regularPrice; //normally this is lowest

			//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="";
			
			
			
			if (percentDiscount > 0) {
				percentdiscountlabel="You Save:";
				percentdiscounttxt=Math.round(percentDiscount) + "%";
				document.getElementById('percentDiscount' +  prodItem).style.display = "block";
				document.getElementById('percentDiscountLabel' + prodItem).style.display = "block";
			}
			else
			{
				document.getElementById('percentDiscount' + prodItem).style.display = "none";			
				document.getElementById('percentDiscountLabel' + prodItem).style.display = "none";
			}

			if (showList) {
				listlabel="List Price:";
				listpricetxt=currencyProductList(listPrice);
				document.getElementById('listPriceLabel' + prodItem).style.display="block";
				document.getElementById('listPrice' + prodItem).style.display="block";
			}
			else
			{
				document.getElementById('listPriceLabel' + prodItem).style.display="none";
				document.getElementById('listPrice' + prodItem).style.display="none";			
				
			}
			
			if (showSale) {
				salepricelabel="Sale Price:";
				salepricetxt=currencyProductList(salePrice);
				//also show regular price with line thru it.
				oldregpricetxt=currencyProductList(regularPrice);
				
				//alert('salePriceLabelOut' + suffix + currentID);				
				document.getElementById('salePriceLabelOut' + prodItem).style.display="block";
				//alert('salePrice' + suffix + currentID);
				document.getElementById('salePrice' + prodItem ).style.display="block";
				document.getElementById('oldregularPriceLabel' + prodItem ).style.display="block";
				document.getElementById('oldregularPrice' + prodItem ).style.display="block";
				
				document.getElementById('regularPriceLabel' + prodItem ).style.display="none";
				document.getElementById('regularPrice' + prodItem ).style.display="none";
				
				if(ourPriceText.length > 0)
					oldreglabel=ourPriceText + ":";				
			} else {
				// there is no sale so don't put line
				regpricetxt=currencyProductList(regularPrice);
				reglabel=regularPriceLabel;
				
				document.getElementById('salePriceLabelOut' + prodItem ).style.display="none";
				document.getElementById('salePrice' + prodItem ).style.display="none";
				document.getElementById('oldregularPriceLabel' + prodItem ).style.display="none";
				document.getElementById('oldregularPrice' + prodItem ).style.display="none";
				
				document.getElementById('regularPriceLabel' + prodItem ).style.display="block";
				document.getElementById('regularPrice' + prodItem ).style.display="block";
			}
			
			//alert(listlabel + listpricetxt);
			//alert(reglabel + regpricetxt);
			//alert(percentdiscountlabel + percentdiscounttxt);								
			
			
			//alert('salprice txt='+ salepricetxt + ' saleprice= ' + salePrice + ' showsale = ' +  showSale + ' percentdiscounttxt = ' + percentdiscounttxt + ' percentDiscount = ' + percentDiscount);
			if (document.getElementById('listPriceLabel' + prodItem))
				document.getElementById('listPriceLabel' + prodItem).innerHTML = listlabel;
			if (document.getElementById('listPrice' + prodItem))
				document.getElementById('listPrice' + prodItem).innerHTML = listpricetxt;
			
			if (document.getElementById('regularPriceLabel' + prodItem))
			{
				document.getElementById('regularPriceLabel' + prodItem).innerHTML = reglabel;
			}
			if (document.getElementById('regularPrice' + prodItem))
			{
				document.getElementById('regularPrice' + prodItem).innerHTML = regpricetxt ;				
			}
			
			if (document.getElementById('percentDiscount' + prodItem))
				document.getElementById('percentDiscount' + prodItem).innerHTML = percentdiscounttxt;
			if (document.getElementById('percentDiscountLabel' + prodItem ))
				document.getElementById('percentDiscountLabel' + prodItem).innerHTML = percentdiscountlabel;

			if (document.getElementById('oldregularPriceLabel' + prodItem))
				document.getElementById('oldregularPriceLabel' + prodItem).innerHTML = oldreglabel;
			if (document.getElementById('oldregularPrice' + prodItem))
				document.getElementById('oldregularPrice' + prodItem).innerHTML = oldregpricetxt ;

			if (document.getElementById('salePrice' + prodItem))
				document.getElementById('salePrice' + prodItem).innerHTML = salepricetxt ;
			if (document.getElementById('salePriceLabelOut' + prodItem ))
				document.getElementById('salePriceLabelOut' + prodItem).innerHTML = salepricelabel;
			
			
			
			
			//alert(" price:" + listPrice);
			//alert("Highest price:" + highestPrice);
			//alert("Lowest price:" + lowestPrice);
			//alert(percentDiscount);
			
			href="/index.cfm/a/catalog.prodshow/vid/"+ vid + "/catid/" + catID;
			
			document.getElementById('imglink_' + prodItem).href = href;
			document.getElementById('imgsrc_' + prodItem).src = mainImage;
			document.getElementById('displayName_' + prodItem).innerHTML = displayName;
			document.getElementById('displayName_' + prodItem).href = href;
			document.getElementById('buybtn_' + prodItem).href = href;
		}
	}
	
}


// MAKE REQUEST AND GET NEW XML
function makeRequestProductList(url, vid, currentID, enabledInventory, callForPricing, currentID, suffix, ourPriceText) 
{
	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) {}
        }
    }

    http_request.onreadystatechange = function() { processContentsProductList(http_request, vid, currentID, enabledInventory, callForPricing, currentID, suffix, ourPriceText); document.body.style.cursor = 'auto'; };
    http_request.open('GET', url, true);
    http_request.send(null);
}




function processContentsProductList(http_request, vid, currentID, enabledInventory, callForPricing, currentID, suffix, ourPriceText)
{
    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 notAvailableUpSell(currentID);
			
			var displayName = xmldoc.getElementsByTagName('displayName').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 mainImage = xmldoc.getElementsByTagName('mainImage').item(0).firstChild.data;
			var idVariation = xmldoc.getElementsByTagName('idVariation').item(0).firstChild.data;
			var inventory = xmldoc.getElementsByTagName('inventory').item(0).firstChild.data;
			var regularPriceLabel = xmldoc.getElementsByTagName('regularPriceLabel').item(0).firstChild.data;
			var showSale=false; //show sale price
			var showList=true; // show list price
			
			//alert("List price:" + listPrice);
			//alert("Sale price:" + salePrice);
			//alert("Regular price:" + regularPrice);	
			
			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

			// set the elements on the page
			document.forms['ProductForm' + suffix]['idVariation' + suffix + '_' + currentID].value = idVariation;
			document.getElementById('displayName' + suffix + '_' + currentID).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="";
			
			
			
			if (percentDiscount > 0) {
				percentdiscountlabel="You Save:";
				percentdiscounttxt=Math.round(percentDiscount) + "%";
				document.getElementById('percentDiscount' + suffix + currentID).style.display = "block";
				document.getElementById('percentDiscountLabel' + suffix + currentID).style.display = "block";
			}
			else
			{
				document.getElementById('percentDiscount' + suffix + currentID).style.display = "none";			
				document.getElementById('percentDiscountLabel' + suffix + currentID).style.display = "none";
			}

			if (showList) {
				listlabel="List Price:";
				listpricetxt=currencyProductList(listPrice);
				document.getElementById('listPriceLabel' + suffix + currentID).style.display="block";
				document.getElementById('listPrice' + suffix + '' + currentID).style.display="block";
			}
			else
			{
				document.getElementById('listPriceLabel' + suffix +  currentID).style.display="none";
				document.getElementById('listPrice' + suffix + currentID).style.display="none";			
			}
			
			if (showSale) {
				salepricelabel="Sale Price:";
				salepricetxt=currencyProductList(salePrice);
				//also show regular price with line thru it.
				oldregpricetxt=currencyProductList(regularPrice);
				
				//alert('salePriceLabelOut' + suffix + currentID);				
				document.getElementById('salePriceLabelOut' + suffix +  currentID).style.display="block";
				//alert('salePrice' + suffix + currentID);
				document.getElementById('salePrice' + suffix +  currentID).style.display="block";
				document.getElementById('oldregularPriceLabel' + suffix + currentID).style.display="block";
				document.getElementById('oldregularPrice' + suffix + currentID).style.display="block";
				
				document.getElementById('regularPriceLabel' + suffix + currentID).style.display="none";
				document.getElementById('regularPrice' + suffix + currentID).style.display="none";
				
				if(ourPriceText.length > 0)
					oldreglabel=ourPriceText + ":";				
			} else {
				// there is no sale so don't put line
				regpricetxt=currencyProductList(regularPrice);
				reglabel=regularPriceLabel;
				
				document.getElementById('salePriceLabelOut' + suffix + currentID).style.display="none";
				document.getElementById('salePrice' + suffix + currentID).style.display="none";
				document.getElementById('oldregularPriceLabel' + suffix + currentID).style.display="none";
				document.getElementById('oldregularPrice' + suffix + currentID).style.display="none";
				
				document.getElementById('regularPriceLabel' + suffix + currentID).style.display="block";
				document.getElementById('regularPrice' + suffix + currentID).style.display="block";
			}
			
			//alert(listlabel + listpricetxt);
			//alert(reglabel + regpricetxt);
			//alert(percentdiscountlabel + percentdiscounttxt);								
			
			
			//alert('salprice txt='+ salepricetxt + ' saleprice= ' + salePrice + ' showsale = ' +  showSale + ' percentdiscounttxt = ' + percentdiscounttxt + ' percentDiscount = ' + percentDiscount);
			if (document.getElementById('listPriceLabel' + suffix + currentID))
				document.getElementById('listPriceLabel' + suffix + currentID).innerHTML = listlabel;
			if (document.getElementById('listPrice' + suffix + currentID))
				document.getElementById('listPrice' + suffix + currentID).innerHTML = listpricetxt;
			
			if (document.getElementById('regularPriceLabel' + suffix + currentID))
			{
				document.getElementById('regularPriceLabel' + suffix + currentID).innerHTML = reglabel;
			}
			if (document.getElementById('regularPrice' + suffix + currentID))
			{
				document.getElementById('regularPrice' + suffix + currentID).innerHTML = regpricetxt ;				
			}
			
			if (document.getElementById('percentDiscount' + suffix +  currentID))
				document.getElementById('percentDiscount' + suffix +  currentID).innerHTML = percentdiscounttxt;
			if (document.getElementById('percentDiscountLabel' + suffix +  currentID))
				document.getElementById('percentDiscountLabel' + suffix +  currentID).innerHTML = percentdiscountlabel;

			if (document.getElementById('oldregularPriceLabel' + suffix +  currentID))
				document.getElementById('oldregularPriceLabel' + suffix +  currentID).innerHTML = oldreglabel;
			if (document.getElementById('oldregularPrice' + suffix +  currentID))
				document.getElementById('oldregularPrice' + suffix +  currentID).innerHTML = oldregpricetxt ;

			if (document.getElementById('salePriceLabelOut' + suffix +  currentID))
				document.getElementById('salePriceLabelOut' + suffix +  currentID).innerHTML = salepricelabel;
			if (document.getElementById('salePrice' + suffix +  currentID))
				document.getElementById('salePrice' + suffix +  currentID).innerHTML = salepricetxt ;

			//alert(document.getElementById('listPrice' + suffix +  currentID).innerHTML);
//			alert(document.getElementById('salePrice' + suffix +  currentID).innerHTML);
	//		alert(document.getElementById('regularPrice' + suffix + currentID).innerHTML);

			//end calculation


		if(enabledInventory)
		{
			if (document.getElementById('quantity' + suffix + '_' + currentID)) 
			{
				if (inventory < 1) 
				{
					alert('This color/size option is out of stock.');
					document.getElementById('quantity' + suffix + '_' + currentID).style.display = 'none';
					document.forms['ProductForm' + suffix]['qty' + suffix + '_' + currentID].value = 0;
				} else
					document.getElementById('quantity' + suffix + '_' + currentID).style.display = 'block';
			}
			if(callForPricing)
			{
				if (document.getElementById('quantity' + suffix + '_' + currentID)) {
					if (inventory < 1) {
						document.getElementById('quantity' + suffix + '_' + currentID).style.display = 'none';
						document.forms['ProductForm' + suffix]['qty' + suffix + '_' + currentID].value = 0;
					} else
						document.getElementById('quantity' + suffix + '_' + currentID).style.display = 'block';
				}
			}
		}
		else
		{
			//alert(inventory);
			if (inventory < 1) {
				alert('This product option is not available.');
				if (document.getElementById('quantity' + suffix + '_' + currentID))
					document.getElementById('quantity' + suffix + '_' + currentID).style.display = 'none';
					document.forms['ProductForm' + suffix]['qty' + suffix + '_' + currentID].value = 0;
					document.getElementById('outOfStock' + suffix + '_' + currentID).style.display = 'block';
					document.getElementById('outOfStock' + suffix + '_' + currentID).innerHTML = '<span class="stock_text">Unavailable</span>';
			}
			else {
				if (document.getElementById('quantity' + suffix + '_' + currentID))
					document.getElementById('quantity' + suffix + '_' + currentID).style.display = 'block';
					document.getElementById('outOfStock' + suffix + '_' + currentID).style.display = 'none';
			}

		}



        } else {
        	return notAvailableProductList(currentID, suffix);
        }
    }
}



function currencyProductList( 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;
}


function notAvailableProductList(currentID, suffix) {
   	//perhaps also post the form instead here
	alert('This color/size option is unavailable.');
	if (document.getElementById('quantity' + suffix + '_' + currentID))	
		document.getElementById('quantity' + suffix + '_' + currentID).style.display = 'none';
		document.forms['ProductForm' + suffix]['qty' + suffix + '_' + currentID].value = 0;
		document.getElementById('outOfStock' + suffix + '_' + currentID).style.display = 'block';
		document.getElementById('outOfStock' + suffix + '_' + currentID).innerHTML = '<span class="stock_text">This variation is unavailable.</span>';
}

