function AddShipTo() {
    if (document.getElementById("shipto").value == "newrecipient") {
        document.getElementById("ship-to-add").style.display = "block";
    }
}

// pop-up windows ... don't think this is even used ... remove after optimization
function openPopUp(PageName) {
    new_window = window.open(PageName,"new_window","width=400,height=400,location=0,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0,left=150,top=50,screenx=150,screeny=50");
    if (new_window.opener == null) {
        new_window.opener = self;
    }
    new_window.focus();
}

//  ... don't think this is even used ... remove after optimization
function openSizedPopUp(PageName, Width, Height) {
    new_window = window.open(PageName,"new_window","width="+Width+",height="+Height+",location=0,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0,left=150,top=50,screenx=150,screeny=50");
    if (new_window.opener == null) {
        new_window.opener = self;
    }
    new_window.focus();
}

// quick order form validation
function validateQuickOrder(form) {
    if ((form.ProductNumber.value == "")|| (form.ProductNumber.value == "Enter Product #")) {
        alert("Please enter an item number.");
        form.ProductNumber.focus();
        return false;
    }
    return true;
}

// used on one-page check out to populate the shipping address from the billing address
function UseBillingAsShipping(form) {

    if (form.ship_to_bill_address.checked) {
        form.FName_SHIP.value = form.FirstName.value;
        form.LName_SHIP.value = form.LastName.value;
        form.Address1_SHIP.value = form.Address1.value;
        form.Address2_SHIP.value = form.Address2.value;
        form.City_SHIP.value = form.City.value;
        form.Country_SHIP.value = form.Country.value;

        var country_ship = form.Country_SHIP.value;

        DmiAjaxFetchWithCallBack( "ShipStateDropDown","DmiAjaxSecure.aspx","request=ShipStateDropDown&recipient=DEFAULT&country_ship=" + country_ship + "&extra=", rnd(), function() {
            document.getElementById("State_SHIP").value = document.getElementById("State").value;
            populateShippingMethods();
        } );

        form.PostalCode_SHIP.value = form.PostalCode.value;
        form.Phone_SHIP.value = form.Phone.value;

        if (form.PO.checked) {
            form.PO_SHIP.checked = true;
        }

    } else {
        form.Address1_SHIP.value = '';
        form.Address2_SHIP.value = '';
        form.City_SHIP.value = '';
        form.State_SHIP.value = '';
        form.PostalCode_SHIP.value = '';
        form.FName_SHIP.value = '';
        form.LName_SHIP.value = '';
        form.Phone_SHIP.value = '';
        form.PO_SHIP.checked = false;

        populateShippingMethods();
    }
}

// used on one-page check out to update order total (for example after state, country, or shipping method change)
function populateCartTotal() {
    updateBillingInfo();
}

function populateShippingMethods(recipientId) {
    populateShippingMethods(recipientId, '', '');
}

function populateShippingMethods(recipientId, giftAction, po_boxAction) {

    //assumes "State_SHIP_RECIPIENT" or "Country_SHIP_RECIPIENT", parse out the actual recipient name.  Need to figure out how to pass the nickname within the select control
    var recipient = '';
    var recipientNamePart = '';
    var tempRecipient = '';
    var recipientId = recipientId;

    //sanity check
    if(recipientId == null || recipientId == 'undefined') {
        tempRecipient = 'Default';
    } else {
        tempRecipient = recipientId;
    }

    //parse only if it's not the default, assume empty is default
    if(tempRecipient.indexOf('State_SHIP') == 0)
        tempRecipient = tempRecipient.substring(11);
    else if(tempRecipient.indexOf('Country_SHIP') == 0)
        tempRecipient = tempRecipient.substring(13);
    else if(tempRecipient == '')
        tempRecipient = 'Default';

    recipient = tempRecipient;

    if(po_boxAction == null)
        po_boxAction = '';

    if(giftAction == null)
        giftAction = '';

    //if not the default, add the name part to retrieve specific recipient form elements
    if(recipient != 'Default' && recipient != '')
        recipientNamePart = '_' + recipient;

    var state_ship = '';
    var country_ship = '';
    var zip_ship = '';

    state_ship = document.getElementsByName("State_SHIP" + recipientNamePart)[0].value;
    country_ship = document.getElementsByName("Country_SHIP" + recipientNamePart)[0].value;
    zip_ship = document.getElementsByName("PostalCode_SHIP" + recipientNamePart)[0].value;

    if (state_ship != " - Select - " && country_ship != " - Select - ") {
        var ship_method;
        if(document.getElementById("shipping_method" + recipientNamePart) != null) {
            //shipping_method dropdown wont exist if page portion is being refreshed by AJAX call
            ship_method = document.getElementsByName("shipping_method" + recipientNamePart)[0].value;
        } else
            ship_method = "";  //won't get set on call, but will use existing or default

        var isPO_selected;
        if(document.getElementById("PO_SHIP" + recipientNamePart) != null) {
            isPO_selected = document.getElementsByName("PO_SHIP" + recipientNamePart)[0].checked;
            //alert('PO=' + isPO_selected);
        } else
            isPO_selected = "";

        //must pass recipient or else the built section will not be able to resend the recipient
        DmiAjaxFetch('CheckoutShippingMethods' + recipientNamePart, 'DmiAjaxSecure.aspx', 'request=' + 'CheckoutShippingMethods' + recipientNamePart + '&state_ship=' + state_ship + '&country_ship=' + country_ship + '&zip_ship=' + zip_ship + '&shipping_method=' + ship_method + '&recipient=' + recipient + '&gift=' + giftAction + '&po_box=' + po_boxAction + '&po_box_checked=' + isPO_selected + '&extra=', rnd());
        setTimeout("populateCartTotal()", 2000);
    }
}

function UpdateShippingStateValues(recipientId) {
    //assumes "State_SHIP_RECIPIENT" or "Country_SHIP_RECIPIENT", parse out the actual recipient name.  Need to figure out how to pass the nickname within the select control
    var recipient = '';
    var recipientNamePart = '';
    var tempRecipient = '';
    var recipientId = recipientId;

    //sanity check
    if(recipientId == null || recipientId == 'undefined')
        tempRecipient = 'Default';
    else
        tempRecipient = recipientId;

    //parse only if it's not the default, assume empty is default
    if(tempRecipient.indexOf('State_SHIP') == 0)
        tempRecipient = tempRecipient.substring(11);
    else if(tempRecipient.indexOf('Country_SHIP') == 0)
        tempRecipient = tempRecipient.substring(13);
    else if(tempRecipient == '')
        tempRecipient = 'Default';

    recipient = tempRecipient;
    //if not the default, add the name part to retrieve specific recipient form elements
    if(recipient != 'Default' && recipient != '') {
        recipientNamePart = '_' + recipient;
    }

    var state_ship = '';
    var country_ship = '';
    var zip_ship = '';

    country_ship = document.getElementsByName("Country_SHIP" + recipientNamePart)[0].value;

    if(country_ship != " - Select - ") {
        TargetSpan = 'ShipStateDropDown-'+recipient;
        if(recipient=='Default') {
            TargetSpan = "ShipStateDropDown";
        }
        DmiAjaxFetchWithCallBack( TargetSpan, 'DmiAjaxSecure.aspx', 'request=ShipStateDropDown&country_ship=' + country_ship +'&recipient='+recipient+'&extra=', rnd(), function() {
            populateShippingMethods( recipientId );
        } );
    }
}

//global variable
var giftCertificateCount = 0;

// used on one-page check out to apply the promo code the customer provides
function applyKeyCodeRefresh() {
    var keyCodeElement = document.getElementsByName("keycode");
    var giftCertElement = document.getElementsByName("giftcert1");
    var keycode = '';
    var giftcert1 = '';

    if(null != keyCodeElement ) {
        keycode = document.getElementsByName("keycode")[0].value;
    }
    if(null != giftCertElement ) {
        giftcert1 = document.getElementsByName("giftcert1")[0].value;
    }

    DmiAjaxFetch('CheckOutCartItemsContent', 'DmiAjaxSecure.aspx', 'request=CheckOutCartItemsContent&keycode=' + keycode + '&giftcert1=' + giftcert1 + '&extra=', rnd());

}

// used on one-page check out to apply the promo code the customer provides
function applyKeyCode() {
    var keyCodeElement = document.getElementsByName("keycode");
    var giftCertElement = document.getElementsByName("giftcert1");

    var keycode = '';
    var giftcert1 = '';
    if(null != keyCodeElement ) {
        keycode = document.getElementsByName("keycode")[0].value;
    }
    if(null != giftCertElement ) {
        giftcert1 = document.getElementsByName("giftcert1")[0].value;
    }

    //alert('DEBUG: gift cert:'+ giftcert1);

    DmiAjaxFetch('CheckOutCartItemsContent', 'DmiAjaxSecure.aspx', 'request=CheckOutCartItemsContent&keycode=' + keycode + '&giftcert1=' + giftcert1 + '&extra=', rnd());

    if(giftCertificateCount <= 0 && keycode == '') {
        //alert('DEBUG: ct = '+ giftCertificateCount );
        setTimeout("applyKeyCodeRefresh()", 1000);  //repaint with new cart object
    }

    if(keycode == '') {
        giftCertificateCount = giftCertificateCount + 1;
    }

    return false;
}

// used to generate random string that will be appended to AJAX requests to avoid getting cached response
function rnd() {
    return String((new Date()).getTime()).replace(/\D/gi,'');
}

// used on one-page check out to update billing state or country and order total
function updateBillingInfo() {
    var state_bill = document.getElementsByName("State")[0].value;
    var country_bill = document.getElementsByName("Country")[0].value;
    var zip_bill = document.getElementsByName("PostalCode")[0].value;

    if (state_bill != " - Select - " && country_bill != " - Select - ") {
        DmiAjaxFetch('CheckoutCartTotalContent', 'DmiAjaxSecure.aspx', 'request=CheckoutCartTotalContent&state_bill=' + state_bill + '&country_bill=' + country_bill + '&zip_bill=' + zip_bill + '&extra=', rnd());
    }

}

function UpdateBillingStatesValues() {
    var state_bill = document.getElementsByName("State")[0].value;
    var country_bill = document.getElementsByName("Country")[0].value;
    var zip_bill = document.getElementsByName("PostalCode")[0].value;
    DmiAjaxFetch( 'BillStateDropDown', 'DmiAjaxSecure.aspx', 'request=BillStateDropDown&country_bill=' + country_bill + '&extra=', rnd() );
}

// used on one-page check out handle the gift order option
function updateGiftOrder(recipientId) {
    var gift = '';
    var recipient = '';
    var recipientNamePart = '';

    //sanity check
    if(recipientId == null || recipientId == 'undefined' || recipientId == '' || recipientId == 'Default') {
        recipientNamePart = '';
    } else {
        recipientNamePart = '_' + recipientId;
        recipient = recipientId;
    }

    //check if gift order added or removed
    if(document.getElementById("giftorder" + recipientNamePart) != null) {
        //get checked value
        if(document.getElementsByName("giftorder" + recipientNamePart)[0].checked == 1) {
            gift = 'add';
            //alert('gift add:' + document.getElementsByName("giftorder" + recipientNamePart)[0].value);
        } else {
            gift = 'remove';
            //alert('gift rem:' + document.getElementsByName("giftorder" + recipientNamePart)[0].value);
        }

        populateShippingMethods(recipient, gift, '');
    }

}

// used on one-page check out handle the Post Office Box
function updatePostOfficeBox(recipientId) {
    var po_box = '';
    var recipient = '';
    var recipientNamePart = '';
    var po_box_checked = '';

    //sanity check
    if(recipientId == null || recipientId == 'undefined' || recipientId == '' || recipientId == 'Default') {
        recipientNamePart = '';
        recipient = 'Default';
    } else {
        recipientNamePart = '_' + recipientId;
        recipient = recipientId;
    }

    var state_ship = '';
    var country_ship = '';
    var zip_ship = '';

    state_ship = document.getElementsByName("State_SHIP" + recipientNamePart)[0].value;
    country_ship = document.getElementsByName("Country_SHIP" + recipientNamePart)[0].value;
    zip_ship = document.getElementsByName("PostalCode_SHIP" + recipientNamePart)[0].value;

    //check if po box added or removed
    if(document.getElementById("PO_SHIP" + recipientNamePart) != null) {
        //get checked value
        if(document.getElementsByName("PO_SHIP" + recipientNamePart)[0].checked == 1) {
            po_box = 'add';
            po_box_checked = 'true';
            // alert('po box add:' + document.getElementsByName("PO_SHIP" + recipientNamePart)[0].value);
        } else {
            po_box = 'remove';
            po_box_checked = 'false';
            // alert('po box rem:' + document.getElementsByName("PO_SHIP" + recipientNamePart)[0].value);
        }

        populateShippingMethods(recipient);

    }

    setTimeout("populateCartTotal()", 2000);
}
