var QuickContact_Sending = false;
var ClearedComments = false;

function QuickContact_Comments_onfocus() {
	if (($('comments').value == '(Enter Comments Here)') && !ClearedComments) {
		ClearedComments = true;
		$('comments').value = '';
	}
}

function QuickContact_Send () {
	if (QuickContact_Sending || !QuickContact_Verify_All())
		return;

	QuickContact_Sending = true;
	QuickContact_StateChange();
	
	QuickContact_Send_Request();
}

function QuickContact_Verify_Name () {
	$('name').value = $('name').value.trim();
	var name_failed = ($('name').value.length == 0);
	$('v_name').style.color = (name_failed ? 'red' : '');
	return !name_failed;
}

function QuickContact_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 QuickContact_Verify_Phone () {
	$('phone').value = $('phone').value.trim();
	var phone_failed = $('phone').value.replace(/[^\d]/g, '').length < 10;
	$('v_phone').style.color = (phone_failed ? 'red' : '');
	return !phone_failed;
}

function QuickContact_Verify_Comments () {
	QuickContact_Comments_onfocus();
	$('comments').value = $('comments').value.trim();
	var comments_failed = ($('comments').value.length == 0);
	$('v_comments').style.color = (comments_failed ? 'red' : '');
	return !comments_failed;
}

function QuickContact_Verify_All () {
	var fail = !QuickContact_Verify_Name();
	fail = !QuickContact_Verify_Email() || fail;
	fail = !QuickContact_Verify_Phone() || fail;
	fail = !QuickContact_Verify_Comments() || fail;
	return !fail;
}

function QuickContact_StateChange() {
	$('name').disabled = QuickContact_Sending;
	$('email').disabled = QuickContact_Sending;
	$('phone').disabled = QuickContact_Sending;
	$('comments').disabled = QuickContact_Sending;
	$('btnSend').src = '/images/' + (QuickContact_Sending ? 'send_d.gif' : 'send.gif');
	$('btnSend').style.cursor = (QuickContact_Sending ? '' : 'pointer');
}

function QuickContact_Send_Request () {
	new Ajax.Request('/ajax/quickcontact/', {
			method: 'post',
			parameters: {
				name:		$('name').value,
				email:		$('email').value,
				phone:		$('phone').value,
				comments:	$('comments').value
			},
			requestHeaders: {Accept: 'application/json'},
			onFailure: function () {
				alert('There was an issue while processing your request.\n\nPlease try again later.');
				QuickContact_Sending = false;
				QuickContact_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.');
					QuickContact_Sending = false;
					QuickContact_StateChange();
				} else {
					if (json.success) {
						QuickContact_Send_Request_Succcess();
					} else {
						QuickContact_Verify();
						if (json.message) {
							alert(json.message);
						} else {
							alert('There was an issue while processing your request.\n\nPlease try again later.');
						}
						QuickContact_Sending = false;
						QuickContact_StateChange();
					}
				}
			}
		}
	);
}

function QuickContact_Send_Request_Succcess () {
	new Effect.Fade('quickcontact-body-content');
	new Effect.Appear('quickcontact-body-thankyou', {queue: 'end'});
}
