/**********************************************************************
 * 0: uninitialized - open has not been called yet                    *
 * 1: loading       - send has not been called yet                    *
 * 2: loaded        - send has been called, but the response is       *
 *                    not yet available                               *
 * 3: interactive   - The response is being downloaded, and the       *
 *                    responseText property holds partial data        *
 * 4: completed     - The resp has been loaded and the req is done    *
 *********************************************************************/


var httpRequest = false;
var defaultURL = "http://www.phototides.com/test/reflector.php?";
//var defaultURL = "http://localhost/LocationSelector/reflector.php?";
//var defaultURL = "http://127.0.0.1:8080/tidelinesonline/calendar/location?";

// Write the user selected region into the hidden, zencart "region" form input field
function writeRegion( myObj )
{
	var regionBox = document.getElementById('attrib-15-0'); // Get the ID of the region box that zencart creates
	regionBox.value=myObj.value;
}

// initial call that each select box object makes when changed...
function ajaxWidgetCall( myObj ) {

	var areaSelectBox 		= document.getElementById('areaSelect'); 
	var subareaSelectBox 	= document.getElementById('subareaSelect'); 
	var stationSelectBox 	= document.getElementById('stationSelect'); 


	if( myObj.name == areaSelectBox.name ) { 
		if( areaSelectBox.value == -1 ) {
			// clear all children boxes and return...
			clearSelect( subareaSelectBox );
			clearSelect( stationSelectBox );
			return;
		}
		makeRequest( defaultURL + "reqType=subarea&id=" + areaSelectBox.value, myObj );
	} else if( myObj.name == subareaSelectBox.name ) {
		if( subareaSelectBox.value == -1 ) {
			clearSelect( stationSelectBox );
			return false;
		}		
		makeRequest( defaultURL + "reqType=substation&id=" + subareaSelectBox.value, myObj );
	} 
}

function makeRequest( url, selectObj ) { 
	// Mozilla, Safari, etc...
	if( window.XMLHttpRequest ) { 
	/**
	   try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	   } catch (e) {
		alert("Permission UniversalBrowserRead denied.");
	   }
	   **/	
		httpRequest = new XMLHttpRequest();		
		if( httpRequest.overrideMimeType ) {
		httpRequest.overrideMimeType( 'text/xml' );
		}
	} else if (window.ActiveXObject) { // IE
	try {
		httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		   } catch (e) {}
		}
	}
		
	if (!httpRequest) {
		alert('Cannot create an XMLHTTP instance - Please use Internet Explorer Browswer');
		return false;
	}

	httpRequest.open('GET', url, true);	
	var serverResp = null;

	httpRequest.onreadystatechange = function() {
		if( httpRequest.readyState == 4 ) {
			if( httpRequest.status == 200 ) {
				var result = httpRequest.responseText;
//				alert( result );
				populateSelectBoxes( result, selectObj );
//				document.getElementById( 'myspan' ).innerHTML = result;				
			}
		}
	}	
	httpRequest.send(null);
}

function populateSelectBoxes( jsonTxtObj, selectObj ) {
	var areaSelectBox 		= document.getElementById('areaSelect'); 
	var subareaSelectBox 	= document.getElementById('subareaSelect'); 
	var stationSelectBox 	= document.getElementById('stationSelect'); 
	
	var oListObj = eval( "(" + jsonTxtObj + ")" );
	
	// area select box
	if( selectObj.name == areaSelectBox.name ) { 
		// clear the other selectc boxes below this one...
		clearSelect( subareaSelectBox );
		clearSelect( stationSelectBox );
								
		// Populate the substation/subarea select box...								
		for( var i = 0; i < oListObj.subareas.length; i++ ) {
			subareaSelectBox.options[i] = new Option( oListObj.subareas[i].name, oListObj.subareas[i].id );
			subareaSelectBox.disabled = false;
		}
	} else if( selectObj.name == subareaSelectBox.name ) { // subarea select
		// clear the station select box
		clearSelect( stationSelectBox );
		
		// populate the station select box
		for( var i = 0; i < oListObj.stations.length; i++ ) {
			stationSelectBox.options[i] = new Option( oListObj.stations[i].name, oListObj.stations[i].id );		
			stationSelectBox.disabled = false;
		}
		
	} 
}

function clearSelect( selectObj ) {
	while( selectObj.firstChild ) {
		selectObj.removeChild( selectObj.firstChild );
		selectObj.disabled = true;
	}
}

