// ///////////////////////////////////////////////////////////////////////////////////////////
//      SCRIPT NAME:	xmlHttpRequest.js
//    SCRIPT AUTHOR:	Mark Vega
//      SCRIPT DATE:	March 2007
//  SCRIPT LOCATION:	pitcairn://target1s3/Web2/htdocs/www/libraries/grunigen/scripts/
//  SCRIPT FUNCTION:	Object for sending JavaScript xmlhttprequests.	
//     SCRIPT NOTES:	Creates new xmlhttprequest object when passed the following 
//                  	parameters:
//                  	rqstmethod		(GET or POST)
//                  	rqsturl			(url to retrieve information from)
//                  	rqsthandler		(function to handle http response)
//                  	rqstasynch		(TRUE for asynchronous request)
//                  	rqstsendxml		(TRUE if data should be sent as XML)
//                  	rqstinterval	(optional repeat interval in milliseconds,)
//                  	rqstdata		(uri-encoded query string, required for POST request)
// OTHER FILES USED:
//    LAST MODIFIED:	Changed all paths for new home on pitcairn.  06/13/2007 MFV
//
// ///////////////////////////////////////////////////////////////////////////////////////////
//
// DEBUG WRITE STATEMENT
// alert("loading xmlHttpRequest.js");
//
// initialize global variables
var _rqstasynch = '';
var _rqstdata = '';
var _rqsthandler = '';
var _rqstinterval = '';
var _rqstmethod = '';
var _rqstsendxml = '';
var _rqsturl = '';
var rqstasynch = '';
var rqstdata = '';
var rqsthandler = '';
var rqstinterval = '';
var rqstmethod = '';
var rqstsendxml = '';
var rqsturl = '';
var hreflength = '';
var httpRequest = null;
var requestErrFlag = false;
var requestTimeout;

//
// FUNCTION TO SEND HTTP REQUEST
//
// requires parameters rqstmethod (GET or POST), rqsturl (page or script to request),rqsthandler (function to handle the http response), 
// rqstsendxml (true or false for send request as XML), rqstasynch (true or false for asynchronous request 
// without reloading page). rqstinterval (repeat frequency in milliseconds) is always optional parameter 
// and rqstdata is optional for GET, required for POST.
//
function sendRequest(rqstmethod,rqsturl,rqsthandler,rqstasynch,rqstsendxml,rqstinterval,rqstdata) {
// initialize local variables
var textDoc = '';
var xmlDoc = '';

// save request parameters to global variables
_rqstmethod = rqstmethod;
_rqsturl = rqsturl;
_rqsthandler = rqsthandler;
_rqstasynch = rqstasynch;
_rqstsendxml = rqstsendxml;
_rqstinterval = rqstinterval;
_rqstdata = rqstdata;

// DEBUG ALERT STATEMENT
// alert("starting send request");
// alert(referer);

// if netscape, mozilla, firefox, safari
if (window.XMLHttpRequest) {
// try creating request object using netscape method
try {
httpRequest = new XMLHttpRequest();
}
// if request object not created
catch(e) {
// set error flag
requestErrFlag = true;
}
// if internet explorer
} else if (window.ActiveXObject) {
// try creating request object using new microsoft method
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
// if request object not created
catch(e) {
// try creating request object using old microsoft method
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
// if request object not created
catch(e) {
// set error flag
requestErrFlag = true;
}
}
// if all methods fail
} else {
// set error flag
requestErrFlag = true;
}
// check error flag
if (!requestErrFlag) {
// assign request values
httpRequest.onreadystatechange = rqsthandler; 
httpRequest.open(rqstmethod,rqsturl,rqstasynch); 
// if method is get
if ((rqstmethod == 'GET') || (rqstmethod == 'get')) {
// send request
httpRequest.send(null);
// if method is post
} else if ((rqstmethod == 'POST') || (rqstmethod == 'post')){
// check value of rqstdata
if (!rqstdata) {
// if empty, build query string
rqstdata = buildPostData();
}
// if rqstdata is xml format
if (rqstsendxml == true) {
// set content-type header and send request
httpRequest.setRequestHeader('Content-Type','text/xml');
httpRequest.send(rqstdata);
// if rqstdata is plain text
} else {
// set content-type header and send request
httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
httpRequest.send(rqstdata);
}
// if neither
} else {
// call error handler function
rqstErrHandler();
}
//
if (rqstinterval) {
// set timeout
requestTimeout = setTimeout('sendRequest(_rqstmethod,_rqsturl,_rqsthandler,_rqstasynch,_rqstsendxml,_rqstinterval,_rqstdata)',rqstinterval);
}
} else {
// call error handler function
rqstErrHandler();
}
}
//
// FUNCTION TO HANDLE HTTP REQUEST ERRORS
//
function rqstErrHandler() {
var errordiv = '';
errordiv = document.getElementById('d_results');
errordiv.innerHTML = "something went terribly wrong!";
}
//
// TEXT RESPONSE HANDLER FUNCTION FOR DEBUGGING HTTP REQUEST
//
// called by including function name as _rqsthandler parameter in call to sendRequest function.
//  
function getTextResponse() {
var textDoc = '';
var textdiv = '';
// get div element
textdiv = document.getElementById('d_results');
// check value of readystate
if (httpRequest.readyState == 4) {
// check value of http status
if (httpRequest.status == 200) {
// set value of statusresponse
textDoc = httpRequest.responseText;
textdiv.innerHTML = textDoc;
} else {
textdiv.innerHTML = "response code was not 200";
}
}
//
if (window.requestTimeout) {
window.clearTimeout(requestTimeout);
}
}
//
// XML RESPONSE HANDLER FUNCTION FOR DEBUGGING HTTP REQUEST
//
// called by including function name as _rqsthandler parameter in call to sendRequest function.
// 
function getXMLResponse() {
//
alert("getting xml");
var xmlDoc = '';
var xmldiv = '';
// get div element
xmldiv = document.getElementById('d_results');
// check value of readystate
if (httpRequest.readyState == 4) {
// check value of http status
if (httpRequest.status == 200) {
//
alert("status is 200");
// set xmlDoc equal to response
xmlDoc = httpRequest.responseXML.documentElement;
//
alert(xmlDoc);
// build data arrays by xmldoc tag names
var testitem1 = xmlDoc.getElementsByTagName('testitem1');
var testitem2 = xmlDoc.getElementsByTagName('testitem2');
//
alert(testitem1);
alert(testitem2);
// loop through arrays and display each item
for (x = 0; x <testitem1.length; x++) {
xmldiv.innerHTML += testitem1[x].childNodes[0].nodeValue + " and " + testitem2[x].childNodes[0].nodeValue;
}
} else {
xmldiv.innerHTML += "response code was not 200";
}
}
//
if (window.requestTimeout) {
window.clearTimeout(requestTimeout);
}
}
//
//
//
function buildPostData() {
return;
}
//
// END