/**
 * Competition Entry
 *
 */
XT.COMPETITION = (function () {
   var str_competition_form = '#competition_form',
   str_submit_button = '#btn_enter_competition',
   initialise = function() {
      $(str_submit_button).click(submit);
   },
   submit = function() {
      if(!is_valid()) {
         return false;
      }

      AjaxHandler.process_form($(str_competition_form).get(0));
      AjaxHandler.dispatch(function (obj_response) {
         alert('You have been entered into the Competition.  Good Luck.');
      }, function(obj_response) {
         if($('reason', obj_response).length && $('reason', obj_response).text() == 'EXISTING_CONTACT') {
            alert("You've already entered the competition. Good Luck.");
         } else {
		if ($('reason', obj_response).length > 0 ){
            alert('Sorry, you could not be entered into the competition at this time. Please try again.');
            }	 
	}
      });

      return false;
   },
   is_valid = function() {
      var arr_errors = [];

      if($('#first_name').val() == '') {
         arr_errors.push('First Name is a required field.');
      }

      if($('#last_name').val() == '') {
         arr_errors.push('Last Name is a required field.');
      }

      if($('#email').val() == '') {
         arr_errors.push('Email is a required field.');
      }

      if($('#telephone').val() == '') {
         arr_errors.push('Telephone is a required field.');
      }

      if(arr_errors.length) {
         var str_alert_text = "Your entry form errors: \n";

         for(int_index in arr_errors) {
            str_alert_text = str_alert_text + " - " + arr_errors[int_index] + "\n";
         }

         str_alert_text = str_alert_text + '\nPlease correct the errors and try again.';

         alert(str_alert_text);
         return false;
      }

      // Email confirm
      if($('#email').val() != $('#confirm_email').val()) {
         alert('Email address must match Confirm email address');
         return false;
      }

      return true;
   };

   $(document).ready(function () {
	   initialise();
   });

	return {};
}());

