var customMeters;
var customFeet;
var standardLength;
var minimumLength;
var minimumPrice;
var rememberedIndex;
var logicTrip = 0; 

function setPricing() {
	if (document.getElementById("id[2]")){
		standardLength = document.getElementById("id[2]");
		if(standardLength.options.length-2 >= 0) {
		    tmpMinimumLength = standardLength.options[standardLength.options.length-2].firstChild.nodeValue;		    
		} else {
		    tmpMinimumLength = standardLength.options[0].firstChild.nodeValue;		    
		}

		tmpMinumumLength = tmpMinimumLength.substring(0,tmpMinimumLength.indexOf(" "));
		minimumLength = parseFloat(tmpMinumumLength);
		if(standardLength.options.length-2 >= 0) {
		    tmpMinimumPrice = standardLength.options[standardLength.options.length-2].firstChild.nodeValue;		    
		} else {
            tmpMinimumPrice = standardLength.options[0].firstChild.nodeValue;		    
		}

		tmpMinimumPrice = tmpMinimumPrice.substring(tmpMinimumPrice.indexOf("(") + 1, tmpMinimumPrice.length - 1);
		tmpMinimumPrice = tmpMinimumPrice.replace(/[,\+\$]/g,"");
		minimumPrice = parseFloat(tmpMinimumPrice);
	}
//PW.Mac add gives custom meters of feet empty value if other is selected.  Needed for logic at Loc0001
	if (document.getElementById("id[txt_1]")) {
		customMeters = document.getElementById("id[txt_1]");
		customFeet = "";
	}
	if (document.getElementById("id[txt_6]")) {	
	    customFeet = document.getElementById("id[txt_6]");
    	customMeters = "";
	}
// end add


    var pricingInfo = document.getElementById("pricing-info");
    var pricingInfoInputs = pricingInfo.getElementsByTagName("input");
    for(var i=0; i<pricingInfoInputs.length; i++) {
        if(pricingInfoInputs[i].name.match(/id\[.*?\]/)) {
            pricingInfoInputs[i].onkeyup = tallyTotal; }
    }
    var pricingInfoSelects = pricingInfo.getElementsByTagName("select");
    for(var j=0; j<pricingInfoSelects.length; j++) {
        if(pricingInfoSelects[j].name.match(/id\[.*?\]/)) {
            pricingInfoSelects[j].onchange = tallyTotal; }
    }

}
addLoadEvent(setPricing);

function tallyTotal() {
//PW.Mac add - if custom value less than min offered, no change to drop down Loc0001
	if(this.type == "text") {

		if(customMeters.value <= minimumLength || customFeet.value <= minimumLength) {
      		rememberedIndex = standardLength.selectedIndex;
      		standardLength.selectedIndex = standardLength.options.length-standardLength.options.length; 
	  		logicTrip = 1;//end add
		} else if(standardLength.selectedIndex != standardLength.options.length-1) {
       		rememberedIndex = standardLength.selectedIndex;
      		standardLength.selectedIndex = standardLength.options.length-1; 
			logicTrip = 0;
		}
	}
		
	if(this == standardLength && customMeters != undefined) {
        customMeters.value = ""; 
		}
    if(this == standardLength && customFeet != undefined) {
        customFeet.value = ""; }

	// var runningTotal = 0;
	var getBasePrice = document.getElementById("basePrice"); 
	getBasePrice = getBasePrice.getAttribute("value")
	var runningTotal = parseFloat(getBasePrice);
	
	
    var pricingInfo = document.getElementById("pricing-info");
    var pricingInfoInputs = pricingInfo.getElementsByTagName("input");
    for(var i=0; i<pricingInfoInputs.length; i++) {
        if(pricingInfoInputs[i].name.match(/id\[.*?\]/)) {
            if(pricingInfoInputs[i].type == "text" && pricingInfoInputs[i].value != '') {
				if (document.getElementById("id[txt_1]")) {
					chngLenBy = .5;
				}
				if (document.getElementById("id[txt_6]")) {	
					chngLenBy = 1;
				}
				
                var chngPriceBy = pricingInfoInputs[i].getAttribute("altervalue");
				if(logicTrip != 1) {
					var lengthDiff = pricingInfoInputs[i].value-minimumLength;
					lengthDiff = Math.round(lengthDiff*100)/100;
					if (lengthDiff % chngLenBy	> 0) {
							lengthDiff = lengthDiff / chngLenBy;

							lengthDiff = lengthDiff.toString();
							lengthDiff = lengthDiff.split(".");
							lengthDiff = parseFloat(lengthDiff[0]); // + chngLenBy;
							runningTotal += ((lengthDiff) * chngPriceBy) + minimumPrice;
							chngPriceBy = parseFloat(chngPriceBy);
							runningTotal += chngPriceBy;
						} else {
							runningTotal += (((lengthDiff) / chngLenBy) * chngPriceBy) + minimumPrice; 
						}
				}
			}
        }
    }
    var pricingInfoSelects = pricingInfo.getElementsByTagName("select");
    for(var j=0; j<pricingInfoSelects.length; j++) {
        if(pricingInfoSelects[j].name.match(/id\[.*?\]/)) {
            var text = pricingInfoSelects[j].options[pricingInfoSelects[j].selectedIndex].firstChild.nodeValue;
            var priceStart = text.indexOf("(");
            var priceEnd = text.indexOf(")");
            if(priceStart != -1) {
// PW.Mac add - check for len... if it exists, process price differently

				if(this.type == "text") {
					
					if(logicTrip != 1) {
						var price = parseFloat(text.substring(priceStart+1, priceEnd).replace(/[^\d\.]/g, ''));
//						var price = 0;	
					} else {
						// the custom length value is too low.  If it's empty, run the prices.
						if(pricingInfoInputs[1].name.match(/id\[.*?\]/)) {
							if (pricingInfoInputs[1].value == 0) {
								var price = parseFloat(text.substring(priceStart+1, priceEnd).replace(/[^\d\.]/g, ''));
							} else {						
								var price = 0;	
							}
						}
					}
				} else {
						var price = parseFloat(text.substring(priceStart+2, priceEnd).replace(/[^\d\.]/g, ''));				
				}
// end add

	                runningTotal += price; 
			}
        }
    }
    
    setTotal(runningTotal);
}

function setTotal(priceIn) {
    var runningTotal = document.getElementById("running_total");
    if(priceIn > 0) {
        var formatted = new NumberFormat(priceIn).toFormatted();
        runningTotal.innerHTML = "<span>Price: $"+ formatted +"</span>";
    } else  if(logicTrip = 1) {
        runningTotal.innerHTML = "<font color='red'>Custom value too low.</font>";
    } else {
        runningTotal.innerHTML = "Price: $0.00";	
	}
}