//
//             Copyright (c) Smartlink Corp., 2002.
//                    All rights reserved.
//

//////////////////////////////////////////////////////////////////////////////
//
// Browser type
//

function Browser () {
   var userAgent = navigator.userAgent.toLowerCase ();
   this.major    = parseInt (navigator.appVersion);
   this.version  = parseFloat (navigator.appVersion);
   if (userAgent.indexOf ("msie") >= 0) {
      this.ie    = true;
   }
   else if (userAgent.indexOf ("mozilla") >= 0) {
      this.ns    = true;
      this.ns4   = (this.version >= 4 && this.version < 5);
   }
   if (userAgent.indexOf ("opera") >= 0) {
      this.opera = true;
   }
   this.win      = (userAgent.indexOf ("win") >= 0);
   this.mac      = (userAgent.indexOf ("mac") >= 0);
   this.unix     = (userAgent.indexOf ("x11") >= 0);
   this.lineSep  = (this.mac ? '\r' : '\n');
}

var browser = new Browser ();

//////////////////////////////////////////////////////////////////////////////
//
// Controls
//

function disableControl (ctrl) {
   if (browser.ns4 || browser.opera)
      return;
   ctrl.disabled = true;
}

function enableControl (ctrl, enable) {
   if (browser.ns4 || browser.opera)
      return;
   var disabled = true;
   if (typeof (enable) == "undefined" || enable)
      disabled = false;
   ctrl.disabled = disabled;
}

function hideControl (ctrl) {
   if (ctrl.style)
      ctrl.style.display = "none";
   else
      ctrl.visibility = "hidden";
}

function showControl (ctrl, show) {
   var show = (typeof (show) == "undefined" || show ? true : false);
   if (ctrl.style)
      ctrl.style.display = (show ? "block" : "none");
   else
      ctrl.visibility = (show ? "show" : "hidden");
}

function getListValue (ctrl) {
   var index = ctrl.selectedIndex;
   if (index < 0 || index >= ctrl.length)
      return;
   return ctrl [ctrl.selectedIndex].value;
}

function removeListItem (ctrl, index) {
   var length = ctrl.length;
   if (index < 0 || index >= length)
      return;
   if (browser.ie) {
      ctrl.options.remove (index);
   }
   else {
      var options = ctrl.options;
      for (var i = index; i < length - 1; i++) {
         options [i].value = options [i + 1].value;
         options [i].text  = options [i + 1].text;
      }
      setListLength (ctrl, length - 1);
   }
}

function insertListItem (ctrl, index, text, value) {
   var length = ctrl.length;
   if (index < 0 || index > length)
      index = length;
   setListLength (ctrl, length + 1);
   var options = ctrl.options;
   for (var i = length; i > index; i--) {
      options [i].value = options [i - 1].value;
      options [i].text  = options [i - 1].text;
   }
   options [index].text  = text;
   options [index].value = value;
}

function setListItem (ctrl, index, text, value) {
   var options = ctrl.options;
   if (browser.opera)
      options [index] = new Option;
   options [index].text  = text;
   options [index].value = value;
}

function setListLength (ctrl, len) {
   if (browser.opera) {
      if (ctrl.length > len) {
         for (var i = ctrl.length - 1; i >= len; i--)
            ctrl.options [i] = null;
      }
      if (ctrl.length < len) {
         for (i = ctrl.length; i < len; i++)
            ctrl.options [i] = new Option;
      }
   }
   ctrl.length = len;
}

//////////////////////////////////////////////////////////////////////////////
//
// Elements
//

function getDocElementById (doc, id) {
   if (!doc)
      doc = document;
   if (browser.ns4) {
      var elem = doc [id];
      if (elem)
         return elem;
   
      var forms = doc.forms;
      for (var i = 0; i < forms.length; i++) {
         elem = forms [i] [id];
         if (elem)
            return elem;
      }
      return null;
   }
   return doc.getElementById (id);
}

function setInnerHTML (elem, html) {
   if (!elem)
      return;
   if (typeof (elem.innerHTML) == "string") {
      elem.innerHTML = html;
   }
   else if (browser.ns4) {
      var doc = elem.document;
      html = '<div class="' + elem.id + '">' + html + '</div>';
      doc.open (); doc.writeln (html); doc.close ();
   }
}

function setElementStyle (elem, name, value) {
   var style = elem.style;
   if (style) {
      switch (name) {
         case "color":       style.color = value;      break;
         case "font-weight": style.fontWeight = value; break;
      }
   }
}

//////////////////////////////////////////////////////////////////////////////
//
// Windows/Dialogs
//

function isDialog (wnd) {
   if (!wnd)
      wnd = window;
   return (wnd.dialogHeight ? true : false);
}

function adjustWindowSize () {
   var cx = 0, cy = 0;
   if (browser.ie) {
      var body = document.body;
      var cx = body.scrollWidth - body.clientWidth;
      var cy = body.scrollHeight - body.clientHeight;
      if (window.dialogWidth) {
         // debugAlert ("window.resizeBy (" + cx + ", " + cy + ")");
         window.dialogWidth  = (parseInt (window.dialogWidth)  + cx) + "px";
         window.dialogHeight = (parseInt (window.dialogHeight) + cy) + "px";
         return;
      }
   }
   else if (document.height && window.innerHeight) {
      cy = document.height + 20 - window.innerHeight;
      cx = document.width + 16 - window.innerWidth;
   }
   // debugAlert ("window.resizeBy (" + cx + ", " + cy + ")");
   window.resizeBy (cx, cy);
}

//////////////////////////////////////////////////////////////////////////////
//
// String
//

function strtrim (str) {
   var spaces = " \r\n\t";
   
   var start;
   for (start = 0; start < str.length; start++) {
      if (spaces.indexOf (str.charAt (start)) < 0)
         break;
   }
   
   var end;
   for (end = str.length; end > 0 && end > start; end--) {
      if (spaces.indexOf (str.charAt (end - 1)) < 0)
         break;
   }
   
   return str.substring (start, end);
}

function strcmp (str1, str2) {
   if (str1 < str2)
      return -1;
   if (str1 > str2)
      return 1;
   return 0;
}

function compareStr (str1, str2) {
   var cmp = strcmp (str1.toLowerCase (), str2.toLowerCase ());
   if (cmp == 0)
      cmp = strcmp (str1, str2);
   return cmp;  
}
      
function htmlEncode (str) {
   str = str.replace (/&/g, "&amp;");
   str = str.replace (/</g, "&lt;");
   str = str.replace (/>/g, "&gt;");
   // str = str.replace (/'/g, "&apos;");
   // str = str.replace (/"/g, "&quot;");
   return str;
}

//////////////////////////////////////////////////////////////////////////////
//
// Array
//

function removeAt (arr, index) {
   if (index < 0 || index >= arr.length)
      return;
   for (index++; index < arr.length; index++)
      arr [index - 1] = arr [index];
   arr.length = arr.length - 1;
}

//////////////////////////////////////////////////////////////////////////////
//
// Debug
//

function debugAlert (message) {
   alert (message);
}

function debugProperties (obj) {
   var arr = new Array ();
   var prop
   for (prop in obj)
      arr.push (prop.toString ());
   arr.sort ();
   var str = obj + ":\n";
   for (i = 0; i < arr.length; i++)
      str += arr [i] + '\t';
   debugAlert (str);
}
