/*
contact_form.js (this script) copyright (c) Steve Tucker 2006.
Pretty please do not use this script without my knowledge and permission
but by all means feel free to read through it for educational and learning purposes.
Cheers!
*/

function prepareContactForm() {
	if (!document.getElementById) return false;
	if (!document.getElementById('contact_form_send_button')) return false
	
	var $contact_form_button = document.getElementById('contact_form_send_button');
	var $fields = Array();
	$fields[0]	= document.getElementById('contact_form_name_field');
	$fields[1]	= document.getElementById('contact_form_email_field');
	$fields[2]	= document.getElementById('contact_form_question_field');
	
	$contact_form_button.onclick = function() {
		submitContactForm();
		return false;
	}
	for (var $i=0; $i < $fields.length; $i++) {
		$fields[$i].onfocus = function() {
			$active_mouseout = false;
		}
		$fields[$i].onblur = function() {
			$active_mouseout = true;
		}
	}
}
addLoadEvent(prepareContactForm);


function submitContactForm() {
	var $fields = Array();
	$fields['name']		= document.getElementById('contact_form_name_field').value;
	$fields['email']	= document.getElementById('contact_form_email_field').value;
	$fields['question']	= document.getElementById('contact_form_question_field').value;
	
	if (!$fields['name'] || !$fields['email'] || !$fields['question']) {
		alert('Please complete all the fields');
		return false;
	}

	var $post_string = 'a=post_contact_form&name='+$fields['name']+'&email='+$fields['email']+'&question='+$fields['question'];
	
	createXMLHttpRequest();
	$xmlHttp.open("POST", "scripts/functions.php", true);
	$xmlHttp.onreadystatechange = submitContactForm_handleStateChange;
	$xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	$xmlHttp.send($post_string);
	return false;
}


function submitContactForm_handleStateChange() {
	if ($xmlHttp.readyState == 4) {
		if ($xmlHttp.status == 200) {
			if ($xmlHttp.responseText == 'email_error') {
				var $email_field_header = document.getElementById('email_field_header');
				$email_field_header.className = 'row_error';
				alert('The email address you entered is not valid. Please try again');
				if (document.getElementById('contact_form_loading_icon')) {
					var $loading_icon = document.getElementById('contact_form_loading_icon');
					removeNode($loading_icon);
				}
				return false;
			}
			else {
				var $contact_form_container = document.getElementById('contact_form_container');
				var $paragraph = document.createElement('p');
				var $text = document.createTextNode('Great - thanks for helping to fill my inbox with more meaningful messages than just the usual spam. I\'ll get back to you as soon as I can.');
				$paragraph.appendChild($text);
				innerXHTML($contact_form_container,' ');
				$contact_form_container.appendChild($paragraph);
				return true;
			}
		}
	}
	else if ($xmlHttp.readyState == 1) {
		var $contact_form_container = document.getElementById('contact_form_container');
		var $loading_icon = document.createElement('img');
		$loading_icon.setAttribute('src','media/icons/ajax_loading.gif');
		$loading_icon.setAttribute('id','contact_form_loading_icon');
		$contact_form_container.appendChild($loading_icon);
	}
}