String.prototype.trim = function() {
   a = this.replace(/^\s+/, '');
   return a.replace(/\s+$/, '');
   };

function Sleep(naptime) {
   naptime = naptime * 1000;
   var sleeping = true;
   var now = new Date();
   var alarm;
   var startingMSeconds = now.getTime();
   while(sleeping){
      alarm = new Date();
      alarmMSeconds = alarm.getTime();
      if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
      }      
   }
  
function newCell(row, data) {
   cell = row.insertCell(row.cells.length);
   if (data.substr(0,1) == "[") {
      y = data.substring(1);
      z = y.split("]");
      cell.width = z[0];
      cell.innerHTML = z[1];
//    alert("data=" + data + ", y=" + y + ", z[0]=" + z[0] + ", z[1]=" + z[1]);
      }
   else {
      if (data.substr(0,1) == "{") {
         y = data.substring(1);
         z = y.split("}");
         cell.colSpan = z[0];
         cell.innerHTML = z[1];
         }
      else { cell.innerHTML = data; }
      }
   }

function makeURL(URL) {
   URL +=  escape(otp) + "|" + escape(whoIam) + "|" + escape(thePass) + "|" + escape(myLang) + "|" + escape(theHref) + "|";
   return URL;
   }

function handleADUResponse() {
   if (http.readyState == 4) { // Split the response into an array
      if (http.status == 200) {
         alert(http.responseText); 
         if (http.responseText.substr(0,3) == "Add") {
            document.getElementById("c_form").bupd.disabled = false; 
            document.getElementById("c_form").bdel.disabled = false;
            document.getElementById("c_form").badd.disabled = true;
            }
         if (http.responseText.substr(0,3) == "Del") {
            document.getElementById("c_form").bupd.disabled = true; 
            document.getElementById("c_form").bdel.disabled = true;
            document.getElementById("c_form").badd.disabled = false;
            }
         if (http.responseText.substr(0,3) == "Upd") {
            document.getElementById("c_form").bupd.disabled = false;
            document.getElementById("c_form").bdel.disabled = false;
            document.getElementById("c_form").badd.disabled = true;
            }
         }
      isWorking = false;
      }
   }

function handleIFind() {
  if (http.readyState == 4) { // Split the response into an array
     if (http.status == 200) {
        var x = http.responseText;
        if (x != "" &&  x.substr(0,5) != "ERROR") {
           var parm = x.split("#");
           for (i = 0; i < parm.length; i++) {
              var pair = parm[i].split("=");
              if (pair[0].indexOf("description") >= 0) {
                 document.getElementById("t_description").value = pair[1]; }
// RJT 20061110 incoming weight is weight per unit length, so if there is 
//              a length the weight needs to be extended accordingly
              if (pair[0].indexOf("weight") >= 0) { // RJT 20061110
                 var a = document.getElementById("t_length").value;
                 if (a > 0) { 
                    document.getElementById("t_weight").value = a * pair[1]; }
                 else { document.getElementById("t_weight").value = pair[1]; }
                 }
              if (pair[0].indexOf("length") >= 0) { // RJT 20061110
                 var y = document.getElementById("t_length").value;
                 var z = document.getElementById("t_weight").value;
                 if (y > 0)
       { document.getElementById("t_weight").value = z / y * pair[1]; }
                 document.getElementById("t_length").value = pair[1];
                 }
              }
           }
        }
     isWorking = false;
     }
  }

function handleFind() {
  if (http.readyState == 4) { // Split the response into an array
     if (http.status == 200) {
        var xmlDocument = http.responseText;
//      alert(xmlDocument);
        if (xmlDocument == "") {
           document.getElementById("c_form").badd.disabled = false;
           document.getElementById("c_form").bdel.disabled = true;
           document.getElementById("c_form").bupd.disabled = true;
           }
        else {
           if (xmlDocument.substr(0,5) == "ERROR") { alert(xmlDocument); }
           else {
              document.getElementById("c_form").badd.disabled = true;
              document.getElementById("c_form").bdel.disabled = false;
              document.getElementById("c_form").bupd.disabled = false;
              var parm = xmlDocument.split("#");
              for (i = 0; i < parm.length; i++) {
                 var pair = parm[i].split("=");
                 if (pair[0] == "c_taxauthority") {
                    var selBox = document.getElementById("c_taxauthority");
                    for (j = 0; j < selBox.length; j++) {
                       var sel = selBox.options[j].value;
                       if (sel.trim() == pair[1].trim()) {
                          selBox.selectedIndex = j; }
                       }
                    }
                 else {
                    var el = document.getElementById(pair[0]);
                    if (el != null) {
                       if (pair[0] == "c_created" || pair[0] == "c_changed") {
                          el.innerHTML = pair[1]; }
                       else { el.value = pair[1]; }
                       }
                    }
                 }
              }
           }
        }
     isWorking = false;
     }
  }

function handleLookup() {
  if (http.readyState == 4) { // Split the response into an array
     if (http.status == 200) {
        var xmlDocument = http.responseText;
//      alert(xmlDocument);
        if (xmlDocument.substr(0,5) == "ERROR") { alert(xmlDocument); }
        else {
           var parm = xmlDocument.split("#");
           for (i = 0; i < parm.length; i++) {
              var pair = parm[i].split("=");
              var el = document.getElementById(pair[0]);
              if (el != null) {
// RJT 20061110 Processing a lookup on the customer, supplier or item file:
//              incoming weight is weight per unit length, so if there is 
//              a length the weight needs to be extended accordingly
                 if (pair[0].indexOf("weight") >= 0) { // RJT 20061110
                    var a = document.getElementById("c_length").value;
                    if (a > 0) { 
                       document.getElementById("c_weight").value = a * pair[1];
                       }
                    else {
                       document.getElementById("c_weight").value = pair[1];
                       }
                    }
                 if (pair[0].indexOf("length") >= 0) { // RJT 20061110
                    var y = document.getElementById("c_length").value;
                    var z = document.getElementById("c_weight").value;
                 if (y > 0)
       { document.getElementById("c_weight").value = z / y * pair[1]; }
                    document.getElementById("c_length").value = pair[1];
                    }
                 if (pair[0] != "c_weight" && pair[0] != "c_length") {
                    el.value = pair[1]; }
                 }
              }
           }
        }
     isWorking = false;
     }
  }

function IsDecimal(sText) {
   var ValidChars = "0123456789";
   var IsNumber = true;
   var Char;
   var Dec = false;
   if (sText.length == 0) { IsNumber = false; }
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (Char == "." && !Dec) { Dec = true; continue; }
      if (ValidChars.indexOf(Char) == -1) { IsNumber = false; }
      }
   return IsNumber;
   }

function IsNumeric(sText) {
   var ValidChars = "0123456789";
   var IsNumber = true;
   var Char;
   if (sText.length == 0) { IsNumber = false; }
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) { IsNumber = false; }
      }
   return IsNumber;
   }

function chkStamp(dt) {
   var test = document.getElementById(dt).value;
   if (IsNumeric(test) && test.length == 8) {
      document.getElementById(dt).value = test + "235959";
      return;
      }
   if (IsNumeric(test) && test.length == 14) { return; }
   var now = new Date();
   var yr = now.getFullYear();
   var tyr = test.substr(0,4) * 1;
   var mdy = test.split("/");
   if (!mdy[2]) { mdy[2] = yr; }
   if (!mdy[1]) { mdy[1] = mdy[0]; mdy[0] = now.getMonth() + 1; }
   if (mdy[2] < 100) { mdy[2] = 2000 + mdy[2] * 1; }
   if (mdy[0] > 12) { x = mdy[0]; mdy[0] = mdy[1]; mdy[1] = x; }
   if (mdy[1] == 0 || mdy[1] > 31) { mdy[1] = now.getDate(); }
   document.getElementById(dt).value =
      mdy[2] * 10000 + mdy[0] * 100 + mdy[1] * 1;
   document.getElementById(dt).value += "235959";
   }

function chkDate(dt) {
   var test = document.getElementById(dt).value;
   var now = new Date();
   var yr = now.getFullYear();
   var tyr = test.substr(0,4) * 1;
   if (IsNumeric(test) && test.length == 8 
       && (yr == tyr || yr == tyr -1)) { return; }
   var mdy = test.split("/");
   if (!mdy[2]) { mdy[2] = yr; }
   if (!mdy[1]) { mdy[1] = mdy[0]; mdy[0] = now.getMonth() + 1; }
   if (mdy[2] < 100) { mdy[2] = 2000 + mdy[2] * 1; }
   if (mdy[0] > 12) { x = mdy[0]; mdy[0] = mdy[1]; mdy[1] = x; }
   if (mdy[1] == 0 || mdy[1] > 31) { mdy[1] = now.getDate(); }
   document.getElementById(dt).value = 
      mdy[2] * 10000 + mdy[0] * 100 + mdy[1] * 1;
   }

function IsCoil(class) {
   if (class == "Master" || class == "Slit") { return true; }
   else return false;
   }

function chkRating() {
   var myrate = IsNumeric(document.getElementById("c_rating").value);
   if (myrate) {
      document.getElementById("c_rating").value =
         document.getElementById("c_rating").value * 1;
      }
   if (!myrate) {
      alert("Rating must be a number");
      document.getElementById("c_rating").setFocus;
      }
   return myrate;
   }

function findSup() {
  theSup = document.getElementById("c_supcode").value;
  if (theSup == "") { return; }
  var urlFind = "app/findSup.php?param=";
  urlFind = makeURL(urlFind) + escape(theComp) + "|" + escape(theSup) + "|";
  if (!isWorking && http) {
     http.open("GET", urlFind, true);
     http.onreadystatechange = handleLookup;
     http.send(null);
     }
  }
  
function findItem(fld) {
  theItem = document.getElementById(fld).value;
  if (theItem == "") { return; }
  var urlFind = "app/oFindItem.php?param=";
  urlFind = makeURL(urlFind) + escape(theComp) + "|" + escape(theItem) + "|";
  if (!isWorking && http) {
     http.open("GET", urlFind, true);
     if (fld == "t_item") { http.onreadystatechange = handleIFind; }
     else { http.onreadystatechange = handleLookup; }
     http.send(null);
     }
  }
