/***************************************************************************
 *   Copyright (C) 2008 by Chris Holland                                   *
 *   topher@topherdigital.com                                              *
 *                                                                         *
 *   This program is copyright 2008 by Chris Holland. All Rights Reserved  *
 ***************************************************************************/

var tdAjax = {
    bCode : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    bTimer : 0,
    
    setComm : function(status) {
        obj = document.getElementById("tdAjaxComm");
        if (!obj) { return false; }
        if (status) { obj.style.display = "block"; } else { obj.style.display = "none"; }
    },

    setDisplay : function(objName,objDisplay) {
        obj = document.getElementById(objName);
        if (obj) {
            obj.style.display = objDisplay;
        }
    },

    setCenteredDimensions : function(objName,w,h) {
        obj = document.getElementById(objName);
        if (obj) {
            marginH = Math.round(w / 2);
            marginV = Math.round(h / 2);
            obj.style.width = w + "px";
            obj.style.height = h + "px";
            obj.style.marginLeft = "-" + marginH + "px";
            obj.style.marginTop = "-" + marginV + "px";
        }
    },
    
    formToQuery : function(fName) {
        var query = '';
        formObj = document.forms[fName];
        if (!formObj) { return ''; }
        for (i=0;i<formObj.elements.length;i++) {
            query += formObj.elements[i].name + "=" + encodeURIComponent(formObj.elements[i].value) + "&";
        }
        return query;
    },

    rpcQuery : function(action,container,query) {
        // Here we send to query to the server, by creating a script tag.
        // The resultant script takes care of the rest.
        oldScript = document.getElementById("tdAjaxScript");
        if (oldScript) {
            oldParent = oldScript.parentNode;
            oldParent.removeChild(oldScript);
            oldScript = null;
        }
        script = document.createElement('script');
        script.setAttribute('src','tdAjax/tdAjaxRPC.php5?tdAjaxContainer=' + container + '&tdAjaxAction=' + action + '&tdAjaxData=' + encodeURIComponent(this.base64Encode(query)) );
        script.setAttribute('id','tdAjaxScript');
        script.setAttribute('language','Javascript');
        script.setAttribute('type','text/javascript');
        pageHead = document.getElementById('head');
        pageHead.appendChild(script);
        this.setComm(true);
        if (this.bTimer != 0) {
            clearTimeout(this.bTimer);
        }
    },
    
    rpcDelayQuery : function(action, container, query, delay) {
        queryCode = "tdAjax.rpcQuery('" + action + "','" + container + "','" + query + "')";
        this.bTimer = setTimeout(queryCode,delay);
    },
    
    base64Encode : function(rawdata) {
        var res = '';
        var c1,c2,c3,b1,b2,b3,b4;
        var i = 0;
        while (i < rawdata.length) {
            c1 = rawdata.charCodeAt(i++); c2 = rawdata.charCodeAt(i++); c3 = rawdata.charCodeAt(i++);
            b1 = c1>>2; b2 = ((c1&3)<<4)|(c2>>4); b3 = ((c2&15)<<2)|(c3>>6); b4 = c3&63;
            if (isNaN(c2)) { b3 = b4 = 64; } else if (isNaN(c3)) { b4 = 64; }
            res = res + this.bCode.charAt(b1) + this.bCode.charAt(b2) + this.bCode.charAt(b3) + this.bCode.charAt(b4);
        }
        return res;
    },

    base64Decode : function(b64data) {
        var res = '';
        var c1,c2,c3,b1,b2,b3,b4;
        var i = 0;
        b64data = b64data.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < b64data.length) {
            b1=this.bCode.indexOf(b64data.charAt(i++)); b2=this.bCode.indexOf(b64data.charAt(i++)); b3=this.bCode.indexOf(b64data.charAt(i++)); b4=this.bCode.indexOf(b64data.charAt(i++));
            c1 = (b1<<2)|(b2>>4); c2 = ((b2&15)<<4)|(b3>>2); c3 = ((b3&3)<<6)|b4;
            res = res + String.fromCharCode(c1);
            if (b3 != 64) { res = res + String.fromCharCode(c2); }
            if (b4 != 64) { res = res + String.fromCharCode(c3); }
        }
        return res;
    },

    version : 0.01
}



