/* COPYRIGHT BennySutton.com
notes: 
	strOrder is a hidden field to perpetuate the cart without cookies 
	_scripts/dotnet.js must be included on page 
						'library string item format:    [prodID from Products int]  =  [PriceBandID int]  ~[Price decimal (optional)]|    e.g. 1234=20~90.00|
					'store string item format:      [prodID from Products int]  =  [Qty int]  ~[Price decimal (optional)]~[options string (optional)]|            e.g. 1234=20~90.00|
*/ 
	
//strings for alerts/prompts
//TODO could be translated and passed in on page
var SearchText='Enter search text';var SelectOption='Select an ordering option';var FillQuantity='Fill in Quantity with a number';
var CurrencyCalc='Use the currency calculator as a rough guide only';var ProperEmail='Please enter a proper e-mail address';
var FillInFields='Fill in all fields then press submit';var EmptyCart='Your shopping cart is empty';var Instructions='Instructions for use: (1) add items to your shopping cart (2) review shopping cart (3) go to checkout';

/* add a product by ticking a checkbox */
	function addCart(cart,ProdID,intID){
		var ctl = document.getElementById("chk"+intID);
		if (ctl==null){return false;}
		if (ctl.type!="checkbox"){return false;}
		if(ctl.checked){//add
				parseControlsToCart(cart,ProdID,intID);
		}else{ //remove
				delChip(cart,ProdID,expMonth);
				var strOrder = document.getElementById("strOrder");
				if (strOrder!=null){strOrder.value=delToken(strOrder.value,ProdID);}
		}
	}
/* add product from named form controls - must ensure intID on page does not clash with cart intID*/
	function parseControlsToCart(cart,ProdID,intID)
	{
	  // get priceband
		var qty = null;var ctl = document.getElementById("cbo"+intID); // try combo
		if (ctl!=null){if(ctl.type=="select-one"){qty = getListValue(ctl);} } // priceband
		if (qty==null){ // try textbox
			var ctl = document.getElementById("txt"+intID);
			if (ctl!=null){if(isNaN(ctl.value)==false){qty = ctl.value;}}else{qty = 1;}
			}
		// get price
		var price=0;		var ctlprice = document.getElementById("price"+intID);		if (ctlprice!=null ){price=ctlprice.value;}
		//get options
		var options="";		var ctloptions = document.getElementById("options"+intID);		if (ctloptions!=null){options=ctloptions.value;}
		// fill chip
		var chip=qty+"~"+price+"~"+options;
		setChip(cart,ProdID,chip,expMonth);
		// fill hidden text box on page
		var strOrder = document.getElementById("strOrder");		if (strOrder!=null){strOrder.value=setToken(strOrder.value,ProdID,chip);}
		return true;
	}
/* add to cart by values */
	function addToCart(cart,prodid,qty,price)
	{
		if(cart.length==0){return false;}
		if(isNaN(prodid)){return false;}
		if(isNaN(qty)){return false;}
		var chip=qty;
		if(isNaN(price)==false){chip=qty+"~"+price;}
		setChip(cart,prodid,chip,expMonth);
		var strOrder = document.getElementById("strOrder");if (strOrder!=null){strOrder.value=setToken(strOrder.value,prodid,chip);}
		return true;
	}
/* check for the cart cookie */
	function cartExists(cart){if(getCookie(cart)==null){return false;}else{return true;}}
/* check cookie and hidden field for cartstring, post form1 to catalog if found*/
	function viewCart(strType,cart){
		if(cartExists(cart)==false){
			var strOrder = document.getElementById("strOrder");
			if (strOrder!=null){
				if (strOrder.value==""){
					alert(EmptyCart);
					return;
				}
			}
		}
		if(document.getElementById("STR")!=null){document.getElementById("STR").value="";}
		document.form1.action="Catalog.aspx";document.form1.cart.value=strType;zeroSearchFields();document.form1.submit();
	}
/* delete a cart */
	function delCart(cart){delCookie(cart);
		var strOrder = document.getElementById("strOrder");
		if (strOrder!=null){strOrder.value="";}
		window.location.href='Catalog.aspx?strOrder=';}
/* go to cart */
	function goToCart(cart)
		{
		if(cartExists(cart)==false)
			{
				var strOrder = document.getElementById("strOrder");
				if (strOrder!=null)
				{
					if (strOrder.value=="")
					{
						alert(EmptyCart);
						return;
					}
				}
			}
				document.form1.action=strCompanyWeb+"store/catalog.aspx?cart=cart";
				document.form1.target="_self";
				document.form1.submit();
		}
/* go to checkout.aspx */
	function checkout(cart)
		{
		if(cartExists(cart)==false)
			{
				var strOrder = document.getElementById("strOrder");
				if (strOrder!=null)
				{
					if (strOrder.value=="")
					{
						alert(EmptyCart);
						return;
					}
				}
			}
			if(checkCart(document.form1)==false){return;}
	//		if(cart!="cart1"){
				document.form1.action=strCompanyWeb+"store/Checkout.aspx";
				document.form1.target="_self";
	//		}else{/*https*/
	//			var checkoutURL=strCompanyWeb+"store/checkout.aspx";
	//			checkoutURL = checkoutURL.toLowerCase();
	//			checkoutURL = checkoutURL.replace("http://", "https://");
	//			document.form1.action=checkoutURL;
	//			document.form1.target="_top";
	//		}
			document.form1.submit();
		}
/* check a cart is complete from form1 objects - only works on catalog.aspx cart mode */
	function checkCart(formObj){
		for(i=0;i<formObj.length;i++)if(formObj.elements[i].type=="select-one")
		{
			if(formObj.elements[i].title.indexOf("required")==0&&formObj.elements[i].selectedIndex==0&&eval("document.form1.chk"+formObj.elements[i].name.substring(3)+".checked")){
							alert(SelectOption);formObj.elements[i].focus();return false;
				}
		}else{
		if(formObj.elements[i].type=="text"&&formObj.elements[i].name.indexOf("txt")!=-1){
			if(isNaN(formObj.elements[i].value)||formObj.elements[i].value==""){
							alert(FillQuantity);formObj.elements[i].focus();formObj.elements[i].select();return false;
			}
		}
	}
	return true;
	}
/* check that cart is complete from current ajax postback - only works for one cart per page*/
	function canCheckout()
	{
		if(document.getElementById("CanCheckout")!=null){return true;}else{return false;}
	}
/* ajax the cart onto the page */
	function showCart(divCart){xmlHttp=GetXmlHttpObject();if(xmlHttp==null){return;}
		var url=strCompanyWeb+"/Store/admin/LookupCart.aspx";
		var strOrder = document.getElementById("strOrder");
		if (strOrder!=null){url=url+"?strorder="+strOrder.value;}
		url=url+"?sid="+Math.random()+"&span="+divCart;xmlHttp.onreadystatechange=cartChanged;xmlHttp.open("GET",url,true);xmlHttp.send(null);
		function cartChanged(){if(xmlHttp.readyState==4){document.getElementById(divCart).innerHTML=xmlHttp.responseText;}}
	} 
/* empty hidden fields for new string search */
	function zeroSearchFields(){var x;var myFields = new Array("PT","CY","SID","PTI","CTI","CategoryID");for (x in myFields){if(document.getElementById(myFields[x])!=null){document.getElementById(myFields[x]).value="";}}}
/* lookup price band string NOT USED AND NOT TESTED */
	function getPricing(id){xmlHttp=GetXmlHttpObject();if(xmlHttp==null){return;}
		var url="LookupPricing.aspx";url=url+"?sid="+Math.random()+"&id="+id;xmlHttp.onreadystatechange=priceChanged;xmlHttp.open("GET",url,true);xmlHttp.send(null);
		function priceChanged(){if (xmlHttp.readyState==4){document.getElementById("WzBoDy").innerHTML = xmlHttp.responseText;}}
	} 
/* tooltip to aid navigation */
	function imgTooltip(id,ct,pt,words){var txt = "<img src=\"" + strCompanyWeb + "upload/stock/watermarked/" + id + ".jpg\" /><br /><a href='zoom.aspx?prodid=" + id + "'>more info...</a><br />";
			txt += "<a href='?ct=" + ct + "'>more in same category</a><br />";
			txt += "<a href='?pt=" + pt + "'>more from same place</a><br />";
			txt += words;
			return txt; } 
/* fill the popup with price bands */
	var priceBandOptions;    
	function priceBandTooltip(id,cartName){
	var txt="<div style=\"padding-right: 3px; padding-left: 3px; padding-bottom: 3px; padding-top: 3px\"><strong><img src=\"" + strCompanyWeb + "images/icons/info.gif\" width='16' height='16' alt='options' title='select option' /> Click on an item below:</strong> Select the option that best describes your use.</div><select name='"+id+"' id='"+id+"' size='1' class='formcontrol' title='required' multiple style=\"width: 300px; height: 100px\" ";
		txt+=" onchange='addToCart(\"" + cartName + "\", " + id + ",this.value);showCart(\"divCart\");'>";
		txt+="<option value='0'>&nbsp;-------------- select use ------------- </option>";
		txt+=priceBandOptions;
		txt+="</select>";
		txt+="<br /><img src=\"" + strCompanyWeb + "images/icons/info.gif\" width='16' height='16' alt='download' title='All images are available for immediate download' />All images are available for immediate download as<br />high-resolution digital files upon completion of order.";
		txt+="</div>";
	return txt;
	}
