function ltrim(str)
{
    while (str.substring(0,1) == ' ')
    {
        str = str.substring(1, str.length);
    }
    return str;
}

function rtrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

var getSelValue = function(selObj){
    return selObj.options[selObj.selectedIndex].value;
}
var getSelText = function(selObj){
    return selObj.options[selObj.selectedIndex].text;
}

function buildMN(){
    var modnum =rtrim(document.form1.sku1.value);
    var obj;
    var obj1;
    var obj2;
    var allsel = 0;

    var bIsSTD = false;

    if(document.getElementById("t5")) {
        if(document.getElementById("t5").value == "STD") {
            bIsSTD = true;
        }
    }

    for (i=1;i<=12;i++)
    {
        obj = document.getElementById("t" + i)
        if (obj) {
            allsel = 1;
            if(!(i == 6 && bIsSTD))
                modnum += rtrim(obj.value)
            obj1 = document.getElementById("attr_name_" + i)
            if(obj1) {
                obj2 = document.getElementById(rtrim(obj1.value));
                if(obj2.type == "select-one") {
                    if(obj2.options[obj2.selectedIndex].value) {
                        if(!(i == 6 && bIsSTD)) {
                            if(!obj.value)
                                modnum += obj2.options[obj2.selectedIndex].value;
                            obj.value = obj2.options[obj2.selectedIndex].value;
                        }
                    }
                    obj2a = document.getElementById("attr_val_" + i);
                    obj2a.value = obj2.options[obj2.selectedIndex].text;
                }
            }
        }
        else
            if(allsel == 1)
                allsel = 2;
    }


    document.form1.mn.value = modnum;
}

function Right(str, n)
        /***
                IN: str - the string we are RIGHTing
                    n - the number of characters we want to return

                RETVAL: n characters from the right side of the string
        ***/
{
    if (n <= 0)     // Invalid bound, return blank string
        return "";
    else if (n > String(str).length)   // Invalid bound, return
        return str;                     // entire string
    else { // Valid bound, return appropriate substring
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

function setCodeSel(oSel,oTxt){
    var bIsSTD = false;
    var bIsSTDSel = false;

    if(document.getElementById("L_Sheath_Length") && document.getElementById("L_Sheath_Length").options) {
        if(document.getElementById("L_Sheath_Length").options[1].value == "STD") {
            bIsSTD = true;
            if(document.getElementById("L_Sheath_Length").selectedIndex == 1)
                bIsSTDSel = true;
                var txts = document.getElementsByTagName("input");
                for(i = 0; i < txts.length; i++) {
                    if(txts[i].id == "L_Sheath_Length" || txts[i].id == "t6")
                        txts[i].value = '';
                }
        }
    }

    oTxt.value=oSel.options[oSel.selectedIndex].value;
    buildMN();
    if(document.getElementById("sku1").value == "695") {
        objt1 = document.getElementById("t1");
        if(objt1.value == "D") {
            //alert(objt1.value);
            document.getElementById("Fitting_Size_or_Process_and_Control_Threads").options[3].selected = true;
            document.getElementById("t2").value = "3";
            buildMN();
        }
    }
}


function setCodeTxt(oTxtIn,oTxt){
    var t = parseFloat(String(oTxtIn.value));
    //alert("attr_text_" + rtrim(oTxt.name.substring(1)));
    var sAttrText = document.getElementById("attr_text_" + rtrim(oTxt.name.substring(1))).value;
    var fMin = document.getElementById("min_" + rtrim(oTxt.name)).value;
    var fMax = document.getElementById("max_" + rtrim(oTxt.name)).value;
    var iDec = document.getElementById("dec_" + rtrim(oTxt.name)).value;
    var sInstr = document.getElementById("di_" + rtrim(oTxt.name)).value;

    var sUnit = " inches";
    var bIsSTD = false;
    var bIsSTDSel = false;

    if(document.getElementById("L_Sheath_Length") && document.getElementById("L_Sheath_Length").options) {
        if(document.getElementById("L_Sheath_Length").options[1].value == "STD") {
            bIsSTD = true;
            if(document.getElementById("L_Sheath_Length").selectedIndex == 1)
                bIsSTDSel = true;
        }
    }

    if(oTxtIn.id == "L_Sheath_Length") {
        if(oTxtIn.value != '' && bIsSTDSel) {
            document.getElementById("L_Sheath_Length").options[1].selected = false;
            document.getElementById("L_Sheath_Length").options[0].selected = true;
            document.getElementById("t5").value = '';
        }
        else if(bIsSTDSel) {
            oTxt.value = '';
            oTxtIn.value = '';
            document.getElementById("t6").value = '';
            var txts = document.getElementsByTagName("input");
            for(i = 0; i < txts.length; i++) {
                if(txts[i].id == "L_Sheath_Length" || txts[i].id == "t6")
                    txts[i].value = '';
            }

            buildMN();
            return;
        }
    }


    if(sInstr.indexOf("feet") != -1)
        sUnit = " feet";
    else if(sInstr.indexOf("AWG") != -1)
        sUnit = " AWG";

    if(isNaN(t)) {
        alert('Please enter a valid number');
        return;
    }

    if(fMin != '') {
        fMin = parseFloat(fMin);
        if(t < fMin) {
            alert('Minimum value is ' + fMin + sUnit);
            return;
        }
    }
    if(fMax != '') {
        fMax = parseFloat(fMax);
        if(t > fMax) {
            alert('Maximum value is ' + fMax + sUnit);
            return;
        }
    }

    if(iDec != '') {
        iDec = parseInt(iDec);
        if(iDec > 0) {
            switch(iDec) {
                case 1:
                    if(t < 1)
                        oTxt.value = '0' + (t*1000);
                    else
                        oTxt.value = '' + (t*1000);
                    buildMN();
                    return;
                    break;
                case 2: // only use 2 when e.g. 12 = 12 inches
                    // mdp 7/17/07 if immersion length and 2 decimals, make sure it allows for
                    // 2 whole numbers and 2 decimals, where the 2 decimals are treated like:
                    // 10.25 = 102
                    // 10.5 or 10.50 = 105
                    // 10.75 = 107

                    // mdp 2/10/08 - change 2 decimals to handle 4 spots
                    // 10.25 = 1025
                    
                    // mdp 12/21/08
                    // .25 == 0025


                    var sText = String(oTxtIn.value);
                    if(sText.indexOf(".") != -1) {
                        var aSplit = sText.split(".");
                        var s1 = aSplit[0];
                        var s2 = aSplit[1];
                        if(s1.length == 0)
                            s1 = "00" + s1;
                        else { 
                            if(s1.length == 1)
                                s1 = "0" + s1;
                        }                                
                        if(s2.length == 1)
                            s2 = s2 + "0";
                        oTxt.value = s1 + s2;
                    }
                    else {
                         // no decimal point, and max is 99 inches, so
                         // 6 should be 0600, 12, 1200, and 120 shouldn't  b allowed
                         var s1 = "" + t;
                         if(s1.length == 1)
                            s1 = "0" + s1 + "00";
                         else if(s1.length==2)
                            s1 = s1 + "00";
                         else if(s1.length==3) // leave this here to catch database errors for maxlen
                            s1 = s1 + "0";


                         oTxt.value = s1;
                    }
                    buildMN();
                    return;



                    if(sAttrText.indexOf("Immersion") == -1) {
                        if(t < 1)
                            oTxt.value = '0' + (t*100);
                        else if(t < 10)
                            oTxt.value = '0' + t;
                        else
                            oTxt.value =  t;
                    }
                    else {
                        var sText = String(oTxtIn.value);
                        if(sText.indexOf(".") != -1) {
                            var aSplit = sText.split(".");
                            var s1 = aSplit[0];
                            var s2 = aSplit[1];
                            var i1 = parseInt(String(s2));
                            //alert(i1 + " " + s2.value + " " + aSplit[1]);
                            var s3 = "7";
                            if(s2.length == 2) {
                                if(i1 < 25)
                                    s3 = "0";
                                else if(i1 < 50)
                                    s3 = "2";
                                else if(i1 < 75)
                                    s3 = "5";
                            }
                            else {
                                s3 = i1;
                            }

                            //alert(s1);
                            if(s1.length == 1)
                                oTxt.value = '0' + s1 + '' + s3;
                            else
                                oTxt.value = s1 + '' + s3;
                        }
                        else {
                            if(sText.length == 1)
                                oTxt.value = '0' + sText;
                            else if(sText.length == 2)
                                oTxt.value = sText + '0';
                            else if(sText.length == 3)
                                oTxt.value = sText;
                        }

                    }
                    buildMN();
                    return;
                    break;
                case 3: // only use 3 when e.g. 120 = 120 inches
                    if(t < 10)
                        oTxt.value = '00' + t;
                    else if(t < 100)
                        oTxt.value =  '0' + t;
                    else
                        oTxt.value = '' + t;
                    //alert(t);
                    buildMN();
                    return;
                    break;
                case 4: // only use 4 when e.g. 1500 = 1500 inches

                    // 6/1/09 mdp - strip decimals
                    var sText = String(oTxtIn.value);
                    if(sText.indexOf(".") != -1) {
                        var aSplit = sText.split(".");
                        t = aSplit[0];
                        oTxtIn.value = t;
                    }
                    if(t < 1)
                        oTxt.value = '0001';
                    else if(t < 10)
                        oTxt.value = '000' + t;
                    else if(t < 100)
                        oTxt.value =  '00' + t;
                    else if(t < 1000)
                        oTxt.value =  '0' + t;
                    else
                        oTxt.value = '' + t;
                    buildMN();
                    return;
                    break;
                default: break;
            }
        }
        else {
            // 0 decimals for Leadwire lengths
            // first strip decimal places
            t = parseInt(t);
            if(t < 10)
                oTxt.value = '00' + t;
            else if(t < 100)
                oTxt.value =  '0' + t;
            else
                oTxt.value = '' + t;
            buildMN();
            return;
        }
    }

    if (t < 1.5 && fMin == '' && fMin != 0) {
        alert('Minimum value is 1.5' + sUnit);
    }
    else if (t <10) {
        oTxt.value = '0' + (t*10);
        buildMN();
    }
    else if ((t >=10)&&(t <100)) {
        oTxt.value = (t * 10) + '';
        buildMN();
    }
    else if ((t >=100)&&(t <1000)) {
        oTxt.value = Right((t*1)+'',3);
        buildMN();
    }
    else {
        alert('Please enter a value less then 1000' + sUnit);
        buildMN();
    }
}
