// AP Hosted 2.0 Dynamic Script Tags, for JSON requests.
//
// Based on JSONscriptRequest
//    Author: Jason Levitt
//    Date: December 7th, 2005

function JSONscriptRequest(fullUrl, uniqueID)
{
    this.fullUrl = fullUrl;
    this.noCacheIE = '/noCacheIE-' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
    if (uniqueID) {
        this.uniqueID = '/uid-' + uniqueID;
    }
}

JSONscriptRequest.scriptCounter = 1;

JSONscriptRequest.prototype.buildScriptTag = function ()
{
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    var thisURL = this.fullUrl + this.noCacheIE;
    if (this.uniqueID) {
        thisURL = thisURL + this.uniqueID;
    }
    this.scriptObj.setAttribute("src", thisURL);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
JSONscriptRequest.prototype.removeScriptTag = function ()
{
    this.headLoc.removeChild(this.scriptObj);
}

JSONscriptRequest.prototype.addScriptTag = function ()
{
    this.headLoc.appendChild(this.scriptObj);
}

function sendScriptRequest(reqScriptFile)
{
    var localScriptObj = document.createElement("script");
    localScriptObj.setAttribute("type", "text/javascript");
    localScriptObj.setAttribute("charset", "utf-8");
    localScriptObj.setAttribute("src", reqScriptFile);
    var thisheadLoc = document.getElementsByTagName("head").item(0);
    thisheadLoc.appendChild(localScriptObj);
}

function dynamicLoadPage(url, uniqueID)
{
    var aObj = new JSONscriptRequest(url, uniqueID); aObj.buildScriptTag(); aObj.addScriptTag();
}

function dynamicRefresh(url, timeSpan, checkInput, uniqueID)
{
    if (checkInput == '' || document.getElementById(checkInput).value == '')
    {
        dynamicLoadPage(url, uniqueID);
    }
    if (timeSpan > -1)
        setTimeout("dynamicRefresh('" + url + "', " + timeSpan + ", '" + checkInput +
            "', '" + uniqueID + "')", timeSpan);
}

function callBack(jsonData)
{
    // load any script files passed in
    try{
        var theseScriptFiles = jsonData.data.scriptFiles;
        if (theseScriptFiles != null)
        {
            for (var i = 0; i < theseScriptFiles.length; i++)
            {
                sendScriptRequest(theseScriptFiles[i]);
            }
        }
    }catch(e){
        //alert(e);
        //alert(jsonData.data.jsonDynamicDivId);
    }
    
    // update the div to be replaced with HTML
    try{
        var dynamicDivId = jsonData.data.jsonDynamicDivId;
        document.getElementById(dynamicDivId).innerHTML = '<input type="hidden" />\n' + jsonData.data.html;
    }catch(e){
        //alert(e);
        //alert(jsonData.data.jsonDynamicDivId);
    }
    
    // update any client side cookies being passed in
    try{
        var numCookies = parseInt(jsonData.data.cookieCount);
        for(var x = 0 ; x < numCookies; x++){
            var cookieName = 'jsonData.data.cookies.cookie' + x + ".name";
            var cookieValue = 'jsonData.data.cookies.cookie' + x + ".value";
            setCookie(eval(cookieName), eval(cookieValue), 2000);
        }
    }catch(e){
    }

    // fire any client side JS being passed in    
    try{
        var jsCode = jsonData.data.javascript;
        if(jsCode != null && jsCode != ''){
            eval(jsCode);
        }
    }catch(e){
    }
}

function cloneNode(source, recurse)
{
    var newNode = document.createElement("div");
    try{
        document.createElement(source.nodeName);
        for(var x = 0; x < source.attributes.length; x++){
            newNode.setAttribute(source.attributes[x].nodeName, source.attributes[x].nodeValue);
        }
        newNode.nodeValue = source.nodeValue;

        if(recurse){
            for(var x = 0 ; x < source.childNodes.length; x++){
                newNode.appendChild(cloneNode(source.childNodes[x], recurse));
            }
        }
    }catch(e){
        alert("Error, Tag: " + source.nodeName + " Error: " + e);
    }
    return(newNode);
}
