function getFacilities(currElem, elemName)
{
	var elem = document.forms[0].elements[elemName];
	var state = currElem.options[currElem.selectedIndex].value;
	
	if (state == "") {
		return false;
		
	}
	
	elem.options.length = 0;
	
	for (var i = 0; i<districts.length; i++) {
		if (districts[i]['state'] == state) {
			var newOpt = new Option(districts[i]['name'], districts[i]['id']);
			elem.options[elem.options.length] = newOpt;
		}
	}//for
	
	
	
}

if ( document.captureEvents ) {
	document.captureEvents(Event.MOUSEMOVE);
		
}

/**
 * Launches a new window
 *
 * Usage: <a href="http://www.yahoo.com" onclick="NewWindow(this.href, 'somename', 300, 300, 'yes');return false;">Visit Yahoo</a>
 *
 * @param string url The url to open
 * @param string winname The name to name the window
 * @param int width 
 * @param int height
 * @param string scroll Valid values are "yes" and "no"
 * @return void
 */
function NewWindow(url, winname, width, height, scroll, toolbar, menubar, location) {
	var left = (screen.width  - width)  / 2 ;
	var top  = (screen.height - height) / 2;
	if ( typeof toolbar == 'undefined' ) {
		toolbar = 'no';
		
	}
	
	if ( typeof menubar == 'undefined' ) {
		menubar = 'no';
		
	}
	
	if ( typeof location == 'undefined' ) {
		location = 'no';
		
	}
	winprops = 'height='+height+',width='+width+',top='+top+',left='+left+',scrollbars='+scroll+',resizable=yes,toolbar='+toolbar+',menubar='+menubar+'location=' +location;
	win = window.open(url, winname, winprops);
	
}//end NewWindow

/**
* @param HTMLElement element - the element to modify
* @param string attribute_name - the name of the attribute to modify
* @param string additional_value - the new value to be placed into the attribute
* @param string position - (optional) before or after the current values in the attribute
*/
function modify_attribute(element, attribute_name, additional_value, position)
{	
	if ( element[attribute_name] ) {
		//This should mean that it's not blank
		var str_orig = String(element[attribute_name]);
		
	} else {
		//this should mean that it's blank
		var str_orig = "";
		
	}
	
	if ( str_orig.indexOf("function") != -1 ) {
		var open_curly_pos = str_orig.indexOf('{');
		var end_curly_pos = str_orig.lastIndexOf('}');
		var str_old_stuff = str_orig.slice(open_curly_pos+1, end_curly_pos);
		
		if ( position == "after" ) {
			element[attribute_name] = new Function(str_old_stuff + additional_value);	
			
		} else {
			element[attribute_name] = new Function(additional_value + str_old_stuff);		
			
		}
		
	} else {
		element[attribute_name] = new Function(additional_value);
		
	} 
	
}//end modify_attribute

/**
 * 
 */
function insertAfter(parent, node, referenceNode) {
	if ( referenceNode.nextSibling ) {
		parent.insertBefore(node, referenceNode.nextSibling);	
		
	} else {
		parent.appendChild(node);
		
	}	
	
}//end insertAfter

function findNextSiblingByTagName(elem, tagname)
{
	if ( elem.nextSibling == null ) {
		return null;
		
	}
	
	var elem_sibling = elem;
	var arr_return = new Array();
	while ( (elem_sibling = elem_sibling.nextSibling) != null ) {
		if ( elem_sibling.nodeType == 1 && elem_sibling.tagName == tagname ) {
			arr_return.push(elem_sibling);
			
		}
		
	}//while
	
	if ( arr_return.length == 1 ) {
		return arr_return[0];
		
	} else if ( arr_return.length == 0 ) {
		return null;
		
	} else {
		return arr_return;
		
	}
	
}//end findNextSiblingByTagName

function findPreviousSiblingByTagName(elem, tagname)
{
	if ( elem.previousSibling == null ) {
		return null;
		
	}
	
	var elem_sibling = elem;
	var arr_return = new Array();
	while ( (elem_sibling = elem_sibling.previousSibling) != null ) {
		if ( elem_sibling.nodeType == 1 && elem_sibling.tagName == tagname ) {
			arr_return.push(elem_sibling);
			
		}
		
	}//while
	
	if ( arr_return.length == 1 ) {
		return arr_return[0];
		
	} else if ( arr_return.length == 0 ) {
		return null;
		
	} else {
		return arr_return;
		
	}
	
}//end findNextSiblingByTagName

function findChildByTagName(elem, tagname)
{
	try {
		if ( elem.childNodes.length == 0 ) {
			return null;
		}
	} catch (e) {
		return null;
		
	}
	
	var arr_return = new Array();
	for ( var i=0; i<elem.childNodes.length; i++ ) {
		if ( elem.childNodes[i].nodeType == 1 && elem.childNodes[i].tagName == tagname ) {
			arr_return.push(elem.childNodes[i]);
			
		}
		
	}//for
	
	if ( arr_return.length == 1 ) {
		return arr_return[0];
		
	} else if ( arr_return.length == 0 ) {
		return null;
		
	} else {
		return arr_return;
		
	}
	
}//end findChildByTagName

var EventRegister = { 
	registered_events: [],	
	add: function(elem, event_name, func, capture)
	{					
		if ( elem.addEventListener ) {
			//Standards
			elem.addEventListener(event_name, func, capture);
			
		} else if ( elem.attachEvent ) {
			//IE
			if ( !elem.attachEvent('on'+event_name, func) ) {
				return false;	
			}
			
		} else {
			return false; 
				
		}	
		
		return true;
		
	}//end add
	
};

function IntervalExecuter(callback, interval_sec) 
{
	this.callback  = callback;
	this.interval  = interval_sec * 1000;
	this.running   = false;
	this.cancel_id = null;
	
	this.start = function()
	{
		this.cancel_id = setInterval(this.callback, this.interval);
		this.count = 0;
		this.running = true;
	}
	
	this.stop = function()
	{
		clearInterval(this.cancel_id);
		this.running = false;
		
	}
	
	this.isRunning = function()
	{
		return this.running;
	}
	
}//end IntervalExecuter

/**
 * Create a new select element and populate it with obj_items and 
 * remove any of the items in arr_items_ignore
 *
 * @param obj_items - The object to loop over to create options
 * @param arr_items_ignore - Optional - Numerically indexed array of values to remove from the options
 * return Select
 */
function createSelect(obj_items, arr_items_ignore)
{
	//Create the element
	var elem_select = document.createElement('select');
	
	//Add the options to the element
	object_to_options(obj_items, elem_select)
	
	//Remove the items to ignore, if any
	if ( arr_items_ignore != null ) {
		removeOptions(elem_select, arr_items_ignore);
		
	}
	
	return elem_select;	
		
}//end createSelect

/**
 * Remove options from an elem_select by an array of option vlaues
 *
 * @param elem_select - The select to remove the options from
 * @param arr_option_values - A numerical array of values to be removed
 */
function removeOptions(elem_select, arr_option_values, skip_selected)
{
	//Setup the call back
	var callback = function (opt) { return opt.value; };
	
	//Default value for skip_selected if not provided
	if ( skip_selected == null ) {
		skip_selected = false;
		
	}
	
	//Remove any of the options that are in arr_items_ignore
	for ( var i=0; i<arr_option_values.length; i++ ) {
		index = array_search(arr_option_values[i], elem_select.options, callback);
		if ( index != -1 ) {
			if ( elem_select.selectedIndex == index && skip_selected == true ) {
				continue;
				
			}
			
			elem_select.remove(index);
			
		}
		
	}//for	
	
}//end removeOptions

/**
 * Search haystack for needle.  If found, return the index, if not 
 * found return -1
 *
 * @param needle - Value to search for
 * @param haystack - Array to search through
 * @param callback - function to call on each value
 * @return int
 */
function array_search(needle, haystack)
{
	if ( arguments.length > 2 ) {
		var callback = arguments[2];
		
	} else {
		var callback = function(value) { return value; };
		
	}
	
	for ( var i=0; i<haystack.length; i++ ) {
		if ( callback(haystack[i]) == needle ) {
			return i;
			
		}
		
	}//for
	
	return -1;
	
}//array_search

/**
 * 
 *
 * @param obj - Array or object to iterate over
 * @param elem_select - The select element to add the options to
 * @param default_selected - Optional, the value to select by default
 * @param first_option - Optional, An Option object to place in the first position
 * @return null
 */
function object_to_options(obj, elem_select, default_selected, first_option)
{
	elem_select.options.length = 0;
	if ( first_option != null ) {
		try {
			elem_select.add(first_option);
			
		} catch (e) {
			elem_select.add(first_option, null);
			
		}
		
	}
	
	for ( a in obj ) {
		var tmp_obj = new Option(obj[a].toString(), a);
		try {
			elem_select.add(tmp_obj);
			
		} catch (e) {
			elem_select.add(tmp_obj, null);
			
		}			
		
	}//for
	
	if ( default_selected != null ) {
		elem_select.value = default_selected;
		
	} else {
		elem_select.selectedIndex = 0;
		
	}
	
}//end object_to_options
