var dom = (document.getElementById) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var ie5 = (document.all && dom) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true : false;
var ns6 = (!document.all && dom) ? true : false;
var nodyn = (!ns5 && !ns4 && !ie4 && !ie5) ? true : false;
if (nodyn) { event = "nope" }

if(ie4)
	document.getElementById = function(id) {return document.all[id];}
else if(!document.getElementById && !document.all)
  document.getElementById = function(id) {return {style:{}};}

String.prototype.trim = function() // trims white space off both ends of a string
{
  return (this.replace(/^\s*/, "")).replace(/\s*$/, "");
}

function HidePopUpBox()
{
  var oDiv = document.getElementById("PopUpBox");
  
  if (oDiv)
  {
    oDiv.style.visibility = "hidden";
  }
}

function ShowPopUpBox(evt, HTML)
{
  var oDiv = document.getElementById("PopUpBox");
  
  if (oDiv)
  {
    var offX = -15;
    var offY = 15;
    var mousePosition = new XYCoords();
    
    mousePosition = getEventCoords(evt);
    oDiv.style.left = (mousePosition.x+offX)+'px';
    oDiv.style.top = (mousePosition.y+offY)+'px';
    oDiv.innerHTML = HTML;
    oDiv.style.visibility = "visible";
  }
}

XYCoords = function (x,y)
{ 
  this.x = x || 0;
  this.y = y || 0;	
  return this;
}

function getWindowSize()
{
  var WindowSize = new XYCoords();
  
  if (typeof( window.innerWidth ) == 'number')
  {
    // Non-IE
    WindowSize.x = window.innerWidth;
    WindowSize.y = window.innerHeight;
  }
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight) )
  {
    // IE 6+ in 'standards compliant mode'
    WindowSize.x = document.documentElement.clientWidth;
    WindowSize.y = document.documentElement.clientHeight;
  }
  else if (document.body && (document.body.clientWidth || document.body.clientHeight))
  {
    // IE 4 compatible
    WindowSize.x = document.body.clientWidth;
    WindowSize.y = document.body.clientHeight;
  }
  return WindowSize;
}

function getScrollXY()
{
  var scrOfX = 0, scrOfY = 0;
  
  if (typeof( window.pageYOffset ) == 'number' )
  {
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	}
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	}
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
}

function getEventCoords(e)
{
  var coords = new XYCoords();
  
  if (!e)
  {
    e = window.event;
  }
  
  if (!e || (typeof(e.pageX) != 'number' && typeof(e.clientX) != 'number'))
  {
    return coords;
  }
  
	if (typeof(e.pageX) == 'number')
	{
	  coords.x = e.pageX;
	  coords.y = e.pageY;
	}
	else
	{
		coords.x = e.clientX;
		coords.y = e.clientY;
		
		if (!((window.navigator.userAgent.indexOf('Opera') + 1 ) || (window.ScriptEngine && ScriptEngine().indexOf( 'InScript') + 1 && !document.createElement) || window.navigator.vendor == 'KDE'))
		{
			if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft))
			{
				coords.x += document.documentElement.scrollLeft;
				coords.y += document.documentElement.scrollTop;
			}
			else if (document.body && (document.body.scrollTop || document.body.scrollLeft))
			{
				coords.x += document.body.scrollLeft;
				coords.y += document.body.scrollTop;
			}
		}
	}
  return coords;
}

function StartClock(idImg, stime)
{
  var d = new Date();
  var ltime = d.getTime();
  document.clockDiff = (ltime - stime);
  document.clockID = null;
  document.clockRunning = false;
  document.clockImg = idImg;
  document.lMinutes = -1;
  UpdateClock();
}

function StopClock()
{
	if (document.clockRunning)
	{
	  clearTimeout(clockID);
  }
	document.clockRunning = false;
}

function UpdateClock()
{
	var ltime = new Date(); 
	var ltime_unix = ltime.getTime();
	ltime = new Date(ltime_unix - document.clockDiff);
	
	var lHours = ltime.getHours();
	var lMinutes = ltime.getMinutes();
	var sHours = ltime.getUTCHours();
	var sMinutes = ltime.getUTCMinutes();
	
  var oIMG = document.getElementById(document.clockImg);
	if (oIMG && lMinutes != document.lMinutes)
	{
    oIMG.alt = 'UTC Time: ' + fmtTime(sHours) + ':' + fmtTime(sMinutes) + ' Local Time: ' + fmtTime(lHours) + ':' + fmtTime(lMinutes);
    document.lMinutes = lMinutes;    
	}
	var wtime = ltime_unix - document.clockDiff;
	clockID = setTimeout('UpdateClock()',1000 - (wtime % 1000));
	clockRunning = true;
}

function fmtTime(t)
{
  return (t < 10) ? ('0' + t) : t;
}

function addToFavorites()
{
  if (window.external)
    window.external.AddFavorite('http://www.gujlu.com', 'Gujlu');
  else 
    alert("Sorry! Your browser doesn't support this function.");
}

function goURL(url,target)
{
  if (target == null || target == "_self")
  {
    window.location.href=url;
  }
  if (target == "_blank")
  {
    window.open(url);
 }
}

function openWin(url, name, scrollbars, resizeable, width, height)
{
  getWin(url, name, scrollbars, resizeable, width, height);
}

function getWin(url, name, scrollbars, resizeable, width, height)
{
  return window.open(url, name, 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars='+ scrollbars +',width='+ width +',height='+ height +',resizable=' + resizeable);
}

function PrintIt()
{
  if (window.print)
  {
    window.print();
  } else
  {
    msg = "Javascript cannot print from your browser. To print this page, select the menu item File - Print at the top of this page.";
    alert(msg);
  }
  return false ;
}

function setFocus(id)
{
  var oControl = document.getElementById(id);
  if (oControl)
  {
    oControl.focus();
  }
}

function onTextBoxFocus(oInput, maxLength)
{
  if (oInput)
  {
    oInput.style.backgroundColor = '#FFFFFF';
    if (oInput.type == 'text' || oInput.type == 'textarea' || oInput.type == 'password') {
      if (maxLength >= 1)
      {
        if (oInput.onkeydown == null)
        {
          oInput.onkeydown = (function(){textCounter(oInput, maxLength)});
          oInput.onkeyup = (function(){textCounter(oInput, maxLength)});
        }
        textCounter(oInput, maxLength);
      }
    }
  }
}

function onTextBoxBlur(oInput)
{
  if (oInput)
  {
    oInput.style.backgroundColor = '#FFFFFF';
    window.status = '';
  }
}

function textCounter(oInput, maxLength)
{
  if (oInput)
  {
    if (oInput.value.length > maxLength) // if too long...trim it!
    {
      oInput.value = oInput.value.substring(0, maxLength);
    }
    
    var spanUsed = document.getElementById(oInput.id + '_CharsUsed')
    var spanLeft = document.getElementById(oInput.id + '_CharsLeft')
    
    if (spanUsed)
    {
      spanUsed.innerHTML = oInput.value.length;
    }
    if (spanLeft)
    {
      spanLeft.innerHTML = (maxLength - oInput.value.length);
    }
  }
}

function getConfirm()
{
  return (confirm('Are you sure?'));
}
