window.onload = 
function () {
    checkRCO();
    document.getElementById("OrigZip").focus();
    return;
}
var itemNumber = 1;
var NumberRE = "^\\d*$|^\\d*\\.\\d{1,5}$";
// -------------------------------------------------------------
function round(number,X) {
    // rounds number to X decimal places, defaults to 2
    X = (!X ? 0 : X);
    return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}
// -------------------------------------------------------------
function testRE(REStrv,strv,flagsv) {
    if(flagsv) {
        REv = new RegExp(REStrv, flagsv);
    }
    else {
        REv = new RegExp(REStrv);
    }
    if(REv.test(strv) == true) {
        return true;
    }
    return false;
}
// -------------------------------------------------------------
function calcValues(fldLengthv,fldWidthv,fldHeightv) {
    var fldMeasure = document.getElementById("measure");
    var fldCf = document.getElementById("CubicFt" + itemNumber);
    if (fldMeasure.options[fldMeasure.selectedIndex].value == 'inches') {
        var cubic_inches = fldLengthv * fldWidthv * fldHeightv; // 53760 = 48 * 40 * 28
        var cubic_feet = cubic_inches / 1728; //!! 31.110912 = 53760 * 0.0005787
        var cubic_meter = cubic_inches * 0.00001639; //!! 0.8811264 = 53760 * 0.00001639
    }
    if (fldMeasure.options[fldMeasure.selectedIndex].value == 'feet') {
        var cubic_feet = fldLengthv * fldWidthv * fldHeightv; //!! 53760 = 48 * 40 * 28
        var cubic_meter = cubic_feet * 0.02832; //!! 1522.4832 = 53760 * 0.02832
    }
    //----------------------------------------------------------------------------------
    fldCf.value = round(cubic_feet,0);
}
// -------------------------------------------------------------
function chkFields() {
    var fldLength = document.getElementById("calcLength").value;
    var fldWidth  = document.getElementById("calcWidth").value;
    var fldHeight = document.getElementById("calcHeight").value;
    if (fldLength != '' && fldWidth != '' && fldHeight != '') {
        if (!testRE(NumberRE,fldLength) || !testRE(NumberRE,fldWidth) || !testRE(NumberRE,fldHeight)) {
            setErrorMsg('One or more of the values that you entered are not a valid number');
        }
        else {
            calcValues(fldLength,fldWidth,fldHeight);
        }
    }
}
isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
DoIt=false;
function ddInit(e){
    TWD=isIE ? "BODY" : "HTML";
    TWD1=isIE ? document.all.theLayer : document.getElementById("theLayer");
    TWD2=isIE ? event.srcElement : e.target; while (TWD2.id!="titleBar"&&TWD2.tagName!=TWD) {
        TWD2=isIE ? TWD2.parentElement : TWD2.parentNode; 
    }
    if (TWD2.id=="titleBar") {
        offsetx=isIE ? event.clientX : e.clientX; offsety=isIE ? event.clientY : e.clientY; nowX=parseInt(TWD1.style.left); nowY=parseInt(TWD1.style.top); ddEnabled=true; document.onmousemove=dd; 
    }
} function dd(e) { 
    if (!ddEnabled) return; TWD1.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx; TWD1.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety; return false; 
}
function ddN4(TWD3){ 
    if (!isN4) return; N4=eval(TWD3); N4.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
    N4.onmousedown=function(e) {
        N4.captureEvents(Event.MOUSEMOVE);
        N4x=e.x;
        N4y=e.y;
    }
    N4.onmousemove=function(e) {
        if (DoIt){
            N4.moveBy(e.x-N4x,e.y-N4y);
            return false;
        }
    }
    N4.onmouseup=function(){
        N4.releaseEvents(Event.MOUSEMOVE);
    }
}
function hideMe(){
    if (isIE||isNN) TWD1.style.visibility="hidden"; else if (isN4) document.theLayer.visibility="hide";
}
function showMe(lineNo){
    var dispfldLength = document.getElementById("calcLength");
    var dispfldWidth = document.getElementById("calcWidth");
    var dispfldHeight = document.getElementById("calcHeight");
    itemNumber = lineNo;
    dispfldHeight.value = "";
    dispfldWidth.value = "";
    dispfldLength.value = "";
    if (isIE||isNN) TWD1.style.visibility="visible"; else if (isN4) document.theLayer.visibility="show";
    document.getElementById("calcLength").select();
    document.getElementById("calcLength").focus();
}
document.onmousedown=ddInit;
document.onmouseup=Function("ddEnabled=false");
var showMode = 'table-cell';
if (document.all)  {
    showMode='block';
}
function checkRCO(){
    //hard coded zip codes (first 3) for RCO, 008 is Virgin Islands (which count as RCO)
    var dest = document.getElementById("DestZip");
    var orig = document.getElementById("OrigZip");
    if ((dest.value.substring(0,3) == "006" ||
    dest.value.substring(0,3) == "007" ||
    dest.value.substring(0,3) == "008" ||
    dest.value.substring(0,3) == "009") ^
    (orig.value.substring(0,3) == "006" ||
    orig.value.substring(0,3) == "007" ||
    orig.value.substring(0,3) == "008" ||
    orig.value.substring(0,3) == "009")) {
        cfcells = document.getElementsByName('RCOShow');
        for(x = 0; x < cfcells.length; x++) {
            cfcells[x].style.display = 'block'; 
        }
        cfcells = document.getElementsByName('RCOHide');
        for(x = 0; x < cfcells.length; x++) {
            cfcells[x].style.display = 'none'; 
        }
        document.getElementById("RateDate").value = "20080310";
        document.getElementById("RateDate").disabled = true;
        maxAccessorials = 5;
    } else {
        cfcells = document.getElementsByName('RCOShow');
        for(x = 0; x < cfcells.length; x++) {
            cfcells[x].style.display = 'none'; 
        }
        cfcells = document.getElementsByName('RCOHide');
        for(x = 0; x < cfcells.length; x++) {
            cfcells[x].style.display = 'block'; 
        }
        document.getElementById("RateDate").disabled = false;
        maxAccessorials = 7;
    }
} 
function checkboxLayer(layer,checkbox) {
    var vis;
    vis=checkbox.checked?"show":"hide";
    MM_showHideLayers(layer,'',vis);
}
function validateForm(theForm){
    checkRCO();
    if (theForm.OrigZip.value.length < 5) {
        theForm.OrigZip.focus();
        setErrorMsg("Please enter a 5 digit origin zip code.");
        return false;
    }
    if (theForm.OrigZip.value.length == 5) {
        var msg = isWholeNumber(theForm.OrigZip.value);
        if (msg != null) {
            theForm.OrigZip.focus();
            setErrorMsg(msg);
        }
    }
    if (theForm.DestZip.value.length < 5) {
        theForm.DestZip.focus();
        setErrorMsg("Please enter a 5 digit destination zip code.");
        return false;
    }
    if (theForm.DestZip.value.length == 5) {
        var msg = isWholeNumber(theForm.DestZip.value);
        if (msg != null) {
            theForm.DestZip.focus();
            setErrorMsg(msg);
        }
    }
    //hard coded zip codes (first 3) for RCO, 008 is Virgin Islands 
    if ((theForm.OrigZip.value.substring(0,3) == "006") ||
    (theForm.OrigZip.value.substring(0,3) == "007") ||
    (theForm.OrigZip.value.substring(0,3) == "008") ||
    (theForm.OrigZip.value.substring(0,3) == "009")) {
        theForm.OrigRCOFlag.value = "Y";
    } else {
        theForm.OrigRCOFlag.value = "N";		
    }
    if ((theForm.DestZip.value.substring(0,3) == "006") ||
    (theForm.DestZip.value.substring(0,3) == "007") ||
    (theForm.DestZip.value.substring(0,3) == "008") ||
    (theForm.DestZip.value.substring(0,3) == "009")) {
        theForm.DestRCOFlag.value = "Y";
    } else {
        theForm.DestRCOFlag.value = "N";		
    }
    if (theForm.Discount.value == "") {
        theForm.Discount.focus();
        setErrorMsg("Please enter a discount.");
        return false;
    }
    if (theForm.MinimumFloor.value == "") {
        theForm.MinimumFloor.focus();
        setErrorMsg("Please enter a mimimum floor.");
        return false;
    }
    for (var i=1; i<=8; i++) {
        var objClass = eval("theForm.Class" + i);
        var objWeight = eval("theForm.Weight" + i);
        if (objClass.value == "null") {
            if (i == 1 || objWeight.value.length > 0) {
                setErrorMsg("Please enter a class.");
                objClass.focus();
                return false;
            }
        } else {
            if (theForm.OrigRCOFlag.value == "Y" ^ theForm.DestRCOFlag.value == "Y") {
                var objCubic = eval("theForm.CubicFt" + i);
                if (objCubic.value == "") {
                    objCubic.focus();
                    setErrorMsg("Please enter cubic feet.");
                    return false;
                }
                if (objCubic.value.length > 0) {
                    var msg = isWholeNumber(objCubic.value);
                    if (msg != null) {
                        objCubic.focus();
                        setErrorMsg(msg);
                        return false;
                    }
                }
            }
            if (objWeight.value == "") {
                objWeight.focus();
                setErrorMsg("Please enter a weight.");
                return false;
            }
            if (objWeight.value == 0) {
                objWeight.focus();
                setErrorMsg("Please enter a weight greater than zero.");
                return false;
            }
            if (objWeight.value.length > 0) {
                var msg = isWholeNumber(objWeight.value);
                if (msg != null) {
                    objWeight.focus();
                    setErrorMsg(msg);
                    return false;
                }
            }
        }
    }
    checkAcc = checkAccessorials(document.forms.Quote);
    if (checkAcc != null) {
        setErrorMsg(checkAcc);
        return false;
    }
    setErrorMsg("Submitting request...");
    return true;
}
function isWholeNumber(entry) {
    var errorMsg = null;
    flg=0;
    str="";
    spc=""
    arw="";
    for (var i=0;i<entry.length;i++){
        cmp = "0123456789"
        tst = entry.substring(i, i + 1)
        if (cmp.indexOf(tst)<0){
            flg++;
            str+=" "+tst;
            spc+=tst;
            arw+="^";
        }
        else{arw+="_";}
    }
    if (flg!=0){
        if (spc.indexOf(" ")>-1) {
            str+=" including spaces";
        }
        errorMsg = "This entry must be a number. I found "+flg+" unacceptable character(s): "+str+".";
    }
    return errorMsg;
}
function setErrorMsg(msg) {
    if (msg == undefined) {
        msg = "&nbsp;";
    }
    document.getElementById("errorMsg").innerHTML = msg;
}