// This mmCalc Function goes through each field in a form
// checks if productX has a value, if so it builds the cart
// and totals the price. If productX does not have a value
// no price is shown and the customer is told to select a value

function mmCalc(that){
	var intFormFields, intNumberDropDowns, curPriceArray, strProductElement
	var strProductList, intUserID, intFormUserID, strPriceBandsArray, intPriceBands, strPriceBandsDivisionsArray
	var blnPriceBands = false
	var blnPriceBandsUpper = false
	
	intNumberDropDowns = parseInt(that.elements['numberSelections'].value);
	intFormFields = that.elements.length;
	intNumberDropDowns += 1;

	curPrice = 0;
	strProductList = "";
	intUserID = "9179684";
	intFormUserID = that.userid.value;
	
		// Checks userid valid
	if (intFormUserID != intUserID){
		that.strMessage.value="Please use correct User ID "+intUserID;
		that.curCost.value="";
		return false;
	}
	
	// Calculates quantity bands if present
	for (i=0;i<intFormFields;i++){
		if (that.elements[i].name.indexOf('qtyBands') > -1){
		
			strPriceBandsArray = that.elements[i].value.split("~");
			intPriceBands = strPriceBandsArray.length;
			if (intPriceBands > 0) {
				blnPriceBands = true;
			}else{
				break;			
			}
			
			var intQtyArray = new Array(intPriceBands-1);
			var curPriceArray = new Array(intPriceBands-1);

			// Create price and strProductList
			for (p=0;p<intPriceBands;p++){
				strPriceBandsDivisionsArray = strPriceBandsArray[p].split(":");
				intQtyArray[p] = strPriceBandsDivisionsArray[0] - 0;
				curPriceArray[p] = strPriceBandsDivisionsArray[1] - 0;
			}
			
			break;
		}
	}
	
	// Is upper limit set?
	for (i=0;i<intFormFields;i++){
		if (that.elements[i].name.indexOf('qtyBandsRoundUp') > -1){
			blnPriceBandsUpper = that.elements[i].value.toUpperCase();
			if (blnPriceBandsUpper == 'TRUE'){
				blnPriceBandsUpper = true;
			}else{
				blnPriceBandsUpper = false
			}
		}
	}
	
// Calculates price if price bands given
var intCurrentQtyCheck = -1;
			
for (j=0;j<intPriceBands;j++){
	if ((that.qty.value > intCurrentQtyCheck) && (that.qty.value <= intQtyArray[j])){
		curPrice = curPriceArray[j];
		if (blnPriceBandsUpper) {
			that.qty.value = intQtyArray[j];
		}	
		break;
	}
	intCurrentQtyCheck = intQtyArray[j];
}

if (blnPriceBands == true && curPrice == 0){
	curPrice = curPriceArray[intPriceBands-1];
	curPriceArray[j] = curPrice;
	if (blnPriceBandsUpper) {
		that.qty.value = intQtyArray[intPriceBands-1];
		intQtyArray[j] = intQtyArray[intPriceBands-1];
	}
}

	if (blnPriceBands == false){

		// Checks each productX drop-down
		for (var i = 1; i<intFormFields; i++) {
			
			// Gets price and name of product	
			strProductElement = that.elements[i].value;
			curPriceArray = strProductElement.split(":");
			if (curPriceArray[1]!=null) curPrice += parseFloat(curPriceArray[1]);
			if (curPriceArray[1]!=null) {
				if (curPriceArray[0]!='') strProductList = strProductList + strProductDelimeter + " " + curPriceArray[0];
			}
		
			for (var p = 1; p<intNumberDropDowns; p++) {						
				if ((that.elements[i].name.indexOf('product'+p) > -1)) {
					
					// Ensures product type is selected
					if ((that.elements[i].value == "")) {
						that.strMessage.value=strTellCustomer;
						that.curCost.value="";
						return false;
					}
				}
			}
		}
	}
	 
	// Creates price for product

	curPrice = Math.ceil(curPrice/.05);
	curPrice*=.05;
	curDisplayPrice = currency(curPrice*that.qty.value);

	// Displays price
	that.curCost.value = strCurrency+curDisplayPrice;
	that.price.value = curPrice;
	that.strMessage.value = "Total";
	if (blnPriceBands) {
		that.strMessage.value="Total @ "+strCurrency+curPriceArray[j]+" per item";
		if (blnPriceBandsUpper) {that.strMessage.value=that.strMessage.value+' in packs of '+intQtyArray[j]}

	}
	
	// Updates Mals product field
	strProductList = strMainProductName + strProductList;
	if (blnPriceBands) {strProductList = that.product.value;}
	that.product.value = strProductList;
	return true;
}

function currency(dbAmount) { 
	// returns the amount in the .99 format 
	dbAmount -= 0; 
	dbAmount = (Math.round(dbAmount*100))/100; 
	return (dbAmount == Math.floor(dbAmount)) ? dbAmount + '.00' : ( (dbAmount*10 == Math.floor(dbAmount*10)) ? dbAmount + '0' : dbAmount); 
} 

