
function sniffer () {
   agent = navigator.userAgent.toLowerCase();
   this.major = parseInt(navigator.appVersion, 10);
   this.minor = parseFloat(navigator.appVersion);
   this.ie = (agent.indexOf("msie") != -1);
   this.mozilla = (agent.indexOf("mozilla") != -1);
   this.ie55 = (this.ie && (this.major == 4) && (agent.indexOf("msie 5.5") != -1));
   this.ie6 = (this.ie && (agent.indexOf("msie 6.0") != -1));
   this.mac = (agent.indexOf("mac") != -1);
}

function CopyArrayValues (ArrayFrom, ArrayTo) {
   for (fld in ArrayFrom) {
      ArrayTo[fld] = ArrayFrom[fld];
   }
}

function _CInt(V) {
   return (parseInt(V, 10));
}

function validateNumeric (n) {
   var num = "0123456789.,$";
   var retValue = true;
   if (n.value.charAt(0) == '-' || n.value.charAt(0) == '+' || (num.indexOf(n.value.charAt(0)) > -1))
      retValue = true;
   else
      retValue = false;
   for (var I = 1; I < n.value.length; I++)
      if (num.indexOf(n.value.charAt(I)) == -1)
         retValue = false;
   return retValue;
}

function getNumericPart (n) {
   var num = "0123456789.,";
   var retValue = 0;
   var sindex = 0;
   if (typeof n == "string") {
      if (n.charAt(0) == '-' || n.charAt(0) == '+') {
         sindex = 1;
      }
      for (var I = sindex; I < n.length; I++) {
         if (num.indexOf(n.charAt(I)) == -1)
            break;
      }
      retValue = n.substring (0, I);
   }
   else
      retValue = n;
   return retValue;
}

function RmNonNum (M, NuNum) {
   // remove non-numeric chars with two different modes of usage
   if (M == 0) {
       if (NuNum.value.length > 0) {
         // remove "," for conversion
         if (NuNum.value.indexOf(",") != -1)
            NuNum.value = NuNum.value.replace(/,/g, "");

         // remove "$" for conversion
         if (NuNum.value.indexOf("$") != -1)
            NuNum.value = NuNum.value.replace(/\$/g, "");

         // remove multiple "." for conversion
         var P = NuNum.value.indexOf(".");
         if (P != -1) {
            if (NuNum.value.indexOf(".", P + 1) != -1) {
               alert ("More than decimal place was entered, removing extra periods!");
               NuNum.value = NuNum.value.replace(/\./g, "");
               NuNum.value = NuNum.value.substring(0, P) + "." + NuNum.value.substring(P);
            }
         }
      }
      return NuNum.value;
   }
   else {
      if (NuNum.length > 0) {
         // remove "," for conversion
         if (NuNum.indexOf(",") != -1)
            NuNum = NuNum.replace(/,/g, "");

         // remove "$" for conversion
         if (NuNum.indexOf("$") != -1)
            NuNum = NuNum.replace(/\$/g, "");

         // remove multiple "." for conversion
         var P = NuNum.indexOf(".");
         if (P != -1) {
            if (NuNum.indexOf(".", P + 1) != -1) {
               alert ("More than decimal place was entered, removing extra periods!");
               NuNum = NuNum.replace(/\./g, "");
               NuNum = NuNum.substring(0, P) + "." + NuNum.substring(P);
            }
         }
      }
      return NuNum;
   }
}

function FPNFormat (FPNum) {
   NuNum = FPNum;
   // remove non-numeric chars
   NuNum.value = RmNonNum(0, FPNum);

   // Number format the Total
   var dotindex = NuNum.value.indexOf(".");
   var dotsize = NuNum.value.length;
   if (dotindex < 0) {
      NuNum.value += ".00";
   }
   else {
      NuNum.value = Math.round ((NuNum.value * 100) + .49);
      NuNum.value = parseFloat(NuNum.value / 100);
      dotindex = NuNum.value.indexOf(".");
      if (dotindex < 0)
         NuNum.value += ".00";
      if ((NuNum.value.length - dotindex) == 2)
         NuNum.value += "0";
   }
   return NuNum.value;
}

function FPNFormatZS (FPNum, Fmt) {
   NuNum = FPNum;
   if (FPNum.value == "0" || FPNum.value == "") {
      NuNum.value = "";
   }
   else {
      var T = "";
      for (I = 0; I < FPNum.value.length; I++) {
         C = FPNum.value.substring (I, I+1);
         if ((C.charCodeAt(0) >= 48 && C.charCodeAt(0) <= 57) || C.charCodeAt(0) == 46)
            T += C;
      }
      FPNum.value = T;
      // Number format the Total
      var dotindex = FPNum.value.indexOf(".");
      var dotsize = FPNum.value.length;
      if (dotindex < 0) {
         NuNum.value += ".00";
      }
      else {
         NuNum.value = Math.round ((FPNum.value * 100) + .49);
         NuNum.value = parseFloat(NuNum.value / 100);
         dotindex = NuNum.value.indexOf(".");
         if (dotindex < 0)
            NuNum.value += ".00";
         if ((NuNum.value.length - dotindex) == 2)
            NuNum.value += "0";
      }
      if ((typeof Fmt) != "undefined" && Fmt.length) {
         if (Fmt == "#,##0.00") {
            var pindex = NuNum.value.indexOf(".");
            var wnum = NuNum.value.substring (0, pindex);
            var n3 = wnum.substring (wnum.length - 3);
            wnum = wnum.substring (0, wnum.length - 3);
            while (wnum.length > 3) {
               n3 = wnum.substring (wnum.length - 3) + "," + n3;
               wnum = wnum.substring (0, wnum.length - 3);
            }
            if (wnum.length) {
               n3 = wnum + "," + n3;
            }
            var dnum = NuNum.value.substring (NuNum.value.length - 3);
            NuNum.value = n3 + dnum;
         }
         else
         if (Fmt == "$#,###" || Fmt == "#,###") {
            var pindex = NuNum.value.indexOf(".");
            var wnum = NuNum.value.substring (0, pindex);
            var n3 = wnum.substring (wnum.length - 3);
            wnum = wnum.substring (0, wnum.length - 3);
            while (wnum.length > 3) {
               n3 = wnum.substring (wnum.length - 3) + "," + n3;
               wnum = wnum.substring (0, wnum.length - 3);
            }
            if (wnum.length) {
               n3 = wnum + "," + n3;
            }
            NuNum.value = n3;
            if (Fmt.indexOf("$") == 0)
               NuNum.value = "$" + NuNum.value;
         }
      }
   }
   return NuNum.value;
}

function TparseFloat (FPNumStr) {
   // remove non-numeric chars
   FPNumStr = RmNonNum(1, FPNumStr);

   if (isNaN(parseFloat(FPNumStr)))
      return 0;
   else
      return (parseFloat(FPNumStr));
}

function isEmpty (str) {
   for (var I = 0; I < str.length; I++) {
      if (str.charAt(I) != " ")
         return false;
   }
   return true;
}

function CountTextLines (TLine) {
   var LCount = 1;
   for (var I = 0; I < TLine.length; I++) {
      if (TLine.charAt(I) == "\n")
         ++LCount;
   }
   return LCount;
}

function CountBRLines (TLine) {
   var LCount = 1;
   var tlen = TLine.length;
   var i = 0;
   while (i < (tlen - 4)) {
      var j = i + 4;
      if (TLine.substring (i, j) == "<BR>" || TLine.substring (i, j) == "<br>")
         ++LCount;
      ++i;
   }
   return LCount;
}

function FindTextLineEnd (TLine, MaxLines) {
   var LCount = 1;
   var CCount = 0;
   for (var I = 0; I < TLine.length; I++) {
      if (TLine.charAt(I) == "\n")
         ++LCount;
      ++CCount;
      if (LCount > MaxLines)
         break;
   }
   return CCount;
}

function CountTextChars (TLine) {
   var CCount = 0;
   for (var I = 0; I < TLine.length; I++) {
      if (TLine.charAt(I) != "\n" && TLine.charAt(I) != "\r")
         ++CCount;
   }
   return CCount;
}

function CnvToBRs (TLine) {
   var RLine = "";
   for (var I = 0; I < TLine.length; I++) {
      if (TLine.charAt(I) != "\n" && TLine.charAt(I) != "\r") {
         RLine += TLine.charAt(I);
      }
      else {
         RLine += "<BR>";
         I += 1;
      }
   }
   return RLine;
}

function LimitInputArea (OrigString, MAXL, MAXC) {
   var TargetString = OrigString;
   var lcnt = CountTextLines (TargetString);
   var ccnt = CountTextChars (TargetString);
   if (lcnt > MAXL || ccnt > MAXC) {
      var msg = "Imprint Area has more than " + MAXL + " lines or more than " + MAXC + " chars";
      alert (msg);
      if (ccnt > MAXC) {
         var s = TargetString;
         TargetString = s.substring (0, MAXC - 1);
         lcnt = CountTextLines (TargetString);
      }
      if (lcnt > MAXL) {
         ccnt = FindTextLineEnd (TargetString, MAXL);
         var s = TargetString;
         TargetString = s.substring (0, ccnt-2);
      }
   }
   return TargetString;
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
   var keyCode = (isNN) ? e.which : e.keyCode;
   var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
   if(input.value.length >= len && !containsElement(filter,keyCode)) {
      input.value = input.value.slice(0, len);
      input.form[(getIndex(input)+1) % input.form.length].focus();
   }
   function containsElement(arr, ele) {
      var found = false, index = 0;
      while(!found && index < arr.length)
      if(arr[index] == ele)
         found = true;
      else
         index++;
      return found;
   }
   function getIndex(input) {
      var index = -1, i = 0, found = false;
      while (i < input.form.length && index == -1)
      if (input.form[i] == input)index = i;
      else i++;
      return index;
   }
   return true;
}

function ASpecC (SpecStr, SpecFont) {
   if (LastImprintArea != "undef") {
      if (SpecFont.length > 0 && SpecFont != "Basefont") {
         LastImprintArea.value = LastImprintArea.value + "<FONT FACE=" + SpecFont + ">" + SpecStr + "</FONT>";
      }
      else {
         LastImprintArea.value = LastImprintArea.value + SpecStr;
      }
      LastImprintArea.focus();
   }
}

function CloseSpecChar () {
   SpecCharLegend.style.visibility = "hidden";
   if (LastSpecList != "undef") {
      LastSpecList.style.visibility = "hidden";
   }
   if (LastImprintArea != "undef") {
      LastImprintArea.focus();
   }
}

function MakeSubmitID () {
   TDate = new Date();
   var SubmitID = "";
   var YRM = TDate.getFullYear() % 100;
   if (YRM < 10)
      SubmitID += "0";
   SubmitID += YRM;
   if (TDate.getMonth() < 10)
      SubmitID += "0";
   SubmitID += ("" + TDate.getMonth());
   if (TDate.getDate() < 10)
      SubmitID += "0";
   SubmitID += ("" + TDate.getDate());
   if (TDate.getHours() < 10)
      SubmitID += "0";
   SubmitID += ("" + TDate.getHours());
   if (TDate.getMinutes() < 10)
      SubmitID += "0";
   SubmitID += ("" + TDate.getMinutes());
   if (TDate.getSeconds() < 10)
      SubmitID += "0";
   SubmitID += ("" + TDate.getSeconds());
   return SubmitID;
}

function _TodaysDate () {
   TDate = new Date();
   var cDate = "";
   if (parseInt(TDate.getMonth(), 10) + 1 < 10)
      cDate += "0";
   cDate += ("" + parseInt(TDate.getMonth() + 1, 10));
   cDate += "/";
   if (TDate.getDate() < 10)
      cDate += "0";
   cDate += ("" + TDate.getDate());
   cDate += "/";
   cDate += ("" + TDate.getFullYear());
   return cDate;
}

function _initSelection (qURL, rStr, bLine, comboObj) {
// usage:   var reqstr = "CompID=" + document.forms[0].CompanyName0.value;
// usage:   initSelection ("../query/CompanyLocList-pl.cgi", reqstr, 1, document.forms[0].LocationID0);
   var _TList = new Array;
   var _VList = new Array;
   var cline = 1;
   var cindex = 0;
   var eindex = 0;
   var rtext = "";
   var comboline = "";
   var ctext = "";

   if ((bsniff.ie55 || bsniff.ie6) && !bsniff.mac) {
      var http = new ActiveXObject("Microsoft.XMLHTTP");
      http.open ("POST", qURL, false);
      http.setRequestHeader ("Content-type", "application/x-www-form-urlencoded");
      http.send (rStr);
      ctext = http.responseText;
   }
   else {   // Use the applet code instead
      var rURL = qURL;
      if (rStr.length)
         rURL += "?" + rStr;
      ctext = document.getURL.fetch (rURL, null);
      var z = ctext.substr(4);
      ctext = z + "\n";
   }
   _VList.length = _TList.length = cline;
   _VList[cline-1] = _TList[cline-1] = "";
   while (ctext.length > 0) {
      cindex = ctext.indexOf ("\n");
      if (cindex > 0) {
         ++cline;
         _VList.length = _TList.length = cline;
         comboline = ctext.substring (0, cindex);
         eindex = comboline.indexOf ("=");
         if (eindex > 0) {
            _VList[cline-1] = comboline.substring (0, eindex);
            _TList[cline-1] = comboline.substring (eindex+1);
         }
         ctext = ctext.substring (cindex+1);
      }
      else {
         ++cline;
         _VList.length = _TList.length = cline;
         _VList[cline-1] = _TList[cline-1] = ctext;
         ctext = "";
      }
   }
   if (bLine == 0) {
      // ignore 1st blank line entry
      var tlen = comboObj.options.length = _TList.length-1;
      for (i = 0; i < tlen; i++) {
         comboObj.options[i].text = _TList[i+1];
         comboObj.options[i].value = _VList[i+1];
      }
   }
   else {
      var boffset = 0;
      // all entries w/blank line
      var tlen = comboObj.options.length = _TList.length;
      if (bLine == -1) {
         if (tlen == 2) {
            boffset = 1;
            --tlen;
         }
      }
      for (i = 0; i < tlen; i++) {
         comboObj.options[i].text = _TList[i+boffset];
         comboObj.options[i].value = _VList[i+boffset];
      }
   }
}


function _InitDependentFields (qURL, rStr, ObjList, KeyList, len, pfdata) {
// usage:   var reqstr = "CompID=" + form.CompanyName0.value;
//          var OList = new Array ();
//          OList[0] = form.Field0; // Repeat for all dependent fields
//          var KList = new Array ();
//          KList[0] = "KeyName"; // Repeat for all dependent fields
//          pfdata ; // Pre-filled in data (server is not contacted, just parse string passed)
//          _InitDependentFields ("../query/CompanyLocData-pl.cgi", reqstr, OList, KList, KList.length);
   var cindex = 0;
   var eindex = 0;
   var ctext = "";

   if (pfdata == null || pfdata.length == 0) {
      if ((bsniff.ie55 || bsniff.ie6) && !bsniff.mac) {
         var http = new ActiveXObject("Microsoft.XMLHTTP");
         http.open ("POST", qURL, false);
         http.setRequestHeader ("Content-type", "application/x-www-form-urlencoded");
         http.send (rStr);
         ctext = http.responseText;
      }
      else {   // Use the applet code instead
         var rURL = qURL;
         if (rStr.length)
            rURL += "?" + rStr;
         ctext = document.getURL.fetch (rURL, null);
         var z = ctext.substr(4);
         ctext = z + "\n";
      }
   }
   else {
      ctext = pfdata;
   }
   if (ctext.length < 2) return;

   while (cindex < len) {
      if (ObjList[cindex] != null && (typeof ObjList[cindex]) == "object") {
         if (ObjList[cindex].tagName == "P")
            ObjList[cindex].innerText = "";
         else
            ObjList[cindex].value = "";
      }
      cindex++;
   }
   while (ctext.length > 0) {
      cindex = ctext.indexOf ("\n");
      if (cindex > 0) {
         var comboline = ctext.substring (0, cindex);
         eindex = comboline.indexOf ("=");
         if (eindex > 0) {
            key = comboline.substring (0, eindex);
            val = comboline.substring (eindex+1);
            for (J = 0; J < len; J++) {
               if (KeyList[J] == key) {
                  if (ObjList[J] != null && (typeof ObjList[J]) == "object") {
                     if (ObjList[J].type == "checkbox") {
                        if (val == "1" || val.toLowerCase() == "true" || val.toLowerCase() == "y") {
                           ObjList[J].value = "1";
                           ObjList[J].checked = true;
                        }
                        else {
                           ObjList[J].value = "0";
                           ObjList[J].checked = false;
                        }
                     }
                     else {
                        if (ObjList[J].tagName == "P")
                           ObjList[J].innerText = val;
                        ObjList[J].value = val;
                     }
                  }
                  break;
               }
            }
         }
         ctext = ctext.substring (cindex+1);
      }
   }
}

function _submitData (qURL, rStr) {
//          _submitData ("../scripts/CompanySaveData-pl.cgi", reqstr);

   var http = new ActiveXObject("Microsoft.XMLHTTP");
   http.open ("POST", qURL, false);
   http.setRequestHeader ("Content-type", "application/x-www-form-urlencoded");
   http.send (rStr);
   var ctext = http.responseText;
   if (ctext.substring (0, 1) == "0")
      retcode = false;
   else
      retcode = true;
   return (retcode);
}


function switchColor (newColorName) {
   var newColor = "#000000";
   if (newColorName == "Black")
      newColor = "#000000";
   return newColor;
}

function reverseColor (curColorValue) {
   var curColorName = "Black";
   if (curColorValue == "#000000")
      curColor = "Black";
   return curColorName;
}

function getCookie (name) {
   var cname = name + "=";
   var clen = cname.length;
   var tlen = document.cookie.length;
   var i = 0;
   while (i < tlen) {
      var j = i + clen;
      if (document.cookie.substring (i, j) == cname) {
         var eindex = document.cookie.indexOf (";", j);
         if (eindex == -1)
            eindex = document.cookie.length;
         return (unescape(document.cookie.substring(j, eindex)));
      }
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0)
         break;
   }
   return null;
}
