if(!Shop) {
    throw "Shop object is required to use"
}

var Voucher = {
	request: function(code) {
		var url = "/common/shop/xhr_cart.php";
		new Ajax.Request(url, {
			method: 'post',
			parameters: 'mode=voucher&code='+code,
			evalJSON: true,
			onFailure: this.failed.bind(this),
			onSuccess: this.apply.bind(this)
		});
	},
	apply: function(t) {
		var json = t.responseText.evalJSON();
		//console.log(json);

        Shop.updatePage(json);

	},
    
    //- this is called by Shop.updatePage - do not call locally!!
    updatePage: function(data) { 
        if($('voucher-msg') && typeof data.voucherMessage != 'undefined') {
            $('voucher-msg').update(data.voucherMessage);
        }
    },
	failed: function(t) {
		alert('Voucher request failed. Please try again');
	}
}

document.observe("dom:loaded", function() {
	var el = $('voucher-code');
	if(el && el.value) {
		Voucher.request(el.value);
	}
});
