/***************
   "search"
  select Element Type-Ahead for IE/Windows by Danny Goodman (www.dannyg.com)
  A bonus recipe for readers of O'Reilly's
    "JavaScript & DHTML Cookbook"
  This recipe first published at O'Reilly Network (www.oreillynet.com)
  For full implementation notes, read the article.
****************/

// global storage object for type-ahead info, including reset() method
var typeAheadInfo = {last:0,
                     accumString:"",
                     delay:500,
                     timeout:null,
                     reset:function() {this.last=0; this.accumString=""}
                    };
// function invoked by select element's onkeydown event handler
function typeAhead() {
   // limit processing to IE event model supporter; don't trap Ctrl+keys
   if (window.event && !window.event.ctrlKey) {
      // timer for current event
      var now = new Date();
      // process for an empty accumString or an event within [delay] ms of last
      if (typeAheadInfo.accumString == "" || now - typeAheadInfo.last < typeAheadInfo.delay) {
         // make shortcut event object reference
         var evt = window.event;
         // get reference to the select element
         var selectElem = evt.srcElement;
         // get typed character ASCII value
         var charCode = evt.keyCode;
         // get the actual character, converted to uppercase
         var newChar =  String.fromCharCode(charCode).toUpperCase();
         // append new character to accumString storage
         typeAheadInfo.accumString += newChar;
         // grab all select element option objects as an array
         var selectOptions = selectElem.options;
         // prepare local variables for use inside loop
         var txt, nearest;
         // look through all options for a match starting with accumString
         for (var i = 0; i < selectOptions.length; i++) {
            // convert each item's text to uppercase to facilitate comparison
            // (use value property if you want match to be for hidden option value)
            txt = selectOptions[i].text.toUpperCase();
            // record nearest lowest index, if applicable
            nearest = (typeAheadInfo.accumString > txt.substr(0, typeAheadInfo.accumString.length)) ? i : nearest;
            // process if accumString is at start of option text
            if (txt.indexOf(typeAheadInfo.accumString) == 0) {
               // stop any previous timeout timer
               clearTimeout(typeAheadInfo.timeout);
               // store current event's time in object
               typeAheadInfo.last = now;
               // reset typeAhead properties in [delay] ms unless cleared beforehand
               typeAheadInfo.timeout = setTimeout("typeAheadInfo.reset()", typeAheadInfo.delay);
               // visibly select the matching item
               selectElem.selectedIndex = i;
               // prevent default event actions and propagation
               evt.cancelBubble = true;
               evt.returnValue = false;
               // exit function
               return false;
            }
         }
         // if a next lowest match exists, select it
         if (nearest != null) {
            selectElem.selectedIndex = nearest;
         }
      } else {
         // not a desired event, so clear timeout
         clearTimeout(typeAheadInfo.timeout);
      }
      // reset global object
      typeAheadInfo.reset();
   }
   return true;
}

function toggleVis(id) {
    if( document.getElementById(id).style.display=='none' ){
        document.getElementById(id).style.display = '';
    }else{
        document.getElementById(id).style.display = 'none';
    }
}

function toggleLabel(layerid,labelid,label1,label2) {
    if ( document.getElementById(layerid).style.display=='none' ){
        document.getElementById(labelid).innerHTML=label1;
    }else{
        document.getElementById(labelid).innerHTML=label2;
    }
}

function collapseVis(id) {
    document.getElementById(id).style.display = 'none';
}

function setLabel(labelid,labeltext) {
    document.getElementById(labelid).innerHTML=labeltext;
}

function expandVis(id) {
    document.getElementById(id).style.display = '';
}


function AjaxObject()
{
this.xmlHttp = null;

	this.GetXmlHttpObject = function GetXmlHttpObject()
	{
		var xmlHttp=null;
		try
		{
			xmlHttp=new XMLHttpRequest();
		}
		catch (e)
		  {
		  // Internet Explorer
		  try
			{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
		  catch (e)
			{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		  }
		return xmlHttp;
	}
	
	this.ViewResult = function ViewResult(url,res)
	{
		var xmlHttp = this.xmlHttp;
		if(url.indexOf("?") != -1)
		{
			url = url + "&sid="+Math.random();
		}
		else
		{
			url = url + "?sid="+Math.random();
		}
		xmlHttp = this.GetXmlHttpObject();

		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState==4)
			{
				document.getElementById(res).innerHTML=xmlHttp.responseText;
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	
	this.Execute = function Execute(url)
	{
		var xmlHttp = this.xmlHttp;
		if(url.indexOf("?") != -1)
		{
			url = url + "&sid="+Math.random();
		}
		else
		{
			url = url + "?sid="+Math.random();
		}
		xmlHttp = this.GetXmlHttpObject();

		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState==4)
			{
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}