/* Promocode uses jQuery to Run. */

// Set the code that runs when the page is ready
$(document).ready(function () {
                      // Add events
                      $('#continue').click(validatePromoCode);
                      $('#nopromo').click(noPromoCode);
                      $('#mypromocode').focus();
                      $('#mypromocode').keypress(function(e) {
                                                     // If the ENTER key is pressed, do the action
                                                     if (e.which == 13) {
                                                         validatePromoCode();
                                                         return false;
                                                     }
                                                 });
                  });

// This function validates promo codes
function validatePromoCode() {
    // Get the promo code
    var promoCode = $('#mypromocode').val();
    if (promoCode.length < 1) {
        showFaderMsg({msg: 'A Promo Code must be specified. [GT_E0141]',
                 msgContainer: '#errMsg', msgIsErr: true, pauseDur:15000});
        return;
    }

    // Validate the promo code
    jQuery.getJSON(BASEURL+'promocodes/validatePromoCode',
                   {promocode: promoCode, syn: SYN, ack: ACK, synlist:genS(['promocode']), acklist:genH(promoCode), rand: Math.random()},
                   function(data, textStatus) {
                       // Decode the JSON
                       results = data;

                       // Check to make sure there was no error
                       if (!results.success) {
                           // Show a message
                           showFaderMsg({msg: 'The Promo Code is not valid, or an error occurred validating the Promo Code.&nbsp;[' + results.errorNumber + ']&nbsp;' + results.extendedText,
                                        msgContainer: '#errMsg', msgIsErr: true, pauseDur:15000});
                           return;
                       }

                       // Now that we know the code is valid, inject some
                       // stuff on the form. Doing things this way prevents
                       // us from "showing our cards" to potential snoopers
                       $('#regForm').append('<input type="hidden" name="promocode" value="' + promoCode + '" />');
                       $('#regForm').append('<input type="hidden" name="promocodevalid" value="y" />');
                       $('#regForm').submit();
                   });
}

// This function just skips validating a promo code and moves
// the user to the next step in registration
function noPromoCode() {
    $('#regForm').submit();
}
