$(document).ready(function() {
	
	$('#comment').textLimiter(160);
	
	//if submit button is clicked
	$('#submit').click(function () {		

		//Get the data from all the fields
		var fullName = $('input[name=full_name]');
		var emailAddress = $('input[name=email_address]');
		var emailAddressConfirm = $('input[name=email_address_confirm]');
		var location = $('input[name=location]');
		var comment = $('textarea[name=comment]');
		var displayName = $('input[name=display_name]');
		var keepInformed = $('input[name=keep_informed]');

		//Simple validation to make sure user entered something
		//If error found, add highlight class to the text field
		if (emailAddress.val()=='') {
			emailAddress.addClass('highlight');
			return false;
		} else emailAddress.removeClass('highlight');
		
		if (emailAddressConfirm.val()=='') {
			emailAddressConfirm.addClass('highlight');
			return false;
		} else emailAddressConfirm.removeClass('highlight');
		
		if (!IsValidEmail(emailAddress.val())) {
			emailAddress.addClass('highlight');
			return false;
		} else emailAddress.removeClass('highlight');

		if (!IsValidEmail(emailAddressConfirm.val())) {
			emailAddressConfirm.addClass('highlight');
			return false;
		} else emailAddressConfirm.removeClass('highlight');
		
		if (emailAddress.val() != emailAddressConfirm.val()) {
			emailAddress.addClass('highlight');
			emailAddressConfirm.addClass('highlight');
			return false;
		} else {
			emailAddress.removeClass('highlight');
			emailAddressConfirm.removeClass('highlight');
		}

		if (fullName.val()=='') {
			fullName.addClass('highlight');
			return false;
		} else fullName.removeClass('highlight');

		if (location.val()=='') {
			location.addClass('highlight');
			return false;
		} else location.removeClass('highlight');
		
		if(displayName.attr('checked')) displayNameValue = '1';
		else displayNameValue = '';

		if(keepInformed.attr('checked')) keepInformedValue = '1';
		else keepInformedValue = '';

		//organize the data properly
		var data = 'full_name=' + fullName.val() + '&email_address=' + emailAddress.val() + '&location=' + location.val() + '&comment='  + encodeURIComponent(comment.val()) + '&display_name=' + displayNameValue + '&keep_informed=' + keepInformedValue;

		//disabled all the text fields
		$('.text').attr('disabled','true');

		//show the loading sign
		$('.loading').show();

		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/index/pledge",

			//GET method is used
			type: "POST",

			//pass the data			
			data: data,		

			//Do not cache the page
			cache: false,

			//success
			success: function (result) {				

				$('.loading').hide();
				$('.pledgebox-inner').html(result);					
				
			}		
		});

		//cancel the submit button default behaviours
		return false;
	});
	
	function IsValidEmail(email) {
		
		var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		return filter.test(email);
	}
	
});	

