function ChangeProductGroup(id)
{
    var select_box = document.getElementById('item_group_select-'+id);
    var group_id   = select_box.options[select_box.selectedIndex].value;
    var item_count = 0;
    while(product_div = document.getElementById('item_group-'+id+'-'+item_count))
    {
        if(group_id != item_count) {
            product_div.style.visibility = "hidden";
            product_div.style.display    = "none";
            document.getElementById('select_quantity'+id+'-'+item_count).value=0;
        } else {
            product_div.style.visibility = "visible";
            product_div.style.display    = "block";
        }
        item_count++;
    }
    fadeBGCol('item_group-'+id+'-'+group_id);
}

function ChangeProductStyle(id)
{
    var select_box   = document.getElementById('select_style'+id);
    var item_count   = 0;
    var option_id    = select_box.options[select_box.selectedIndex].id;
    var product_id   = option_id.substr(option_id.lastIndexOf('-')+1);
    var product_div;
    while(product_div = document.getElementById('item-'+id+'-'+item_count))
    {
        if(product_id != item_count) {
            product_div.style.visibility = "hidden";
            product_div.style.display    = "none";
        } else {
            product_div.style.visibility = "visible";
            product_div.style.display    = "block";
        }
        item_count++;
    }
    CheckProductQuantity(id);
}

function CheckProductQuantity(id)
{
    var quantity_box = document.getElementById('select_quantity'+id);
    var select_box   = document.getElementById('select_style'+id);
    var item_count   = 0;
    if(quantity_box.value > 0) {
        if(select_box.options) {
            var option_id    = select_box.options[select_box.selectedIndex].id;
            var product_id   = option_id.substr(option_id.lastIndexOf('-')+1);
        } else if (select_box.value) {
            var product_id = 0;
        }
        var product_value = document.getElementById('item-add-'+id+'-'+product_id).value;

        if(product_value != 1) {
            quantity_box.value = 0;
            alert("Sorry this item is out of stock.");
        }
    }
    if(!quantity_box.value || !parseFloat(quantity_box.value) || quantity_box.value < 1) quantity_box.value = 0;
    //if(quantity_box.value > 99) quantity_box.value = 99;
}

function EraseQuantity(id)
{
    var quantity_box = document.getElementById('select_quantity'+id);

    if(quantity_box.value == '0') {
        quantity_box.value = '';
    }
    return false;
}

function AddQuantity(id)
{
    document.getElementById('select_quantity'+id).value++;
    CheckProductQuantity(id);
    return false;
}

function RemoveAllQuantities()
{
    if (!document.forms['sub_items']) return;
    var id=0;
    var inputs = document.forms['sub_items'].getElementsByTagName('INPUT');
    var selected=false;
    while(obj = inputs.item(id)){
        id++;
        if(obj.id.substr(0,15) == 'select_quantity' && obj.value > 0) obj.value = 0;
    }
}

function CheckQuantitySubmit()
{
    var id=0;
    var inputs = document.forms['sub_items'].getElementsByTagName('INPUT');
    var selected=false;
    while(obj = inputs.item(id)){
        id++;
        if(obj.id.substr(0,15) == 'select_quantity' && obj.value > 0) selected=true;
    }

    if(!selected) {
        alert('Please select a quantity.');
        return false;
    }

    return true;
}

function CheckQuantitySubmitNoMessage()
{
    var id=0;
    var inputs = document.forms['sub_items'].getElementsByTagName('INPUT');
    var selected=false;
    while(obj = inputs.item(id)){
        id++;
        if(obj.id.substr(0,15) == 'select_quantity' && obj.value > 0) selected=true;
    }
    return selected;
}

function ProductZoom(id, type, size, caption)
{
    var zoombox = document.getElementById('product_zoom');
    var zoomboxlink = document.getElementById('product_zoom_more');
    zoombox.style.display = 'block';
    zoomboxlink.href = "javascript:ProductZoomSet('"+id+"','"+type+"',1,['"+size.join("','")+"'],'"+caption+"')";
    ProductZoomSet(id,type,0,size,caption);
    //product_zoom.MoveDown();
}

function ProductZoomSet(id, type, show, size, caption)
{
    var zoombox = document.getElementById('product_zoom');
    var zoomtitle   = document.getElementById('product_zoom_title');
    var zoomboxlink = document.getElementById('product_zoom_more');
    var zoomimage = document.getElementById('product_zoom_image');
    var next = show+1;
    zoomimage.src = "http://www.bobbyberkhome.com/images/loadanimation.gif";

    if(caption) {
        zoomtitle.innerHTML = caption;
        zoomtitle.style.display = 'block';
    } else {
        zoomtitle.innerHTML = '';
        zoomtitle.style.display = 'none';
    }

    if(size[next]) {
        zoomboxlink.style.display = 'block';
        zoomboxlink.href = "javascript:ProductZoomSet('"+id+"','"+type+"',"+next+",['"+size.join("','")+"'],'"+caption+"')";
    } else {
        zoomboxlink.style.display = 'none';
        zoomboxlink.href = "#";
    }
    zoomimage.src = size[show];

}

function ProductZoomHide()
{
    var zoombox = document.getElementById('product_zoom');
    var zoomtitle   = document.getElementById('product_zoom_title');
    var zoomimage = document.getElementById('product_zoom_image');
    var zoomboxlink = document.getElementById('product_zoom_more');

    //product_zoom.MoveUp();
    zoomtitle.innerHTML = '';
    zoomimage.src = "http://www.bobbyberkhome.com/images/loadanimation.gif";
    zoomboxlink.style.display = 'none';
    zoomtitle.style.display = 'none';
    zoombox.style.display = 'none';

}

function ProductDetailsShow()
{
    var wind = document.getElementById('child_window');
    wind.style.display = 'block';
}
function ProductDetailsHide()
{
    var wind = document.getElementById('child_window');
    wind.style.display = 'none';
}
function ProductDetailsTop()
{
    document.getElementById('child_window_content').scrollTop=0;
}

function ProductChangeStyle(name)
{
    var section = document.getElementById('items');
    var styles = section.getElementsByTagName('select');
    var j=0;
    var o=0;
    var select_name="";
    while(styles[j]) {
        o=0;
        while(styles[j].options[o]) {
            select_name = styles[j].options[o].innerHTML;
            if(select_name.toLowerCase() == name) {
                styles[j].selectedIndex = o;
                styles[j].onchange();
                break;
            }
            o++;
        }
        j++;
    }
}

function openWindow (url, name, width, height, toolbar, menubar, status, location, scrollbar)
{
    var w;
    w = window.open(url, name, 'width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes,directories=no,toolbar='+toolbar+',menubar='+menubar+',status='+status+',location='+location);
    w.moveTo(200,200);
}


//BG Fader by www.hesido.com
function fadeBGCol(id) {
    var obj = document.getElementById(id);
        doBGFade(obj,[255,255,200],[255,255,255],[255,255,255],20,20,1);
}

function doBGFade(elem,startRGB,endRGB,finalColor,steps,intervals,powr) {
    finalColor = "rgb("+finalColor.join(',')+")";
    if (elem.bgFadeInt) window.clearInterval(elem.bgFadeInt);
    var actStep = 0;
    elem.bgFadeInt = window.setInterval(
        function() {
            elem.style.backgroundColor = "rgb("+
                easeInOut(startRGB[0],endRGB[0],steps,actStep,powr)+","+
                easeInOut(startRGB[1],endRGB[1],steps,actStep,powr)+","+
                easeInOut(startRGB[2],endRGB[2],steps,actStep,powr)+")";
            actStep++;
            if (actStep > steps) {
            elem.style.backgroundColor = finalColor;
            window.clearInterval(elem.bgFadeInt);
            }
        }
        ,intervals)
}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
//Generic Animation Step Value Generator By www.hesido.com
    var delta = maxValue - minValue;
    var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
    return Math.ceil(stepp)
}


ScrollWindow = function (id,accel,min,max)
{
    var SetWindow        = document.getElementById(id);
    var id               = id;
    var UsePower         = accel;
    var MinVelocity      = min;
    var MaxVelocity      = max;
    var VelocityRange    = MaxVelocity - MinVelocity;
    var CurrentDirection;

    this.MoveDown = function()
    {
        Direction = -1;
        Height = SetWindow.clientHeight;
        //SetWindow.style.marginTop = Height * Direction + "px";
        Current = Height * Direction;
        CurrentDirection = Direction;
        this.MoveIt(Direction,Height,Current);
    }

    this.MoveUp = function()
    {
        Direction = 1;
        Height = SetWindow.clientHeight;
        Current = 1;
        CurrentDirection = Direction;
        this.MoveIt(Direction,Height,Current);
    }

    this.MoveIt = function(Direction,Height,Current)
    {
        FlipDirection = Direction * -1;
        if(UsePower) {
            Power = MinVelocity + (Math.floor(VelocityRange * ((Current * Direction)/ Height)) * FlipDirection);
        } else {
            Power = MinVelocity;
        }

        if(Direction > 0) {
            Current = Current - Power;
        } else {
            Current = Current + Power;
        }

        if(!Current && Direction > 0) Current = -1;

        SetWindow.style.marginTop = Current + "px";

        if(Direction > 0 && Current > (Height * -1) || Direction < 0 && Current < 0) {
             if(CurrentDirection == Direction) window.setTimeout(id+".MoveIt("+Direction+","+Height+","+Current+")",10);
        } else if (Direction > 0) {
            SetWindow.style.marginTop = Height * -1;
        } else {
            SetWindow.style.marginTop = 0;
        }
    }
}
