// LarcLib.js - these are commonly used javascript routines

// These routines should be included with the html:
//
//     <script type='text/javascript' src='/site/javascript/LarcLib.js'></script>
//
// This is for the google maps call:
//	<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA" type="text/javascript"></script>
//
//##############################################################################################################
// Fade controls are below
//##############################################################################################################
var _fade_fade_state;
var _fade_new_body;
var _fade_direction;
var _fade_opacity;
var _fade_control;
var _fade_time = 15;
var _fade_div_id;

// allow the default time to be overridden
function fade_set_duration (duration_in_milliseconds) {
	return (_fade_time = duration_in_milliseconds);
}

function start_fade (div_id, body) {
	_fade_fade_state = 0;
	_fade_opacity = 100;
	_fade_new_body = body;
	_fade_direction = 0;
//	_fade_time = 15;		// 15 ms between iterations
	_fade_div_id = div_id
	setTimeout (fade_control, _fade_time);
}

function set_opacity (obj_name, amt) {
	var object = document.getElementById (obj_name).style;
	object.opacity = amt;
	object.MozOpacity = amt;
	object.KhtmlOpatcity = amt;
	object.filter = "alpha(opacity=" + amt + ")";
	return (0);
}

function fade_control () {
	if (_fade_direction == 0) {	// fade out
		_fade_opacity = _fade_opacity - 20;
		if (_fade_opacity >= 0) {
			var amt = _fade_opacity / 100;
			set_opacity (_fade_div_id, amt);
		} else {	// we've faded out, set a zero opacity and the body - then count up
			set_opacity (_fade_div_id, 0);
			var obj = document.getElementById (_fade_div_id);
			obj.innerHTML = _fade_new_body;
			_fade_direction = 1;
		}
		setTimeout (fade_control, _fade_time);
	} else {	// fade in
		_fade_opacity = _fade_opacity + 20;
		if (_fade_opacity <= 100) {	// we are done
			var amt = _fade_opacity / 100;
			set_opacity (_fade_div_id, amt);
			setTimeout (fade_control, _fade_time);
		} else {
			//alert ("opacity = " + _fade_opacity);	// we are done!
		}
	}
}
// ##############################################################################################################

function set_image (image_id, new_source) {
	var imgobj = document.getElementById (image_id);
	imgobj.setAttribute ("src", new_source);
}

// ##############################################################################################################
// drop_all_children (node-id) - deletes all the children of the node whose ID is passed in
// ##############################################################################################################
function drop_all_children (nodename) {
	var dropnode = document.getElementById (nodename);
	if (dropnode.childNodes.length > 0) {			// there is only ever going to be one child
		var nodecount = dropnode.childNodes.length
		for (j = 0; j < nodecount; j++) {
			dropnode.removeChild (dropnode.childNodes[0]);
		}
	}
	return (nodename);
}

var Debug_Ajax = 0;

// ##############################################################################################################
// alertContents is called when the AJAX request is completed.  It calls a user supplied function named
//  processXML (xml) passing the server response as an XML object.  The return value is TESTED (!) for errors.
// errors are signaled by a tag named "<error msg='errormessage' />"
// ##############################################################################################################
function alertGetContents () {
	switch (http_request.readyState) {
		case 1:		// initialization
			// setup for making the user wait a long time...
			document.body.style.cursor = "wait";
			break;
		case 2:		// fall through
		case 3:		// continue to make user wait....
			break;
		case 4:		// completion
			document.body.style.cursor = "default";
			if (http_request.status == 200) {
				if (Debug_Ajax == 1) {
					var myText = http_request.responseText;
					alert ("Got " + myText);
				}
				var xml = http_request.responseXML;
				var errors = xml.getElementsByTagName ("error");
				if (errors.length > 0) {
					alert ("Errors have occured.\n\n" + xml.getElementsByTagName("error").item(0).getAttribute("msg") );
				} else {
					processXML (xml);
				}
			} else {
				alert ("There was a problem with the request.\nThe status returned was " + http_request.status);
			}
			break;
	}
}

// ##############################################################################################################
// makeRequest (url, parameters) - this instantiates the http object and sends the request.
// it also sets up the intra-transaction processing with calls to "alertContents()" defined above.
// ##############################################################################################################
function performGetRequest(url, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = alertGetContents;
	http_request.open('GET', url + parameters, true);
	http_request.send(null);
	return(true);
}
