//****************************************************
//
//                         Global vars
//
//****************************************************
var xhr = new Array();        // Array of xmlHttpO objects

asResultDiv = "main";         // Default div to display asProcess results

//*******************************
// xmlHttp Object
//*******************************
function xmlHttpO(x, h, msg, id)
{
   this.xmlHttpObject = x;
   this.handler = h;
   this.msg = msg;
   this.resultDIV = id;

   this.showWorking = showWorking;
}

function showWorking()
{
   if(this.msg)
   {
      var x = document.getElementById(this.resultDIV);
      if(x) x.innerHTML = '<h1>' + this.msg + '</h1>';
   }
}

//
//	Ajax function for open async calls
//
function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null
		
	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		alert("This example doesn't work in Opera") 
		return 
	}

	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP"
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} 	
		catch(e)
		{ 
		 	alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
}

function show_working(message, id)
{
	var x = document.getElementById(id);
   if(x)
   	x.innerHTML = "<h1>"+message+"</h1>";
}

//
//	Generic asynchronous process call displays waiting message and then results in the main div
//
function asProcess(url, args, msg, func, id)
{
   asResultDiv = id;                             // Set the global result div
	var rand = parseInt(Math.random()*99999999);  // cache buster

	if(args)
		args = args + "&";
	args = args + "rand=" + rand;

	xmlHttp = GetXmlHttpObject(func);
   if(msg)
   	show_working(msg, id);
	xmlHttp.open("GET", url+"?"+args, true);
	xmlHttp.send(null);
}

function asProcessM(url, args, msg, func, id)
{
	var rand = parseInt(Math.random()*99999999);  // cache buster

	if(args)
		args = args + "&";
	args = args + "rand=" + rand;

   var i = xhr.length;
	var y = func.toString();
	var z = func.id;

	var x = new xmlHttpO(GetXmlHttpObject(func), func.name, msg, id);
   xhr[i] = x;
   xhr[i].showWorking();
	xhr[i].xmlHttpObject.open("GET", url+"?"+args, true);
	xhr[i].xmlHttpObject.send(null);
}

function handleASProcess()
{
	if(xmlHttp.readyState == 4)
	{
		var x = document.getElementById(asResultDiv);
      if(x)
   		x.innerHTML = xmlHttp.responseText;
   }
	else
		show_working("Fetching page...", asResultDiv);
}

function handleASProcessM()
{
   var i = getXhrIndex('handleASProcessM');

   if(i >= 0)
   {
      var x = document.getElementById(xhr[i].resultDIV);
      if(x)	x.innerHTML = xhr[i].xmlHttpObject.responseText;
      xhr.splice(i, 1);
   }
}
handleASProcessM.name = 'handleASProcessM';

function getXhrIndex(n)
{
   var l = xhr.length;

   for(var i = 0; i < l; i++)
   {
   	if((xhr[i].xmlHttpObject.readyState == 4) && (xhr[i].handler == n))
         return i;
   }

   return -1;
}


function showDD(id)
{
   var x = document.getElementById(id); 

   if(x)
   {
      if(x.style.display != "block")
      {
         // Clicked on dt has a dd that is not visible.  Make it visible
         x.style.display = "block";
      }
      else
      {
         // Clicked on dt has a dd that is already visible.  Hide it
         x.style.display = "none";
      }
   }
}

function getRadioVal(id)
{
   var val = '';
   var x = document.getElementsByName(id);

   // Cycle through the radio buttons to see which one is checked
   if(x[0])
   {
      for(var i=0; i<x.length; i++)
      {
         if(x[i].checked)
            val = x[i].value;
      }
   }
   else
   {
      if(x.checked)
         val = x.value;
   }

   return val;
}


//
// Convert an enter keypress into a button click
//
function crToButtClick(butt, e)
{
   var key;

   if(window.event)
      key = window.event.keyCode;      // ie
   else
      key = e.which;                   // firefox

   if(key == 13)
   {
      //Get the button the user wants to have clicked
      var btn = document.getElementById(butt);

      if(btn != null)
      {
         //If we find the button click it
         btn.click();
         event.keyCode = 0
      }
   }
}

//
// Get the value of the selected option in a select group
//
function getSelectValue(id)
{
   var x = document.getElementById(id);
   if(x)
      return x.options[x.selectedIndex].value;
   else
      return '';
}

//
// Get an array of selected indexes from a multi-select option list
//
function getSelectValues(id)
{
   var result = '';
   var x = document.getElementById(id);

   if(x)
   {
      var c = x.options.length;
      for(var i=0; i<c; i++)
      {
         if(x.options[i].selected)
            result += x.options[i].value + ',';
      }
   }

   return result.substr(0, result.length - 1);
}

//
// Get the text of the selected option in a select group
//
function getSelectText(id)
{
   var x = document.getElementById(id);
   if(x)
      return x.options[x.selectedIndex].text;
   else
      return '';
}

//
// Select the option in the select element that has the given value
//
function setSelectValue(id, val)
{
   var x = document.getElementById(id);
   if(x)
   {
      var o = x.options;
      if(o)
      {
         var l = o.length;

         for(var i=0; i<l; i++)
         {
            if(o[i].value == val)
            {
               x.selectedIndex = i;
               return i;
            }
         }
      }
   }

   return -1;
}

// Remove all options from a select element
function removeAllOptions(id)
{
   var x = document.getElementById(id);
   if(x)
   {
      var len = x.options.length;

      for(var i=0; i<len; i++)
         x.remove(0);
   }
}

//
// Select the option in the select element that has the given text
//
function setSelectText(id, val)
{
   var x = document.getElementById(id);
   var o = x.options;
   var l = o.length;

   for(var i=0; i<l; i++)
   {
      if(o[i].text == val)
      {
         x.selectedIndex = i;
         return i;
      }
   }

   return -1;
}

function addSize(id, val)
{
   var x = document.getElementById(id);

   if(x)
   {
      x.size += 5;
      return x.size;
   }
   else
      return 0;
}

//
// Check the characters of an input field to make sure they are legal
// Remove any illegal characters and replace with the new value
function validateInput(id, val, allowed)
{
   var new_val = '';
   var c = '';
   var l = val.length;
   
   // Step through each character of the input value
   for(var i = 0; i < l; i++)
   {
      c = val.charAt(i)+'';
      // Check to see if the character is in the allowed string
      if(allowed.indexOf(c.toLowerCase()) >= 0)
         new_val += c;     // Append char to the end of the new value if legal
   }

   if(val != new_val)
      alert("Illegal characters removed from input.");

   // Put the new value in the input field
   document.getElementById(id).value = new_val;

   return new_val;
}

function makePhoneNumber(p)
{
   var num = '';
   var l = p.length;
   var c = 0;

   for(var i=0; i<l; i++)
   {
      code = p.charCodeAt(i);
      if((code >= 48) && (code <= 57))
      {
         switch(c)
         {
            case 0 :
               num += '(';
               c++;
               break;
            case 4 :
               num += ') ';
               c += 2;
               break;
            case 9 :
               num += '-';
               c++;
               break;
         }
         num += p.charAt(i);
         c++;
      }
   }

   return num;
}