/* -------------------------------------------------------------------------------------------------------- *\
   Global Function
\* -------------------------------------------------------------------------------------------------------- */
function GetCookie(sName)
{
  iStart = document.cookie.indexOf(sName + "=");
  if (iStart != -1)
  { 
    iStart = iStart + sName.length + 1;
    iEnd = document.cookie.indexOf(";", iStart)

    if (iEnd==-1)
      iEnd = document.cookie.length;

    return unescape(document.cookie.substring(iStart, iEnd));
  }
}
function SetCookie(sName, sValue, dDate)
{
  document.cookie = sName + "="  + escape(sValue);
}
  
function GI(sId)
{
  return document.getElementById(sId);
}

function GC(needle)
{
  var my_array = document.getElementsByTagName("*");
  var retvalue = new Array();
  var i, j;

  for (i=0,j=0;i<my_array.length;i++)
  {
    var c = " " + my_array[i].className + " ";
    if (c.indexOf(" " + needle + " ") != -1)
      retvalue[j++] = my_array[i];
  }
  return retvalue;
}

function GT(sId)
{
  return document.getElementsByTagName(sId);
}

function uniq_id()
{
  sGuid = "";
  for(i=0; i<8; i++)
    sGuid += (((1+Math.random())*0x10000)|0).toString(16).substring(1);

  return sGuid;
}

function trace_array(aArray, iLevel)
{
  if (arguments.length == 1)
    var iLevel = 0

  var sR = "",
      sLevel = "";

  for(var i = 0; i<iLevel; i++)  
    sLevel += "  ";

  for(var i in aArray)
  {
    if (typeof(aArray[i]) != "function")
    {
      if (typeof(aArray[i]) == "undefined" || typeof(aArray[i]) == "")
        sR += sLevel + "[" + i + "] => undefined<br />";
      else if (aArray[i] == null)
        sR += sLevel + "[" + i + "] => null<br />";
      else if (typeof(aArray[i].constructor) != "undefined" && aArray[i].constructor.toString().indexOf("Array") != -1)
        sR += sLevel + "[" + i + "] => array\n" + sLevel + "(\n" + trace_array(aArray[i], iLevel+1) + sLevel + ")\n";
      else if (typeof(aArray[i]) == "object" && aArray.id)
        sR += sLevel + "<a style=\"font-family:Lucida Console;font-size:8pt;color:#000;\" href=\"javascript:void(0);\" onclick=\"trace(GI('" + aArray.id + "')." + i + ");\">[" + i + "]</a> => " + htmlencode(aArray[i]) + "<br />";
      else if (typeof(aArray[i]) == "object")
        sR += sLevel + "[" + i + "] => " + htmlencode(aArray[i]) + "<br />";
      else
        sR += sLevel + "[" + i + "] => " + htmlencode(aArray[i]) + "<br />";
    }
  }
  
  return sR; 
}

function trace()
{
  if (GI('debug') && GI('debug').tagName != "PRE")
    GI('debug').parentNode.removeChild(GI('debug'));

  if (!GI('debug'))
  {
    var oPre = document.createElement('pre');
    oPre.setAttribute('id','debug');
    oPre.style.width = "95%";
    oPre.style.background = "#FFFFE1";
    oPre.style.padding = "10px";
    oPre.style.border = "solid 1px #999999";
    oPre.style.margin = "6px";
    oPre.style.position = "relative";
    oPre.style.zIndex = "999";
    document.body.insertBefore(oPre, document.body.firstChild);
  }

  for(var i=0; i<arguments.length; i++)
  {
      if (typeof(arguments[i]) == "undefined")
      GI('debug').innerHTML += "<span style=\"font-family:Lucida Console;font-size:8pt;color:gray;\">[arg:" + i + "] [array]\n<span style=\"font-family:Lucida Console;font-size:8pt;color:black;\">undefined</span><hr style=\"width: 100%; border-right: none;border-left: none;border-bottom: none;border-top: 1px solid grey;height: 1px;margin:10px 0 5px 0;\" />";
    else if (arguments[i] == null)
      GI('debug').innerHTML += "<span style=\"font-family:Lucida Console;font-size:8pt;color:gray;\">[arg:" + i + "] [array]\n<span style=\"font-family:Lucida Console;font-size:8pt;color:black;\">null</span><hr style=\"width: 100%; border-right: none;border-left: none;border-bottom: none;border-top: 1px solid grey;height: 1px;margin:10px 0 5px 0;\" />";    
    else if (typeof(arguments[i].constructor) != "undefined" && arguments[i].constructor.toString().indexOf("Array") != -1)
      GI('debug').innerHTML += "<span style=\"font-family:Lucida Console;font-size:8pt;color:gray;\">[arg:" + i + "] [array]\n<span style=\"font-family:Lucida Console;font-size:8pt;color:black;\">" + trace_array(arguments[i]) + "</span><hr style=\"width: 100%; border-right: none;border-left: none;border-bottom: none;border-top: 1px solid grey;height: 1px;margin:10px 0 5px 0;\" />";
    else if (typeof(arguments[i]) == "object")
      GI('debug').innerHTML += "<span style=\"font-family:Lucida Console;font-size:8pt;color:gray;\">[arg:" + i + "] [object]\n<span style=\"font-family:Lucida Console;font-size:8pt;color:black;\">" + trace_array(arguments[i]) + "</span><hr style=\"width: 100%; border-right: none;border-left: none;border-bottom: none;border-top: 1px solid grey;height: 1px;margin:10px 0 5px 0;\" />";
    else if (typeof(arguments[i]) == "function")
      GI('debug').innerHTML += "<span style=\"font-family:Lucida Console;font-size:8pt;color:gray;\">[arg:" + i + "] [function]\n<span style=\"font-family:Lucida Console;font-size:8pt;color:black;\">" + trace_array(arguments[i]) + "</span><hr style=\"width: 100%; border-right: none;border-left: none;border-bottom: none;border-top: 1px solid grey;height: 1px;margin:10px 0 5px 0;\" />";
    else
      GI('debug').innerHTML += "<span style=\"font-family:Lucida Console;font-size:8pt;color:gray;\">[arg:" + i + "] [" + typeof(arguments[i]) + "]\n<span style=\"font-family:Lucida Console;font-size:8pt;color:black;\">" + htmlencode(arguments[i]) + "</span><hr style=\"width: 100%; border-right: none;border-left: none;border-bottom: none;border-top: 1px solid grey;height: 1px;margin:10px 0 5px 0;\" />";
  }
}

function htmlencode(sHTML)
{
  var sHTML = new String(sHTML);
  sHTML = sHTML.replace(/&/g, "&amp;");
  sHTML = sHTML.replace(/</g, "&lt;");
  sHTML = sHTML.replace(/>/g, "&gt;");
  sHTML = sHTML.replace(/"/g, "&quot;");
  return sHTML;
}

function GetScroolTop()
{
  bIE = document.all && document.getElementById;
  bGK = document.getElementById && !document.all;

  if (bGK)
  {
    iScrool = parseInt(window.pageYOffset);
    iTotal = window.innerHeight;
  }
  else if (bIE)
  {
    iScrool = document.body.scrollTop ? parseInt(document.body.scrollTop) :  parseInt(document.documentElement.scrollTop);
    iTotal = document.body.clientHeight;
  }

  return Number(iScrool);
}

var GetClientXY = function()
{
  var myWidth = 0, myHeight = 0;
  if (typeof(window.innerWidth) == 'number')
  {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  }
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
  {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  }
  else if (document.body && (document.body.clientWidth || document.body.clientHeight))
  {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

var GetScroolXY = function()
{
  var scrOfX = 0, scrOfY = 0;
  if (typeof(window.pageYOffset) == 'number')
  {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  }
  else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function GetAbsoluteTop(oNode)
{
  var oCurrentNode = oNode;
  var iTop = 0;
  while (oCurrentNode.tagName != "BODY") {
    iTop += oCurrentNode.offsetTop;
    oCurrentNode = oCurrentNode.offsetParent;
  };
  return iTop;
}

function IsStudioServer()
{
  sDomain = document.location.href.substring(7);
  sDomain = sDomain.substring(0, sDomain.indexOf('\/'));
  sTLD = sDomain.substr(sDomain.lastIndexOf('.')+1);
  return sTLD == "lan" || sTLD == "dev" ? true : false;
}

function GetQueryString()
{
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for(var i = 0; i < pairs.length; i++)
  {
		var pos = pairs[i].indexOf('=');
		if (pos == -1)
      continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
  return args;
} 

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}