function get_r(obj, bAll) {
    var strValues = "";
    for (var o in obj) {
        var strNext = obj[o];
        if (bAll || strNext != "" && strNext != null) { strValues += o + ": " + strNext + "\n"; }
    }
    return strValues;
}

function print_r(obj, bAll) {
    strValues = get_r(obj, bAll);
    if (strValues != "") { alert(strValues); }
}

function print_r_keys(obj) {
    var strValues = "";
    for (var o in obj) { strValues += o + "\n"; }
    alert(strValues);
}

function localNavBar(page){
    document.getElementById("navImg_" + page).src = "images/site_" + page + "_over.jpg";
}

function swapImage(locationID, imageID) {
    $('#'+locationID).find("li").hide();
    $('#'+imageID).show();
}

function UpdateShipping(oInput, sId) {
    var differentShipping = document.getElementById("shippingNeeded").checked;
    if (differentShipping) { CalculateShipping(); return; }
    $('#' + sId).val(oInput.value);
    CalculateShipping();
}

function UpdateStateAndShipping(oInput, sCountryId) {
    if ($(oInput).val() == "0") { 
        $('#bill_province_row').hide(); 
        $('#bill_state_row').show(); 
        $('#BillStateLabel').text('State');
    } else { 
        $('#bill_state_row').hide(); 
        $('#bill_province_row').show(); 
        $('#BillStateLabel').text('Province');   
    }
    UpdateShippingByCountry(oInput, sCountryId);
}

function UpdateShippingState(oInput) {
    if ($(oInput).val() == "0") {
        $('#ship_province_row').hide();
        $('#ship_state_row').show();
        $('#ShipStateLabel').text('State');
    } else {
        $('#ship_state_row').hide();
        $('#ship_province_row').show();
        $('#ShipStateLabel').text('Province');
    }
    CalculateShipping();
}

function UpdateShippingByCountry(oInput, sCountryId) {
    var differentShipping = document.getElementById("shippingNeeded").checked;
    if (differentShipping) { CalculateShipping(); return; }
    if ($(oInput).val() == "0") {         
        $('#ship_province_row').hide(); 
        UpdateShipping($('#bill_state'), 'ship_state');
        $('#ship_state_row').show(); 
        $('#ShipStateLabel').text('State');
    } else { 
        $('#ship_state_row').hide(); 
        UpdateShipping($('#bill_province'), 'ship_province');
        $('#ship_province_row').show(); 
        $('#ShipStateLabel').text('Province');
    }
    UpdateShipping(oInput, 'ship_country');
}


function EnableShipAddr() {
    var displayState = !document.getElementById("shippingNeeded").checked;
    $('#shippingAddress').find('INPUT, SELECT').each(function () { this.disabled = displayState; });
    if (displayState) { 
        $('#shippingAddress').hide();
        $('#shippingPickup').show();
        $('#billingAddress').find('INPUT, SELECT').each(function () {    
            var strName = "";
            strName = this.name.replace(/^bill/,'ship');
            UpdateShipping(this, strName);});
        UpdateShippingByCountry($('#bill_country'), 'ship_country'); 
    } else {    
        $('#shippingPickup').hide(); 
        $('#forPickup').attr('checked', false);
        $('#shippingAddress').show(); 
    }
}
function GatewayShipAddr() {
    var noShip = document.getElementById("forPickup").checked;
    
    if (noShip) {
        $('#shippingNeeded').val('false');
        $('#shippingAddress').hide();
    } else {
        $('#shippingNeeded').val('true');
        $('#shippingAddress').show();
    }
}

var oForms = new Object();
function SubmitOrder(sFormId) {
    if (oForms.hasOwnProperty(sFormId)) {
        alert("Your order is already being processed. Please wait a moment for the results of the transaction.");
        return;
    }
    if (!ValidateForm(sFormId)) {
        alert("Please enter information in all fields to complete your order. Missing fields have been highlighted for you.");
        return;
    }
    oForms[sFormId] = true;
    $('#' + sFormId).submit();
}
function ValidateForm(sFormId) {
    var bValid = true;
    $('#' + sFormId + ' .required').each(
        function() {
            var oElement = $(this);
            var bMissing = (oElement.val() == '');
            if (bMissing) {
                oElement.addClass('missing');
                bValid = false;
            } else {
                oElement.removeClass('missing');
            }
        }
    );
    return bValid;
}
function PayPalPayment(sFormId) {
    if (oForms.hasOwnProperty(sFormId)) {
        alert("Your order is already being processed. Please wait a moment for the results of the transaction.");
        return;
    }
    oForms[sFormId] = true;
    $('#' + sFormId + ' .formAction').val('PayPalPay');
    $('#' + sFormId).submit();
}

function CalculateShipping() {
    var shippingLocationSurcharge = 0;
    var grandTotal = 0;
    var country = document.getElementById('ship_country');
    var state = document.getElementById('ship_state');
    var noShipping = document.getElementById('forPickup').checked;

    // CANADA = 1; US = 0
    if      ( country.value == '1') { shippingLocationSurcharge = 20; }
    else if ( country.value == '0' && ( state.value == "AK" || state.value == "HI")) { shippingLocationSurcharge = 15; }
    else    { shippingLocationSurcharge = 0; }

    var shippingCost = baseShipping + shippingLocationSurcharge;
    if (noShipping) { shippingCost = 0; }
    $('#shipping_total').text('$'+shippingCost.toFixed(2));
    grandTotal = (shippingCost+subTotal-discount).toFixed(2);
    $('#grand_total').text('$'+grandTotal);
}

function UpdateComment() {
    var commentText = document.getElementById("commentEntry").value;
    $('#comment').val(commentText);
}