YAHOO.namespace('gaia.app.Redeem');

YAHOO.gaia.app.Redeem = function() {
    var redemption_code_button_onClick = function(evt) {
        // disable the submit button... no redeem spam for j00!
        try{
            this.disabled = true;
            YAHOO.util.Dom.setStyle(this, 'opacity', 0.5);
        }catch(err){
            ; // do nothing... this is just to prevent JS from breaking if the browser doesn't support something
        }
        
        document.getElementById('error_container').innerHTML = "loading...";
        var redeem_code = document.getElementById('redeem_code').value;
        
        var redeemSource = "http://" + GAIA_config('main_server') + "/redeem/go?code=" + redeem_code + '&ajax=true';
        
        var callback = {
            success: function(o) {
                document.getElementById('error_container').innerHTML =  o.responseText;
            },
            failure: function(o) {
                document.getElementById('error_container').innerHTML =  'Redeem unsuccessful. Please try again at a later time.';
            }
        }
        
        var transaction = YAHOO.util.Connect.asyncRequest('POST', redeemSource, callback, null);
    };
    
    var redemption_code_box_onFocus = function() {
        // assumed to be called within the scope of the INPUT element... hence "this.value" means "InputElement.value"
        
        // erase the word "pin" after they focus on the input for the first time...
        if(this.value == this.defaultValue)
            this.value = '';
        // don't do anything if the word "pin" isn't the only content in the input
    }
    
    var constructor = function() {
        YAHOO.util.Event.addListener('redeem_code', 'focus', redemption_code_box_onFocus);
        YAHOO.util.Event.addListener('redeem_button', 'click', redemption_code_button_onClick);
    };
    
    return constructor;
}();

YAHOO.util.Event.onAvailable('redeem_button', YAHOO.gaia.app.Redeem);
