
$(document).ready(function() {
    
    var successCallback= function(result) {
       if (result.errors) {
           var errors=result.errors;
           var s="<ul>";
           for (i=0; i<errors.length; i++) {
           	s+="<li>"+errors[i]+"</li>";
           }
           s+='</ul>';
           $('#status_div').html(s);
       } else {
           s='<h1>Congratulations!</h1><br><br';
           s+='<p>You have been awarded '+result.points+' Qantas Frequent Flyer points and have entered the grand prize draw.</p>';
           s+='<br><p>Your points will be credited to your Qantas Frequent Flyer account within 7 days.</p>'; 
           s+='<p>Your Qantas Frequent Flyer number is:'+result.ff_number+'</p>';
           s+='<p>Your Unique Code is:'+result.promo_code+'</p>';
           s+='<br><p>For all the latest Qantas Frequent Flyer member offers visit <a href="http://www.qantas.com/memberspecials" title="Member Specials" target="_blank">qantas.com/memberspecials</a></p>';
           s+='<br><br><br><br><br>';
           s+='<button id="back" onclick="location.href=\'index.jsp\';return false;"><span>Return to Home page</span></button>&nbsp;&nbsp;';
           s+='<br><br><br>';
           
           $('#form_div').html(s);
           
           setTimeout("location.href='index.jsp'",300000);
       }

       $('#submit').attr("disabled", false);
	   $('#back').attr("disabled", false);
		
    };
  
    var errorCallback= function (xhr, ajaxOptions, thrownError){
       $('#status_div').html('A system error has occurred ('+ xhr.status + ')');
       $('#submit').attr("disabled", false);
	   $('#back').attr("disabled", false);
	};  
    		  
	$("#submit").click(function() {
		// disable buttons
		$('#submit').attr("disabled", true);
		$('#back').attr("disabled", true);
		
		// show status
		$('#status_div').html("<img src='images/processing_stat.gif' />&nbsp;&nbsp;Processing...");
		
		var promocode = $("input#promocode").val();
		var ffnumber = $("input#ffnumber").val();
		var mobile = $("input#mobile").val();
		var email = $("input#email").val();
		var consent= $("input#consent").is(':checked');

		var dataString = 'channel=web&promo_code='+ promocode + 
		                 '&ff_number=' + ffnumber + 
		                 '&mobile=' + mobile +
		                 '&email=' + email +
		                 '&consent=' + consent;
		$.ajax({
		      type: "POST",
		      url: "coupons",
		      data: dataString,
		      dataType: "json",
		      success: successCallback,
		      error:errorCallback
		});
    	return false;
	});
});

