/* ##########################################################################
    Copyright 2005 BBB Systems, LLC, All rights reserved
########################################################################## */

var ajaxEnabled = true;
var b_xml = '';

ajxmlObject = function( ){
    //May add functionality later where the following variables are set
    //as of right now, it's just not necessary.
    this.responseText = '';
    this.responseXML = '';

    this.status = '';
    this.statusText = '';

    //If this is set, the title of the document will dynamicly change
    this.title = '';

    this.isCurrentRequest = false;

    this.xml_http = null;
    this.isSupported = false;

    //If you don't want the workNode to be a part of the actual document,
    //you can change this to be a document.createElement instead.
    this.workNodeID = 'workNode';
    this.workNode = null;

    if(window.XMLHttpRequest || window.ActiveXObject){
        this.isSupported = true;
        this.initialize( );
    }

}

ajxmlObject.prototype = {
/* ---------------------------------------------- */

request: function(url, rMethod){
    if(!this.supported( ) || this.isCurrentRequest){
        return;
    }

    if(!this.workNode){
        this.workNode = document.getElementById(this.workNodeID);
    }

    this.isCurrentRequest = true;

    var oMethod = "GET";
    if(rMethod == "POST"){
        oMethod = "POST";
    }

    this.url = url;

    this.xml_http.open(oMethod, url, true);

    if(this.xml_http.overrideMimeType){
        this.xml_http.overrideMimeType("text/xml");
    }
    else{
        this.xml_http.setRequestHeader("Content-Type", "text/xml");
    }

    this.xml_http.send(null);
},

initialize: function( ){

    if(this.xml_http){
        this.xml_http = null;
    }

    if(window.ActiveXObject){
        this.xml_http = new ActiveXObject("Microsoft.XMLHTTP");
        if(!this.xml_http){
            this.isSupported = false;
        }
    }
    else if(window.XMLHttpRequest){
        this.xml_http = new XMLHttpRequest( );
    }

    //this doesn't work with active x objects apparently
    //this.xml_http.ajxmlObject = this;

    var ajxmlObj = this;

    //this.xml_http.onreadystatechange = ajxmlObjectProcess;
    this.xml_http.onreadystatechange = function( ){
        ajxmlObjectProcess(ajxmlObj);
    };
},

supported: function( ){
    return this.isSupported;
}

/* ---------------------------------------------- */
}//end ajxmlObject.prototype

function ajxmlObjectProcess(ajxObj){
    if(ajxObj.xml_http.readyState != 4){
        return;
    }

    if(ajxObj.xml_http.status == 200){
        //alert(b_xml.xml_http.readyState);
        var rtext = ajxObj.xml_http.responseText;

        //rtext = rtext.replace(/\*\*\+\^\!/, '&');

        if(document.all){
            //ie bug which refuses to put quotes around text/plain
            rtext = rtext.replace(/type\=\"text\/plain\"/gi, '');
        }

        ajxObj.workNode.innerHTML = unescape(rtext);
        //document.getElementById('copyright').innerHTML = '<pre style="text-align: left;">' + htmlschars(b_xml.workNode.innerHTML) + '</pre>';;

        if(ajxObj.workNode.getElementsByTagName("div")[0].className != 'xmldata'){
            ajxObj.isCurrentRequest = false;
            return;
        }

        //hack for images that fail to load (for various reasons)
        ajxObj.maxImgIterations = 10;
        ajxObj.imgIterations = 0;

        defaultComplete(ajxObj);
    }
    else if(ajxObj.xml_http.readyState == 4 && ajxObj.xml_http.status == 404){
        //alert(b_xml.xml_http.readyState);
        window.location = '/404/';
    }
    else{
        //alert(b_xml.xml_http.readyState);
        //alert(b_xml.xml_http.status + ' - ' + b_xml.xml_http.statusText);
    }

}

//This is the default most basic completion of ajax requests
function defaultComplete(ajxObj){
    var xChildren = ajxObj.workNode.getElementsByTagName("div")[0].childNodes;

    for(var i = 0; i < xChildren.length; i++){

        var node = xChildren[i];

        if(node.nodeType != 1 || node.className == ''){ continue; }

        if(node.className == "title"){
            window.scroll(0,0);
            for(j = 0; j < node.childNodes.length; j++){
                if(node.childNodes[j].nodeValue){
                    document.title = node.childNodes[j].nodeValue;
                    j = node.childNodes.length;
                }
            }
            continue;
        }

        var docNode = document.getElementById(node.className);

        if(!docNode){ continue; }

        node.normalize( );

        docNode.innerHTML = node.innerHTML;

        if(node.className == 'chat_status'){
            if(node.innerHTML == 'Chat session is no longer active'){
                window.chat_is_active = false;
            }
        }
        if(node.className == 'chat_tbody_update' && window.chat_tbody_update && window.chat_tbody){
            //find out if the user has been scrolling up...
            var do_scroll = true;
            var cb_height = chat_body.clientHeight > 0 ? chat_body.clientHeight : chat_body.offsetHeight;
            if(chat_body.scrollHeight - chat_body.scrollTop > cb_height + 10){
                //the + 10 is for in case the users didn't quite scroll back down to the bottom
                do_scroll = false;
            }

            var trs = chat_tbody_update.getElementsByTagName('tr');

            var tr_list = new Array( );
            for(var j = 0; j < trs.length; j++){
                tr_list[j] = trs[j];
            }

            for(var j = 0; j < tr_list.length; j++){
                var tr_node = tr_list[j];
                chat_tbody.appendChild(tr_node);
            }
            chat_tbody_update.innerHTML = '';

            if(do_scroll){
                chat_body.scrollTop = chat_body.scrollHeight + chat_body.clientHeight;
            }
        }
        if(node.className == 'coupon_work'){
            var inputs = node.getElementsByTagName('input');
            var cid = 0;
            var cwidth = 0;
            var cheight = 0;
            for(var j = 0; j < inputs.length; j++){
                var cname = inputs[j].name;
                var cvalue = inputs[j].value;

                switch(cname){
                case 'coupon_id':
                    cid = parseInt(cvalue);
                    break;
                case 'coupon_width':
                    cwidth = parseInt(cvalue);
                    break;
                case 'coupon_height':
                    cheight = parseInt(cvalue);
                    break;
                }
            }
            if(cid > 0 && cwidth > 0 && cheight > 0){
                openCouponPopup(cid, cwidth, cheight);
            }
        }
    }

    ajxObj.isCurrentRequest = false;
    ajxObj.initialize( );

    //must be defined in pscript.js on a per site basis
    if(window.xmlInit){
        xmlInit( );
    }

}

//getCookie and setCookie must be defined somewhere.
//element navContainer must be defined.
function ajaxCheck( ){
    if(window.name != 'ajaxApp'){
        if(ajaxEnabled && b_xml){
            if(b_xml.supported( )){
                setcookie('ajaxSupported','1')
            }
        }
    }
}

function htmlschars(str){
    str = str.replace(/\&/g, "&amp;");
    str = str.replace(/\</g, "&lt;");
    str = str.replace(/\>/g, "&gt;");
    str = str.replace(/\"/g, "&quot;");
    str = str.replace(/\'/g, "&#039;");
    str = str.replace(/\™/g, "&trade;");

    return str;
}


//The default ajax object that can be used for various tasks
window.b_xml = new ajxmlObject( );
