function check_dollar_value(el) {
	//check value of donation
	amt = String(el.val()).replace(/\s*/g,'');
	if ( !/^\$?[0-9]+(\.[0-9]{2})?$/.test(amt) ) {
		return false;
	}
	if ( !/\.[0-9]{2}$/.test(amt) ) {
		amt = amt+'.00';
	}
	amt = amt.replace(/^\$/,'');
	//check min amount...
	if (Number(amt) < 10) {
		return false;
	}
	el.val(amt);
	return true;
}

function check_email_value(el) {
	email = el.val();
	//email = email.replace(/\s*/g,'');
	//Semi RFC 2822 compliant...
	result = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/.test(email);
	return result;
}

$(document).ready(function() {
	fake_error = $('<div id="form_errors"></div>');
	fake_error.hide();
	$('body').append(fake_error);
	/*$('form#donation_form').attr({
		target: '_blank'
	});*/
	$('form#donation_form input.submit-btn').click(function(){
		$('div#form_errors').fadeOut(300,function(){
			$('div#form_errors').remove();
			document.required_fields = [];
			$('form#donation_form label.required').each(function(){
				document.required_fields.push('[name='+$(this).attr('for')+']');
			});
			document.required_fields = document.required_fields.join(',');
			document.form_errors = [];
			$(document.required_fields).each(function() {
				$this = $(this);
				error = false;
				error_message = $this.attr('name')+' is required.';
				switch ( $this.attr('name') ) {
				case 'Donation':
					if ( !check_dollar_value($this) ) {
						error = true;
						error_message = 'The specified amount doesn\'t look like a monetary value.';
					}
				break;
				case 'Email':
					if ( !check_email_value($this) ) {
						error_message = 'Please ensure you have entered your email address correctly.';
						error = true;
					}
				break;
			    case 'FirstName':
			        error_message = 'First Name is required.';
				default:
					val = $this.val();
					val = val.replace(/[^a-z0-9]/gi,'');
					if ( val == '' ) {
						error = true;
					}
				break;
				}
				if ( error ) {
					$([this,$('label[for='+$this.attr('name')+']')[0]]).addClass('err');
					document.form_errors.push(error_message);
				} else {
					$([this,$('label[for='+$this.attr('name')+']')[0]]).removeClass('err');
				}
			});
			if ( document.form_errors.length > 0 ) {
				error_notification = $('<div id="form_errors"><h4></h4><ol></ol></div>');
				error_notification.hide();
				$('form#donation_form').prepend(error_notification);
				$('div#form_errors h4').text('There were errors processing your submission');
				$(document.form_errors).each(function(){
					$('div#form_errors ol').append('<li>'+this+'</li>');
				});
				$('div#form_errors').fadeIn(500);
			} else {
				overlay = $('<div><img src="images/ajax_loading.gif" /><br />Sending data...</div>');
				pos = $('form#donation_form').offset();
				overlay.css({
					width: $('form#donation_form').width(),
					height: $('form#donation_form').height(),
					position: 'absolute',
					left: pos.left,
					top: pos.top,
					background: '#ffffff',
					textAlign: 'center',
					paddingTop: '1em'
				});
				overlay.animate({
					opacity: 0
				},1,function(){
					$this = $(this);
					$('body').append(this);
					if ( $.browser.msie && $.browser.version == 6 ) {
						$('form#donation_form select').hide();
					}
					$this.animate({
						opacity: 0.85
					},300,function() {
						$('form#donation_form').submit();
					});
				});
			}
		});
		return false;
	});
});
