// JavaScript Document
function docalc(option){
	
	if(isNaN(document.getElementById(option+'_qty').value) && document.getElementById(option+'_qty').value != "."){
		alert("You can not enter letters into the quantity field");
		subStr = document.getElementById(option+'_qty').value.split("");
		var endString="";
		
		for(i=0; i<subStr.length; i++){
			if(!isNaN(subStr[i]) || subStr[i] == "."){
				endString += subStr[i];
			}					
		}
			
		document.getElementById(option+'_qty').value = endString;
	}
	
	//-- store the amounts to remove
	removeGST = new Number(document.getElementById(option + '_totalgst').value);
	removeTotal = new Number(document.getElementById(option + '_total').value);
	removeSub = new Number(document.getElementById(option + '_totalinc').value);
	//---
	
	document.getElementById(option + '_total').value = new Number(eval('document.orderForm.' + option + '_cost.value')*eval('document.orderForm.' + option + '_qty.value'));
	document.getElementById(option + '_totalgst').value = new Number(eval('document.orderForm.' + option + '_gst.value')*eval('document.orderForm.' + option + '_qty.value'));
		
	var itemTotal = Number(document.getElementById(option + '_total').value);		
	document.getElementById(option + '_totalinc').value = itemTotal.toFixed(2);
	
	document.getElementById(option + '_total').value = "$ "+ itemTotal.toFixed(2);
	
	/*
	//---- do sub totals
	var itemSubTotal = new Number(document.orderForm.subTotal.value - Number(removeSub) + Number(document.getElementById(option + '_totalinc').value));		
	document.orderForm.subTotal.value = itemSubTotal.toFixed(2);
	
	document.getElementById('subText').innerHTML = document.orderForm.subTotal.value;
	
	//---- do gst totals
	var itemGSTTotal = new Number(document.orderForm.gst.value) - Number(removeGST) + Number(document.getElementById(option + '_totalgst').value);		
	document.orderForm.gst.value = itemGSTTotal.toFixed(2);
	
	document.getElementById('gstText').innerHTML = document.orderForm.gst.value;
	
	//---- do grand totals
	var itemGrandTotal = new Number(document.orderForm.gst.value) + Number(document.orderForm.subTotal.value);		
	document.orderForm.grand.value =  itemGrandTotal.toFixed(2);
	
	document.getElementById('totalText').innerHTML =  document.orderForm.grand.value;
	*/
	
	//alert(document.orderForm.totalGST.value);
	
}
	
function doOthercalc(option, optionArea){
	
	if(isNaN(document.getElementById(option+'_qty').value) && document.getElementById(option+'_qty').value != "."){
		alert("You can not enter letters into the quantity field");
		subStr = document.getElementById(option+'_qty').value.split("");
		var endString="";
		
		for(i=0; i<subStr.length; i++){
			if(!isNaN(subStr[i]) || subStr[i] == "."){
				endString += subStr[i];
			}					
		}
			
		document.getElementById(option+'_qty').value = endString;
	}
		
	//-- store the amounts to remove
	removeGST = new Number(document.getElementById(option + '_totalgst').value);
	removeTotal = new Number(document.getElementById(option + '_total').value);
	removeSub = new Number(document.getElementById(option + '_totalinc').value);
	//---
	
	document.getElementById(option + '_total').value = new Number(eval('document.orderForm.' + option + '_cost.value')*eval('document.orderForm.' + option + '_qty.value'));
	document.getElementById(option + '_totalgst').value = new Number(eval('document.orderForm.' + option + '_gst.value')*eval('document.orderForm.' + option + '_qty.value'));
		
	var itemTotal = Number(document.getElementById(option + '_total').value);		
	document.getElementById(option + '_totalinc').value = itemTotal.toFixed(2);
	
	document.getElementById(option + '_total').value = "$ "+ itemTotal.toFixed(2);
	/*
	//---- do sub totals
	var itemSubTotal = new Number(document.orderForm.subTotal.value - Number(removeSub) + Number(document.getElementById(option + '_totalinc').value));		
	document.orderForm.subTotal.value = itemSubTotal.toFixed(2);
	
	document.getElementById('subText').innerHTML = document.orderForm.subTotal.value;
	
	//---- do gst totals
	var itemGSTTotal = new Number(document.orderForm.gst.value) - Number(removeGST) + Number(document.getElementById(option + '_totalgst').value);		
	document.orderForm.gst.value = itemGSTTotal.toFixed(2);
	
	document.getElementById('gstText').innerHTML = document.orderForm.gst.value;
	
	//---- do grand totals
	var itemGrandTotal = new Number(document.orderForm.gst.value) + Number(document.orderForm.subTotal.value);		
	document.orderForm.grand.value =  itemGrandTotal.toFixed(2);
	
	document.getElementById('totalText').innerHTML =  document.orderForm.grand.value;
	*/
	//alert();
	
	if(eval('document.orderForm.' + option + '_qty.value')>0){		
		jQuery("#"+optionArea).show();
		jQuery("#"+optionArea).next("tr.gadd").show();
	}else{
		jQuery("#"+optionArea).hide();
		jQuery("#"+optionArea).next("tr.gadd").hide();
		$("input.optquantity").each( function(){
			$(this).val("");
		});
		$("input.prodquantity").each( function(){
			$(this).val("");
		});
	}
	
	
	
	//alert(document.orderForm.totalGST.value);
	
}	


	
function submitForm(error){
		
	if(document.orderForm.grand.value!="0" && document.orderForm.grand.value!="0.00"){
		//return true;
	}else{
		error += "You need to order at least one item.\n";		
		//return false;
	}
	
	//alert(error);
	
	//return false;
	
	if(error==""){
		return true;
	}else{
		alert("The following errors occured:\n\n"+error);
		return false;
	}
	
	
}

var textarray = new Array();

function openBox(row, optionsarray, id2){
	var incvals = "";
	var product = "";
	
	var mySplitResult = optionsarray.split(";");	
	
	//alert(mySplitResult.length);
	incvals +=  '<tr>'+
					'<td colspan="2" id="options">';
	
	for(i=0; i<mySplitResult.length; i++){
		
		incvals += '<div><table cellpadding="3" cellspacing="0" width="630" style="border:1px solid #ccc; margin:10px;" >';
		
		var individualSplit = mySplitResult[i].split(":");
		
		incvals +=  '<tr>'+
						'<td class="oddRow" colspan="3" style="color:#c20000;"><strong>'+ individualSplit[0] +'</strong></td>'+
					'</tr>';
					
		var individualOptions = individualSplit[1].split("~");
		
		incvals += '<tr>';
		var counting = 0;
		
		for(j=0; j<individualOptions.length; j++){
		
			if(counting == 2){
				counting = 0;
				incvals += '</tr><tr>';
			}
		
			var splitUp = individualOptions[j].split("^");
			
			incvals += '<td class="evenRow" width="250" style="font-size:9pt;"><input type="radio" value="'+ splitUp[0] +'" name="optionname['+i+']" onclick="javascript: textarray['+i+'] = this.value;"  /> '+ splitUp[0] +'</td>';
			
			counting++;
						
		}				
		incvals += '</tr>';
		
		incvals += '</table></div>';
	}
	
	incvals +=  '</td>'+
				'</tr>';
	
	Shadowbox.open({ 
		player:     'html', 
		title:      'Select Combination:', 
		content:    '<div style=\'min-height:430px; width:680px; background-color:#ffffff; padding:0px;\'>'+
						'<form name="optiondata" style="margin:0; padding:0;">'+
							'<input type="hidden" name="row" value="'+row+'">'+
							'<input type="hidden" name="product" value="'+product+'">'+							
							'<table cellpadding="3" cellspacing="0" width="660" style="margin:10px;">'+
									incvals +	
							'</table>'+
							
							'<table cellpadding="3" cellspacing="0" width="660" style="margin:10px;">'+
								'<tr>'+
									'<td><input type="button" name="save" value="Save Selection" onclick="return passBack('+row+', '+id2+', '+mySplitResult.length+');"></td>'+
								'</tr>'+
							'</table>'+
						'</form>'+
					'</div>', 
		height:     430, 
		width:      680 
	}); 
}

function passBack(id, id2, limit){
	
	var string = textarray.toString();
	
	string = string.replace(",", " and ");
			
	document.getElementById("option-"+id2+"-"+id+"-name").value = string;
	
	document.getElementById("option-"+id2+"-"+id+"-selection").innerHTML = string;
	
	textarray = new Array();
	
	Shadowbox.close();
	
	return true;
}

jQuery(document).ready( function($){
	$(".showRow").live( 'click', function(){
		$("tr.furtherDetalis").each( function(){
			$(this).hide();
		});
		$("tr.options").each( function(){
			$(this).hide();
		});
		$("tr.gadd").each( function(){
			$(this).hide();
		});
		$("input.optquantity").each( function(){
			$(this).val("");
		});
		$("input.prodquantity").each( function(){
			$(this).val("");
		});
		$(this).parent("td").parent("tr").next("tr.furtherDetalis").show();
		
		
	});
	
	$(".itemtrash").live( 'click', function(){
		//alert("");
		//--- product, childrow
		
		//alert( $(this).parent("div.product").html() );
		
		//$(this).parent("div.product").delete().remove();
		
		moveon = false;
		
		while(moveon==false){		
			if( $(this).parent("div.product").next("div").attr("class") == "childrow" ){
				//alert("must delete here");				
				//-- subtract gst and total
				
				
				cost = $(this).parent("div.product").next("div").children("div:nth-child(4)").html();
				gst = $(this).parent("div.product").next("div").children("div:nth-child(5)").children("input:nth-child(2)").val();
					/*
				$(".cartSubTotaltext").html( ($(".cartSubTotaltext").html() - cost.substring(2)).toFixed(2) );				
				$(".cartGSTtext").html( ($(".cartGSTtext").html() - gst).toFixed(2) );
				
				gtotal = (Number($(".cartTotaltext").html()) - cost.substring(2) + Number(gst)).toFixed(2);
				disc = gtotal * globalDisc;	
				
				$(".cartDisctext").html( (disc).toFixed(2) );				
				$(".cartTotaltext").html( (gtotal - disc).toFixed(2) );
				*/
				
				$(".cartSubTotaltext").html( ($(".cartSubTotaltext").html() - cost.substring(2)).toFixed(2) );
				$(".cartGSTtext").html( ($(".cartGSTtext").html() - gst).toFixed(2) );
					
				gtotal = ( Number($(".cartSubTotaltext").html()) + Number($(".cartGSTtext").html()) ).toFixed(2);
				
				disc = gtotal * globalDisc;					
				$(".cartDisctext").html( (disc).toFixed(2) );				
				$(".cartTotaltext").html( (gtotal - disc).toFixed(2) );
				
				$(this).parent("div.product").next("div").remove();
			}else{
				moveon = true;	
			}
		}
		
		cost = $(this).parent("div.product").children("div:nth-child(4)").html();
		gst = $(this).parent("div.product").children("div:nth-child(5)").children("input:nth-child(2)").val();
		
		$(".cartSubTotaltext").html( ($(".cartSubTotaltext").html() - cost.substring(2)).toFixed(2) );
		$(".cartGSTtext").html( ($(".cartGSTtext").html() - gst).toFixed(2) );
			
		//gtotal = ($(".cartSubTotaltext").html() - (Number(cost.substring(2)) + Number(gst))).toFixed(2);
		gtotal = ( Number($(".cartSubTotaltext").html()) + Number($(".cartGSTtext").html()) ).toFixed(2);
		disc = gtotal * globalDisc;	
		
		$(".cartDisctext").html( (disc).toFixed(2) );
		$(".cartTotaltext").html( (gtotal - disc).toFixed(2) );
			
			
		$(this).parent("div.product").remove();
		
		var itemsvalues = new Array();
		var gstvalues = new Array();
		
		$(".item").each( function(){
			itemsvalues[itemsvalues.length] = $(this).val();
			gstvalues[gstvalues.length] = $(this).next("input").val();
		});
		
		//alert(itemsvalues+"\n\n"+gstvalues);
		
		$.ajax({ 
		   type: "POST", 
		   url: "plugins/catering_cart/functions/updateCart.php", 
		   data: "cartTotal="+$(".cartTotaltext").html()+"&cartGST="+$(".cartGSTtext").html()+"&cartDisc="+$(".cartDisctext").html()+"&itemsvalues="+itemsvalues+"&gstvalues="+gstvalues, 
		   success: function(msg){ 
			 //alert( msg ); 
			 //alert( "cartTotal="+$(".cartTotaltext").html()+"&cartGST="+$(".cartGSTtext").html()+"&cartDisc="+$(".cartDisctext").html()+"&itemsvalues"+itemsvalues+"&gstvalues"+gstvalues );
		   } 
		 });
		 
	});
	
	$(".addtocart").live( 'click', function(){
		var order = "";
		
		order += "The following is being added to the order:\n\n";
		
		order += "Product ID: "+	$("."+$(this).attr("title")+"_id").val() + "\n" ;
		order += "Product Name: "+	$("."+$(this).attr("title")+"_name").val() + "\n" ;
		order += "Product Qty: "+	$("input[name='"+ $(this).attr("title")+"_qty" +"']").val() + "\n" ;
		order += "Product Comments: "+	$("input[name='"+ $(this).attr("title")+"_comments" +"']").val() + "\n" ;
		order += "Product Total Ex: "+	 ( $("input[name='"+ $(this).attr("title")+"_qty" +"']").val() * $("input[name='"+ $(this).attr("title")+"_cost" +"']").val() ).toFixed(2) + "\n" ;
		order += "Product GST: "+	 ( $("input[name='"+ $(this).attr("title")+"_qty" +"']").val() * $("input[name='"+ $(this).attr("title")+"_gst" +"']").val() ).toFixed(2) + "\n" ;
		
		if($("input[name='"+ $(this).attr("title")+"_qty" +"']").val()	== "" || $("input[name='"+ $(this).attr("title")+"_qty" +"']").val()	== "0"){
			$("input[name='"+ $(this).attr("title")+"_qty" +"']").val(1);
		}	
		
		name 	= $("."+$(this).attr("title")+"_name").val();
		qty 	= $("input[name='"+ $(this).attr("title")+"_qty" +"']").val();
		total 	= $("input[name='"+ $(this).attr("title")+"_qty" +"']").val() * $("input[name='"+ $(this).attr("title")+"_cost" +"']").val();
		gst 	= $("input[name='"+ $(this).attr("title")+"_qty" +"']").val() * $("input[name='"+ $(this).attr("title")+"_gst" +"']").val();
		
		comments = $("input[name='"+ $(this).attr("title")+"_comments" +"']").val();
		
		//-- add this hidden
		prod = $("."+$(this).attr("title")+"_id").val()+"~"+$("."+$(this).attr("title")+"_name").val()+"~"+$("input[name='"+ $(this).attr("title")+"_qty" +"']").val()+"~"+( $("input[name='"+ $(this).attr("title")+"_qty" +"']").val() * $("input[name='"+ $(this).attr("title")+"_cost" +"']").val() ).toFixed(2);
		
		prodrow = "  <div class='product'>"+
						"<div class='itemtrash'><img src='plugins/catering_cart/images/trash.png' width='16' align='absmiddle' /></div>"+
						"<div class='itemlabel'>"+name+"</div>"+
						"<div class='itemqty'>"+qty+"</div>"+
						"<div class='itemcost'>$ "+( total ).toFixed(2)+"</div>"+
						"<div class='clear'><input type='text' name='cartItem[]' value='"+prod+"' class='hiddenField item' /><input type='text' name='cartGST[]' value='"+gst+"' class='hiddenField itemgst' /></div>"+
					"</div>";
			
		$("#cartItems").append(prodrow);
		
		if(comments!=""){
			prodrow = "<div class='childrow'>"+
						"<div class='itemtrash'><img src='plugins/catering_cart/images/div.png' width='16' align='absmiddle' /></div>"+
						"<div class='itemlabel'>"+comments+"</div>"+
						"<div class='itemqty'></div>"+
						"<div class='itemcost'></div>"+
						"<div class='clear'><input type='text' name='cartItem[]' value='0~%20%20ins%20%20"+comments+"~0~0' class='hiddenField item' /><input type='text' name='cartGST[]' value='0' class='hiddenField itemgst' /></div>"+
						"</div>";
			
			$("#cartItems").append(prodrow);	
		}
			
		$(".cartSubTotaltext").html( (Number(total) + Number($(".cartSubTotaltext").html()) ).toFixed(2) );
		$(".cartGSTtext").html( (Number(gst) + Number($(".cartGSTtext").html())).toFixed(2) );
		
		gtotal = (Number($(".cartSubTotaltext").html()) + Number($(".cartGSTtext").html())).toFixed(2);
		disc = gtotal * globalDisc;
		
		$(".cartDisctext").html( (disc).toFixed(2) );	
		$(".cartTotaltext").html( (gtotal - disc).toFixed(2) );
		//--- the above is the root product
		
		if($(this).parent("td").parent("tr").prev("tr").attr("class") == "options"){
			//order += "\nIncluding the extra items:\n\n";
									
			$(this).parent("td").parent("tr").prev("tr.options").children("td").children("table").children("tbody").children("tr").each( function(){				
				if( $(this).children("td").children("span").children("input:nth-child(2)").val() ){				
					if( $(this).children("td:nth-child(3)").children("input:nth-child(1)").val() > 0 ){				
						/*
						order += "Extra ID: "+	$(this).children("td").children("span").children("input:nth-child(3)").val() + "\n" ;
						order += "Extra Name: &nbsp;&nbsp;+&nbsp;&nbsp;"+	$(this).children("td").children("span").children("input:nth-child(2)").val() + "\n" ;
						order += "Extra Qty: "+	$(this).children("td:nth-child(3)").children("input:nth-child(1)").val() + "\n" ;
						order += "Extra Total Ex: "+	 ( $(this).children("td:nth-child(3)").children("input:nth-child(1)").val() * $(this).children("td:nth-child(2)").children("input:nth-child(2)").val() ).toFixed(2) + "\n" ;
						order += "Extra GST: "+	 ( $(this).children("td:nth-child(3)").children("input:nth-child(1)").val() * $(this).children("td:nth-child(2)").children("input:nth-child(3)").val() ).toFixed(2) + "\n\n" ;
						*/
						
						extraname 	= "  +  "+	$(this).children("td").children("span").children("input:nth-child(2)").val();
						extraqty 	= $(this).children("td:nth-child(3)").children("input:nth-child(1)").val();
						extratotal 	= $(this).children("td:nth-child(3)").children("input:nth-child(1)").val() * $(this).children("td:nth-child(2)").children("input:nth-child(2)").val();
						extragst 	= $(this).children("td:nth-child(3)").children("input:nth-child(1)").val() * $(this).children("td:nth-child(2)").children("input:nth-child(3)").val();
						
						extra = $(this).children("td").children("span").children("input:nth-child(3)").val()+"~%20%20plus%20%20"+	$(this).children("td").children("span").children("input:nth-child(2)").val()+"~"+$(this).children("td:nth-child(3)").children("input:nth-child(1)").val()+"~"+( $(this).children("td:nth-child(3)").children("input:nth-child(1)").val() * $(this).children("td:nth-child(2)").children("input:nth-child(2)").val() ).toFixed(2);
						
						prodrow = "<div class='childrow'>"+
									"<div class='itemtrash'><img src='plugins/catering_cart/images/div.png' width='16' align='absmiddle' /></div>"+
									"<div class='itemlabel'>"+extraname+"</div>"+
									"<div class='itemqty'>"+extraqty+"</div>"+
									"<div class='itemcost'>$ "+( extratotal ).toFixed(2)+"</div>"+
									"<div class='clear'><input type='text' name='cartItem[]' value='"+extra+"' class='hiddenField item' /><input type='text' name='cartGST[]' value='"+extragst+"' class='hiddenField itemgst' /></div>"+
									"</div>";
						
						$("#cartItems").append(prodrow);
												
						$(".cartSubTotaltext").html( (Number(extratotal) + Number($(".cartSubTotaltext").html()) ).toFixed(2) );	
						$(".cartGSTtext").html( (Number(extragst) + Number($(".cartGSTtext").html())).toFixed(2) );													
						
						gtotal = (Number($(".cartSubTotaltext").html()) + Number($(".cartGSTtext").html()) ).toFixed(2);
						disc = gtotal * globalDisc;
						
						$(".cartDisctext").html( (disc).toFixed(2) );						
						$(".cartTotaltext").html( (gtotal - disc).toFixed(2) );
						
					}				
				}
			});
		}
		
		var itemsvalues = new Array();
		var gstvalues = new Array();
		
		$(".item").each( function(){
			thisstr = $(this).val();
			thisstr = thisstr.replace(/,/i, " ");
			itemsvalues[itemsvalues.length] = thisstr.replace(/&/i, "and");
			gstvalues[gstvalues.length] = $(this).next("input").val();
		});
		
		//alert(itemsvalues+"\n\n"+gstvalues);
		
		$.ajax({ 
		   type: "POST", 
		   url: "plugins/catering_cart/functions/updateCart.php", 
		   data: "cartSubTotal="+$(".cartSubTotaltext").html()+"&cartTotal="+$(".cartTotaltext").html()+"&cartGST="+$(".cartGSTtext").html()+"&cartDisc="+$(".cartDisctext").html()+"&itemsvalues="+itemsvalues+"&gstvalues="+gstvalues, 
		   success: function(msg){ 
			 //alert( msg ); 
			 //alert( "cartTotal="+$(".cartTotaltext").html()+"&cartGST="+$(".cartGSTtext").html()+"&cartDisc="+$(".cartDisctext").html()+"&itemsvalues"+itemsvalues+"&gstvalues"+gstvalues );
		   } 
		 });
		
		//--- the above is for production options
		
		//alert(order);
				
		
	});
	//furtherDetalis
});
