$(function() {
	$('#validatePromoCodeButton').click(function(){
		validatePromoCode();
		
	});
	
});

function validatePromoCode() {
	var code = $.trim($('#promoCode').val());
	
	if(code == '') {
		return;
		
	}

	var $promoCodeFlash = $('#promoCodeFlash').html('').hide();
		
	$.ajax({
		type: 'GET',
		url: context + '/payments/checkPromotionCode?code=' + code,
		dataType: 'json',
		beforeSend: function() {
			$('#gridLoadingDiv').show();
			$('#validatePromoCodeButton').hide();
			
		},
		success: function(data) {
			switch(data.status) {
				case 'invalid':
					$promoCodeFlash
						.html(data.msg)
						.css('color', 'red')
						.show()
						.effect('highlight', null, 2000, null);
					break;

				case 'valid':
					$promoCodeFlash
						.html(data.msg)
						.css('color', '#349F15')
						.show()
						.effect('highlight', null, 2000, null);
					
					if (data.redirectToPlan !== undefined)
						window.location = context + data.redirectToPlan;
					
					break;
					
			}
			
		},
		complete: function() {
			$('#gridLoadingDiv').hide();
			$('#validatePromoCodeButton').show();
			
		}
		
	});	
	
}