/* 
	file:			servercom.js
	description:	This is the Javascript file which works with the ServerCom set of 
					functions which uses an iframe/ilayer for server side
					communication without a browser page refresh.
	source:			http://pengoworks.com/index.cfm?action=articles:gatewayApi
*/

// debug variable -- set to true to catch errors like not being able to find objects 
var ServerComDebug = true;

// This is the function that does all the work of updating the iframe/ilayer. The
// inFrameID parameter is a string that is the ID of the iframe/ilayer to update.
// The inURL parameter is the URL to set the iframe/ilayer to. The inArray parameter
// is an even numbered array of keys and values that are appended to form a querystring
// for the inURL parameter.
function ServerComSetFrame(inFrameID, inURL, inMessageFunc, inErrorFunc, inArray)
{
	// the URL of the script on the server to run
	var sURL = inURL;
	// set initial state of url delimiter
	var sQSDelim = "?";

	// if there are querystring parameters to append...
	if (inArray.length > 0) {
		// loop through array...
		for (var i = 0; i < inArray.length; i = i + 2) {
			// append key
			sURL = sURL + sQSDelim + inArray[i];
			// if there is a value...
			if (inArray.length >= i+2)
				// append a value
				sURL = sURL + "=" + inArray[i+1];
			else
				if (ServerComDebug) alert("ServerComSetFrame() - odd number of values in inArray parameter.");
			// change url delimiter
			sQSDelim = "&";
		}
	}
	// this will append a random number to the URL string that will ensure the document isn't cached
	sURL = sURL + sQSDelim + Math.random();
	// add function names
	sURL = sURL + sQSDelim + "MessageFunc=" + inMessageFunc;
	sURL = sURL + sQSDelim + "ErrorFunc=" + inErrorFunc;
	//alert(sURL);

	// if IE then change the location of the IFRAME
	if (document.all) {
		// this loads the URL stored in the sURL variable into the hidden frame
		if (document[inFrameID])
			document[inFrameID].location = sURL;
		else
			if (ServerComDebug) alert("ServerComSetFrame() - can't find frame '" + inFrameID + "'.");

	// otherwise, change Netscape v6's IFRAME source file
	} else if (document.getElementById) {
		// this loads the URL stored in the sURL variable into the hidden frame
		if (document.getElementById(inFrameID))
			document.getElementById(inFrameID).contentDocument.location = sURL;
		else
			if (ServerComDebug) alert("ServerComSetFrame() - can't find frame '" + inFrameID + "'.");

	// otherwise, change Netscape v4's ILAYER source file
	} else if (document.layers) {
		// this loads the URL stored in the sURL variable into the hidden frame
		if (document[inFrameID])
			document[inFrameID].src = sURL;
		else
			if (ServerComDebug) alert("ServerComSetFrame() - can't find frame '" + inFrameID + "'.");
	}

	return true;
}

// This function can be called by the loading page in the iframe/ilayer to
// set a message on the page. The function takes the name of the DIV (inMessageDiv)
// and the message (inMessage) to set. The function will first delete all the 
// children of the DIV since older messages may be present. Optionally input
// the name of a CSS class (inClassName) so that the class of the DIV can be
// set to change the style of the DIV depending on the message.
/*
function ServerComSetMessage(inMessageID, inMessage, inMessageClass, inImageID, inImageSrc, inDivID, inDivClass)
{
	if (document.getElementById(inMessageID))
	{
		// remove existing elements
		ServerComRemoveAllChildNodes(document.getElementById(inMessageID));
		
		// create text node
		document.getElementById(inMessageID).appendChild(document.createTextNode(inMessage));
		
		// if class is inputted set class
		if ((inMessageClass != null) && (inMessageClass != ""))
			document.getElementById(inMessageID).className = inMessageClass;
		
		// if image id is inputted set image source
		if ((inImageID != null) && (inImageID != ""))
			document.getElementById(inImageID).src = inImageSrc;
		
		// if div id is inputted set to class
		if ((inDivID != null) && (inDivID != ""))
			document.getElementById(inDivID).className = inDivClass;
	}
	else
		if (ServerComDebug) alert("ServerComSetMessage() - can't find element '" + inMessageID + "'.");
}
*/
		
// This function removes all child nodes from a node.
function ServerComRemoveAllChildNodes(inObject) {
	if (inObject.childNodes) {
		for (var i = 0; i < inObject.childNodes.length; i++) {
			inObject.removeChild(inObject.childNodes[i]);
		}
	}
	else
		if (ServerComDebug) alert("ServerComRemoveAllChildNodes(inObject) - parameter inObject has no childNodes member.");
}