/* ******************************************************************* incAJAX.js Contains the functions for sending and receiving AJAX requests/responses -scott 6/1/06 ******************************************************************** */ // ------------------------------------------------------------------- // Global Request Object and Other Global Variables // ------------------------------------------------------------------- var gHTTP_Request; var gsResponseFunction; var gsErrorResponseFunction; var gbDebugAJAX = false; // ------------------------------------------------------------------- // gfunSendRequest(sURL, sMethod, sPostData, aryHeaderData) // Sends an AJAX Request to a URL (with optional posted data) // Parameters: // sURL: The URL to send the Request to // sMethod: "Get" or "Post" // sPostData: String of Name/Value pairs to send with the request // aryHeaderData: a 2 Deminsional Array of Header data to send with the request // sFunctionName: The function to be called with the response result // sent as a lone parameter // bDebug: Set to True to get Alerts for Testing and Debugging // sErrorFunctionName: The function to be called with any Error Response // sent as paramters: (status, responseText) // bIsAsynchronous: Set to false, then the function returns the AJAX Response // and the thread halts while waiting for a response. // ------------------------------------------------------------------- function gfunSendRequest(sURL, sMethod, sPostData, aryHeaderData, sFunctionName, bDebug, sErrorFunctionName, bIsAsynchronous) { if (sMethod == null) sMethod = "get"; if (bDebug == null) bDebug = false; if (bIsAsynchronous == null) bIsAsynchronous = true; var sTemp = ""; if (gHTTP_Request != null) { // A Request is already in progress if (aryHeaderData) { if (typeof aryHeaderData == "string") { sTemp = aryHeaderData; } else { // Change the Headers array to a string name value pairs so that it can be passed. for (var i = 0; i < aryHeaderData.length; i++) { sTemp = aryHeaderData[i][0] + "=" + aryHeaderData[i][1] + "&"; } sTemp = sTemp.substring(0,sTemp.length-2); } } // Wait 0.1 seconds and try the request again if (bIsAsynchronous) setTimeout("gfunSendRequest('" + sURL + "', '" + sMethod + "', '" + sPostData + "', '" + sTemp + "', '" + sFunctionName + "', " + bDebug + ", '" + sErrorFunctionName + "', " + bIsAsynchronous + ");",100); return; } if (window.XMLHttpRequest) { // Mozilla, Safari,... gHTTP_Request = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE try { gHTTP_Request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { return false; } } if (bIsAsynchronous) gHTTP_Request.onreadystatechange = gfunLoadExternalData; gHTTP_Request.open(sMethod, sURL, bIsAsynchronous); if (sMethod.toLowerCase() == "post") gHTTP_Request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); if (typeof aryHeaderData == "string") { if (aryHeaderData.length > 0) { // Build the Header Array from a string of name/Value pairs (Querystring format) sTemp = aryHeaderData; aryHeaderData = new Array(); var i = 0; while (sTemp.length > 0) { aryHeaderData[i] = new Array(); aryHeaderData[i][0] = sTemp.substring(0,sTemp.indexOf("=")); if (sTemp.indexOf("&") > 0) { aryHeaderData[i][1] = sTemp.substring(sTemp.indexOf("=")+1,sTemp.indexOf("&")); sTemp = sTemp.substr(sTemp.indexOf("&")+1); } else { aryHeaderData[i][1] = sTemp.substr(sTemp.indexOf("=")+1); sTemp = ""; } i++; } } else { aryHeaderData = null; } } if (aryHeaderData) { for (var i = 0; i < aryHeaderData.length; i++) { gHTTP_Request.setRequestHeader(aryHeaderData[i][0], aryHeaderData[i][1]) } } gsResponseFunction = sFunctionName; gsErrorResponseFunction = sErrorFunctionName; if (bDebug != null) gbDebugAJAX = bDebug; gHTTP_Request.send(sPostData); if (bIsAsynchronous) return true; else { var sReturnString = (window.location.href.indexOf("http") == -1 || gHTTP_Request.status==200) ? gHTTP_Request.responseText : gHTTP_Request.status; gHTTP_Request = null; return sReturnString; } } // ------------------------------------------------------------------- // gfunLoadExternalData() // Called when the AJAX request object's ready state changes // Uses the sFunctionName sent in the call to gfunSendRequest to // handle the response text. // ------------------------------------------------------------------- function gfunLoadExternalData() { if (gHTTP_Request.readyState == 4) { var sText = gHTTP_Request.responseText; if (gHTTP_Request.status == 200 && gsResponseFunction != null) { eval(gsResponseFunction + "(gHTTP_Request.responseText);"); //eval(gsResponseFunction + "(\"" + sText.replace(/\"/gi,"\\\"") + "\")"); } else if (gsErrorResponseFunction != null && gsErrorResponseFunction.length > 0) { eval(gsErrorResponseFunction + "(gHTTP_Request.status, gHTTP_Request.responseText);"); } if (gbDebugAJAX) { alert("gHTTP_Request.status: " + gHTTP_Request.status); alert("gHTTP_Request.responseText:\r\n"); while (sText.length > 0) { alert(sText.substr(0, 500)); sText = sText.substr(500); } gbDebugAJAX = false; } gHTTP_Request = null; } } // ------------------------------------------------------------------- // gfunParseAjaxValue() // Used to get data from AJAX response text split into separate variables // NOTE: By the default, the sData should be formatted: // |var|VariableName1|var|VariableValue1|var|VariableName2|var|VariableValue2|var| // ------------------------------------------------------------------- function gfunParseAjaxValue(sData, sVarName, sDelimiter) { if (sDelimiter == null) sDelimiter = "|var|"; var sSearchString = sDelimiter + sVarName + sDelimiter; var sValue = ""; var nPos = sData.indexOf(sSearchString); var nNextPos; if (nPos >= 0) { nPos = nPos + sSearchString.length; nNextPos = sData.indexOf(sDelimiter,nPos); if (nNextPos == -1) nNextPos = sData.length; // Last Variable, so get until the end of the string sValue = sData.substring(nPos, nNextPos); } return sValue; } function funGetData(sURL, sFunctionName) { var nRan = Math.random()*10000; var sMethod = "get"; var sPostData = null; var aryHeaderData = null; var bDebug = false; var sErrorFunctionName = null; sURL = sURL + "&" + nRan; gfunSendRequest(sURL, sMethod, sPostData, aryHeaderData, sFunctionName, bDebug, sErrorFunctionName) } function funWriteNowPlaying(sText) { var nRefresh = 15; //refresh var objNowPlaying = document.getElementById("NowPlayingData"); objNowPlaying.innerHTML = sText; nRefresh = (nRefresh*1000); //convert to seconds setTimeout("funGetData('/jsfiles/NowPlaying.aspx?lid=5869&source=http%3A%2F%2Fipex%2Ecitcomm%2Ecom%2FNowPlaying%2FWNTQFM%2Exml&refresh=15','funWriteNowPlaying')", nRefresh); } funGetData("/jsfiles/NowPlaying.aspx?lid=5869&source=http%3A%2F%2Fipex%2Ecitcomm%2Ecom%2FNowPlaying%2FWNTQFM%2Exml&refresh=15", "funWriteNowPlaying");