// Author: Yaxing Wei

var myPopup = '';
function openPopup(url, width, height)
{
	myPopup = window.open(url, '', 'width=' + width + ',height=' + height + ',scrollbars=yes');
	if(!myPopup.opener)
	myPopup.opener = self;
}

function goto(url)
{
	window.location = url;
}

function go_back() {
	history.back();
}

get_element = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };
	
function createXMLRequestObject() {
	var xmlhttp;
//	alert("In createXMLRequestObject()");
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) 
	{
		try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	}
		catch(e) 
		{
			try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
			catch(f) { xmlhttp = null;}
		}
	}
		
	return xmlhttp;
}


// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

/*!
	\brief Expands/collapses an element
	\param id - id of the element to expand/collapse (<XXX id="[id here]" ...>...</XXX>)
	\param retractor (optional) - reference to an html element that is triggering the toggle event - when passed, its inner html is changed to reflect the current expanded/retracted status
	\param contracted (optional) - text that retractor.innerHTML should show when controlled object is in its contracted state
	\param expanded (optional) - text that retractor.innerHTML should show when controlled object is in its expanded state
	\returns nothing
*/
function toggle_div(id, retractor, contracted, expanded)
{
	var element = get_element(id);
	var status=null;
	
	if(document.getElementById)
	{
		element.style.display = (element.style.display == 'none') ? 'block' : 'none';
		status = element.style.display;
	}
	else if(document.layers)
	{
		element.display = (element.display == 'none') ? 'block' : 'none';
		status = element.display;
	}
	/*else
	{
		eval("document.all." + id + ".style.display") = (eval("document.all." + id + ".style.display")=='none') ? 'block' : 'none';
		status = eval("document.all." + id + ".style.display");
	}*/
	
	if(arguments.length > 1)
		retractor.innerHTML = (status == "block") ? expanded : contracted;
}



var mark_bg = "#EEEEFF";		// The color to mark cells with
var default_bg = "#FFFFFF";		// The color to un-highlight cells with*/
var last_marked = null;		// Reference to an HTML element, the last element that was marked

var highlight_bg = "#70AAD8";	// The color to highlight cells with
var mark_bg = "#BDCADA";		// The color to mark cells with
var default_bg = "#FFFFFF";		// The color to un-highlight cells with

/*!
	\brief Highlights an HTML element by changing its background color (HTMLElement.style.backgroundColor
	\param element - id of the element to highlight
	\returns nothing
	
	This is usually called as part of a mouseover event
*/
function Highlight(element_id)
{
	var element = get_element(element_id);
	
	if(element.marked == null || element.marked == false)
	{
		element.style.backgroundColor = highlight_bg;
	}
}

/*!
	\brief Un-highlights an HTML element by changing its background color (HTMLElement.style.backgroundColor
	\param element - id of the element to un-highlight
	\returns nothing
	
	This is usually called on a mouseout event
*/
function Restore(element_id)
{
	var element = get_element(element_id);

	if(element.marked == null || element.marked == false)
	{
		element.style.backgroundColor = default_bg;
	}
}



	function make_blank(elem)
	{
		var cv = get_element(elem).value;
		if((cv == "Northernmost") || (cv == "Southernmost") 
			|| (cv == "Easternmost") || (cv == "Westernmost")
			|| (cv == "Begin Date") || (cv == "End Date"))
			get_element(elem).value ="";
	}
	
	
		function xmlHttpGet(url) {
		var xmlHttpReq = false;
		var self = this;
		
		try
		{
			//// Initialize XMLHttpRequest object
			self.xmlHttpReq = createXMLRequestObject();
	//		alert("In xmlHttpGet(), xmlHttpReq=" + self.xmlHttpReq);
			if(self.xmlHttpReq == null)
			{
				alert("In xmlHttpGet(), xmlHttpReq is null");
				get_element('comp_check').checked=false;	// Failed. Uncheck the box
				document.getElementById('inner_div').style.visibility='hidden';	// Failed. Hide the status bar
				return;
			}
				
			//// Prepare request
	//		alert("In xmlHttpGet(), url=" + url);
			self.xmlHttpReq.open("GET", url);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			self.xmlHttpReq.onreadystatechange = function() 
			{
	//			alert("In onreadystatechange()");
				if (self.xmlHttpReq.readyState == 4) 
				{
					if(self.xmlHttpReq.status == 200) {
						update_comp_list(self.xmlHttpReq.responseText);
					} else if(self.xmlHttpReq.status == 404) {
						alert("Status: 404 - Unable to locate target service.");
						get_element('comp_check').checked=false;	// Failed. Uncheck the box
						document.getElementById('inner_div').style.visibility='hidden';	// Failed. Hide the status bar
					} else if(self.xmlHttpReq.status == 500) {
						alert("Status: 500 - Internal server error.");
						get_element('comp_check').checked=false;	// Failed. Uncheck the box
						document.getElementById('inner_div').style.visibility='hidden';	// Failed. Hide the status bar
					} else {
						alert("Status: " + self.xmlHttpReq.status);
						get_element('comp_check').checked=false;	// Failed. Uncheck the box
						document.getElementById('inner_div').style.visibility='hidden';	// Failed. Hide the status bar
					}
				} 
			}
	
			//// Do request
			self.xmlHttpReq.send(null);
		}
		catch(e) { alert("e=" + e); }
		finally{}
	}