function validate_field(obj)
{

	//add onkeyup validation
	obj.setAttribute('onkeyup', 'validate_field(this)');
	
	var el = obj.getAttribute('id');
	var id = el.split('_')[1];
	var val = obj.value;
	
	var parent = obj.parentNode;
	var gParent = parent.parentNode;
	var para = document.createElement('p');
	var curr_class = gParent.className;
	var error;
	
	// boolean error check
	var hasErrors = true;
	
	// if name fields object and field contains text characters
	if( (id == 'fname' || id == 'lname') && alpha(val))
	{			
		// value does not have errors
		hasErrors = false;
	} 
	
	// if email is object and valid
	if( (id == 'email') && isValidEmail(val))
	{			
		// value does not have errors
		hasErrors = false;
	}
	
	// if comments object and value is not empty
	if( (id == 'comments') && (val != '') )
	{			
		// value does not have errors
		hasErrors = false;
	}
	
	// check to see if fields empty or there are errors present
	if (val == '' || hasErrors )
	{ 
		//if first or last name
		if (id == 'fname' || 'lname')
		{
			error = document.createTextNode('Invalid name entry');
		}
		
		//if email address
		if (id == 'email')
		{
			error = document.createTextNode('Please provide a valid email address');
		}
		
		//if a comment
		if (id == 'comments')
		{
			error = document.createTextNode('Please fill out a comment');
		}		
		
		if (obj.nextSibling)
		{
			//if message already exisits erase it, prevents error message doubling up
			parent.removeChild(parent.lastChild);
		}
		
		//append error msg to field
		para.className = "error";
		para.appendChild(error);
		parent.appendChild(para);
		
		//assign error class to the li
		gParent.className = curr_class + " error";
		
		return false
	}
	
	//if there are no errors
	else
	{
		//if an error message does not exisist stop
		if (obj.nextSibling) {
		
			//if an error message exists
			if (obj.nextSibling.className == "error")
			{			
				//get any classes associated with the element
				grabClass(gParent);
				parent.removeChild(parent.lastChild);
			}
		}
		
		return true;
	}
}


function clearTree()
{
	var form = document.getElementById('contact_form');
	var li = form.getElementsByTagName('li');
	
	for (i=0; i < li.length-1; i++)
	{
		var gParent = li[i];
		var parent = gParent.childNodes[3];
		var error = parent.lastChild;
		
		grabClass(gParent);
		
		if (error.className == 'error') parent.removeChild(error);
	}
}


function validateSubmission()
{
	var successCount = 0;
	//var objectCheckArray = new Array();
	var input = document.getElementsByTagName('input');
	var textarea = document.getElementsByTagName('textarea');
	var maxCount = 0;
	//alert('input: ' + input.length + '\n' + 'textarea: ' + textarea.length);
	
	for (i=0; i < input.length; i++ )
	{
		if (input[i].className == 'text')
		{
			maxCount++;
			if (validate_field(input[i])) successCount++;
		}
	}
	
	for (i=0; i < textarea.length; i++ )
	{
		if (textarea[i].className == 'text')
		{
			maxCount++;
			if (validate_field(textarea[i])) successCount++;
		}
	}
	
	if (successCount != maxCount) return false;
	
	sendMsg();
}


function sendMsg() {

	CreateRequest();
	
	var fname = document.getElementById('field_fname').value;
	var lname = document.getElementById('field_lname').value;
	var email = document.getElementById('field_email').value;
	var msg = document.getElementById('field_comments').value;
	
	var url = "components/php/formMail.php?";
	url += "&fname=" + fname;
	url += "&lname=" + lname;
	url += "&email=" + email;
	url += "&msg=" + msg;
	
	request.open('GET', url, true);
	request.onreadystatechange = showSuccess;
	request.send(null);

}



function showSuccess()
{
	if (request.readyState == 4)
	{
		if (request.status == 200)
		{			
			var container = document.getElementById('form_container');
			var para = document.createElement('p');
			var text = document.createTextNode('Thankyou, your message has been delivered.');
		
			//clear form container elements
			Remove_Children(container);
	
			//show confirmation message
			para.appendChild(text);
			container.appendChild(para);
		}
	}
	else
	{
		var form = document.getElementById('form_container');
		form.className = "loading";
	}
}


function grabClass(obj) 
{
	//check if a class name exisits, if it does is it an error class
	if (!obj.className || (obj.className == 'error') == -1) return false;
	var class_split = obj.className.split('error');
	
	if (class_split[0] != '')
	{
		obj.className = class_split[0];
	}
	else
	{
		obj.removeAttribute('class');
	}
}


function alpha(val)
{
	regEx = /[^A-Za-z\s\s\-\']/;
	
	//check if value exists & run it against the regular exp
	if( (val.length > 0 ) && ( regEx.test( val ) ) )
	{
		return false;
	}
	else return true;
}


function isValidEmail( val )
{
	regEx1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	regEx2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	
	//check if value exists & run it against both regular expressions
	if( ( val.length > 0 ) && ( !( !regEx1.test( val ) && regEx2.test( val ) ) ) )
	{
		return false;
	}
	else return true;
}










