var ContactUs_Sending = false;

function ContactUs_Send () {
	if (ContactUs_Sending || !ContactUs_Verify_All())
		return;

	ContactUs_Sending = true;
	ContactUs_StateChange();
	
	ContactUs_Send_Request();
}

function ContactUs_Verify_YourName () {
	$('yourname').value = $('yourname').value.trim();
	var yourname_failed = ($('yourname').value.length == 0);
	$('v_yourname').style.color = (yourname_failed ? 'red' : '');
	return !yourname_failed;
}

function ContactUs_Verify_Email () {
	$('email').value = $('email').value.trim();
	var email_failed = (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test($('email').value));
	$('v_email').style.color = (email_failed ? 'red' : '');
	return !email_failed;
}

function ContactUs_Verify_Comments () {
	$('comments').value = $('comments').value.trim();
	var comments_failed = ($('comments').value.length == 0);
	$('v_comments').style.color = (comments_failed ? 'red' : '');
	return !comments_failed;
}

function ContactUs_Verify_All() {
	var fail = !ContactUs_Verify_YourName();
	fail = !ContactUs_Verify_Email() || fail;
	fail = !ContactUs_Verify_Comments() || fail;
	return !fail;
}

function ContactUs_StateChange() {
	$('yourname').disabled = ContactUs_Sending;
	$('email').disabled = ContactUs_Sending;
	$('comments').disabled = ContactUs_Sending;
	$('phone').disabled = ContactUs_Sending;
	$('eventtype').disabled = ContactUs_Sending;
	$('eventdate').disabled = ContactUs_Sending;
	$('btnSend').src = '/images/' + (ContactUs_Sending ? 'send_d.gif' : 'send.gif');
	$('btnSend').style.cursor = (ContactUs_Sending ? '' : 'pointer');
}

function ContactUs_Send_Request () {
	new Ajax.Request('/ajax/contact.asp', {
			method: 'post',
			parameters: {
				name:		$('yourname').value,
				email:		$('email').value,
				phone:		$('phone').value,
				eventtype:		$('eventtype').value,
				eventdate:		$('eventdate').value,
				comments:	$('comments').value
			},
			requestHeaders: {'Accept': 'application/json'},
			onFailure: function () {
				alert('There was an issue while processing your request.\n\nPlease try again later.');
				ContactUs_Sending = false;
				ContactUs_StateChange();
			},
			onSuccess: function (transport) {
				var requestFailed = false;
				try {
					json = transport.responseText.evalJSON();
				} catch (e) {
					requestFailed = true;
				}
				
				if (requestFailed) {
					alert('There was an issue while processing your request.\n\nPlease try again later.');
					ContactUs_Sending = false;
					ContactUs_StateChange();
				} else {
					if (json.success) {
						ContactUs_Send_Request_Succcess();
					} else {
						ContactUs_Verify();
						if (json.message) {
							alert(json.message);
						} else {
							alert('There was an issue while processing your request.\n\nPlease try again later.');
						}
						ContactUs_Sending = false;
						ContactUs_StateChange();
					}
				}
			}
		}
	);
}

function ContactUs_Send_Request_Succcess () {
	new Effect.BlindUp('quickcontact-content', {afterFinish: function () {new Effect.BlindDown('quickcontact-content-thankyou'); }});
}