// Utilities, for general availability throughout the livescribe store.


// For use with form naming conflicts (in particular, fields and non
// submit-method attributes named 'submit'
function cache_form_submitter(frm) {
	
    var real_submit_method = frm.submit;

    return function() {

        var ret;
        if("undefined" == typeof real_submit_method["apply"])
            ret = real_submit_method();
        else
            ret = real_submit_method.apply(document.frm);

        return ret;
    };

}

function StoreCheckoutOverLay() {
    var overlay = "<div id='storeOverlay'></div>";
    $('store_wide_col2').relativize();
    $('store_wide_col2').insert(overlay);
    
}
function getBody(content){
     var from = content.indexOf("<body ");
     from = content.indexOf(">", from);
     var to = content.lastIndexOf("</body>");
     return content.slice(from + 1, to);
}
document.observe("dom:loaded", function() {
    if($("applyCoupon")) {
        Event.observe("applyCoupon", "click", function(event) {
            event.stop();
            if(!this.disabled) {
                StoreCheckoutOverLay();
                document.frm.t.value = 'prom';
                setAnchorName('payment');
                //document.frm.submit();
                this.disabled = true;
                var vid = document.frm.vid.value;
                var url = '/store/yourinfo.html?vid='+vid;
                var params = $('frmCheckout').serialize();
                new Ajax.Request(url, {
                         method: 'post',
                         asynchronous: true,
                         parameters: params,
                         onSuccess: function(transport) {
                                $("applyCoupon").stopObserving();
                                document.body.innerHTML = getBody(transport.responseText);
                         }
                });
            }
            return false;
        });
    }                                                                                         
    if($("frmCheckout")) {
        $("frmCheckout").observe("submit", function(event) {
            if(poBoxWarning('Address1') || poBoxWarning('Address2'))
                return false;
            return true;
        });
    }
});


