/* 	REFERENCES:

	Unobtrusive JavaScript self training course, Chapter 5
	http://www.onlinetools.org/articles/unobtrusivejavascript/chapter5.html
	
	inserAfter function by 1man
	http://snipplr.com/view/2107/insertafter-function-for-the-dom
	
	Quirksmode W3C DOM - Form error messages
	http://www.quirksmode.org/dom/error.html
	
*/

function checkform(of)
	{
	// Test if DOM is available and there is an element called required
		if(!document.getElementById || !document.createTextNode){return;}
		if(!document.getElementById('required')){return;}
	
	// Define error messages and split the required fields
		var errorID='errormsg';
		var errorClass='error'
		var errorMsg='Please note that all fields are required to send your message.';
		var reqfields=document.getElementById('required').value.split(',');

	// Cleanup old mess
		// if there is an old errormessage field, delete it
		if(document.getElementById(errorID))
		{
			var em=document.getElementById(errorID);
			em.parentNode.removeChild(em);
		}
		// remove old images and classes from the required fields
		for(var i=0;i<reqfields.length;i++)
		{
			var f=document.getElementById(reqfields[i]);
			if(!f){continue;}
			f.parentNode.className='noerror';
		}
	// loop over required fields
		for(var i=0;i<reqfields.length;i++)
		{
	// check if required field is there
			var f=document.getElementById(reqfields[i]);
			if(!f){continue;}
	// test if the required field has an error, 
	// according to its type
			switch(f.type.toLowerCase())
			{
				case 'text':
					if(f.value=='' && f.id!='email'){cf_adderr(f)}							
	// email is a special field and needs checking
					if(f.id=='contactemail' && !cf_isEmailAddr(f.value)){cf_adderr(f)}							
				break;
				case 'textarea':
					if(f.value==''){cf_adderr(f)}							
				break;
				case 'checkbox':
					if(!f.checked){cf_adderr(f)}							
				break;
				case 'select-one':
					if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f)}							
				break;
			}
		}
		return !document.getElementById(errorID);
		
		/* Tool methods */
		function cf_adderr(o)
		{
			// add class to error input fields
			o.parentNode.className=errorClass;

		// Check if there is no error message
			if(!document.getElementById(errorID))
			{
			// Create errormessage and insert after submit button
				var em=document.createElement('p');
				em.id=errorID;
				em.appendChild(document.createTextNode(errorMsg))
				var sb=document.getElementById('contactformmessage');
				
				// Target is what you want it to go after. Look for this elements parent.
				var parent = sb.parentNode;	
				// If the parents lastchild is the targetElement...
				if(parent.lastchild == sb) {
				// Add the newElement after the target element.
					parent.appendChild(em);
					} else {
					// Else the target has siblings, insert the new element between the target and it's next sibling.
					parent.insertBefore(em, sb.nextSibling);
				}			

			} 
		}
		function cf_isEmailAddr(str) 
		{
		    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
		}
	}
	
if(document.getElementById){
	window.onload=trigger
}	
	
// Anal retentive seperation of structure and presentation	
function trigger() {
	document.getElementById('contactform').onsubmit = function (){
		return checkform(this)
	}
}
