// contact.js

validateForm();

function validateForm() {
	if (!document.getElementById || !document.createTextNode) { return; }
	elements = ['first_name', 'last_name',  'subject', 'message'];
	hiliteElements();
	
	elements.each(
		function(element) {
			$(element).onblur =  requiredField;
		}
	);
	
	document.getElementById('email').onblur = emailField;
	
}




// -----------------------------------------------------------------------------
// Highlight the elements with a YFT when clicked or tabbed to
// -----------------------------------------------------------------------------


function hiliteElements() {
elementCount = elements.length;
for(i=0;i<elementCount;i++) {
	$(elements[i]).idx = elements[i];
	$(elements[i]).onfocus = function() { hiliteMe(this.idx); };
}

//document.getElementById('first_name').onclick = function() { hiliteMe(this); };

Field.activate('first_name');
}


// -----------------------------------------------------------------------------
// The actual hilight effect
// -----------------------------------------------------------------------------
function hiliteMe(which) {
	new Effect.Highlight(which);
}



// -----------------------------------------------------------------------------
// pre-validate the required fields
// -----------------------------------------------------------------------------


function requiredField() {

	if($F(this).length < 1)  {
		if(!$(this).nextSibling) new Insertion.After(this, '<span class="notice"></span>');
		$(this).nextSibling.innerHTML = 'Required Field';
	} else {
		Element.remove($(this).nextSibling);
		//$('first_name').nextSibling.innerHTML = '';
	}

}


// -----------------------------------------------------------------------------
// pre-validate the e-mail field
// -----------------------------------------------------------------------------
function emailField() {

	var regex = /^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z.]{2,4}$/;


	if($F(this).length < 1)  {
		if(!$(this).nextSibling) new Insertion.After(this, '<span class="notice"></span>');
		$(this).nextSibling.innerHTML = 'Required Field';
	} else if(!regex.test($F(this))) {
		if(!$(this).nextSibling) new Insertion.After(this, '<span class="notice"></span>');
		$(this).nextSibling.innerHTML = 'E-mail address may be incorrect';
		new Effect.Shake(this);
	} else {
		Element.remove($(this).nextSibling);
		//$('first_name').nextSibling.innerHTML = '';
	}
}