/***************************************************************************
 *   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"; }
        return true;
    },

    setDisplay : function(objName,objDisplay) {
        obj = document.getElementById(objName);
        if (obj) {
            obj.style.display = objDisplay;
        }
    },
    setDisplayGroup : function(objID,tagName,objDisplay) { obj = document.getElementById(objID); objGroup = document.getElementsByTagName(tagName); if (objGroup) { for (i=0;i<objGroup.length;i++) { obj = objGroup.item(i); if (obj.id.substr(0,objID.length) == objID) { obj.style.display = objDisplay; } } } },
    toggleDisplay : function(objID,mode1,mode2) { this.obj = document.getElementById(objID); if (this.obj) { objDisplay = this.obj.style.display; if (objDisplay == mode1) { this.obj.style.display = mode2; } else { this.obj.style.display = mode1; } } },
    toggleDisplayGroup : function(objID,tagName,mode1,mode2) { objGroup = document.getElementsByTagName(tagName); masterSet = "_"; if (objGroup) { for (i=0;i<objGroup.length;i++) { obj = objGroup.item(i); if (obj.id.substr(0,objID.length) == objID) { if (masterSet != '_') { obj.style.display = masterSet; } if (obj.style.display == mode1) { obj.style.display = mode2; } else { obj.style.display = mode1; } } } } },
    
    loadAnimation : function(placeId,objId,count,imgSrc) { 
    	obj = document.getElementById(objId); 
    	if (obj) { 
    		offset = this.findOffset(obj,count); 
    		div = document.createElement('div'); 
    		div.style.backgroundImage = "url('img/fade.png')"; 
    		div.style.position = 'absolute'; 
    		div.style.top = (offset.dy - 4) + 'px'; 
    		div.style.left = offset.dx + 'px'; 
    		div.style.width = obj.clientWidth + 'px'; 
    		div.style.height = obj.clientHeight + 'px'; 
    		div.id = 'loading_' + obj.id; 
    		div.innerHTML = '<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=' + obj.clientWidth + ' HEIGHT=' + obj.clientHeight + '><TR><TD ALIGN=CENTER VALIGN=MIDDLE><IMG SRC="' + imgSrc + '"></TD></TR></TABLE>'; 
    		div = document.getElementById(placeId).appendChild(div); 
    	} 
    },
    
    delayDestroy : function(objId) { oldScript = document.getElementById(objId); if (oldScript) { oldParent = oldScript.parentNode; oldScript = oldParent.removeChild(oldScript); oldScript = null; } },

    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";
        }
    },

    findOffset : function(obj,count) {
    	var offset = { dx : 0, dy : 0, dt : 0 };
    	count--;
        offset.dx += obj.offsetLeft;
        offset.dy += obj.offsetTop;
        offset.dt += "[" + obj.nodeName + "] : " + obj.offsetLeft + "x" + obj.offsetTop + "  " + obj.offsetWidth + "w " + obj.offsetHeight + "h  \n";
        //alert('1:' + obj.id + '  2:' + stopId);
        if (count > 0) {
	        if (obj.offsetParent) {
	            parentOffset = this.findOffset(obj.offsetParent,count);
	            offset.dx += parentOffset.dx;
	            offset.dy += parentOffset.dy;
	            offset.dt += parentOffset.dt;
	        }
        } 
        return offset;
    },
    
    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.
    	// Need a function to create a covering div with animation saying "loading!" and to destroy it upon loaded...
    	this.scriptNum++;
        oldScript = document.getElementById("tdAjaxScript_"+this.scriptNum);
        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_'+this.scriptNum);
        script.setAttribute('language','Javascript');
        script.setAttribute('type','text/javascript');
        pageHead = document.getElementsByTagName('head').item(0);
        pageHead.appendChild(script);
        this.setComm(true);
        setTimeout("tdAjax.delayDestroy('tdAjaxScript_"+this.scriptNum+"')",60000);
    },
    
    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
}



