//RPC COMMON FUNCTIONS

var IFrameObj; 	// our IFrame object
var btnGoID = '';	// id of div where go button is placed
var btnGoHTML = '';	// html to replace back to execute the call again

function rpc_Call(btnGo,command,urlParams,responseFunction) {
	//make the rpc call
	if (!document.createElement) {return true};
	var IFrameDoc;
	
	//build the url to submit
	var url = 'rpc.cfm?command=' + command + '&responsefunction=' + responseFunction;
	if (urlParams != '') url = url + '&' + decodeURIComponent(urlParams);

	if (!IFrameObj && document.createElement) {
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time 
		// rpc_Call() is called
		try {
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','RSIFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			IFrameObj = document.body.appendChild(tempIFrame);
			
			if (document.frames) {
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames['RSIFrame'];
			}
		} catch(exception) {
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			iframeHTML='<iframe id="RSIFrame" style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
			IFrameObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}
	
	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout('rpc_Call("'+url+'")',10);
		return false;
	}
	
	if (IFrameObj.contentDocument) {
		// For NS6
		IFrameDoc = IFrameObj.contentDocument; 
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) {
		// For IE5
		IFrameDoc = IFrameObj.document;
	} else {
		return true;
	}
	
	//Set cursor to hourglass
	document.body.style.cursor = "wait";
	if (btnGo != '') {
		btnGoID = btnGo;
		btnGoHTML = document.getElementById(btnGo).innerHTML;
		document.getElementById(btnGo).innerHTML = '<img src="images/icons/timer.gif" border="0">';
	}
		
	//submit the request
	IFrameDoc.location.replace(url);
	//alert(url);
	return false;
}

function rpc_Parse(text) {
	//parse the returned results
	if (window.DOMParser) {
		parser=new DOMParser();
	  	xmlDoc=parser.parseFromString(text,"text/xml");
	} else {
		// Internet Explorer
	 	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  	xmlDoc.async="false";
	  	xmlDoc.loadXML(text); 
	}
	return xmlDoc;
}

function rpc_Response(doc,bDisplayError,strResponseFunction) {
	//handle the response

	// get the results
	var textXML = doc.getElementById("rpcXML").innerHTML;
	var textStatus = doc.getElementById("rpcStatus").innerHTML;
	var textMessage = doc.getElementById("rpcMessage").innerHTML;

	if (textStatus == "0") {
		if (strResponseFunction != "") {
			//parse the xml returned result
			var xmlDoc = rpc_Parse(textXML);
			//call the function to run based on the result(s) returned
			if (typeof(window[strResponseFunction]) === "function") {
				window[strResponseFunction](xmlDoc);
			} else {
				if (bDisplayError) { 
					throw("Error.  Function " + strResponseFunction + " does not exist.");
				}
			}
		}
	} else {
		if (bDisplayError) { 
			alert(textMessage); 
		}
	} 
	//reset the status
	if (btnGoID != '') {
		document.getElementById(btnGoID).innerHTML = btnGoHTML;
		btnGoHTML = '';
		btnGoID = '';
	}
	document.body.style.cursor = "default";
}

function rpc_Case(nodeName) {
	//if IE, then node will be upper case otherwise will be lower case
	if (window.DOMParser) {
		return nodeName.toLowerCase();
	} else { 
		return nodeName.toUpperCase();
	}
}

function rpc_SingleResult(xmlDoc,nodeName) {
	//extract the single node result from the single record results xml
	var result = '';
	var nodeNameCase = rpc_Case(nodeName);
	if (xmlDoc.getElementsByTagName(nodeNameCase)[0].childNodes.length) {
		result = xmlDoc.getElementsByTagName(nodeNameCase)[0].childNodes[0].nodeValue;
	}
	return result;
}
