var postBack = false;
var maxMessageLength = 255;

function popUpGiftServices(giftServicesUrl) {
    var url = giftServicesUrl;
    var width = 850;
    var height = 520;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=850,height=520,left="+left+"top="+top+", scrollbars=yes, resizable";

    window.open (url,"giftservices",windowFeatures);
}

function popUp(giftUrl) {
	var url = giftUrl;
	window.open (url,"giftbox","width=480,height=310, left = 100,top = 100");
}

function notesPopUp (popupURL) {
	var notesURL = popupURL;
	window.open (notesURL,"giftnotes","width=340,height=230, left = 100,top = 100");

}

function loadGiftWrapBag(){
    // use this for pages that show the shipping bag - these require post back to recalc values
    postBack = true;
    loadGiftWrap();
}

function loadGiftWrap(){
    $$('.gift_wrap_box_outer').each(
        function(target){
            target.postBack = postBack;
            var id = target.id;
            var toBox = document.getElementById("gw_to_1");
            var fromBox = document.getElementById("gw_from_1");
            var messageBox = document.getElementById("gw_message_1");
            var check = document.getElementById("giftWrapChk1");
            var editlink = document.getElementById("editNoteLink");

            // set this before we add it to the html
            //messageBox.setProperty('onkeyup','processMessageboxLength(\'' + messageBox.id + '\');');

            // build the real html for this section
            var html = '<div class="gift_wrap_box_inner">' +
                '<div><img src="/media/TB001/images/global/title_giftwrap.gif" alt="Gift Wrap Note" /></div>' +
                '<div class="gift_wrap_line"></div>' +
                '<table width="190" cellspacing="0" cellpadding="0" border="0" class="gift_wrap_table">' +
                    '<tr align="right">' +
                        '<td width="55">To:</td>' +
                        '<td>' + new Element('div').adopt(toBox).innerHTML + '</td>' +
                    '</tr>' +
                    '<tr align="right">' +
                        '<td>From:</td>' +
                        '<td>' + new Element('div').adopt(fromBox).innerHTML + '</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td colspan="2" class="message_textbox">' +
                            '<div>Message:</div>' +
                            new Element('div').adopt(messageBox).innerHTML +
                        '</td>' +
                    '</tr>' +
                    '<tr>' +
                        '<td colspan="2" align="left" class="comment"><div id="' + messageBox.id + '_counter"></div></td>' +
                    '</tr>' +
                '</table>' +
                '<div class="gift_wrap_buttons" align="right">' +
                    '<span class="btn_cancel">' +
                        '<a href="javascript:cancelGiftWrap(\'' + id + '\');"><img src="/media/TB001/images/global/btn_cancel_off.gif" border="0" /></a>' +
                    '</span>' +
                    '<span class="btn_save">' +
                        '<a href="javascript:saveGiftWrap(\'' + id + '\');"><img src="/media/TB001/images/static/btn_save_off.gif" border="0" /></a>' +
                    '</span>' +
                '</div>' +
            '</div>';
            target.setHTML(html);
            // set some element variables and events
            //check.addEvent('click',function(){showGiftWrap(id);});
            editlink.href='javascript:showGiftWrapEdit(\'' + id + '\');';
            target.checkid = check.id;
            target.to = toBox.id;
            target.from = fromBox.id;
            target.message = messageBox.id;
            target.lasttoval = toBox.value;
            target.lastfromval = fromBox.value;
            target.lastmessageval = messageBox.value;
            target.editClicked = false;
        });
}

function showGiftWrap(giftWrapID){
    // cancel any other open gwb's
    $$('.gift_wrap_box_outer').each(
        function(target){
            if (target.getStyle('display') != 'none')
            {
                cancelGiftWrap(target.id);
            }
    });

    var gwb = $(giftWrapID);

    if ($(gwb.checkid).checked == true)
    {
        // user checks the checkbox
        gwb.setStyle('display','');
        if(window.ie && $('cartshipping') && (($(giftWrapID).getTop()+260) > $('cartshipping').getTop())){
            $('cartshipping').setStyle('display','none');
        }

        if(window.ie && $('ADD_CART_ITEM_ARRAY<>quantity')){

            $('ADD_CART_ITEM_ARRAY<>quantity').setStyle('display','none');
        }

        saveVars(giftWrapID);
        processMessageboxLength(gwb.message);
    } else {
        // user clears the checkbox
        if (gwb.postBack == true){
            triggerPostBack();
        } else {
            gwb.setStyle('display','none');

            if(window.ie){
                document.getElementById('ADD_CART_ITEM_ARRAY<>quantity').setStyle('display','');
            }
            $(gwb.checkid).getNext().setStyle('display','none'); // edit link
            clearVars(giftWrapID);
        }
    }

}

function showGiftWrapEdit(giftWrapID){
    $(giftWrapID).editClicked = true;
    showGiftWrap(giftWrapID);
}
function saveGiftWrap(giftWrapID){
    var gwb = $(giftWrapID);
    gwb.setStyle('display','none');
    $(gwb.checkid).checked = true;
    $(gwb.checkid).getNext().setStyle('display',''); // edit link
    gwb.editClicked = false;
    if (gwb.postBack == true){
        triggerPostBack();
    } else if(window.ie && $('ADD_CART_ITEM_ARRAY<>quantity')){
        $('ADD_CART_ITEM_ARRAY<>quantity').setStyle('display','');
    }
    var lineItemOrderNotes = document.getElementById('ADD_CART_ITEM_ARRAY<>longDesc');
    var fromNotes = document.getElementById('gw_from_1').value;
    var toNotes = document.getElementById('gw_to_1').value;
    var messageNotes = document.getElementById('gw_message_1').value;
    lineItemOrderNotes.value = toNotes+"|"+fromNotes+"|"+messageNotes;
}
function cancelGiftWrap(giftWrapID){
    var gwb = $(giftWrapID);
    gwb.setStyle('display','none');
    if (gwb.editClicked == true)
    {
        $(gwb.checkid).checked = true;
    } else {
        $(gwb.checkid).checked = !$(gwb.checkid).checked;
    }
    if($(gwb.checkid).checked){
        $(gwb.checkid).getNext().setStyle('display',''); // edit link
    } else {
        $(gwb.checkid).getNext().setStyle('display','none'); // edit link
    }
    if(window.ie && $('cartshipping')){
        $('cartshipping').setStyle('display','');
    }
    if(window.ie && $('ADD_CART_ITEM_ARRAY<>quantity')){
        $('ADD_CART_ITEM_ARRAY<>quantity').setStyle('display','');
    }
    resetVars(giftWrapID);
    gwb.editClicked = false;
}
function saveVars(giftWrapID){
    var gwb = $(giftWrapID);
    gwb.lasttoval = $($(giftWrapID).to).value;
    gwb.lastfromval = $($(giftWrapID).from).value;
    gwb.lastmessageval = $($(giftWrapID).message).value;
}
function resetVars(giftWrapID){
    var gwb = $(giftWrapID);
    $(gwb.to).value = gwb.lasttoval;
    $(gwb.from).value = gwb.lastfromval;
    $(gwb.message).value = gwb.lastmessageval;
}
function clearVars(giftWrapID){
    var gwb = $(giftWrapID);
    $(gwb.to).value = '';
    $(gwb.from).value = '';
    $(gwb.message).value = '';
}
function processMessageboxLength(msgbxid){
    var msgbox = $(msgbxid);
    var counter = $(msgbxid + "_counter");
    if (msgbox.value.length > maxMessageLength){
        msgbox.value=msgbox.value.substring(0,maxMessageLength);
    }
    counter.setHTML("(Remaining: "+(maxMessageLength-msgbox.value.length)+" of "+maxMessageLength + ")");
}

function triggerPostBack(){
    /* ****************************************************************** */
    /* TODO:
       This is only used for shopping bag listing pages that have a gift wrap
       selection option. It is not used for the product detail page since
       that is posted back by the user.

       Change this code to post back your form so that totals can be
       calculated correctly on save or de-selection of gift wrap and to
       break up product quantities as necessary.
       After post back the selected gift wrap area should reflect the
       selected state.
    /* ****************************************************************** */
    //document.forms[0].submit();
    alert("post back called");
}