// commonly-used JavaScript functions.

var statusPrefix = 'status_';    // used by all AJAXy pages to indicate a status span.

// base function that sets up a propert AJAX object for the browser.  This is used
// by all the AJAX functions in the owner set.
function GetXmlHttpObject()
{
  var objXmlHttp=null;

  try 
  {
    objXmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    try
    {
      objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      }    
      catch(e)
      {
        alert("AJAX not enabled on this browser.\nContact GateMaster at 1-877-823-0220 for more information.")
      }
    }
  }
  
  return objXmlHttp;
}


// used when fetching the result from an arbitrary PHP script and dumping it
// in a div or span tag (with id "spanID") without any pre-processing.
function GetXmlHttpObject_ToSpan(spanID) 
{ 
  var objXmlHttp=GetXmlHttpObject();
    
  objXmlHttp.onreadystatechange=function () 
  { 
    if (objXmlHttp.readyState==4 || objXmlHttp.readyState=="complete")
    {
      if (TimeOutCheck(objXmlHttp.responseText))
        document.getElementById(spanID).innerHTML=objXmlHttp.responseText;
    }
  }
      
  return objXmlHttp;
}

// toggle the AddUser/AddProperty box (no sense in having two separate functions).
function ToggleAddBlock(elementToShow, elementToHide) 
{
  document.getElementById(elementToShow).style.display = '';
  document.getElementById(elementToHide).style.display = 'none';
}

// get data from a form and parse it for posting to a PHP
// script.  Taken from http://www.state26.com/downloads/formdata2querystring.txt
// this script is VERY useful for AJAX programming.  All you have to do
// is make sure that all your input tags have a "name" attribute, and that
// your form has an ID.

function FormatFormData(docForm) 
{
  var submitContent   = '';
  var formElem;
  var lastElemName    = '';
  
  if (!docForm.elements)
    return '';
        
  for (i = 0; i < docForm.elements.length; i++) 
  {
    formElem = docForm.elements[i];
    switch (formElem.type) 
    {
     // Text, select, hidden, password, textarea elements
      case 'text':
      case 'select-one':
      case 'hidden':
      case 'password':
      case 'textarea':
        submitContent += formElem.name + '=' + escape(formElem.value) + '&'
        break;
        
     // Radio buttons
      case 'radio':
        if (formElem.checked) 
          submitContent += formElem.name + '=' + escape(formElem.value) + '&'

        break;
        
     // Checkboxes
      case 'checkbox':
        if (formElem.checked) 
        {
         // Continuing multiple, same-name checkboxes
          if (formElem.name == lastElemName) 
          {
           // Strip of end ampersand if there is one
            if (submitContent.lastIndexOf('&') == submitContent.length-1) 
              submitContent = submitContent.substr(0, submitContent.length - 1);
            
           // Append value as comma-delimited string
            submitContent += ',' + escape(formElem.value);
          }
          else 
            submitContent += formElem.name + '=' + escape(formElem.value);

          submitContent += '&';
          lastElemName = formElem.name;
        }
        break;
    }
  }
  
 // Remove trailing separator
  submitContent = submitContent.substr(0, submitContent.length - 1);
  return submitContent;
}


function ChangeColor(tableRow, highLight) 
{
  if (highLight) 
    tableRow.style.backgroundColor = '#DBDBDB';
  else 
    tableRow.style.backgroundColor = '';
}

function DoNav(theUrl)
{
  document.location.href = theUrl;
}

//oh, goody, browser detection
var HM_IE  = document.all ? true : false;              // ie
var HM_NS4 = document.layers ? true : false;
var HM_DOM = document.getElementById ? true : false;   // firefox and ie


// hide the date label for a particular selectable date box.
function DateLabelHide(labelName)
{
  var dateLabelSpan = document.getElementById(labelName);
  
  if (dateLabelSpan)
    dateLabelSpan.style.visibility = "hidden";
}


// check the date fields that have labels overlaying them.  If any of them are empty,
// then put the labels back in place.
function ToggleDateLabels(labelPrefix, idPostfix, resetArgs)
{
  var txtDay = document.getElementById(labelPrefix+"TxtDay"+idPostfix);
  var lblDay = document.getElementById(labelPrefix+"LblDay"+idPostfix);
  
  var txtYear = document.getElementById(labelPrefix+"TxtYear"+idPostfix);
  var lblYear = document.getElementById(labelPrefix+"LblYear"+idPostfix);

  if (txtDay)
  {    
    if (resetArgs)
    {
      ResetPullDown(document.getElementById(labelPrefix+"LstMonths"+idPostfix));
      txtDay.defaultValue = "";
      txtYear.defaultValue = "";
      
      lblDay.style.visibility = "visible";
      lblYear.style.visibility = "visible";
    }
    else
    {
      lblDay.style.visibility = (txtDay.value == "" ? "visible" : "hidden");
      lblYear.style.visibility = (txtYear.value == "" ? "visible" : "hidden");
    }

    return true;
  }
  else
    return false;
}


// reset a pulldown menu.  pulldown is a handle to a select input on the page.
function ResetPullDown(pulldown)
{
  var i;
  
  pulldown.options[0].defaultSelected = true;
  
  for (i=1;i<pulldown.length;i++){
    pulldown.options[i].defaultSelected = false;
  }
}

// trim off spaces from the front of a string.
function ltrim(string)
{
  var i = 0;
  while(string.charAt(i) == ' ') 
    i++;
    
  return string.substring(i, string.length);
}


// if a session times out while the user is doing Ajax-y stuff, dump the result
// to the browser's primary document (rather than in the div where it normally goes)
// This function also returns true if the session hasn't timed out yet, false otherwise
function TimeOutCheck(result)
{
  if (result.indexOf("<html>") < 5 && result.indexOf("<html>") >= 0)  // it's usually in position 2
  {
    document.body.innerHTML = result;     // this is a dirty hack, but serves the purpose.
    return false;
  }
  else
    return true;
}

// center the display, stretch out the repeating background
function resize(size)
{
  var winH;
 
 // how big is the window? 
  if (parseInt(navigator.appVersion)>3) 
  {
    if (navigator.appName=="Netscape") 
      winH = window.innerHeight;
   
    if (navigator.appName.indexOf("Microsoft")!=-1) 
      winH = document.body.offsetHeight;
  }

  var wrap = document.getElementById('wrap');
  
  if (size == undefined)
  {
    var content = document.getElementById('main_content');
    wrap.style.width = content.offsetWidth + 185;   // 170 is the width of the menu
  }
  else
    wrap.style.width = size + 185;
  
 // for short pages, make sure the menu's white underlay is as long as the page.
  if (wrap.offsetHeight < winH)
    wrap.style.height = winH;
}

function popitup(url) {
	newwindow=window.open(url,'name','location=0,status=0,scrollbars=1,height=540px,width=640px');
	if (window.focus) {newwindow.focus()}
	return false;
}
	x = 600;
	y = 20;
	once = 0;
function timeMsg()
	{
	var t=setTimeout("setVisible('layer1')",0);
	}
function setVisible(obj)
	{
	//	if(once == 0)
		{
			obj = document.getElementById(obj);
			obj.style.visibility = 'visible';//(obj.style.visibility == 'visible') ? 'hidden' : 'visible';
		}
		//obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';
	//	once = 1;
	}
function close(obj)
	{
		obj = document.getElementById(obj);
		obj.style.visibility = 'hidden';//(obj.style.visibility == 'visible') ? 'hidden' : 'visible';
		//obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';
	}
function placeIt(obj)
	{
		obj = document.getElementById(obj);
		if (document.documentElement)
		 {
			 theLeft = document.documentElement.scrollLeft;
			 theTop = document.documentElement.scrollTop;
		 }
		 else if (document.body)
		 {
			 theLeft = document.body.scrollLeft;
			 theTop = document.body.scrollTop;
		 }
		 theLeft += x;
		 theTop += y;
		 obj.style.left = theLeft + 'px' ;
		 obj.style.top = theTop + 'px' ;
		// setTimeout("placeIt('layer1')",500);
	}
