/* #################################################################
    Copyright 2005 BBB Systems, LLC, All rights reserved
################################################################## */

var bScrollAmount = 20;

var navHistory = new Array( );
var curNavPoint = -1;

var globals = new Array( );

var ajaxEnabled = false;
var bgzDDEnabled = true;

var stopProdRotator = false;
var allowedPRIndex = 1;

if(window.special_init){
    window.load_function = special_init;
}
else{
    window.load_function = function( ){ init( ); }
}


if(document.addEventListener){
    document.addEventListener('DOMContentLoaded', function( ){ window.load_function( ); }, false);
    //just in case, if DomContentLoaded ran, load will quit right away
    document.addEventListener('load', function( ){ window.load_function( ); }, false);

    window.onload = initDesignHacks;
}
else if(document.attachEvent){
    document.attachEvent('onreadystatechange', function( ){ window.load_function( ); });

    window.onload = initDesignHacks;
}
else if(window.onload){
    window.onload = function( ){ window.load_function( ); initDesignHacks( ); }
}

function init( ){
    if(!document.getElementsByTagName || window.already_loaded){
        return;//if it doesn't support this, it doesn't support a whole lot.
    }

    if(ajaxEnabled){
        ajaxCheck( );
    }

    //initDesignHacks( );
    initTopMenuLinks( );
    initInputButtons( );
    initA( );
    initObjects( );

    window.already_loaded = true;
}

function initDesignHacks( ){
    if(window.ActiveXObject || window.opera){
        var mb = document.getElementById('menubar');
        mb.style.padding = "0px 5px 0px 5px";
        var div = document.createElement('div');
        div.className = 'mbleft';
        mb.appendChild(div);

        var div = document.createElement('div');
        div.className = 'mbright';
        mb.appendChild(div);
    }

    //turn this next section into a loop, looking for divs with className of content or contentBlock
    var divs = document.getElementsByTagName('div');

    for(i = 0; i < divs.length; i++){
        var c_div = divs[i];
        if(c_div.className == 'content' || c_div.className == 'c_block'){
            var div = document.createElement('div');
            div.className = 'content_transparent';

            var c_width = 0;
            var c_height = 0;

            //alert(c_div.scrollHeight);
            //alert(c_div.clientHeight);

            if(c_div.clientWidth){
                c_width = c_div.clientWidth;
                c_height = c_div.clientHeight;
            }
            else if(c_div.offsetWidth){
                c_width = c_div.offsetWidth;
                c_height = c_div.offsetHeight;
            }

            if(c_div.className == 'content' && window.isOldIE){
                c_width = c_width - 10;
            }

            div.style.width = c_width + 'px';
            div.style.height = c_height + 'px';

            c_div.appendChild(div);
        }
    }
}

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"){
                /* not quite standardized yet, so apply them with javascript */
                inputs[i].style.borderRadius = '6px';
                inputs[i].style.MozBorderRadius = '6px';
                inputs[i].style.WebkitBorderRadius = '6px';
            }
            else if(window.ActiveXObject && (inputs[i].type == 'checkbox' || inputs[i].type == 'radio')){
                inputs[i].style.border = '0px solid #ffffff';
                inputs[i].style.backgroundColor = 'transparent';
            }
        }
    }
}

//val = the block number, mode = (1|0 -> open|closed)
function setBlockControls(val, mode){
    var bk_cookie = getCookie('bk_cookie');

    if(!bk_cookie){
        setCookie('bk_cookie', '');
        bk_cookie = '';
    }


    var bkA = bk_cookie.split(/\,/);

    var found = false;
    var newCookieStr = '';

    for(i = 0; i < bkA.length; i++){
        var bk = bkA[i];

        if(bk == val){
            found = true;

            if(mode == 0){
                continue;
            }
        }

        if(newCookieStr != ''){
            newCookieStr += ',';
        }
        newCookieStr += bk;
    }

    if(!found && mode == 1){
        if(newCookieStr != ''){
            newCookieStr += ',';
        }

        newCookieStr += val;
    }

    setCookie('bk_cookie', newCookieStr);
}

function initA( ){
    if(document.getElementsByTagName){
        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 || as[i].href.match(/\/ad\.\php\?/)){
                if(!as[i].target){
                    as[i].target = "_blank";
                    continue;
                }
            }

            if(ajaxEnabled){
                if(b_xml){
                    as[i].onclick = function( ){
                        var newHREF = this.href;

                        if(newHREF.match(/\?/)){
                            newHREF += "&xmlOnly=1";
                        }
                        else{
                            newHREF += "?xmlOnly=1";
                        }

                        if(newHREF == navHistory[curNavPoint]){
                            return false;
                        }

                        for(var i = navHistory.length - 1; i > curNavPoint; i--){
                            navHistory.pop( );
                        }

                        curNavPoint += 1;
                        navHistory.push(newHREF);

                        b_xml.request(newHREF);

                        if(b_xml.isSupported){
                            return false;
                        }
                        return true;
                    }
                }
            }
        }
    }
}

function initObjects( ){
    if(document.getElementsByTagName){
        var objects = document.getElementsByTagName("div");
        var len = objects.length;

        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="http://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="http://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="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" ';
                        }
                        else{
                            embedStr += ' pluginspage="http://www.apple.com/quicktime/download/" ';
                            objStr += ' classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" ';
                            objStr += ' codebase="http://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';
                        }

                        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';
                //alert(obj.innerHTML);
                obj.style.display = 'block';
            }//end if bgzobject
        }
    }
}

//Cool input effects
function inputOver(e){
    this.className = "buttonOver";
}

function inputOut(e){
    this.className = "button";
}

//Popups
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");
    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 imageSelectPopUp2(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 fileSelectPopUp(pwinvar){
    imgSelectPopUp = window.open("./fileselect.php?mode=1&fieldID=" + pwinvar, "FileSelector", "width=780,height=480, scrollbars=0, status=no,toolbar=no,resizable=no");
    imgSelectPopUp.focus( );
}

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

//drop down menus ##################################################


var CurMenu = null;
var isDropped = false;
var isHideCheck = false;
var subCurMenu = null;
var subIsDropped = false;
var subIsHideCheck = false;

function initTopMenuLinks( ){
    var tmenu = document.getElementById('tmenu');

    if(!tmenu){
        return;
    }

    var tdA = tmenu.getElementsByTagName('td')
    var len = tdA.length;

    for(var i = 0; i < len; i++){
        var td = tdA[i];
        if(bgzDDEnabled){
            td.bgzdd = new bgzDropDown( );
            var ul = td.getElementsByTagName('ul')[0];
            if(ul){
                td.bgzdd.init(td, ul, 'down');
            }
        }
        else{
            td.onmouseover = pageON;
            td.onmouseout = pageOFF;
        }
    }

    var liA = tmenu.getElementsByTagName('li')
    var len = liA.length;

    for(var i = 0; i < len; i++){
        var li = liA[i];
        if(bgzDDEnabled){
            li.bgzdd = new bgzDropDown( );
            var ul = li.getElementsByTagName('ul')[0];
            if(ul){
                li.bgzdd.init(li, ul, 'right');
            }
        }
        else{
            li.onmouseover = pageON;
            li.onmouseout = pageOFF;
        }
    }
}

function pageON(index){
    var uls = this.getElementsByTagName('ul');
    if(uls.length > 0){
        show(uls[0]);
    }

}
function pageOFF(index){
    if(document.getElementById){
        if(!isHideCheck) hideMenu( );
    }
}
function show(menu){
    if(CurMenu)
        CurMenu.style.display = "none";
    menu.style.display = "block";
    isDropped = true;
    CurMenu = menu;
}
function hideMenu( ){
    if(CurMenu){
        isDropped = false;
        isHideCheck = true;
        setTimeout('checkDropped( );', 500);
        isHideCheck = false;
    }
}
function checkDropped( ){
    if(!isDropped && CurMenu != null) CurMenu.style.display = "none";
}
//############################################################

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();
}

var ipreviewWin = null;
function ipreview(lnk){
    ipreviewWin = window.open(lnk, "ipreviewWin", "width=780,height=480, menubar=yes, location=yes, scrollbars=yes, status=yes,toolbar=yes,resizable=yes");
    ipreviewWin.focus( );
    return false;
}

//category function
function lCategoryPopup(pwinvar){
    lCatPopUp = window.open("/include/category.php?lckey=" + pwinvar, "CategorySelect", "width=780,height=480, scrollbars=yes, status=yes,toolbar=no,resizable=yes");
    lCatPopUp.focus( );
}
function lCategoryPopupA(pwinvar){
    lCatPopUp = window.open("/include/category2.php?lckey=" + pwinvar, "CategorySelect", "width=780,height=480, scrollbars=yes, status=yes,toolbar=no,resizable=yes");
    lCatPopUp.focus( );
}
function lClassifiedPopup(pwinvar){
    lCatPopUp = window.open("/include/classified.php?lckey=" + pwinvar, "ClassifiedSelect", "width=780,height=480, scrollbars=yes, status=yes,toolbar=no,resizable=yes");
    lCatPopUp.focus( );
}
function wsbPopup(loc){
    lCatPopUp = window.open(loc, "wsb", "width=782,height=480, menubar=yes, location=yes, scrollbars=yes, status=yes,toolbar=yes,resizable=yes");
    lCatPopUp.focus( );
}

function whatisPopup(loc){
    whatisPopUp = window.open(loc, "whatis", "width=300,height=300, menubar=no, location=no, scrollbars=yes, status=no,toolbar=no,resizable=yes");
    whatisPopUp.focus( );
}

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

function setCookie(cookieName,cookieValue){
    var dateNow = new Date( );
    var expire = new Date();
    expire.setTime(dateNow.getTime() + 7200000);
    document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString( ) + ";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 convert2RGB(color) {
    var rgb1 = parseInt(color.substr(0, 2), 16);
    var rgb2 = parseInt(color.substr(2, 2), 16);
    var rgb3 = parseInt(color.substr(4, 2), 16);

    return "rgb(" + rgb1 + ", " + rgb2 + ", " + rgb3 + ")";
}

function addslashes(str){
    str = str.replace(/\'/g, '\\\'');
    str = str.replace(/\"/g, '\\"');
    str = str.replace(/\\/g, '\\\\');
    str = str.replace(/\0/g, '\\0');
    return str;
}

function stripslashes(str){
    str=str.replace(/\\'/g, '\'');
    str=str.replace(/\\"/g, '"');
    str=str.replace(/\\\\/g, '\\');
    str=str.replace(/\\0/g, '\0');
    return str;
}

function refreshSImg(sImg){
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomStr = '';
	for (var i = 0; i < string_length; i++) {
		var rnum = Math.floor(Math.random( ) * chars.length);
		randomStr += chars.substring(rnum, rnum+1);
	}

	var sImgSrc = sImg.src;
	if(sImgSrc.match('&')){
	    sImgSrcA = sImgSrc.split('&');
	    sImgSrc = sImgSrcA[0];
	}

    sImg.src = sImgSrc + '&' + randomStr;
}

function setCalCheckinValues(y, m, d) {
    document.forms[0].checkin_y.value = y;
    document.forms[0].checkin_m.value = LZ(m);
    document.forms[0].checkin_d.value = LZ(d);
}

function setCalCheckoutValues(y, m, d) {
    document.forms[0].checkout_y.value = y;
    document.forms[0].checkout_m.value = LZ(m);
    document.forms[0].checkout_d.value = LZ(d);
}

//http://www.actionscript.org/forums/showthread.php3?t=132324
function makeWindowed(div){
    var p_div = document.getElementById(div);
    if(window.isOldIE && (navigator.userAgent.toLowerCase().indexOf("msie 6.") != -1)){
        var html = "<iframe style=\"filter: Alpha(Opacity=60, FinishOpacity=60, Style=2); background-color: #000000; position: absolute; display: inline; z-index: -1; width: 100%; height: 100%; top: 0; left: 0; filter: mask( );\"></iframe>";

        if(p_div){
            p_div.innerHTML += html;
        }

        //force refresh of div
        var olddisplay = p_div.style.display;
        p_div.style.display = 'none';
        p_div.style.display = olddisplay;
    }
}

//******************
// FLash Functions *
//******************

//Requires javascript dom support, gracefully fails otherwise
//Loads flash movie in the specified location

//not done yet.
function loadFlashDocument(movieLocation){

}