/* ##########################################################################
    Copyright 2005 BBB Systems, LLC, All rights reserved

    We don't mind if you learn from our scripts, or even use portions of
    them, but please be tactful and ethical.  If you find our scripts
    useful, let us know, we would like to hear from you.
    (www.bbbsystems.net has a contact form)

    AJAX Note:
    We got much of our information for fixing the broken back button here:
    http://www.contentwithstyle.co.uk/Articles/38/

########################################################################## */

/*
var plusImg = new Image( );
plusImg.src = "/images/plus.gif";
var minusImg = new Image( );
minusImg.src = "/images/minus.gif";
*/
var sGlobals = new Object( );
sGlobals.bScrollAmount = 20;
sGlobals.slowScroll = 75;
sGlobals.fastScroll = 25;
sGlobals.isScrollingR = false;
sGlobals.isScrollingL = false;

var navHistory = new Array( );
var curNavPoint = -1;

var globals = new Array( );

var currentLocation = '';

var recursionCount = 0;//this is so we can better test recursive funtions.

var fieldLen = 0;//personalizedProduct

//detect safari
//In my opinion, safari isn't quit up to speed with the other modern browsers,
//although I hate to have safari hacks, I think it's a necessity in this
//application.  Hopefully they will patch most of their bugs in the future.
var is_safari = (document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)&&(!navigator.accentColorName)?true:false;

if(imgRotatorThinking == undefined){
    var imgRotatorThinking = false;
}

window.js_initialized = false;

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);
}
else if(document.attachEvent){
    document.attachEvent('onreadystatechange', function( ){ window.load_function( ); });
}
else if(window.onload){
    window.onload = function( ){ window.load_function( ); }
}

if(window.load_maps && window.onunload){
    window.onunload = function( ){ GUnload( ); }
}

function init( ){
    if(window.js_initialized){ return; }
    window.js_initialized = true;

    if(!document.getElementsByTagName){
        return;//if it doesn't support this, it doesn't support a whole lot.
    }
    //force ie < 6 to fail (we can't support ie5 forever)
    //document.getElementsByTagName("a")[0].style.cursor = 'pointer';

    if(window.ajaxEnabled){
        ajaxCheck( );
    }
    else{
        window.ajaxEnabled = false;
        //window.b_xml = false;
    }
    if(window.limitedAjax != true){
        window.limitedAjax = false;
    }

    initTopMenuLinks( );
    initInputButtons( );
    initA( );
    initObjects( );
    appThink( );
    updateCardImg( );
    initImgRotator( );

    initProducts( );
}

function xmlInit( ){
    initA( );
    initInputButtons( );
    initObjects( );
    initImgRotator( );

    initProducts( );
}


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;
            }
            else if(window.ActiveXObject && (inputs[i].type == 'checkbox' || inputs[i].type == 'radio')){
                inputs[i].style.border = '0px solid #ffffff';
                inputs[i].style.backgroundColor = 'transparent';
            }
        }
    }
}

function initA( ){
    if(document.getElementsByTagName){
        var as = document.getElementsByTagName("a");
        for(var i = 0; i < as.length; i++){

            if(as[i].parentNode.id != ''){
                if(as[i].parentNode.id == 'bLeftArrow' || as[i].parentNode.id == 'bRightArrow'){
                    as[i].onclick = function( ){
                        return false;
                    }
                    continue;
                }
            }

            var hostnm = as[i].href.replace(/http(s)?\:\/\//, "");
            hostnm = hostnm.replace(/\/.*/, "");

            if((hostnm != window.location.hostname && hostnm != '') || as[i].href.match(/\/ad\.\php\?/)){
                as[i].target = "_blank";
                continue;
            }

            if(ajaxEnabled && as[i].className != "noAJAX" && !as[i].href.match(/\/members\//) && !as[i].href.match(/feed\.php/)){
                if(b_xml){
                    if( as[i].href.match(/(\#)$/) ){
                        continue;
                    }

                    if(as[i].href.match(/\#\w+/)){
                        continue;
                    }

                    as[i].onclick = function( ){
                        if(this.href.match(/\.pdf/)){
                            pdfPopup = window.open(this.href, "PDF");
                            pdfPopup.focus( );
                            return false;
                        }

                        //alert(b_xml.isCurrentRequest);
                        if(b_xml.isCurrentRequest){
                            return false;
                        }


                        /*var curWinLoc = window.location;
                        var curLoc = '';
                        if(window.location.match(/\#\w+/)){
                            alert(curWinLoc);
                            var curLocA = curWinLoc.split("#");
                            if(curLocA.length > 1){
                                curLoc = curLocA[1];
                            }
                            alert(curLoc);
                        }*/

                        //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 tHREF = window.location.href.replace(/^(http(s)?\:\/\/)([^\/]+)/, '');
                        //if(tHREF.match(/^\/\#/)){
                            /*if(this.href != window.location.href){
                                imgRotatorThinking = false;
                            }*/
                            imgRotatorThinking = false;
                            b_xml.request(newHREF);
                        //}

                        if(b_xml.isSupported){
                            return true;
                        }

                        return true;
                    }
                }
            }
        }//end for a

        if(ajaxEnabled){
            if(!b_xml){
                return;
            }

            var productsBlockTabs = document.getElementById('productsBlockTabs');
            if(productsBlockTabs){
                i = 0;
                as = productsBlockTabs.getElementsByTagName("a");
                for(i = 0; i < as.length; i++){
                    as[i].onclick = function( ){
                        if(b_xml.isCurrentRequest){ return false; }
                        //ul = a->li->ul (this->parent->parent)
                        var ul = this.parentNode.parentNode;
                        var p_as = ul.getElementsByTagName('a');

                        for(var j = 0; j < p_as.length; j++){ p_as[j].className = ''; }
                        this.className = 'cur';
                        var newHREF = this.href;
                        newHREF = newHREF.replace(/(\#.*)$/, '');
                        newHREF = newHREF.replace(/(\/content\/)/, '/ajax.php/');
                        newHREF = newHREF + '/type/prodtabs';
                        //alert(newHREF);
                        b_xml.request(newHREF);
                        if(b_xml.isSupported){ return false; }
                        return true;
                    }//end onclick
                }//end foreach a in productsBlockTabs
            }

            var prodContentTabs = document.getElementById('prodContentTabs');
            if(prodContentTabs){
                i = 0;
                as = prodContentTabs.getElementsByTagName("a");
                for(i = 0; i < as.length; i++){
                    as[i].onclick = function( ){
                        if(b_xml.isCurrentRequest){ return false; }
                        //ul = a->li->ul (this->parent->parent)
                        var ul = this.parentNode.parentNode;
                        var p_as = ul.getElementsByTagName('a');

                        for(var j = 0; j < p_as.length; j++){ p_as[j].className = ''; }
                        this.className = 'cur';
                        var newHREF = this.href;
                        newHREF = newHREF.replace(/(\#.*)$/, '');
                        newHREF = newHREF.replace(/(\/content\/)/, '/ajax.php/');
                        newHREF = newHREF + '/type/pctabs';
                        //alert(newHREF);
                        b_xml.request(newHREF);
                        if(b_xml.isSupported){ return false; }
                        return true;
                    }//end onclick
                }//end foreach a in prodContentTabs
            }
        }
    }
    var bbb = document.getElementById('bbb');
    if(bbb){
        bbb.innerHTML = '<a href="http://www.bbbsystems.com/e-commerce-cms.html" target="_blank">E-Commerce Website Development: BBB Systems, LLC</a>';
    }
}

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 initImgRotator( ){
    var image_table = getGalleryImageTable( );
    var display_image = getGalleryMainImage( );
    if(!image_table || !display_image){ return; }

    display_image.style.opacity = 1;

    var imageA = image_table.getElementsByTagName('img');

    //preload the gallery images for smoother transitions
    window.galleryImages = new Array( );

    j = 0;
    for(i = 0; i < imageA.length; i++){
        var img = imageA[i];

        if(!img.id || img.id == '' || img.id == display_image.id){
            continue;
        }

        var d_id = 'gallery_image_div_' + j;
        var next_id = 'gallery_image_div_' + parseInt(j + 1);
        if(i == imageA.length - 1){
            next_id = 'gallery_image_div_0';
        }

        img.parentNode.onmouseover = galleryImageHover;
        img.parentNode.onmouseout = galleryImageOut;
        img.parentNode.onclick = galleryImageClick;

        img.parentNode.id = d_id;
        img.parentNode.next_id = next_id;

        //preload the image
        window.galleryImages[d_id] = new Image( );
        window.galleryImages[d_id].src = img.src.replace(/\_t(\.\w+)$/, "$1");
        window.galleryImages[d_id].setAttribute('alt', img.getAttribute('alt'));

        //set width and height of preloaded images
        var img_width = img.width;
        var img_height = img.height;
        if(window.galleryImages[d_id].width > 0 && window.galleryImages[d_id].height > 0){
            img_width = window.galleryImages[d_id].width;
            img_height = window.galleryImages[d_id].height;

            if(img_width > 500){
                var scale = display_image.width / img_width;

                img_height = img_height * scale;
                img_width = display_image.width;
            }
        }
        else{
            var scale = display_image.width / img_width;

            img_height = img_height * scale;
            img_width = display_image.width;
        }

        if(img_width != ''){
            img_width = img_width + 'px';
        }
        if(img_height != ''){
            img_height = img_height + 'px';
        }

        window.galleryImages[d_id].width_px = img_width;
        window.galleryImages[d_id].height_px = img_height;

        if(i == 0){
            window.galleryCurImageDiv = d_id;
            window.galleryCurImage = window.galleryImages[d_id].src;
            window.galleryCurImageWidth = window.galleryImages[d_id].width_px;
            window.galleryCurImageHeight = window.galleryImages[d_id].height_px;
            window.galleryCurComment = window.galleryImages[d_id].getAttribute('alt');
        }
        j++;
    }

    var scale_time = 10000;
    if(image_table.id == 'home_img_gallery_tbl'){
        scale_time = 4000;
    }

    setGalleryNextRotation(scale_time);

    //imageRotatorThink( );//call this in appThink
}
function setGalleryNextRotation(t_offset){
    var dt = new Date( );
    window.galleryNextRotation = parseInt(dt.getTime( ) + t_offset);
    dt = null;
}
function getGalleryImageTable( ){
    var image_table = document.getElementById('prod_gallery_thumbs');
    return image_table;
}
function getGalleryMainImage( ){
    var display_image = document.getElementById('prod_main_img');
    return display_image;
}
function getGalleryMainCaption( ){
    var display_caption = document.getElementById('prod_img_caption');
    return display_caption;
}

function gallerySwapTransition( ){
    var display_image = getGalleryMainImage( );
    var display_caption = getGalleryMainCaption( );
    var image_table = getGalleryImageTable( );

    if(!display_image || !image_table){
        return;
    }

    var a_amt = .10;
    if(window.ActiveXObject){
        a_amt = .15;
    }

    var cur_alpha = (display_image.style.opacity * 100)/100;
    var alpha = cur_alpha + (window.galleryCurDir * a_amt);

    if(alpha <= 0){
        alpha = 0;
        window.galleryCurDir = 1;
        display_image.src = window.galleryCurImage;
        if(window.galleryCurImageWidth != '' && window.galleryCurImageHeight != ''){
            display_image.style.width = window.galleryCurImageWidth;
            display_image.style.height = window.galleryCurImageHeight;
        }
        display_caption.innerHTML = window.galleryCurComment;

        var host_loc = window.location.hostname;
        var img_rel_loc = display_image.src.replace('http://' + host_loc, '');
        img_rel_loc = img_rel_loc.replace(/uimages\//, '');

        var image_link = document.getElementById('prod_img_link');
        var image_enlarge_link = document.getElementById('prod_img_enlarge');

        if(image_link != null){
            var new_img_link = image_link.href.replace(/(pimg=.*)$/, 'pimg=' + img_rel_loc);

            image_link.href = new_img_link;
            if(image_enlarge_link != null){
                image_enlarge_link.href = new_img_link;
            }
        }
    }
    if(alpha >= 1){
        alpha = 1;
    }

    display_image.style.opacity = alpha;
    if(window.ActiveXObject){
        var ie_alpha = alpha * 100;
        display_image.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + ie_alpha + ")";
    }

    if(alpha != 1){
        setTimeout('gallerySwapTransition( );', 50);
    }
}
function gallerySwapImage(imgObject){
    var display_image = getGalleryMainImage( );
    var image_table = getGalleryImageTable( );

    if(!display_image || !image_table){
        return;
    }

    window.galleryCurImageDiv = imgObject.parentNode.id;
    window.galleryCurImage = window.galleryImages[imgObject.parentNode.id].src;
    window.galleryCurImageWidth = window.galleryImages[imgObject.parentNode.id].width_px;
    window.galleryCurImageHeight = window.galleryImages[imgObject.parentNode.id].height_px;
    window.galleryCurComment = window.galleryImages[imgObject.parentNode.id].getAttribute('alt');
    window.galleryCurDir = -1;
    var divs = image_table.getElementsByTagName('div');

    for(i = 0; i < divs.length; i++){
        var div = divs[i];
        div.className = '';
    }

    imgObject.parentNode.className = 'cur';
    imgObject.parentNode.classNameBak = 'cur';

    if(display_image.style.opacity == 1){
        gallerySwapTransition( );
    }
}
function galleryImageClick( ){
    //make it long on the gallery pages.
    var scale_time = 20000;
    setGalleryNextRotation(scale_time);
    gallerySwapImage(this.getElementsByTagName('img')[0]);
}
function galleryImageHover( ){
    this.classNameBak = this.className;
    this.className = 'over';
}
function galleryImageOut( ){
    this.className = this.classNameBak;
}
function imageGalleryThink( ){
    var display_image = getGalleryMainImage( );
    var image_table = getGalleryImageTable( );

    if(!display_image || !image_table){
        return;
    }

    dt = new Date( );
    if(dt.getTime( ) >= window.galleryNextRotation){
        var scale_time = 10000;
        if(image_table.id == 'home_img_gallery_tbl'){
            scale_time = 4000;
        }
        setGalleryNextRotation(scale_time);
        var div = document.getElementById(window.galleryCurImageDiv);
        var next_div = document.getElementById(div.next_id);
        var img = next_div.getElementsByTagName('img')[0];
        gallerySwapImage(img);
    }
}


function appThink( ){

    //imageGalleryThink( );

//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;
                    }
                }
            }

            currentLocation = newHREF;
            //if(recursionCount < 5){
            //    alert(newHREF + ' - ' + currentLocation + ' - ' + windowHREF);
            //    recursionCount++;
            //}


            if(newHREF.match(/\?/)){
                newHREF += "&xmlOnly=1";
            }
            else{
                newHREF += "?xmlOnly=1";
            }
            b_xml.request(newHREF);
        }
    }
}//End if ajaxEnabled

    //make the program think every 10th of a second
    setTimeout('appThink( )', 100);
}

//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,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=680, 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=580, scrollbars=0, status=no,toolbar=no,resizable=no");
    imgSelectPopUp.focus( );
}

function openFUploader( ){
    var imgSelectPopUp = window.open("./fileselect.php", "FileSelector", "width=780,height=580, 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 hmenu = document.getElementById('tmenu');
    if(!hmenu){ return; }

    var liA = hmenu.getElementsByTagName('td')
    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, 'down');
            }
        }
        else{
            li.onmouseover = pageON;
            li.onmouseout = pageOFF;
        }
    }
}
function initLeftMenuLinks( ){
    var dleftmenu = document.getElementById('dleftmenu');
    var liA = dleftmenu.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.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 productSelect(pwinvar){
    productPopUp = window.open("./product_select.php?lckey=" + pwinvar, "ProductSelect", "width=780,height=480, scrollbars=yes, status=yes,toolbar=no,resizable=yes");
    productPopUp.focus( );
}
function categoryPopup(pwinvar){
    catPopUp = window.open("./category_select.php?lckey=" + pwinvar, "CategorySelect", "width=780,height=480, scrollbars=yes, status=yes,toolbar=no,resizable=yes");
    catPopUp.focus( );
}
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){
    //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 add2Cart(thisA, pid, qty){
    if(b_xml.isCurrentRequest){ return false; }

    //alert('/ajax.php/pid/' + pid + '/qty/' + qty + '/ctype/p/type/add2cart');
    b_xml.request('/ajax.php/pid/' + pid + '/qty/' + qty + '/ctype/p/type/add2cart');
    if(b_xml.isSupported){ return false; }
}

function removeFromCart(thisA, ckey){
    if(b_xml.isCurrentRequest){ return false; }

    var cartItems = getCookie('cartItems');

    //alert('/ajax.php/remove/1/ckey/' + ckey + '/type/remove/items/' + cartItems);
    b_xml.request('/ajax.php/remove/1/ckey/' + ckey + '/type/remove/items/' + cartItems);
    if(b_xml.isSupported){ return false; }
}

function updatePrice(priceID, newTotal){
    updateID = document.getElementById(priceID);

    if(!updateID){
        return;
    }

    var newTotal = parseFloat(newTotal);
    updateID.innerHTML = '$' + newTotal.toFixed(2);
}

function copyShipping(){
    var thisForm = document.getElementById('infoForm');
    var chkbox = document.getElementById('sameAsShipping');

    if(!chkbox){
        return;
    }

    if(chkbox.checked){
        //populate/update billing fields
        var sStreet1 = document.getElementById('sStreet1');
        var sStreet2 = document.getElementById('sStreet2');
        var sCity = document.getElementById('sCity');
        var sState = document.getElementById('sState');
        var sZipCode = document.getElementById('sZipCode');
        var sPhoneNumber = document.getElementById('sPhoneNumber');

        var bStreet1 = document.getElementById('bStreet1');
        var bStreet2 = document.getElementById('bStreet2');
        var bCity = document.getElementById('bCity');
        var bState = document.getElementById('bState');
        var bZipCode = document.getElementById('bZipCode');
        var bPhoneNumber = document.getElementById('bPhoneNumber');

        bStreet1.value = sStreet1.value;
        bStreet2.value = sStreet2.value;
        bCity.value = sCity.value;
        bState.value = sState.value;
        bZipCode.value = sZipCode.value;
        bPhoneNumber.value = sPhoneNumber.value;
    }
    else{
        var bStreet1 = document.getElementById('bStreet1');
        var bStreet2 = document.getElementById('bStreet2');
        var bCity = document.getElementById('bCity');
        var bState = document.getElementById('bState');
        var bZipCode = document.getElementById('bZipCode');
        var bPhoneNumber = document.getElementById('bPhoneNumber');

        //clear billing fields
        bStreet1.value = '';
        bStreet2.value = '';
        bCity.value = '';
        bState.value = '';
        bZipCode.value = '';
        bPhoneNumber.value = '';
    }
}

function updateBilling(srcVal, dest){
    var destId = document.getElementById(dest);
    var chkbox = document.getElementById('sameAsShipping');

    if(!chkbox){
        return;
    }

    if(chkbox.checked){
        destId.value = srcVal;
    }
}

function fillShipping(newVal){
    var chkbox = document.getElementById('sameAsShipping');

    if(newVal == 'new address'){
        document.getElementById('shippingName').value = '';
        document.getElementById('sFirstName').value = '';
        document.getElementById('sMiddleInitial').value = '';
        document.getElementById('sLastName').value = '';
        document.getElementById('sCompanyName').value = '';
        document.getElementById('sStreet1').value = '';
        document.getElementById('sStreet2').value = '';
        document.getElementById('sCity').value = '';
        document.getElementById('sState').value = '';
        document.getElementById('sZipCode').value = '';
        document.getElementById('sPhoneNumber').value = '';
        document.getElementById('sComments').value = '';

        if(chkbox.checked){
            document.getElementById('bStreet1').value = '';
            document.getElementById('bStreet2').value = '';
            document.getElementById('bCity').value = '';
            document.getElementById('bState').value = '';
            document.getElementById('bZipCode').value = '';
            document.getElementById('bPhoneNumber').value = '';
        }
    }
    else{
        if(!shipA[newVal]){
            return;
        }

        var thisShipping = shipA[newVal];

        document.getElementById('shippingName').value = thisShipping.shippingName;
        document.getElementById('sFirstName').value = thisShipping.sFirstName;
        document.getElementById('sMiddleInitial').value = thisShipping.sMiddleInitial;
        document.getElementById('sLastName').value = thisShipping.sLastName;
        document.getElementById('sCompanyName').value = thisShipping.sCompanyName;
        document.getElementById('sStreet1').value = thisShipping.sStreet1;
        document.getElementById('sStreet2').value = thisShipping.sStreet2;
        document.getElementById('sCity').value = thisShipping.sCity;
        document.getElementById('sState').value = thisShipping.sState;
        document.getElementById('sZipCode').value = thisShipping.sZipCode;
        document.getElementById('sPhoneNumber').value = thisShipping.sPhoneNumber;
        document.getElementById('sComments').value = thisShipping.sComments;

        if(chkbox.checked){
            document.getElementById('bStreet1').value = thisShipping.sStreet1;
            document.getElementById('bStreet2').value = thisShipping.sStreet2;
            document.getElementById('bCity').value = thisShipping.sCity;
            document.getElementById('bState').value = thisShipping.sState;
            document.getElementById('bZipCode').value = thisShipping.sZipCode;
            document.getElementById('bPhoneNumber').value = thisShipping.sPhoneNumber;
        }
    }
}

function fillBilling(newVal){
    var chkbox = document.getElementById('sameAsShipping');

    if(newVal == 'new card'){
        document.getElementById('billingName').value = '';
        document.getElementById('CCName').value = '';
        document.getElementById('CCNum').value = '';
        document.getElementById('expMonth').value = '';
        document.getElementById('expYear').value = '';

        if(!chkbox.checked){
            document.getElementById('bStreet1').value = '';
            document.getElementById('bStreet2').value = '';
            document.getElementById('bCity').value = '';
            document.getElementById('bState').value = '';
            document.getElementById('bZipCode').value = '';
            document.getElementById('bPhoneNumber').value = '';
        }

        updateCardImg( );
    }
    else{
        if(!billA[newVal]){
            return;
        }

        var thisBilling = billA[newVal];

        document.getElementById('billingName').value = thisBilling.billingName;
        document.getElementById('CCName').value = thisBilling.CCName;
        document.getElementById('CCNum').value = thisBilling.CCNum;
        document.getElementById('expMonth').value = thisBilling.expMonth;
        document.getElementById('expYear').value = thisBilling.expYear;

        if(!chkbox.checked){
            document.getElementById('bStreet1').value = thisBilling.bStreet1;
            document.getElementById('bStreet2').value = thisBilling.bStreet2;
            document.getElementById('bCity').value = thisBilling.bCity;
            document.getElementById('bState').value = thisBilling.bState;
            document.getElementById('bZipCode').value = thisBilling.bZipCode;
            document.getElementById('bPhoneNumber').value = thisBilling.bPhoneNumber;
        }

        updateCardImg( );
    }
}

function updateCardImg( ){
    var ccField = document.getElementById('CCNum');

    if(!ccField){
        return;
    }

    //add jcb and diners if and when supported
    var discover = document.getElementById('discCC');
    var mastercard = document.getElementById('masterCC');
    var visa = document.getElementById('visaCC');

    //does not accept amex
    //var amex = document.getElementById('amexCC');

    discover.src = "/images/discover_gray.gif";
    mastercard.src = "/images/mastercard_gray.gif";
    visa.src = "/images/visa_gray.gif";

    //does not accept amex
    //amex.src = "/images/amex_gray.gif";

    if(ccField.value == ''){
        return;
    }

    var ccnum = ccField.value;

    ccLen = ccnum.length;
    ccnum = ccnum.replace(/\*/g, '');
    //we have to base it off of the first digit and length because we don't know the rest of the #s
    if(ccnum.substr(0, 1) == 6 && ccLen == 16){
        discover.src = '/images/discover.gif';
    }
    else if(ccnum.substr(0, 1) == 5 && ccLen == 16){
        mastercard.src = '/images/mastercard.gif';
    }
    else if(ccnum.substr(0, 1) == 4 && (ccLen == 16 || ccLen == 13)){
        visa.src = '/images/visa.gif';
    }
    else if(ccnum.substr(0, 1) == 3 && ccLen == 15){
        //amex.src = '/images/amex.gif';
    }
    else if((ccnum.substr(0, 1) == 3 && ccLen == 16) || (ccLen == 15 && (ccnum.substr(0, 4) == 2131 || ccnum.substr(0, 4) == 1800))){
        //jcb
    }
    else if(ccLen == 14 && ccnum.substr(0, 3) >= 300 && ccnum.substr(0, 3) <= 305){
        //diners 1
    }
    else if(ccLen == 14 && (ccnum.substr(0, 2) == 36 || ccnum.substr(0, 2) == 38)){
        //diners 2
    }


   /*
    if(ccnum.substr(0, 4) == 6011 && ccLen == 16){
        discover.src = '/images/discover.gif';
    }
    else if(ccnum.substr(0, 2)  && ccLen == 16){
        mastercard.src = '/images/mastercard.gif';
    }
    else if(ccnum.substr(0, 1) == 4 && (ccLen == 16 || ccLen == 13)){
        visa.src = '/images/visa.gif';
    }
    else if(ccnum.substr(0, 1) == 3 && ccLen == 15){
        amex.src = '/images/amex.gif';
    }
    else if((ccnum.substr(0, 1) == 3 && ccLen == 16) || (ccLen == 15 && (ccnum.substr(0, 4) == 2131 || ccnum.substr(0, 4) == 1800))){
        //jcb
    }
    else if(ccLen == 14 && ccnum.substr(0, 3) >= 300 && ccnum.substr(0, 3) <= 305){
        //diners 1
    }
    else if(ccLen == 14 && (ccnum.substr(0, 2) == 36 || ccnum.substr(0, 2) == 38)){
        //diners 2
    }
   */
}

function clearBilling( ){
    document.getElementById('savedBilling').value = 'new card';
    document.getElementById('billingName').value = '';

    var ccnum = document.getElementById('CCNum');
    if(ccnum.value.match(/\*/)){
        ccnum.value = '';
    }
}

function clearShipping( ){
    document.getElementById('savedShipping').value = 'new address';
    document.getElementById('shippingName').value = '';
}

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 getColor(fieldID){
    winTool = window.open("./colorselect.php?fieldID=" + fieldID, "ColorTool", "width=256,height=512,status=no,toolbar=no,resizable=no,alwaysRaised=yes");
    winTool.focus();
}

function initProducts( ){
    var opt_table = getProductOptionsTable( );
    if(!opt_table){ return; }

    var labelA = opt_table.getElementsByTagName('label');
    var label_len = labelA.length;
    for(var i = 0; i < label_len; i++){
        t_label = labelA[i];

        t_label.onclick = productOptionSelect;

        //if(window.isOldIE){
            t_label.oldClassName = t_label.className;
            t_label.onmouseover = function( ){ this.className = this.className + 'Over'; }
            t_label.onmouseout = function( ){ this.className = this.oldClassName; }
        //}
    }

    var radioA = opt_table.getElementsByTagName('input');
    var radio_len = radioA.length;
    for(var i = 0; i < radio_len; i++){
        t_radio = radioA[i];
        if(t_radio.type != 'radio'){ continue; }

        t_radio.onclick = productOptionSelect;
    }
}

function getProductOptionsTable( ){
    var t_element = document.getElementById('prod_option_table');
    if(!t_element){
        return false;
    }

    return t_element;
}

function productOptionSelect( ){
    var opt_table = getProductOptionsTable( );
    if(!opt_table){ return; }

    var radioA = opt_table.getElementsByTagName('input');
    var radio_len = radioA.length;
    for(var i = 0; i < radio_len; i++){
        t_radio = radioA[i];

        if(t_radio.type != 'radio'){ continue; }
        else if(t_radio != this){ t_radio.checked = ''; }
    }
}

function getProdOptions(addtl_url){
    var p_opt_obj = document.getElementById('p_option_select');
    if(!p_opt_obj){ return; }

    addtl_url_str = '';
    if(addtl_url && addtl_url != ''){
        addtl_url_str = addtl_url;
    }

    b_xml.request('/ajax.php/type/po_select/poid/' + p_opt_obj.value + addtl_url_str);
}