// 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
// Also validates form

// Version 2.33

function mmCalc(thatForm){
	var intFormFields, intNumberDropDowns, curPriceArray, strProductElement, intUnits;
	var strProductList, intUserID, intFormUserID, strPriceBandsArray, intPriceBands, strPriceBandsDivisionsArray;
	var blnPriceBands = false;
	var blnPriceBandsUpper = false;
	var blnElementInvalid = false;
	var strLocalMainProductName = strMainProductName;
	if (strLocalMainProductName == null) {strLocalMainProductName = thatForm.strMainProductName.value};
	var blnSubmit = false;
	var strOtherCurrencySymbol = '';
	if(thatForm.strOtherCurrencySymbol){strOtherCurrencySymbol = thatForm.strOtherCurrencySymbol.value};
	var curPriceOtherCurrencyRate = 1;
	if(thatForm.dbOtherCurrencyRate){curPriceOtherCurrencyRate = thatForm.dbOtherCurrencyRate.value};

	if(arguments.length>1){blnSubmit = true;}
	
	intNumberDropDowns = parseInt(thatForm.elements['numberSelections'].value);
	intFormFields = thatForm.elements.length;
	intNumberDropDowns += 1;

	curPrice = 0;
	intUnits = 0;
	strProductList = "";
	intUserID = "9466853";
	intFormUserID = thatForm.userid.value;
	
		// Checks userid valid
	if (intFormUserID != intUserID){
		thatForm.strMessage.value="Please use correct User ID "+intUserID;
		thatForm.curCost.value="";
		return false;
	}
	
	// Calculates quantity bands if present
	for (i=0;i<intFormFields;i++){
		if (thatForm.elements[i].name.indexOf('qtyBands') > -1){
		
			strPriceBandsArray = thatForm.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 (thatForm.elements[i].name.indexOf('qtyBandsRoundUp') > -1){
			blnPriceBandsUpper = thatForm.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 ((thatForm.qty.value > intCurrentQtyCheck) && (thatForm.qty.value <= intQtyArray[j])){
		curPrice = curPriceArray[j];
		if (blnPriceBandsUpper) {
			thatForm.qty.value = intQtyArray[j];
		}	
		break;
	}
	intCurrentQtyCheck = intQtyArray[j];
}

if (blnPriceBands == true && curPrice == 0){
	curPrice = curPriceArray[intPriceBands-1];
	curPriceArray[j] = curPrice;
	if (blnPriceBandsUpper) {
		thatForm.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
			// Checks if element is checkbox and adds to price if checked
			var strElementType = thatForm.elements[i].type;
			
			if (strElementType=='checkbox' || strElementType=='radio'){
				if (thatForm.elements[i].checked){
					strProductElement = thatForm.elements[i].value;
				}else{
					strProductElement = ":0.00";
				}
			}else{
				strProductElement = thatForm.elements[i].value;
			}
			
			// Checks productItem i.e. no price options
			if (thatForm.elements[i].name.indexOf('productItem') > -1){
				strProductElement = strProductElement + ":0.00";
			}
			
			// Checks if product is required
			if (thatForm.elements[i].name.indexOf('productItemRequired') > -1){
			
				strProductElement = strProductElement + ":0.00";
				if (thatForm.elements[i].value=='') {blnElementInvalid = true;}
			}
			
			if (thatForm.elements[i].disabled==false){		
				curPriceArray = strProductElement.split(":");
				if (curPriceArray[1]!=null && isNaN(curPriceArray[1])==false) {curPrice += parseFloat(curPriceArray[1])};
				if (curPriceArray[1]!=null && isNaN(curPriceArray[1])==false) {

					if (trim(curPriceArray[0])!='') {
						strProductList = strProductList + strProductDelimeter + " " + curPriceArray[0];
					}
				}
			}
			
			// Gets units per options
			if (curPriceArray[2]!= null) intUnits += parseFloat(curPriceArray[2]);
		
			for (var p = 1; p<intNumberDropDowns; p++) {						
				if ((thatForm.elements[i].name.indexOf('product'+p) > -1)) {
					
					// Ensures product type is selected
					if ((thatForm.elements[i].value == "")) {
						thatForm.strMessage.value=strTellCustomer;
						thatForm.curCost.value="";
						return false;
					}
				}
			}
		}
	}
	 
	// Creates price for product
	curPrice = currency(curPrice);
	curDisplayPrice = currency(curPrice*thatForm.qty.value);
	curPriceOtherCurrencyRate  = currency(curDisplayPrice*curPriceOtherCurrencyRate);

	// Displays price
	thatForm.curCost.value = strCurrency+curDisplayPrice;
	thatForm.price.value = curPrice;
	if (strOtherCurrencySymbol != ''){
		thatForm.strMessage.value = "(Approx. Total "+strOtherCurrencySymbol+curPriceOtherCurrencyRate+")";
	}else{
		thatForm.strMessage.value = "Total";
	}
	
	if (blnPriceBands) {
		thatForm.strMessage.value="Total @ "+strCurrency+curPriceArray[j]+" per item";
		if (blnPriceBandsUpper) {thatForm.strMessage.value=thatForm.strMessage.value+' in packs of '+intQtyArray[j]}

	}
	
	// Updates Mals Units field
	thatForm.units.value = intUnits;
	
	// Updates Mals product field
	strProductList = strLocalMainProductName + strProductList;
	if (blnPriceBands) {strProductList = thatForm.product.value;}
	strProductList = cleanProduct(strProductList);
	thatForm.product.value = strProductList;

	// Submits form (if applicable) if all required fields have a value
	if (blnSubmit){
		if(blnElementInvalid){
			alert(strRequiredFieldText);
			return false;
		}
	}
	return true;
}

function cleanProduct(strProduct){
	// Gets rid of all [strArrayNames] if used in 
	// conjunction with Dynamic List Script
	// Also, gets rid of (strImageName) if used in
	// conjunction with Image Options Script
	
	var intArrayNameStart = 0;
	var intArrayNameEnd = 0; 
	var strProductTemp = "";
	var strProductTempDelete = "";
	
	strProductTemp = strProduct;
	
	// Loops each text character in string and removes [arraynames]
	for (var i=0;i<strProduct.length;i++){
		if (strProduct.substring(i, i+1)=='['){intArrayNameStart = (i)};
		if (strProduct.substring(i, i+1)==']'){intArrayNameEnd = (i+1)};
		
		if (intArrayNameEnd>0){
			strProductTempDelete = strProduct.substring(intArrayNameStart, intArrayNameEnd);
			strProductTemp = strProductTemp.replace(strProductTempDelete, "");
			intArrayNameEnd = 0;
			intArrayNameStart = 0;
		}
	}
	
	for (var i=0;i<strProduct.length;i++){
		if (strProduct.substring(i, i+1)=='('){intArrayNameStart = (i)};
		if (strProduct.substring(i, i+1)==')'){intArrayNameEnd = (i+1)};
		
		if (intArrayNameEnd>0){
			strProductTempDelete = strProduct.substring(intArrayNameStart, intArrayNameEnd);
			strProductTemp = strProductTemp.replace(strProductTempDelete, "");
			intArrayNameEnd = 0;
			intArrayNameStart = 0;
		}
	}

	return strProductTemp;
}

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); 
} 

function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}

function getIndex(thatForm, thatElement) {
	var intFormFields = thatForm.elements.length;
    for (var i=0;i<intFormFields;i++){
        if (thatElement == thatForm.elements[i]) return i;
	}
    return -1;
}
