	function autoRate(method,inputEl, mileageUpdateField, errorFieldId, mileageCalcdId) {
	//alert("in auto rate");
		var url = "/LTMSLite/autorate?method=" + method;
		if ("13" == method) {
		    var inputValue = "";
		    if (inputEl.type == "text") {
		    	inputValue = inputEl.value;
		    } else {
		    	inputValue = getSelectedValue(inputEl.id);
		    }
		    if (isEmpty(inputValue)) return;
			url += "&paramName=" + escape(inputEl.id) + "&paramValue=" + escape(inputValue);
		}
		else if ("2" == method) {
		    disableButton('addChargeButton','images/smAddClicked.gif');
		    var chargeCode = getValue("add_chargeCode");
		    if (isEmpty(chargeCode)) {
		        clearAddChargeFields();
		    	return;
		    } 
			url += "&addChargeCode=" + escape(chargeCode);
		} else if ("15" == method) {
			url += "&updateFieldId=" + escape(mileageUpdateField) + "&errorFieldId=" + errorFieldId+ "&mileageCalcd=" + mileageCalcdId;
		} else if ("addCharge" == method) {
		    disableButton('addChargeButton','images/smAddClicked.gif');
		    var chargeCode = getValue("add_chargeCode");
		    if (isEmpty(chargeCode)) {
		        setInnerHTML('addChargeError','** Enter a charge code the press the \'Tab\' key.');
		        return;
		    }
			var qtyInputEl = document.getElementById("add_qtyInput");
			if (qtyInputEl) {
				var qty = getValue("add_qtyInput");
				url += "&add_qty=" + escape(qty);
			}
			var unitPriceInputEl = document.getElementById("add_unitPriceInput");
			if (unitPriceInputEl) {
				var unitPrice = getValue("add_unitPriceInput");
				if (isEmpty(unitPrice)) {
					setInnerHTML('addChargeError',"** Unit Price is a required field.");
					enableButton('addChargeButton','images/smAdd.gif','javascript:autoRate("'+method+'");');
					return;
				} else {
					url += "&add_unitPrice=" + escape(unitPrice);
				}
			}
			var extAmtInputEl = document.getElementById("add_extAmtInput");
			if (extAmtInputEl) {
				var extAmt = getValue("add_extAmtInput");
				if (isEmpty(extAmt)) {
					setInnerHTML('addChargeError',"** Ext. Price is a required field.");
					enableButton('addChargeButton','images/smAdd.gif','javascript:autoRate("'+method+'");');
					return;
				} else {
					url += "&add_extAmt=" + escape(extAmt);
				}
			}
			var reasonInputEl = document.getElementById("add_reasonInput");
			if (reasonInputEl) {
				var reason = getSelectedValue("add_reasonInput");
				if (isEmpty(reason)) {
					setInnerHTML('addChargeError',"** Reason is a required field.");
					enableButton('addChargeButton','images/smAdd.gif','javascript:autoRate("'+method+'");');
					return;
				} else {
					url += "&add_reason=" + escape(reason);
				}
			}
		} else if ("14" == method) {
		    disableButton('addChargeButton','images/smAddClicked.gif');
		    var chargeCode = getValue("add_chargeCode");
		    if (isEmpty(chargeCode)) {
		        setInnerHTML('addChargeError','** Enter a charge code the press the \'Tab\' key.');
		        return;
		    }
			var qtyInputEl = document.getElementById("add_qtyInput");
			if (qtyInputEl) {
				var qty = getValue("add_qtyInput");
				if (isEmpty(qty)) {
					setInnerHTML('addChargeError',"** Qty is a required field.");
					enableButton('addChargeButton','images/smAdd.gif','javascript:autoRate("'+method+'");');
					return;
				} else {
					url += "&add_qty=" + escape(qty);
				}
			}
			var unitPriceInputEl = document.getElementById("add_unitPriceInput");
			if (unitPriceInputEl) {
				var unitPrice = getValue("add_unitPriceInput");
				if (isEmpty(unitPrice)) {
					setInnerHTML('addChargeError',"** Unit Price is a required field.");
					enableButton('addChargeButton','images/smAdd.gif','javascript:autoRate("'+method+'");');
					return;
				} else {
					url += "&add_unitPrice=" + escape(unitPrice);
				}
			}
			var extAmtInputEl = document.getElementById("add_extAmtInput");
			if (extAmtInputEl) {
				var extAmt = getValue("add_extAmtInput");
				url += "&add_extAmt=" + escape(extAmt);
			}
			var reasonInputEl = document.getElementById("add_reasonInput");
			if (reasonInputEl) {
				var reason = getSelectedValue("add_reasonInput");
				url += "&add_reason=" + escape(reason);
			}
		}
		else if ("delete" == method) {
		    changeImage(inputEl, 'images/smDeleteClicked.gif');
		    var elId = inputEl.id;
		    var underIndex = elId.indexOf("_");
		    var chargeCode = elId.substring(underIndex + 1);
		    url += "&chargeCode=" + escape(chargeCode);
		} else if ("7" == method) {
			var chargeDesc = getValue('lookupChargeDesc');
			if (isEmpty(chargeDesc)) {
				setValue('lookupChargeDescError','** Enter a partial description');
				return;
			} else {
			  setValue('lookupChargeDescError','');
			  url += "&lookupChargeDesc=" + escape(chargeDesc);
			}
		} else if ("accept" == method) {
		  if (!isEmpty(getValue('add_chargeCode'))) {
		     if (confirm("There is an unadded charge.  Do you want to add it before you accept ?")) {
		     	autoRate("addCharge");
		     	return;
		     } else {
		     	clearAddChargeFields();
		     }
		  }
		  url += "&commentsEntered=" + escape(getValue('commentsEntered'));
		}
		//alert("before disable accept button");
		else if ("1" == method) {
		  if (!isEmpty(getValue('add_chargeCode'))) {
		     if (confirm("There is an unadded charge.  Do you want to add it before you auto rate ?")) {
		     	autoRate("addCharge");
		     	return;
		     } else {
		     	clearAddChargeFields();
		     }
		  }
		  disableButton('acceptButton','images/acceptClicked.gif');
		}  
		else if ('saveEstimatorInfo' == method) {
			url += '&reqLoadDate=' + escape(getValue('reqLoadDate'));
		}
	    var rateToTariff = document.getElementById('forceratetotariff').checked ? 'Y' : 'N';
	    var hazmat = document.getElementById('hz').checked ? 'Y' : 'N';
	    var permitLoad = document.getElementById('pl').checked ? 'Y' : 'N';
	    var specEquip = document.getElementById('se').checked ? 'Y' : 'N';
	    var backhaul = document.getElementById('bh').checked ? 'Y' : 'N';
	    //alert ("rate to tariff is " + rateToTariff);
	    url += "&miles=" + escape(getValue('arEstTotalMileage')) + "&weight=" + escape(getValue('arEstTotalWeight')) + "&commType=" + getSelectedRadioValue('arRatingCommType')
		+ "&rateToTariff=" + rateToTariff + "&hazmat=" + escape(getSelectedValue('hazmatModifier')) + "&backhaul=" + escape(getSelectedValue('backhaulModifier')) + "&permitLoad=" + escape(getSelectedValue('permitLoadModifier'))
		+ "&specEquip=" + escape(getSelectedValue('specEquipModifier')) + "&truckType=" + escape(getValue('arRatingTruckType')) + "&hazmatCk=" + hazmat + "&specEqCk=" + specEquip
		+ "&permLdCk=" + permitLoad + "&backhaulCk=" + backhaul + "&autoRateUsage=" + escape(getValue('autoRateUsage')) + "&mileageOverridden=" + getValue("arMileageOverridden");
		//alert ("url is " + url);
		sendAjaxRequest(url);
	}
	function addChargeUpdate() {
		autoRate('14');
	}
	function repopulateModifiers() {
	    var rateToTariff = document.getElementById('forceratetotariff').checked ? "Y" : "N";
		var url = "/LTMSLite/autorate?method=repopulateModifiers&rateToTariff=" + rateToTariff;
		sendAjaxRequest(url);
	}
	function clearAddChargeFields() {
		setInnerHTML('addChargeError','');
	    setValue("add_chargeCode","");
		setInnerHTML('add_qty','&nbsp;');
		setInnerHTML('add_uom','&nbsp;');
		setInnerHTML('add_unitPrice','&nbsp;');
		setInnerHTML('add_extAmt','&nbsp;');
		setInnerHTML('add_type','&nbsp;');
		setInnerHTML('add_reason','&nbsp;');
		setInnerHTML('add_desc','&nbsp;');
		disableButton('addChargeButton','images/smAddClicked.gif');
	
	}
	function updateAddChargeFields() {
	    var url = "";
		var qtyInputEl = document.getElementById("add_qtyInput");
		if (qtyInputEl) {
			var qty = getValue("add_qtyInput");
			if (isEmpty(qty)) {
				setInnerHTML('addChargeError',"** Qty is a required field.<br/>");
			} else {
				url += "&add_qty=" + escape(qty);
			}
		}
		var unitPriceInputEl = document.getElementById("add_unitPriceInput");
		if (unitPriceInputEl) {
			var unitPrice = getValue("add_unitPriceInput");
			if (isEmpty(unitPrice)) {
				setInnerHTML('addChargeError',"** Unit Price is a required field.<br/>");
			} else {
				url += "&add_unitPrice=" + escape(unitPrice);
			}
		}
		var extAmtInputEl = document.getElementById("add_extAmtInput");
		if (extAmtInputEl) {
			var extAmt = getValue("add_extAmtInput");
			if (isEmpty(extAmt)) {
				setInnerHTML('addChargeError',"** Ext. Price is a required field.<br/>");
			} else {
				url += "&add_extAmt=" + escape(extAmt);
			}
		}
		var reasonInputEl = document.getElementById("add_reasonInput");
		if (reasonInputEl) {
			var reason = getSelectedValue("add_reasonInput");
			if (isEmpty(reason)) {
				setInnerHTML('addChargeError',"** Reason is a required field.<br/>");
			} else {
				url += "&add_reason=" + escape(reason);
			}
		}
		return url;
	}
	function accept() {
		autoRate('accept');
	}
	function showAutoRateDiv(leftOffset, topOffset) {
		var anchorEl = document.getElementById('autoRateAnchor');
		var autoRateDiv = document.getElementById("autoRateDiv");
		autoRateDiv.style.left = (getLeft(anchorEl) + leftOffset) + "px";
		autoRateDiv.style.top = (getTop(anchorEl) + topOffset) + "px";
		autoRateDiv.style.display = "";
	}
	function hideAutoRateDiv() {
		changeImage(document.getElementById('calcEstPriceButton'), 'images/calcEstPrice.gif');
		var autoRateDiv = document.getElementById("autoRateDiv");
		autoRateDiv.style.display = "none";
	}
	function setModifierOptions(selectId, modOptions, selectedIndex) {
		var selectEl = document.getElementById(selectId);
		selectEl.length = modOptions.length + 1;
		for (var i = 0; i < modOptions.length; i++) {
			var optionValue =  modOptions[i][0] + "_" +modOptions[i][1] + "_" + modOptions[i][2];
			var optionText = modOptions[i][0] + "-" + modOptions[i][2] + "-" + modOptions[i][3] + "-" + modOptions[i][4];
			var nextOption = new Option();
			nextOption.value = optionValue;
			nextOption.text = optionText;
			selectEl.options[i + 1] = nextOption;
		}
		selectEl.selectedIndex = selectedIndex;
	}
	function updateDetailTable(details) {
		var detailDiv = document.getElementById("arDetails");
		detailDiv.innerHTML = "";
		var tableEl = document.createElement("table");
		tableEl.style.borderCollapse = "collapse";
		tableEl.style.width = "100%";
  		var thead = document.createElement("thead");
   		thead.style.height = "0px";
    	var tbody = document.createElement("tbody");
    	var tfoot = document.createElement("tfoot");
    	tfoot.style.height = "0px";
    	tableEl.appendChild(thead);
    	tableEl.appendChild(tfoot);
    	tableEl.appendChild(tbody);
    	detailDiv.appendChild(tableEl);
    	if (details.length > 0) {
    	  var errorRow = document.createElement("tr");
    	  var errorCell = document.createElement("td");
    	  errorCell.className = "errors";
    	  errorCell.colSpan = 9;
    	  errorCell.id = "detailErrors";
    	  errorRow.appendChild(errorCell);
    	  var instructionsRow = document.createElement("tr");
    	  var instructionsCell = document.createElement("td");
    	  instructionsCell.className = "instructions";
    	  instructionsCell.style.textAlign = "center";
    	  instructionsCell.innerHTML = "Enter changes, then press 'Tab' to update Total.";
    	  instructionsCell.colSpan = 9;
    	  instructionsCell.style.borderBottom =  "2px solid gray";
    	  instructionsRow.appendChild(instructionsCell);
    	  tbody.appendChild(errorRow);
    	  tbody.appendChild(instructionsRow);
    	}
    	for (var i = 0; i < details.length; i++) {
    	   var rowEl = document.createElement("tr");
    	   rowEl.className = "arDetailRow";
    	   tbody.appendChild(rowEl);
    	   var chargeCode = details[i][0];
    	   var overrideAllowed = "Y" == details[i][7];
    	   var isLineHaul = "LH" == chargeCode;
    	   var calcType = details[i][5];
    	   var percentageBased = "P" == calcType;
    	   var isModifier = "M" == details[i][10];
    	   var deleteButtonCell = document.createElement("td");
    	   deleteButtonCell.className = "arDetailCell";
    	   deleteButtonCell.style.width = "50px";
    	   var deleteAnchor = document.createElement("a");
    	   deleteAnchor.href = "javascript:autoRate('delete',document.getElementById('deleteButton_" + chargeCode + "'));";
    	   deleteAnchor.className = "imageAnchor";
    	   deleteButtonCell.appendChild(deleteAnchor);
    	   if (!isModifier) {
    	   		var deleteImage = document.createElement("img");
    	   		deleteImage.src = "images/smDelete.gif";
    	   		deleteImage.height = "17";
    	   		deleteImage.width = "50";
    	   		deleteImage.border = "0";
    	   		deleteImage.id="deleteButton_" + chargeCode;
    	   		deleteImage.onmousedown = "changeImage(this,'images/deleteClicked.gif')";
    	   		deleteAnchor.appendChild(deleteImage);
    	   } else {
    	   		deleteButtonCell.innerHTML = "&nbsp;";
    	   }
   	   	   var chargeCodeCell = document.createElement("td");
       	   chargeCodeCell.style.width = "50px";
       	   chargeCodeCell.className = "arDetailCell";
    	   chargeCodeCell.innerHTML = chargeCode;
    	   var qty = details[i][1];
    	   var qtyCell = document.createElement("td");
    	   qtyCell.style.width = "65px";
    	   qtyCell.style.textAlign = "right";
    	   qtyCell.className = "arDetailCell";
    	   if (overrideAllowed && !percentageBased) {
    	      var qtyInputHtml = "<input type=\"text\" class=\"arQtyText\" name=\"qty_"+ chargeCode+"\" id=\"qty_"+ chargeCode+"\" value=\"" + qty + "\" onchange=\"autoRate('13',this);\" />" ;    	   	       	      
     	      qtyCell.innerHTML = qtyInputHtml;
    	      qtyCell.style.paddingRight = "1px";
    	   } else {
    	   	  qtyCell.style.paddingRight = "2px";
    	   	  qtyCell.innerHTML = isEmpty(qty) ? "&nbsp;" : qty;
    	   }
    	   var unit = details[i][2];
       	   var unitCell = document.createElement("td");
       	   unitCell.style.width = "50px";
       	   unitCell.style.textAlign = "center";
       	   unitCell.className = "arDetailCell";
    	   unitCell.innerHTML = isEmpty(unit) ? "&nbsp;" : unit;
    	   var unitPrice = details[i][3];
    	   var unitPriceCell = document.createElement("td");
    	   unitPriceCell.style.width = "75px";
    	   unitPriceCell.style.textAlign = "right";
    	   unitPriceCell.className = "arDetailCell";
    	   if (overrideAllowed && !isLineHaul) {
    	       var unitPriceInputHtml = "<input type=\"text\" class=\"arUnitPriceText\" name=\"unitPrice_"+ chargeCode+"\" id=\"unitPrice_"+ chargeCode+"\" value=\"" + unitPrice + "\" onchange=\"autoRate('13',this)\" />" ;    	   	   
    	   	   unitPriceCell.innerHTML = unitPriceInputHtml;
    	   	   unitPriceCell.style.paddingRight = "1px";
    	   } else {
    	   		unitPriceCell.innerHTML = isEmpty(unitPrice) ? "&nbsp;" : unitPrice;
    	   		unitPriceCell.style.paddingRight = "2px";
    	   }
    	   var extendedPrice = details[i][4];
    	   var extendedPriceCell = document.createElement("td");
    	   extendedPriceCell.style.width = "100px";
    	   extendedPriceCell.style.textAlign = "right";
    	   extendedPriceCell.className = "arDetailCell";
    	   if (overrideAllowed && isLineHaul) {
    	       var extendedPriceInputHtml = "<input type=\"text\" class=\"arExtendedAmtText\" name=\"extAmt_"+ chargeCode+"\" id=\"extAmt_"+ chargeCode+"\" value=\"" + extendedPrice + "\" onchange=\"autoRate('13',this)\" />" ;
    	   	   extendedPriceCell.innerHTML = extendedPriceInputHtml;
    	   	   extendedPriceCell.style.paddingRight = "1px";
    	   } else {
    	   	   extendedPriceCell.innerHTML = isEmpty(extendedPrice) ? "&nbsp;" : extendedPrice;
    	   	   extendedPriceCell.style.paddingRight = "2px";
    	   }
    	   var calcTypeCell = document.createElement("td"); 
    	   calcTypeCell.style.width = "50px";
    	   calcTypeCell.style.textAlign = "center";
    	   calcTypeCell.className = "arDetailCell";
    	   calcTypeCell.innerHTML = isEmpty(calcType) ? "&nbsp;" : calcType;
    	   var needReason = "true" == details[i][8];
    	   var reasonCode = details[i][9];
    	   var reasonCell = document.createElement("td");
    	   reasonCell.style.width = "125px";
    	   reasonCell.className = "arDetailCell";
    	   if (needReason) {
    	   		var reasonSelect = document.createElement("select");
    	   		reasonSelect.id="reason_" + chargeCode;
    	   		reasonSelect.name="reason_" + chargeCode;
    	   		reasonSelect.className = "arReasonSelect";
    	   		reasonSelect.length = 3;
    	   		reasonSelect.onchange = function () {
    	   			autoRate('13',reasonSelect);
    	   		}
    	   		var blankOption = new Option();
    	   		blankOption.value = "";
    	   		blankOption.text = "-Select-";
    	   		reasonSelect.options[0] = blankOption;
    	   		var customerOption = new Option();
    	   		customerOption.value = "CUST";
    	   		customerOption.text = "Customer";
    	   		reasonSelect.options[1] = customerOption;
    	   		if (reasonCode == "CUST") {
    	   			reasonSelect.selectedIndex = 1;
    	   		}
    	   		var vendorOption = new Option();
    	   		vendorOption.value = "VEND";
    	   		vendorOption.text = "Vendor";
    	   		reasonSelect.options[2] = vendorOption;
     	   		if (reasonCode == "VEND") {
     	   			reasonSelect.selectedIndex = 2;
    	   		}
    	   		reasonCell.appendChild(reasonSelect);
    	   } else {
    	   		reasonCell.innerHTML = "&nbsp;";
    	   }
    	   var chargeDesc = details[i][6];
    	   var chargeDescCell = document.createElement("td");
    	   chargeDescCell.className = "arDetailCell";
    	   chargeDescCell.innerHTML = isEmpty(chargeDesc) ? "&nbsp;" : chargeDesc;
    	   rowEl.appendChild(deleteButtonCell);
    	   rowEl.appendChild(chargeCodeCell);
    	   rowEl.appendChild(qtyCell);
    	   rowEl.appendChild(unitCell);
    	   rowEl.appendChild(unitPriceCell);
    	   rowEl.appendChild(extendedPriceCell);
    	   rowEl.appendChild(calcTypeCell);
    	   rowEl.appendChild(reasonCell);
    	   rowEl.appendChild(chargeDescCell);
    	}
   		showField('arDetailHeaderDiv');
 	}
	function showChargeCodeLookupDiv(positionAnchor,leftOffset,topOffset) {
		var lookupDiv = document.getElementById('chargeCodeLookupDiv');
		if (!lookupDiv) {
			lookupDiv = new ChargeCodeLookupDiv(positionAnchor, leftOffset, topOffset);
		} else {
		setValue('lookupChargeDesc','');
		var tableEl = document.getElementById('chargeCodeResults');
		// clear previous results, if any
		if (tableEl.childNodes.length > 0)
    		tableEl.removeChild(tableEl.childNodes[0]);
    	lookupDiv.style.visibility = "visible";
    	}
	}
	function enableAssignButton() {
		enableButton('assignLoadButton','images/assignLoad.gif','javascript:submitForm("assign","/LTMSLite/loadMaint");');
	}
	function enableAcceptButton() {
		enableButton('acceptButton','images/accept.gif','javascript:accept();');
	}
	function disableAcceptButton() {
		disableButton('acceptButton','images/acceptClicked.gif');
	}	
	function clearErrorFields() {
		setInnerHTML('arError','');
		setInnerHTML('arMilesError','');
		setInnerHTML('arWeightError','');
		setInnerHTML('arRatingTruckTypeError','');
		setInnerHTML('hazmatError','');
		setInnerHTML('permitLoadError','');
		setInnerHTML('specEquipError','');
		setInnerHTML('backhaulError','');
		setInnerHTML('addChargeError','');
		setInnerHTML('detailErrors','');
		setInnerHTML('arRateTariffError','');
		setInnerHTML('loadDateError','');
		setInnerHTML('customerError','');
	}
	function modifierSelect(selectEl,checkboxId) {
		if (selectEl.selectedIndex != 0) {
			document.getElementById(checkboxId).checked = true;
		}
	}
	function modifierCheck(checkboxEl,selectId) {
		if (!checkboxEl.checked) {
			document.getElementById(selectId).selectedIndex = 0;
		}
	}
	function showEstimatePdf() {
		//document.forms[0].target = '_blank';
		submitForm('print', '/LTMSLite/autorate');
		//document.forms[0].target = '_self';
		cursorDefault();
	}
	function showRatingCommentsDiv() {
		var posAnchor = document.getElementById('ratingCommentsAnchor');
		var ratingCommentsDiv = document.getElementById('ratingCommentsDiv');
	
		ratingCommentsDiv.style.left = (getLeft(posAnchor) - 525) + "px";
		ratingCommentsDiv.style.top = (getTop(posAnchor) - 200) + "px";
		hideField('hazmatModifier');
		hideField('permitLoadModifier');
		hideField('specEquipModifier');
		hideField('backhaulModifier');
		ratingCommentsDiv.style.display = '';
		setValue('commentsEntered','N');
		setValue('ratingComments','');
		var fromMethod = getValue('commentsFromMethod');
		if ("accept" == fromMethod) {
			hideField("cancelAnchor");
		} else {
			showField("cancelAnchor");
		}
	}
	function hideRatingCommentsDiv() {
		hideField('ratingCommentsDiv');
		showField('hazmatModifier');
		showField('permitLoadModifier');
		showField('specEquipModifier');
		showField('backhaulModifier');
	}
	function saveRatingComments() {
		var ratingComments = getValue('ratingComments');
		if (isEmpty(ratingComments)) {
			alert("Comments are required.");
			return;
		}
		var url = "/LTMSLite/autorate?method=saveComments&ratingComments=" + escape(ratingComments) +
			"&fromMethod=" + escape(getValue('commentsFromMethod'));
		sendAjaxRequest(url);
	}
	function validateEstMiles(inputEl) {
		var estMilesStr = inputEl.value;
		if (isEmpty(estMilesStr)) return true;
		if (estMilesStr.indexOf(",") != -1) {
			setInnerHTML('arMilesError','** Est Total Mileage must be a non-decimal (whole) number. Do not use commas.');
			inputEl.focus();
			return false;
		} else if (estMilesStr.indexOf(".") != -1) {
			if (parseInt(estMilesStr) < parseFloat(estMilesStr)) {
				setInnerHTML('arMilesError','** Est Total Mileage must be a non-decimal (whole) number. Do not use commas.');
				inputEl.focus();
				return false;
			}			
		}
		setInnerHTML('arMilesError','');
		return true;
	}
