//This set of scripts allows passive adding of onClicks, etc, to a page.  Cool.

//addLoadListener(initGetProgramBlock); //would be neat if initGetProgramBlock worked!

//addLoadListener(initLimit); //load everything at startup, not here though because done in main page

function initLimit()
{
	ajaxLoadPrograms("");
}

function initGetProgramBlock()
{
	var inputNodes=document.getElementsByTagName('select'); //gets all Selects on the page
	for (var i=0; i < inputNodes.length; i++)
	{
		//alert ("position 1");
		//var pattern = new RegExp("(^| )limit( |$)"); //gets any with regex name of "limit"
		var pattern="selectLimit"; //override the regex
		//if (pattern.text(inputNodes[i].className))
		//alert (inputNodes[i].className);
		if (pattern == inputNodes[i].className)
		{
			//alert ("Here I am!");
			inputNodes[i].onchange = "ajaxLoadPrograms(this.value)"; //does this need quotes?
		}
	}
}


function ajaxLoadPrograms()
{
	xmlHttp=GetXmlHttpObject() //needs that other function
	if (xmlHttp==null)
		{
	 //alert ("Browser does not support HTTP Request")
	 return
		}
	var url="online_programs_ajax_get_GOVT.php"
	url=url+"?limit="+document.mediaselect.limit.value;
	url=url+"&limitTime="+document.mediaselect.limitTime.value;
	//WARNING!!!  This still doesn't deal with ?program=BOS_2008_04_01 etc!!!!!!!!!!!!!!!!!!!
	//url=url+"&sid="+Math.random() //random is to prevent cache loading, not needed here
	//alert(url);
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true) //GET or POST, true means asynchronous request
	xmlHttp.send(null)
}


function stateChanged() 
{ 
	if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading")
	{ 
		document.getElementById("programsBlock").innerHTML="Loading..." 
	} 
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("programsBlock").innerHTML=xmlHttp.responseText 
	} 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

//function below from sitepoint.com
function addLoadListener(fn) 
{ 
 if (typeof window.addEventListener != 'undefined') 
 { 
   window.addEventListener('load', fn, false); 
 } 
 else if (typeof document.addEventListener != 'undefined') 
 { 
   document.addEventListener('load', fn, false);
 }
 else if (typeof window.attachEvent != 'undefined') 
 { 
   window.attachEvent('onload', fn); 
 } 
 else 
 { 
   var oldfn = window.onload; 
   if (typeof window.onload != 'function') 
   { 
     window.onload = fn; 
   } 
   else 
   { 
     window.onload = function() 
     { 
       oldfn(); 
       fn(); 
     }; 
   } 
 } 
}