// JavaScript needed to operate the OrderForm class

function changeQuantity(id, amt, increment){
	var q = parseInt(jQuery("#quantity_"+id).val());
	(increment) ? q += amt : q = amt;
	if(q < 0) q = 0;
	jQuery("#quantity_"+id).val(q);
	jQuery("#quantity_label_"+id).text(q);
}

function resetQuantities(){
	jQuery("[id^='quantity_']").val('0');
	jQuery("[id^='rem_']").hide();
}

function changeQuantity(id, amt){
	var q = parseInt(jQuery("#quantity_"+id).val());
	var oldq = q;
	q += amt;
	if(q < 0) q = 0;
	jQuery("#quantity_"+id).val(q + '');
	jQuery("#quantity_label_"+id).text(q);
	
	if(q > 0 && oldq == 0){
		jQuery("#rem_"+id).show();
	}else if(q == 0 && oldq > 0){
		jQuery("#rem_"+id).hide();
	}
	
	updateTotals();
}

function updateTotals(){
	var subtotal = 0;
	var shipping = 0;
	
	for(var i=0; i<ids.length; i++){
		var id = ids[i];
		var q = parseInt(jQuery("#quantity_"+id).val());
		if(q == 0){
			// disable form elements so they don't get POSTed
			jQuery("#"+id+" input").attr("disabled", true);
		}else{
			jQuery("#"+id+" input").removeAttr("disabled");
		}
		var price = parseFloat(jQuery("#amt_"+id).val());
		var ship = parseFloat(jQuery("#shipping_"+id).val());
		
		subtotal += price * q;
		
		if(q > 0){
			if(shipping == 0 && q == 1){ // only ordered 1 item
				shipping += ship;
			}else{
				var tempQ = q;
				var actualShippingCost = 0;
				var shipAdd = parseFloat(jQuery("#shipadd_"+id).val());
				
				if(shipping == 0){ // first item in cart, use reg shipping cost for first item
					actualShippingCost = ship;
					tempQ--; // reduce quantity for multiplication below
				}
				actualShippingCost += shipAdd * tempQ;
				
				shipping += actualShippingCost;
			}
		}
	}
	
	var total = subtotal + shipping;
	
	if(subtotal.toFixed){
		subtotal = subtotal.toFixed(2);
		shipping = shipping.toFixed(2);
		total = (total).toFixed(2);
	}
	
	jQuery("#subtotal").text(subtotal);
	jQuery("#shipping").text(shipping);
	jQuery("#total").text(total);
	
}

function submitForm(){
	updateTotals();
	if(parseFloat(jQuery("#total").text()) == 0){
		alert("You must add at least one product above to proceed to checkout. Click the green add button to add at least one copy, then you may proceed to check out.");
		return;
	}
	
	var postIds = new Object();
	
	// Format for PayPal...
	// we must number each product sequentailly 1-N
	var num = 1;
	for(var i=0; i<ids.length; i++){
		var id = ids[i];
		var q = parseInt(jQuery("#quantity_"+id).val());
		if(q == 0) continue;
		
		// must calc shipping for each item here... it's less for additional items in cart
		if(num == 1 && q == 1){
			// first item, and only one ordered, reg shipping cost
		}else{
			// use addtnl item value
			var tempQ = q;
			var actualShippingCost = 0;
			var shipAdd = parseFloat(jQuery("#shipadd_"+id).val());
			
			if(num == 1){ // first item in cart, use reg shipping cost for first item
				actualShippingCost = parseFloat(jQuery("#shipping_"+id).val());
				tempQ--; // reduce quantity for multiplication below
			}
			actualShippingCost += shipAdd * tempQ;
			jQuery("#shipping_"+id).val(actualShippingCost);
		}
		
		// set names for PayPal here
		jQuery("#title_"+id).attr("name", "item_name_"+num);
		jQuery("#quantity_"+id).attr("name", "quantity_"+num);
		jQuery("#amt_"+id).attr("name", "amount_"+num);
		jQuery("#shipping_"+id).attr("name", "shipping_"+num);
		jQuery("#handle_"+id).attr("name", "handling_"+num);
		num++;
	}
	
	jQuery("#orderform").submit();
}

