/* ##########################################################################
    Copyright 2005 BBB Systems, LLC, All rights reserved
########################################################################## */

var ajaxEnabled = false;
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.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.XMLHttpRequest){
        this.xml_http = new XMLHttpRequest( );
    }
    else if(window.ActiveXObject){
        this.xml_http = new ActiveXObject("Microsoft.XMLHTTP");
        if(!this.xml_http){
            this.isSupported = false;
        }
    }

    this.xml_http.onreadystatechange = ajxmlObjectProcess;
},

supported: function( ){
    return this.isSupported;
}

/* ---------------------------------------------- */
}//end ajxmlObject.prototype

b_xml = new ajxmlObject( );

function ajxmlObjectProcess( ){
    if(b_xml){
        if(b_xml.xml_http.readyState != 4){
            return;
        }

        if(b_xml.xml_http.status == 200){
            var rtext = b_xml.xml_http.responseText;

            if(document.all){
                //ie bug which refuses to put quotes around text/plain
                rtext = rtext.replace(/type\=\"text\/plain\"/gi, '');
            }

            b_xml.workNode.innerHTML = rtext;
            var ihtml = b_xml.workNode.getElementsByTagName("div")[0].innerHTML;
            var div_id = b_xml.workNode.getElementsByTagName('div')[0].id;

            switch(div_id){
            case 'tabs':
                defaultComplete( );
                break;
            default:
                var dObj = document.getElementById('search_service_type2');
                if(!dObj){
                    dObj = document.getElementById('lsearch_service_type2');
                }

                if(!dObj){
                    break;
                }

                dObj.innerHTML = '';

                var ihtmlA = ihtml.split(/\+\+\+\+\+/);

                for(var i = 0; i < ihtmlA.length; i++){
                    var tmpA = ihtmlA[i].split(/\|\|\|/);
                    if(tmpA[0] != ''){
                        var opt = document.createElement('option');
                        opt.value = tmpA[0];
                        opt.innerHTML = tmpA[1];

                        dObj.appendChild(opt);
                    }
                }

                break;
            }

            b_xml.isCurrentRequest = false;
            b_xml.initialize( );
            return;
        }
    }
}

function defaultComplete( ){
    var i;
    var xChildren = b_xml.workNode.getElementsByTagName("div")[0].childNodes;

    var loadMaps = false;
    for(i = 0; i < xChildren.length; i++){
        var node = xChildren[i];

        if(node.nodeType != 1){ continue; }


        if(node.className == "title"){
            for(j = 0; j < node.childNodes.length; j++){
                if(node.childNodes[j].nodeValue){
                    document.title = node.childNodes[j].nodeValue;
                    break;
                }
            }
            continue;
        }
        //else it's an inner document node.

        var docNode = document.getElementById(node.className);

        if(!docNode){
            continue;
        }

        node.normalize( );
        var nodeCopy = node.cloneNode(true);

        if(nodeCopy.innerHTML.match(/bizMap/)){
            loadMaps = true;
        }
        //alert(nodeCopy.innerHTML);

        docNode.innerHTML = "";
        if(document.insertAdjacentElement){
            docNode.insertAdjacentElement("beforeEnd", nodeCopy);
        }
        else{
            docNode.appendChild(nodeCopy);
        }
    }

    b_xml.isCurrentRequest = false;
    b_xml.initialize( );

    //alert(loadMaps);
    if(loadMaps){
        loadGoogleMaps( );
    }
    initInputButtons( );

    //must be defined in pscript.js on a per site basis
    //if(xmlInit){
    //    xmlInit( );
    //}
}