/* #################################################################
    Copyright 2005 BBB Systems, LLC, All rights reserved
################################################################## */
if(!window.ajaxEnabled){
    ajaxEnabled = false;
}

var navHistory = new Array( );
var curNavPoint = -1;

var globals = new Array( );

var currentLocation = '';

var is_safari = (document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)&&(!navigator.accentColorName)?true:false;

function init( ){
    if(!document.getElementsByTagName){
        return;//if it doesn't support this, it doesn't support a whole lot.
    }

    //if(window.ajaxEnabled){
    //    ajaxCheck( );
    //}
    //else{
        window.ajaxEnabled = false;
        window.b_xml = false;
    //}
    if(window.limitedAjax != true){
        window.limitedAjax = false;
    }

    initA( );
    initAppObjects( );
    initObjects( );

    appThink( );

    initInputButtons( );
}

function xmlInit( ){
    initA( );
    initAppObjects( );
    initObjects( );

    appThink( );

    initInputButtons( );

    if(window.qcast_enabled){
        quantserve();
    }
}


function initInputButtons( ){
    if(document.getElementsByTagName){
        var inputs = document.getElementsByTagName("INPUT");
        for(var i = 0; i < inputs.length; i++){
            if((inputs[i].type == "submit" || inputs[i].type == "reset" || inputs[i].type == "button") && inputs[i].className == "button"){
                inputs[i].onmouseover = inputOver;
                inputs[i].onmouseout = inputOut;
            }
        }
        var buttons = document.getElementsByTagName("BUTTON");
        for(var i = 0; i < buttons.length; i++){
            if((buttons[i].type == "submit" || buttons[i].type == "reset" || buttons[i].type == "button") && buttons[i].className == "button"){
                buttons[i].onmouseover = inputOver;
                buttons[i].onmouseout = inputOut;
            }
        }
        var uls = document.getElementsByTagName("UL");
        for(var i = 0; i < uls.length; i++){
            if(uls[i].id.match(/tdiv_\d/)){
                var tmp = uls[i].id.replace(/tdiv_/, "");
                tmp = tmp * 100;
                uls[i].style.left = tmp + "px";
            }
        }
    }
}

function initBlocks( ){
    if(document.getElementsByTagName){
        SetCookie('domSupport','1');
        var imgs = document.getElementsByTagName("img");
        var bcnt = 0;
        for(var i = 0; i < imgs.length; i++){
            var img = imgs[i];
            if(img.src.match(/\/images\/minus\.gif/) || img.src.match(/\/images\/plus.gif/) || img.src.match(/\/images\/blank.gif/)){
                //get the parent
                var iparent = img.parentNode;
                while(iparent){
                    if(iparent.tagName == 'div' || iparent.tagName == 'DIV'){
                        break;
                    }
                    iparent = iparent.parentNode;
                }
                if(!iparent){ continue };
                var iblock = iparent.nextSibling;
                while(iblock){
                    if(iblock.tagName == 'div' || iblock.tagName == 'DIV'){
                        break;
                    }
                    iblock = iblock.nextSibling;
                }
                if(!iblock){ continue; }
                if(iblock.className != 'blockA'){ continue; }

                var atags = iblock.getElementsByTagName('a');
                var doBlock = false;
                for(var j = 0; j < atags.length; j++){
                    var a = atags[j];
                    if(window.location == a.href){
                        doBlock = true;
                    }
                }
                if(bcnt == 0 || doBlock){
                    iblock.style.display = "block";
                    bcnt++;
                }
                else{
                    iblock.style.display = "none";
                }

                if(iblock.style.display == "none"){
                    img.src = plusImg.src;
                }
                else{
                    img.src = minusImg.src;
                }

                iparent.onclick = function( ){
                    var iblock = this.nextSibling;
                    while(iblock.tagName != 'div' && iblock.tagName != 'DIV'){
                        iblock = iblock.nextSibling;
                    }

                    if(iblock.style.display == "none" || iblock.style.display == "NONE"){
                        iblock.style.display = 'block';
                        this.getElementsByTagName('img')[0].src = img.src = minusImg.src;
                    }
                    else{
                        iblock.style.display = 'none';
                        this.getElementsByTagName('img')[0].src = img.src = plusImg.src;
                    }
                }

                iparent.onmouseover = function( ){
                    this.style.textDecoration = 'underline';
                }

                iparent.onmouseout = function( ){
                    this.style.textDecoration = 'none';
                }

                if(document.all){
                    //the ie way
                    iparent.style.cursor = 'hand';
                }
                else{
                    //every other way
                    iparent.style.cursor = 'pointer';
                }
            }
        }
    }
    else{

    }
}

function initA( ){

    var as = document.getElementsByTagName("a");
    for(var i = 0; i < as.length; i++){

        var hostnm = as[i].href.replace(/http(s)?\:\/\//, "");
        hostnm = hostnm.replace(/\/.*/, "");

        if((hostnm != window.location.hostname && hostnm != '') || as[i].href.match(/\/ad\.\php\?/)){
            if(!hostnm.match(/bbbsystems/)){
                as[i].target = "_blank";
                continue;
            }
        }

        var cname = as[i].className;
        if(!cname){ cname = ''; }

        if(ajaxEnabled && !cname.match(/noAJAX/i) && !as[i].href.match(/\/members\//) && !as[i].href.match(/feed\.php/)){
            if(b_xml){
                if( as[i].href.match(/(\#)$/) ){
                    continue;
                }
                as[i].onclick = aMenuAClick;
            }

        }
    }//end for
}

function aMenuAClick( ){

    if(b_xml.isCurrentRequest){
        return false;
    }

    //we need the protocol
    hostnm = window.location.hostname;

    var newHREF = this.href;

    newHREF = newHREF.replace(/^(http(s)?\:\/\/)([^\/]+)/, '');
    newHREF = newHREF.replace(/(\/\#)/, '');
    newHREF = newHREF.replace(/([\&\?]*refer\=.*)$/, '');

    this.href = '/#' + newHREF;

    if(document.all && window.ieHistoryHack != undefined){
        //add the anchor for our happy ie hack
        ieHistoryHack.src = '/iehack.php?' + newHREF.replace(/^(\/\#)/, '');
        ieHistoryHack.window.location = ieHistoryHack.src
    }

    currentLocation = newHREF;

    if(newHREF.match(/\?/)){
        newHREF += "&xmlOnly=1";
    }
    else{
        newHREF += "?xmlOnly=1";
    }

    //safari hack
    if(is_safari){
        //fakeHistory gets initialized by safari
        var tlen = window.history.length + 1;
        if(window.fakeHistory == undefined){
            window.fakeHistory = new Array( );
        }

        window.fakeHistory[tlen] = newHREF;
    }

    var tmenu = document.getElementById('tmenu');
    var tmenu_a = tmenu.getElementsByTagName('a');

    for(var i = 0; i < tmenu_a.length; i++){
        tmenu_a[i].className = '';
    }

    if(this.parentNode.parentNode){
        if(this.parentNode.parentNode.id == 'tmenu'){
            this.className = 'ta_cur';
        }
    }

    var tHREF = window.location.href.replace(/^(http(s)?\:\/\/)([^\/]+)/, '');
    //if(tHREF.match(/^\/\#/)){
        b_xml.request(newHREF);
    //}

    if(b_xml.isSupported){
        return true;
    }

    return true;
}


function initAppObjects( ){

    if(curNavPoint < 0){
        curNavPoint = 0;

        var newHREF = window.location.href;
        if(newHREF.match(/\?/)){
            newHREF += "&xmlOnly=1";
        }
        else{
            newHREF += "?xmlOnly=1";
        }

        navHistory.push(newHREF);
    }


    var navBack = document.getElementById('navBack');
    var navForward = document.getElementById('navForward');
    if(navBack && navForward && navHistory){
        if(curNavPoint == 0){ navBack.className = "navBackDisabled"; }
        else{ navBack.className = "navBack"; }

        if(curNavPoint == navHistory.length - 1){ navForward.className = "navForwardDisabled"; }
        else{ navForward.className = "navForward"; }
    }

}

function appThink( ){

    //from here down is all specificly ajax stuff

    if(ajaxEnabled && !window.limitedAjax){

        var newHREF = window.location.href;

        //the big ieHack, this is sick, don't try this at home
        var didIE = false;
        var windowHREF = newHREF;//another piece of the ie hack
        //and the all new Safari hack, w00t

        if(is_safari){
            var tlen = window.history.length;
            if(window.cHistoryLength == undefined){
                window.cHistoryLength = tlen;
                window.fakeHistory = new Array( );

                var sfh = getCookie('safariFakeHistory');
                var sfhl = getCookie('safariFakeHistoryLength');

                if(sfh && sfhl == tlen){
                    //Only for refreshing, it will bug up otherwise, not much we can do about it though
                    window.fakeHistory = sfh.split("|");
                }
                else{
                    //back will no longer work at this point, fair sacrifice though
                    window.fakeHistory[tlen] = window.location.href;
                }
            }

            if(window.cHistoryLength != tlen){
                if(window.fakeHistory[tlen] != undefined && tlen != window.cHistoryLength){
                    newHREF = window.fakeHistory[tlen];
                }
                window.cHistoryLength = window.history.length;
                window.fakeHistory[tlen] = newHREF;

                var cookStr = '';
                for(var i = 0; i < window.fakeHistory.length; i++){
                    cookStr += window.fakeHistory[i] + '|';
                }
                setcookie('safariFakeHistory', cookStr);
                setcookie('safariFakeHistoryLength', tlen);
            }
            else{
                newHREF = window.fakeHistory[tlen];
            }
        }
        else if(document.all && window.ieHistoryHack != undefined){
            windowHREF = windowHREF.replace(/^(http(s)?\:\/\/)([^\/]+)/, '');
            windowHREF = windowHREF.replace(/(\/\#)/, '');

            //if(ieHistoryHack.document.body.innerHTML != '/'){
                newHREF = ieHistoryHack.document.body.innerHTML;
                didIE = true;
            //}
        }

        newHREF = newHREF.replace(/^(http(s)?\:\/\/)([^\/]+)/, '');

        var tHREF = newHREF;

        newHREF = newHREF.replace(/(\/\#)/, '');

    /*
        //debug information... pick an id, any id (it beats infinite alerts).
        var tvar = newHREF + ' - ' + currentLocation + '<br />hash: ' + window.location.hash + '<br />location: ' + window.location + '<br />doc-location:' + document.location;
        tvar += '<br />' + window.history.length;
        document.getElementById('bbb').innerHTML = tvar;
    */

        if(newHREF != '' && ( tHREF.match(/^\/#/) || tHREF == '/' || currentLocation == '' || currentLocation != newHREF) ){

            //if(recursionCount < 5){
            //    alert(newHREF + ' - ' + currentLocation + ' - ' + windowHREF);
            //    recursionCount++;
            //}

            //check to see if the location has changed
            if(newHREF != currentLocation && !b_xml.isCurrentRequest && window.location.protocol.toLowerCase( ) != 'https'){
                if(document.all && didIE){
                    //update the location in the address bar to complete the ie hack
                    var ieURL = '/';
                    if(newHREF != windowHREF){
                        ieURL = '/#' + newHREF;
                        if(newHREF == '/'){
                            window.location.href = '/';
                        }
                        else{
                            window.location.href = ieURL;
                        }
                    }
                }

                var firstPage = false;
                if(currentLocation == ''){
                    firstPage = true;
                }
                currentLocation = newHREF;
                //if(recursionCount < 5){
                //    alert(newHREF + ' - ' + currentLocation + ' - ' + windowHREF);
                //    recursionCount++;
                //}



                    if(newHREF.match(/\?/)){
                        newHREF += "&xmlOnly=1";
                    }
                    else{
                        newHREF += "?xmlOnly=1";
                    }
                    b_xml.request(newHREF);

                    firstPage = false;

            }
        }
    }//End if ajaxEnabled

    //make the program think every 10th of a second
    setTimeout('appThink( )', 100);
}

function initObjects( ){

    var objects = document.getElementsByTagName("div");
    var len = objects.length;

    var tLoc = window.location.href;

    var prot = 'http';
    if(document.all && tLoc.match(/^https/)){
        prot = 'https';
    }

    for(var i = 0; i < len; i++){
        var obj = objects[i];
        if(obj.className && obj.className == 'bgzobject'){
            var paramStr = '';
            var objStr = '\n<object ';
            var embedStr = '\n<embed ';
            var params = obj.getElementsByTagName('param');
            var plen = params.length;
            var o_type = '';
            var o_filename = '';
            var o_width = '';
            var o_height = '';
            //need o_type, it will be first or second in the list, but just in case.
            for(j = 0; j < plen; j++){
                var param = params[j];
                var pn = param.name;
                var pv = param.value;
                if(pn == 'o_type'){
                    o_type = pv;

                    //do this here because we have to force video type for wmv files
                    var isWindows = false;
                    //if( o_type != 'wmv' && !document.all){
                    //     isWindows = false;
                    //}else if(o_type == 'wmv' || navigator.userAgent.match(/windows/i)){
                    if(o_type == 'wmv' || navigator.userAgent.match(/windows/i)){
                        isWindows = true;
                    }

                    break;
                }
            }
            for(j = 0; j < plen; j++){
                var param = params[j];
                var pn = param.name;
                var pv = param.value;

                switch(pn){
                case 'o_type':
                    if(o_type == 'swf'){
                        embedStr += ' type="application/x-shockwave-flash" ';
                        objStr += ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
                        objStr += ' codebase="' + prot + '://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" ';
                    }
                    else if(isWindows && o_type != 'mov'){

                        embedStr += ' type="application/x-mplayer2" ';
                        embedStr += '  pluginspage="' + prot + '://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" ';
                        objStr += ' classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" ';
                        objStr += ' standby="Loading..." ';
                        objStr += ' type="application/x-oleobject" ';
                        objStr += ' codebase="' + prot + '://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" ';
                    }
                    else{
                        embedStr += ' pluginspage="' + prot + '://www.apple.com/quicktime/download/" ';
                        objStr += ' classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" ';
                        objStr += ' codebase="' + prot + '://www.apple.com/qtactivex/qtplugin.cab" ';
                    }
                    break;
                case 'o_filename':
                    o_filename = pv;
                    if(o_type == 'swf'){
                        paramStr += ' <param name="movie" value="/docs/' + pv + '" /> \n';
                    }
                    else if(isWindows && o_type != 'mov'){
                        paramStr += ' <param name="filename" value="/docs/' + pv + '" /> \n';
                    }
                    else{
                        paramStr += ' <param name="src" value="/docs/' + pv + '" /> \n';
                    }
                    embedStr += ' src="/docs/' + pv + '" ';

                    break;
                case 'o_width':
                    o_width = pv;
                    obj.width = pv;
                    embedStr += ' width="' + pv + '" ';
                    objStr += ' width="' + pv + '" ';
                    break;
                case 'o_height':
                    o_height = pv;
                    obj.height = pv;
                    embedStr += ' height="' + pv + '" ';
                    objStr += ' height="' + pv + '" ';
                    break;
                default:
                    pn = pn.replace(/^(o\_)/, '');

                    if(!isWindows && pn == 'showcontrols'){
                        pn = 'controller';
                    }

                    if(pn == 'transparency'){
                        pn = 'wmode';
                        pv = 'transparent';

                        if(document.all){
                            //for some reason flash transparency is screwing up on this site,
                            //if we ever figure it out, delete this code
                            //seems to work now *shrug* - hobbit
                            //continue;
                        }
                    }

                    paramStr += ' <param name="' + pn + '" value="' + pv + '" /> ';

                    if(pv.toLowerCase( ) == 'false'){
                        pv = '0';
                    }
                    else if(pv.toLowerCase( ) == 'true'){
                        pv = '1';
                    }

                    if(pn == 'showcontrols'){
                        if(pv == '0'){
                            paramStr += ' <param name="ShowStatusBar" value="false" /> \n';
                            embedStr += ' showstatusbar="0" ';
                        }
                        else{
                            paramStr += ' <param name="ShowStatusBar" value="true" /> \n';
                            embedStr += ' showstatusbar="1" ';
                        }
                    }

                    embedStr += ' ' + pn + '="' + pv + '" ';


                    break;
                }
            }//end params

            embedStr += '></embed>\n';
            objStr += '>\n';
            //alert(objStr + paramStr + embedStr + '</object>');

            obj.innerHTML = objStr + paramStr + embedStr + '</object>\n';
            obj.style.display = 'block';

        }//end if bgzobject
    }//end foreach object

}

function inputOver(e){
    this.className = "buttonOver";
}

function inputOut(e){
    this.className = "button";
}

function generalPopUp(loc, w, h){
    gPopUp = window.open(loc, "gpopup", "width=" + w + ",height=" + h + ", scrollbars=0, status=no,toolbar=no,resizable=yes");
    gPopUp.focus( );
}
function generalPopUp2(loc, w, h){
    gPopUp = window.open(loc, "gpopup", "width=" + w + ",height=" + h + ", scrollbars=1, status=no,toolbar=no,resizable=yes,menubar=yes");
    gPopUp.focus( );
}

function imageSelectPopUp(pwinvar){
    imgSelectPopUp = window.open("./imageselect.php?mode=1&fieldID=" + pwinvar, "ImageSelector", "width=780,height=480, scrollbars=0, status=no,toolbar=no,resizable=no");
    imgSelectPopUp.focus( );
}

function openUploader( ){
    var imgSelectPopUp = window.open("./imageselect.php", "ImageSelector", "width=780,height=480, scrollbars=0, status=no,toolbar=no,resizable=no");
    imgSelectPopUp.focus( );

    return false;
}

function openFUploader( ){
    var imgSelectPopUp = window.open("./fileselect.php", "FileSelector", "width=780,height=480, scrollbars=0, status=no,toolbar=no,resizable=no");
    imgSelectPopUp.focus( );

    if(document.all){
        window.event.cancelBubble = true;
    }
    else{
        e.stopPropagation( );
        e.preventDefault( );
    }
    return false;
}

function printView(plink) {
    var plink2 = "/print/p/" + plink;
    window.open(plink2, "ShoppingList", "width=620,height=400,status=yes,toolbar=no,resizable=yes,raised=yes,scrollbars=yes");
}

function printPage(buttonID) {
    var printButton = document.getElementById(buttonID);
    printButton.style.visibility = "hidden";
    window.print();
    window.close();
}

//###################################################################

function setcookie(cookieName,cookieValue){
    //sets a cookie that expires at the end of the session
    document.cookie = cookieName + "=" + escape(cookieValue) + ";path=/";
}

function getCookie(cname){
    var dc = document.cookie;
    var prefix = cname + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1){
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else{
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1){
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function disableButton(buttonID, msg){
   tButton = document.getElementById(buttonID);

   if(!tButton){
        return;
   }

   tButton.value = msg;
   tButton.style.color = "#606060";
   tButton.style.backgroundColor = "#C0C0C0";
   tButton.disabled = true;
}

function hideById(tid){
    tObj = document.getElementById(tid);

    tObj.style.display = 'none';
}
function showById(tid, dispType){
    tObj = document.getElementById(tid);

    if(!dispType || window.is_ie){
        dispType = 'block';
    }

    tObj.style.display = dispType;
}

function disableBoxes(form, check){
    for(var i = 0; i < form.elements.length; i++){
        if(form.elements[i].type == 'checkbox' && form.elements[i].id != 'acheck'){
            form.elements[i].disabled = check;
            form.elements[i].checked = check;
        }
    }
}
