var retctrl = false;
var retalt = false;
var showtimeout;
var myclose = false;

function KeyPressed(event)
{
  if(event.ctrlKey == 1)
  {
    retctrl = true;
  }
  else
  {
    retctrl = false;
  }
  if(event.altKey == 1)
  {
    retalt = true;
  }
  else
  {
    retalt = false;
  }
}
//------------------------------------------------------------------------------
function in_array(what, where)
{
  var a=false;
  for(var i=0;i<where.length;i++)
  {
    if(what == where[i])
    {
      a=true;
      break;
 	  }
  }
  return a;
}
//------------------------------------------------------------------------------
function trim(str)
{
  if(!str || typeof str != 'string')
    return null;
  return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}
//------------------------------------------------------------------------------
function toggleHeading(heading)
{
  imgctl = document.getElementById(heading + "img");
  if(imgctl.src.match(/triangle-arrow-right\.png/))
    imgctl.src = "/images/triangle-arrow-down.png";
  else
    imgctl.src = "/images/triangle-arrow-right.png";
  toggleList(heading);
}
//------------------------------------------------------------------------------
function toggleList(e)
{
	element = document.getElementById(e).style;
	element.display == 'none' ? element.display = 'block' : element.display='none';
}
//------------------------------------------------------------------------------
function setCheckBox(ctlid, ctlName, clear)
{
  var aStr = new String();
  inputs = document.getElementsByTagName('input');
  var ctl = document.getElementById(ctlid);
  ctlValue = ctl.checked;
  if(!retctrl)
  {
    for(var obj in inputs)
    {
      aStr = obj;
      if(aStr.indexOf(ctlName) >= 0)
      {
        document.getElementById(aStr).checked = false;
      }
    }
  }
  retctrl = false;
  if(!clear)
    ctl.checked = (ctlValue);
}
//------------------------------------------------------------------------------
function getHTTPObject()
{
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
  try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
  alert("XMLHttpRequest not supported");
  return null;
}
//------------------------------------------------------------------------------
function openpopup(url, windowtarget, windowWidth, windowHeight)
{
    window.open(
        url,
        windowtarget,
        'width='+windowWidth+',height='+windowHeight+',toolbar=0,top=200,location=0,directories=0,status=1,menubar=0,scrollbars=0,titlebar=0,resizable=1');
}
//------------------------------------------------------------------------------
function updateColor(id, color)
{
  document.getElementById(id).style.backgroundColor=color;
}
//------------------------------------------------------------------------------
function ConfirmClose()
{
	if (event.clientY < 0)
	{
		setTimeout('myclose=false',500);
		myclose=true;
	}
}
//------------------------------------------------------------------------------
function HandleOnClose()
{
	if(myclose == true)
	{
  	doLogout("windowclose");
  }
}
//------------------------------------------------------------------------------
function doLogout(logoutType)
{
  var sVars = "type="+logoutType;
  var url = "logout.php?"+sVars;
  httpObject = getHTTPObject();
  if (httpObject != null)
  {
    httpObject.open("GET", url, true);
    httpObject.send(sVars);
    httpObject.onreadystatechange = function (){
      if(httpObject.readyState == 4)
      {
        return httpObject.responseText;
      }
    };
  }
}
//------------------------------------------------------------------------------
function validate_required(field,alerttxt)
{
  with (field)
  {
    if (value == null || value == "" || value == "NULL")
    {
      alert(alerttxt);
      return false;
    }
    else
    {
      return true;
    }
  }
}
//------------------------------------------------------------------------------
function validate_email(field,alerttxt)
{
  with (field)
  {
    apos = value.indexOf("@");
    dotpos = value.lastIndexOf(".");
    if (apos<1 || dotpos-apos<2)
    {
      alert(alerttxt);
      return false;
    }
    else
      return true;
  }
}
//------------------------------------------------------------------------------
function Timer(){
this.obj = (arguments.length)?arguments[0]:window;
return this;
}

// The set functions should be called with:
// - The name of the object method (as a string) (required)
// - The millisecond delay (required)
// - Any number of extra arguments, which will all be
// passed to the method when it is evaluated.

Timer.prototype.setInterval = function(func, msec){
var i = Timer.getNew();
var t = Timer.buildCall(this.obj, i, arguments);
Timer.set[i].timer = window.setInterval(t,msec);
return i;
}
Timer.prototype.setTimeout = function(func, msec){
var i = Timer.getNew();
Timer.buildCall(this.obj, i, arguments);
Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
return i;
}

// The clear functions should be called with
// the return value from the equivalent set function.

Timer.prototype.clearInterval = function(i){
if(!Timer.set[i]) return;
window.clearInterval(Timer.set[i].timer);
Timer.set[i] = null;
}
Timer.prototype.clearTimeout = function(i){
if(!Timer.set[i]) return;
window.clearTimeout(Timer.set[i].timer);
Timer.set[i] = null;
}

// Private data

Timer.set = new Array();
Timer.buildCall = function(obj, i, args){
var t = "";
Timer.set[i] = new Array();
if(obj != window){
Timer.set[i].obj = obj;
t = "Timer.set["+i+"].obj.";
}
t += args[0]+"(";
if(args.length > 2){
Timer.set[i][0] = args[2];
t += "Timer.set["+i+"][0]";
for(var j=1; (j+2)<args.length; j++){
Timer.set[i][j] = args[j+2];
t += ", Timer.set["+i+"]["+j+"]";
}}
t += ");";
Timer.set[i].call = t;
return t;
}
Timer.callOnce = function(i){
if(!Timer.set[i]) return;
eval(Timer.set[i].call);
Timer.set[i] = null;
}
Timer.getNew = function(){
var i = 0;
while(Timer.set[i]) i++;
return i;
}