//
//             Copyright (c) 2002, 2003 Smartlink Corp.
//                       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);
   }
   else 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 getListValue (ctrl, defaultValue) {
   var index = ctrl.selectedIndex;
   if (index < 0 || index >= ctrl.length)
      return defaultValue;
   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;
         case "border-style": style.borderStyle = value; break;
         case "border-color": style.borderColor = value; break;
      }
   }
}

function getFrame (name) {
   if (document.frames)
      return document.frames [name];
   return window.frames [name];
}

//////////////////////////////////////////////////////////////////////////////
//
// 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;
}

function getQueryParam (query, param) {
   var value = new RegExp (param + "=([^&]*)").exec (query);
   if (!value || value.length < 2)
      return "";
   return value [1];
}

//////////////////////////////////////////////////////////////////////////////
//
// 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);
}


function findCtrlEx (name, doc) {
   if (!doc)
      doc = document;
   var forms = doc.forms;
   for (var i = 0; i < forms.length; i++) {
      var ctrl = forms [i] [name];
      if (ctrl)
         return ctrl;
   }
   for (i =0; i < doc.frames.length; i++) {
   	return findCtrlEx(name, doc.frames[i].document);
   }
   return null;
}

////////////////////////////////////////////////////////////////////////////
//
//	Cookies
//

function getCookie (name) 
{
//debugAlert(document.cookie);
	var cookies = document.cookie.split(";");
	for(i = 0; i < cookies.length; i++) {
		if (cookies[i].indexOf(name + "=") !=-1 ) {
			return cookies[i];
		}
	}
	return "";
}

function setCookie(name, value)
{
	var dt=new Date((new Date()).getTime() + 24000*3600000);
	 var cookieVal = name + "=" + value
	var cookies = document.cookie.split(";");
	var cur = cookies.length;
	for(i = 0; i < cookies.length; i++) {
		if (cookies[i].indexOf(name + "=") !=-1 ) {
			cur = i; break;
		}
	}
	cookies[cur] = cookieVal;
	document.cookie= cookies.join(";"); 
	expires = dt.toGMTString();
}

///////////////////////////////////////////////////////////////////////
//
// Request
//

function sendRequest (id, url) {
   var request = getDocElementById (document, id);
   var tagName = (browser.ns4 ? "ilayer" : "iframe");
   if ("iframe" == tagName) {
     if(browser.opera ) {
         request.location.href = url;
     }
 /*    else if(browser.ie ){
        document.frames[id].location.replace(url)

      }*/
     else {
          request.src = url;
      } 
    }
   if ("ilayer" == tagName) {
       request.load (url, 1);
     }
      
   return true;
}
function getStr(value, defvalue)
{
	if(value == null || value == "")
		return defvalue;
	return value;	
}
