//
//             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 ("safari") >= 0) {
      this.sfr    = true;
   }
   else if (userAgent.indexOf ("opera") >= 0) {
      this.opera = true;
   }
   else 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);
   }
   this.win      = (userAgent.indexOf ("win") >= 0);
   this.mac      = (userAgent.indexOf ("mac") >= 0);
   this.unix     = (userAgent.indexOf ("x11") >= 0);
   this.lineSep  = (this.mac ? '\r' : '\n');
   this.ie6SP2   = (userAgent.indexOf("sv1") != -1)
   
}

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, wnd) {
	if(!wnd)
		wnd = window;
   if (wnd.document.frames)
      return wnd.document.frames [name];
   return wnd.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) {
	if(application_height && application_width){
var ccy,ccx;
cy = parseInt(application_height) - parseInt(window.innerHeight);
cx = parseInt(application_width) - parseInt(window.innerWidth);
//debugAlert ("window.resizeBy (" + ccx + ", " + ccy + ")");
}
else{
      cy = document.height + 20 - window.innerHeight;
      cx = document.width + 16 - window.innerWidth;
//	alert(window.document.body.scrollWidth +" window.document.body.clientWidth "+ window.document.body.clientWidth  );
//	alert("document.width - window.innerWidth "+document.width + " - "+ 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 openExtLink (exturl) {
		var extwindow = window.open(exturl,"","toolbar=1,scrollbars=1,statusbar=no,width=650,height=450,resizable=1");
		extwindow.focus();
	}
	
	
function resizeWindowBy (cx, cy, wnd) {
   if (!wnd)
      wnd = window;
   if (isDialog (wnd)) {
      wnd.dialogWidth  = (parseInt (window.dialogWidth)  + cx) + "px";
      wnd.dialogHeight = (parseInt (window.dialogHeight) + cy) + "px";
   }
   else {
      window.resizeBy (cx, cy);
   }
}

function expandFilebyLang(name, lang)
{
	if(lang == "")
		return name;
	var arr = name.split(".");
	return arr[0] + "_" + lang + "." + arr[1];
}
 
 
 function getParent()
{
	return ((parent == window  && window.frameElement) ? window.frameElement.parentElement.ownerDocument.parentWindow : parent );
}


////////////////////////////////////////////////////////////////////////////
//
//	Cookies
//

function getCookie (name) 
{
// alert(document.cookie);
	var cookies = document.cookie.split(";");
	for(i = 0; i < cookies.length; i++) {
		if (cookies[i].indexOf(name + "=") !=-1 ) {
			return cookies[i].substr(cookies[i].indexOf(name + "=") + name.length  + 1, cookies[i].length);
		}
	}
	return "";
}

function setCookie(name, value)
{
	var dt=new Date((new Date()).getTime() + 24000*3600000);
	setCookieEx(name, value, dt)
	return;
/*	
	var cookieVal = name + "=" + value
	var cookies = document.cookie.split(";");
	alert(document.cookie);
///
	var cur;
	if(document.cookie == "") {
		cur = 0;
	}
	else {
		cur = cookies.length;
	}
	for(i = 0; i < cookies.length; i++) {
		if (cookies[i].indexOf(name + "=") !=-1 ) {
			cur = i; 
			break;
		}
	}
	cookies[cur] = cookieVal;
	var strCookies = cookies.join(";") + ";expires=" + expires + ";path=/;"; 
///
		document.cookie = cookieVal + ";" ;
	expires = dt.toGMTString();
	path = "/";
	alert(document.cookie);

	*/
	
}

function setCookieEx(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


function setFrameSource(id, url)
{
	var frame = getDocElementById (document, id);
    if(browser.opera &&  browser.operaVer < 7 ) {
         frame.location.href = url;
     }
     else if( browser.ie || browser.operaVer >= 7 ){
		document.frames[id].location.replace(url);
      }
     else {
          frame.src = url;
     } 
	 return;
}
