//****************************************************
//
//                         Global vars
//
//****************************************************

asResultDiv = "main";         // Default div to display asProcess results

//
//	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);
	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 handleASProcess()
{
	if(xmlHttp.readyState == 4)
	{
		var x = document.getElementById(asResultDiv);
		x.innerHTML = xmlHttp.responseText;
   }
	else
		show_working("Fetching page...", asResultDiv);
}

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);
   return x.options[x.selectedIndex].value;
}

//
// Get the text of the selected option in a select group
//
function getSelectText(id)
{
   var x = document.getElementById(id);
   return x.options[x.selectedIndex].text;
}

//
// 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;
}

//
// 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;
}