var RecaptchaOptions = {
    theme : 'clean'
 };

jQuery.fn.charityContactus = function(e) {

	$( "#contactusEmail" ).val( jQuery.trim( $( "#contactusEmail" ).val() ) );
	$( "#contactusMessage" ).val( jQuery.trim( $( "#contactusMessage" ).val() ) );
	
	if ( !$( "#contactusEmail" ).val().length || !$( "#contactusMessage" ).val().length )
	{
		$("#contactusError").html("Please fill in the required fields!");
		if ( !$( "#contactusEmail" ).val().length )
		{
			$( "#contactusEmail" ).css( "background-color", "yellow" );
		}
		if ( !$( "#contactusMessage" ).val().length )
		{
			$( "#contactusMessage" ).css( "background-color", "yellow" );
		}
		e.preventDefault();
		return false;
	}
	
	emailReg = /^[A-Za-z0-9._%+-]+@([A-Za-z0-9-]+\.)+([A-Za-z0-9]{2,4}|museum)$/;
	if ( !emailReg.test( $( "#contactusEmail" ).val() ) )
	{
		$("#contactusError").html("Please enter a valid email address!");
		$( "#contactusEmail" ).css( "background-color", "yellow" );
		e.preventDefault();
		return false;
	}
	
	var dataString = $(this).serialize();
	$("#charityContactus_form input[type='text']").attr("disabled", "disabled");
	$("#charityContactus_form textarea").attr("disabled", "disabled");
	$.ajax({
		type: "POST",
		url: $("#charityContactus_form").attr( "action" ),
		data: dataString,
		dataType: 'json',
		success: function(data) {
			if ( data.ip == false ) {
				$("#charityContactus_form input[type='text']").removeAttr("disabled");
				$("#charityContactus_form textarea").removeAttr("disabled");
				$('.contactusState').html('Your IP address is blacklisted by YASB.');
				$('#recaptcha_response_field').val('');
			} else if ( data.captcha == false ) {
				$("#charityContactus_form input[type='text']").removeAttr("disabled");
				$("#charityContactus_form textarea").removeAttr("disabled");
				Recaptcha.reload();
				$('.contactusState').html('Please do another reCAPTCHA.');
			} else if ( data.clean == false ) {
				$("#charityContactus_form input[type='text']").removeAttr("disabled");
				$("#charityContactus_form textarea").removeAttr("disabled");	
				$('.contactusState').html('Neither URLs nor HTML are allowed, sorry.');
			} else if ( data.log == false ) {
				$("#charityContactus_form input[type='text']").removeAttr("disabled");
				$("#charityContactus_form textarea").removeAttr("disabled");
				$('.contactusState').html('There was an internal error, please try again.');
			} else if ( data.sent == false ) {
				$("#charityContactus_form input[type='text']").removeAttr("disabled");
				$("#charityContactus_form textarea").removeAttr("disabled");
				$('.contactusState').html('There was a problem sending your message, please try again.');
			} else {
				$("#charityContactus_form input[type='text']").removeAttr("disabled");
				$("#charityContactus_form textarea").val("").removeAttr("disabled");
				$("#contact-details").dialog( "close" );
			}
		}
	});
	e.preventDefault();
	return false;
}

