/* 
###################################################################################
# OK, this is the master "update purchase form for mecme
# that updates in real time the "total" box on the registration
# form. 

# The catch is, it assumes it's the first form on the page when it really will be 
# second, but sometimes the first. The point is, b/c of the sometimes login form
# on header, this will be 0 and 1 and so it needs a name. 
###################################################################################
*/



function updatePurchase(formName){
	
	var theForm = document.forms[formName];
	var subTotal = 0;
	for(i=0; i<theForm.elements.length; i++){
		var alertText = "";	
		var my = theForm.elements[i];
		alertText += "Element Type: " + my.type + "\n"
//		CHECKS FOR PRODUCTS AND QUANTITY
		if( my.type == "text" ){
			if( my.value != "" ){
				isProdItem = my.name.indexOf('prodItem');
				if( isProdItem == 0 ){
					product_information = my.name.split("_");				
					
					// MAKE adjustment for price that has decimal
					if( product_information.length == 4 ){
						// if there are 4 elements in this array, it means
						// there's a floating point in the price and that 
						// position 3 is the dollar amount and pos. 4 is the cents. 
						var prod_price = product_information[2] + "." + product_information[3];
					} else if( product_information.length == 3 ){
						var prod_price = product_information[2];
					}
					
					
					var quantity = my.value;
					prod_price = Number(prod_price);
					quantity = Number(quantity);
					var cost = prod_price*quantity;
					subTotal += cost;
					alertText += "Element Value: " + my.value + " Element Name: " + my.name + "\n";
				}		
			}
//		CHECKS FOR WORKSHOPS AND DISCOUNT ITEMS
		} else if( my.type == "checkbox" ){
			if( my.checked != false ){
				isWorkshop = my.name.indexOf('workshop');
				isDiscountItem = my.name.indexOf('discountItem');
				if( isDiscountItem == 0 ){
					var discount = my.value;
//					Should force this negative if not done in db.
					discount = Number(discount);
					if( discount >=0 ){
						discount = -discount;
					}
					subTotal += discount;
				}
				if( isWorkshop == 0 ){
					n = my.value.split("_");
					var wk_tuition = n[n.length-1];
					wk_tuition = Number(wk_tuition);
					subTotal += wk_tuition;
					alertText += "*Element Checked? " + my.checked + "  Element Value: " + my.value + "\n";
				} else {
					alertText += "Element Checked? " + my.checked + " Element Name: " + my.name + "\n";
				}
				
			}
//		LOGS WORKSHOP RANKING FOR FORM PROCESSING	
		} else if( my.type == "select-one" ){
			if( my.options[my.selectedIndex].text != "--" ){
				alertText += "Selected Option's Text: " + my.options[my.selectedIndex].text + "\n";
			} 
//		CHECKS FOR REGISTRANT TYPES AND 'PICK ONE' WORKSHOPS	
		} else if( my.type == "radio" ){
			if( my.checked != false ){
				if( my.name == "crsTuition" ){
					var n = my.value.split("_");
					var reg_id = n[0];
					var crs_tuition = n[1];
					crs_tuition = Number(crs_tuition);
					subTotal += crs_tuition;
					alertText += "Element Value: " + my.value + " Element Name: " + my.name + "\n";
				}
				isWorkshop = my.name.indexOf('workshop');
				if( isWorkshop == 0 ){
					n = my.value.split("_");
					var wk_tuition = n[n.length-1];
					wk_tuition = Number(wk_tuition);
					subTotal += wk_tuition;
					alertText += "*Element Value: " + my.value + " Element Name: " + my.name + "\n";
				} 	
			}
		}
	}
	
	// ROUND to precision. Interesting -- I'd have thought toPrecision(2) would give me two points
	// after the decimal, but it gives me 2 before the decimal, so you need to use 4
	//subTotal =subTotal.toPrecision(6)
	//alert(subTotal + " " + typeof(subTotal))
	// NOT quite working TODO hash this out.
	Math.round(subTotal * 100) / 100;
	//alert(subTotal + " " + typeof(subTotal))
	if( subTotal >= 0 ){
		document.purchases.total.value =  subTotal ;
	} else {
		document.purchases.total.value =  subTotal ;
		alert('Your total must be higher for this discount to be available, as negative totals will not be processed. ');
	}
	
}

