///////////////////////////////////////////////////
// default.js
//
// this file contains functions used by all (or most) of
// the pages of this web site
///////////////////////////////////////////////////

// require some dojo apis
dojo.require("dojo.lfx.*");
dojo.require("dojo.io.*");


// show or hide the given object
function toggle( objid )
{
    var o = dojo.byId(objid);
    if( o.style.visibility == "visible" ) { o.style.visibility = "hidden"; }
    else { o.style.visibility = "visible"; }
}

function toggle_style( objid, s_off, s_on )
{
    // get our object
    var o = dojo.byId(objid);
    // if our class isn't either of these, let's do nothing.
    if( (o.className != s_off) && (o.className != s_on) ) { return; }
    // decide which style element we toggle
    else if( o.className == s_off ) { o.className = s_on; }
    else { o.className = s_off; }
}

function logout()
{
    var args  = logout.arguments;
    var admin = args[0];

    if( !confirm("Are you sure?") ) { return; }

    location.href = (admin) ? "../logout.php" : "logout.php";
}

// edit a user
function edit_user()
{
    var args = edit_user.arguments;
    var uid  = args[0];

    dojo.byId("uid").value = uid;
    dojo.byId("data").submit();
}
function edit_product()
{
    var args = edit_product.arguments;
    var pid  = args[0];

    dojo.byId("pid").value = pid;
    dojo.byId("data").submit();
}

// add an item to the cart
function addtocart( pid )
{
    if( pid < 1 ) { return; }

    // we have a pid, add it
    dojo.byId("product_id").value = pid;
    dojo.byId("cartform").submit();
}

// delete an item from the cart
function delete_item( idx )
{
    dojo.byId("del_idx").value = idx;
    dojo.byId("removeitem").submit();
}

// set the shipping info the same as the billing
function same_as_billing()
{
    var args = same_as_billing.arguments;
    var chk  = args[0].checked;

    // this is an array of the ids of billing info
    var info = new Array("fname","lname","addr","city","state","country","zip");
    var ship = new Array("sfname","slname","saddr","scity","sstate","scountry","szip");
    for( var i=0; i<info.length; i++ )
    {
        if( chk )
        {
            if( (info[i] != "state" ) && (info[i] != "country") ) { dojo.byId(ship[i]).value = dojo.byId(info[i]).value; }
            else
            {
                var sh_list = dojo.byId(ship[i]);
                var bs_list = dojo.byId(info[i]);
                sh_list.selectedIndex = bs_list.selectedIndex;
            }
        } else { dojo.byId(ship[i]).value = ""; }
    }
}