/* COPYRIGHT BennySutton.com
notes: 
strOrder is a hidden field to perpetuate the cart without cookies 
_scripts/dotnet.js must be included on page */ 
//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
			addOptionToCart(cart,ProdID,intID);
	}else{//remove
			delChip(cart,ProdID,expMonth);
			var strOrder = document.getElementById("strOrder");
			if (strOrder!=null){strOrder.value=delToken(strOrder.value,ProdID);}
	}
}
/* add to cart by values */
function addToCart(cart,ProdID,qty,price){
	if(isNaN(ProdID)){return false;} 
	if(isNaN(price)==false){qty=qty+"~"+price;}
	setChip(cart,ProdID,qty,expMonth);
	var strOrder = document.getElementById("strOrder");
	if (strOrder!=null){strOrder.value=setToken(strOrder.value,ProdID,qty);}
	return true;
}
/* check for the cart cookie */
function cartExists(cart){if(getCookie(cart)==null){return false;}else{return true;}}
/* add product from combo */
function addOptionToCart(cart,ProdID,intID){
	var qty = null;var ctl = document.getElementById("cbo"+intID);
	if (ctl!=null){if (ctl.type=="select-one"){qty = getListValue(ctl);}}
	if (qty==null){
		var ctl = document.getElementById("txt"+intID);
		if (ctl!=null){qty = ctl.value;}else{qty = 1;}
	}
	var ctl = document.getElementById("price"+intID);
	if (ctl!=null){qty = qty+"~"+ctl.value;}
	setChip(cart,ProdID,qty,expMonth);
	var strOrder = document.getElementById("strOrder");
	if (strOrder!=null){strOrder.value=setToken(strOrder.value,ProdID,qty);}
}
/* 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 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;
}
