/*  tomliebert.com | copyright (c) 2005 - 2011  | tldc.js */

    //var serverURL = 'http://tomliebert.com/';
    function init() {

    }
    function CDownloadUrl(method, url, func) {
        var httpObj;
        var browser = navigator.appName;
        if(browser.indexOf("Microsoft") > -1)
            httpObj = new ActiveXObject("Microsoft.XMLHTTP");
        else
            httpObj = new XMLHttpRequest();
        httpObj.open(method, url, true);
        httpObj.onreadystatechange = function() {
            if(httpObj.readyState == 4){
                if (httpObj.status == 200) {
                    var contenttype = httpObj.getResponseHeader('Content-Type');
                    if (contenttype.indexOf('xml')>-1) {
                        func(httpObj.responseXML);
                    } else {
                        func(httpObj.responseText);
                    }
                } else {
                    func('Error: '+httpObj.status);
                }
            }
        };
        httpObj.send(null);
    }
    function CDownloadUrlPOST(method, url, params, func) {
        var httpObj;
        var browser = navigator.appName;
        if(browser.indexOf("Microsoft") > -1)
            httpObj = new ActiveXObject("Microsoft.XMLHTTP");
        else
            httpObj = new XMLHttpRequest();
        httpObj.open(method, url, true);

        //Send the proper header information along with the request
        httpObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        httpObj.setRequestHeader("Content-length", params.length);
        httpObj.setRequestHeader("Connection", "close");

        httpObj.onreadystatechange = function() {
            if(httpObj.readyState == 4){
                if (httpObj.status == 200) {
                    var contenttype = httpObj.getResponseHeader('Content-Type');
                    if (contenttype.indexOf('xml')>-1) {
                        func(httpObj.responseXML);
                    } else {
                        func(httpObj.responseText);
                    }
                } else {
                    func('Error: '+httpObj.status);
                }
            }
        };
        httpObj.send(params);
    }
    function acctLogin(){
        var secs = new Date().getSeconds();
        CDownloadUrl('GET', serverURL + 'index.php/account/logindiv/'+ secs, function(data, responseCode) {tldcBanner(data)});
    }
    function acctLoginSubmit(frmName){
        var frm = document.getElementById(frmName);
        var poststr = '';
        for (var i=0;i<frm.length;i++){
            poststr += '"&' + frm.elements[i].name + '="' + escape(frm.elements[i].value);
        }
        poststr = poststr.substr(2);
        CDownloadUrlPOST('POST', serverURL + 'index.php/account/login', poststr, function(data, responseCode) {acctLoggedin(data)});
    }
    function acctLogout(){
        var secs = new Date().getSeconds();
        CDownloadUrl('GET', serverURL + 'index.php/account/logoutdiv/'+ secs, function(data, responseCode) {acctLoggedOut(data)});
    }
    function tldcBanner(data){
        document.getElementById("bannerstripe").innerHTML = data;
        //document.getElementById("bannercontent").innerHTML = '&nbsp got to here...';
    }
    function acctLoggedin(data){
        if(data.substr(0,7)=='Welcome'){
            document.getElementById("bannerstripe").innerHTML = '';
            document.getElementById("bannerstripe").style.Height = '10px';
            document.getElementById("bannercontent").innerHTML = data;
        }else if(data.substr(0,8)=='<div id='){
            var arr = data.split("|");
            document.getElementById("bannerstripe").innerHTML = arr[0];
            document.getElementById("bannererror").style.border='2px solid #FFFFFF';
            document.getElementById("bannererror").innerHTML = arr[1];

        }else{
            document.getElementById("bannererror").style.border='2px solid #FFFFFF';
            document.getElementById("bannererror").innerHTML = data;
        }
    }
    function acctLoggedOut(data){
        var html = '<ul><li><span onClick="acctLogin()" title="sign in to client account">client login</span></li><li> &nbsp; | &nbsp; <a href="http://trac.tomliebert.com" title="work examples, proof-of-concepts, application demos" >wiki</a></li></ul>';
        document.getElementById("bannerstripe").innerHTML = '';
        document.getElementById("bannerstripe").style.Height = '10px';
        document.getElementById("bannercontent").innerHTML = html;
        if(data!==undefined){
            document.getElementById("notifycontent").innerHTML = data;
        }
    }
