var bDisableButtons =false;

function checkOut(){
//	var qty = document.getElementById("totalqty").value;
	var oFrm = document.getElementById("frmBasket");

	var totalqty=0;
	var strItemsQty="";
	var lqty_peritem=0;
	var i=0;
	for (i=0;i<oFrm.elements.length;i++) {
		if (oFrm.elements[i].name.search(/_qty$/) !=-1) {
			 if (isNaN(parseInt(oFrm.elements[i].value))==false) {
				var oElem = document.getElementById(oFrm.elements[i].name.replace(/_qty$/,"_qty_peritem"));
				var oType = document.getElementById(oFrm.elements[i].name.replace(/_qty$/,"_type"));
				
				lqty_peritem=0;
				if (oElem!=null && isNaN(parseInt(oElem.value))==false)
				   lqty_peritem = parseInt(oElem.value);
				if (oType.value!="gift")   
					totalqty+=parseInt(oFrm.elements[i].value) * lqty_peritem;
				else
					totalqty+=12;
				strItemsQty+= "&" + oFrm.elements[i].name + "=" + oFrm.elements[i].value
			 }	 
		}
	}

	
	

//alert(giftcardmsg);
	//	if ( totalqty==0 ||Math.round((parseInt(totalqty)/12))*12 !=parseInt(totalqty) ) {
//	if ( totalqty==0 || parseInt(totalqty)< 12 ) {
	if ( totalqty==0 || parseInt(totalqty)< 0 ) {
	
 		alert('Reminder: To proceed to Checkout orders must be a minimum of 12 bottles' );
		return false;
	}
	
	var oElem = document.getElementById("shipping_cost");	
	if (oElem.options[oElem.selectedIndex].value=="none" || oElem.options[oElem.selectedIndex].value=="" ) {
 		alert('Please selected the type of delivery you require.' );
		return false ;
	}

	var oGiftElem = document.getElementById("giftcard_cost");
    
	var giftcardmsg = "";
	if ( document.getElementById("giftcardmsg") != undefined || document.getElementById("giftcardmsg") !=null) {
	   giftcardmsg = escape(document.getElementById("giftcardmsg").value);
	}
    
    if (oGiftElem.checked==true && giftcardmsg == "" )  {
 		alert('You selected ato have a Gift Card & Message. Please enter your message.' );
		return false ;
	}


	if (bDisableButtons ==false) {
		bDisableButtons =true;
		document.location = "/index.php?op=check_out&tpl=confirm_order" + strItemsQty + "&gift_card_msg=" + giftcardmsg;
	}
	return false ;
}

function checkOut_frominfo(){
//	var qty = document.getElementById("totalqty").value;
	var totalqty=0;
	var strItemsQty="";
	var lqty_peritem=0;
	var i=0;

	var totalqty = document.getElementById("qty_wines_info").value;	
	if ( totalqty==0 ||Math.round((parseInt(totalqty)/12))*12 !=parseInt(totalqty) ) {
 		alert('Reminder: To proceed to Checkout orders must be in multiples of 12 bottles' );
		return false;
	}
	
	var shipping_cost = document.getElementById("shipping_cost_description_info").value;	
	if (shipping_cost=="" ) {
 		alert('Please selected the type of delivery you require.' );
		document.location = "/index.php?tpl=basket"
		return false ;
	}
	if (bDisableButtons ==false) {
		bDisableButtons =true;
		document.location = "/index.php?op=check_out&tpl=confirm_order";
	}
	
	return false ;
}



function recalculate() {
	var oFrm = document.getElementById("frmBasket");

	var totalqty=0;
	var strItemsQty="";
	for (i=0;i<oFrm.elements.length;i++) {
		if (oFrm.elements[i].name.search(/_qty$/) !=-1) {
			 if (isNaN(parseInt(oFrm.elements[i].value))==false) {
				 totalqty+=parseInt(oFrm.elements[i].value)
				 strItemsQty+= "&" + oFrm.elements[i].name + "=" + oFrm.elements[i].value
			 }	 
		}
	}
	
    var giftcardmsg;
	if (document.getElementById("giftcard_cost").checked == true) {
		giftcardmsg = escape(document.getElementById("giftcardmsg").value);
	}
	
	//alert(strItemsQty);
	if (strItemsQty!="") {
		if (bDisableButtons ==false) {
			bDisableButtons =true;
			document.location = "/index.php?op=recalculate&tpl=basket" + strItemsQty+ "&gift_card_msg=" + giftcardmsg;
		}
		return false ;
	}
}

function remove_items() {
	var aRemoveItems = document.getElementsByName("remove_item[]");
	var strRemoveItems="";
	var i=0;
	for (i=0;i<aRemoveItems.length;i++) {
		if (aRemoveItems[i].checked==true)
			strRemoveItems+="&remove_item[]="+aRemoveItems[i].value
	}


    var giftcardmsg;
	if (document.getElementById("giftcard_cost").checked == true) {
		giftcardmsg = escape(document.getElementById("giftcardmsg").value);
	}

    if (strRemoveItems!="") {
		if (bDisableButtons ==false) {
			bDisableButtons =true;
			document.location = "/index.php?op=remove_item&tpl=basket" + strRemoveItems + "&gift_card_msg=" + giftcardmsg;
		}
		return false ;
	}
}

function update_shipping_costs() {
	var oElem = document.getElementById("shipping_cost");
	var strShippingCosts="";
	if (oElem.selectedIndex!=-1) {
		strShippingCosts="&"+oElem.name+"=" +escape(oElem.options[oElem.selectedIndex].value);

    var giftcardmsg;
	if (document.getElementById("giftcard_cost").checked == true) {
		giftcardmsg = escape(document.getElementById("giftcardmsg").value);
	}

    if (bDisableButtons ==false) {

bDisableButtons =true;
			document.location = "/index.php?op=update_shipping&tpl=basket" + strShippingCosts+ "&gift_card_msg=" + giftcardmsg;;
		}
		return false ;
	}
}


function update_giftcard_costs() {
	var oElem = document.getElementById("giftcard_cost");
	var strGiftcardCosts="&giftcard_cost=0.00";
//alert("bDisableButtons="+bDisableButtons);
if (oElem.checked==true)  {
   strGiftcardCosts="&giftcard_cost=1.00";
  // document.getElementById("giftcardmsg").style.visibility = 'visible'; 

}
else {
	strGiftcardCosts+= "&gift_card_msg="
   //document.getElementById("giftcardmsg").style.visibility = 'hidden'; 
}

	if (bDisableButtons ==false) {
			bDisableButtons =true;
//			alert("bDisableButtons="+bDisableButtons);
//alert(oElem.checked);
//alert(strGiftcardCosts);
document.location = "/index.php?op=update_giftcard&tpl=basket" + strGiftcardCosts;

      }
		return false ;
	
}


function confirm_order(){
	var qty = document.frmCheckOut.qty.value;
//alert(Math.round((parseInt(qty)/12))*12)

	document.getElementById('frmCheckOut').submit();
			return false ;
}



function addtobasket( frm, Prod_id, qty, bAddmsg, bJumptobasket ) {
 var oqty= document.getElementById(qty);
 var oProd_id= document.getElementById(Prod_id);
 var ltmp = parseInt(oqty.value)	
 var sJumptobasket;
 
 if (!isNaN(ltmp)) {
// 	alert("/index.php?tpl=basket&op=add_item&product_id=" + oProd_id.value + "&" + oqty.name + "=" + oqty.value);
	 if (bAddmsg==true )
		 alert("wine being added to basket.");
	 if (bJumptobasket==true)
		 sJumptobasket = "&Jumptobasket=true";
	 else
		 sJumptobasket = "&Jumptobasket=";
 	 document.location = "/index.php?tpl=basket&op=add_item&product_id=" + oProd_id.value + "&" + "qty=" + oqty.value+""+ sJumptobasket;
//	 document.getElementById(frm).submit();
 }	 
 else {
 	alert("please enter a valid number for \'No. of bottles\'")
 }
}
