// JavaScript Form Validation Document

/************************************************************************* 
* This document was adapted from a script by Christian Hielmann found at
* http://www.onlinetools.org/articles/unobtrusivejavascript/chapter5.html
**************************************************************************/

/************************************************************************
* This validation will check most field types
* It assumes that you have placed a hidden field in your form with the 
* name and id of 'required' and a value that lists all the names of fields
* in the form that are required separated by commas and no spaces.
* It also assumes that the form has an ID attribute and value.
* The form id and email field id must be delared below.
**************************************************************************/

function checkform()
		{
		// Declare your form and email field names here
			var frmToVal = "frmMain";
			var emailField = "txtEmail";
			
		// Test if DOM is available and there is an element called required
			if(!document.getElementById || !document.createTextNode){return;}
			if(!document.getElementById('required')){return;}
			
			// This listens for the onsubmit event when the form is submitted
			// Thus allowing us to remove the onsubmit event from the actual form
			document.getElementById(frmToVal).onsubmit = function() {

		// Define error messages and split the required fields
			var errorID='errormsg';
			var errorClass='error'
			var errorMsg='Please enter or change the fields marked with a ';
			var errorImg='/images/error.gif';
			var errorAlt='Error';
			var errorTitle='This field has an error!';
			var reqfields=document.getElementById('required').value.split(',');
			var of = document.forms[frmToVal]; 

			// Cleanup old errors that may have already been written to the page
			// 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;}
				if(f.previousSibling && /img/i.test(f.previousSibling.nodeName))
				{
					f.parentNode.removeChild(f.previousSibling);
				}
				f.className='';
			}
		// 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!=emailField){cf_adderr(f)}							
						// email is a special field and needs checking
						if(f.id==emailField && !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)
			{
				// create image, add to and colourise the error fields
				var errorIndicator=document.createElement('img');
				errorIndicator.alt=errorAlt;
				errorIndicator.src=errorImg;
				errorIndicator.title=errorTitle;
				o.className=errorClass;
				o.parentNode.insertBefore(errorIndicator,o);

			// Check if there is no error message
				if(!document.getElementById(errorID))
				{
				// create errormessage and insert before submit button
					var em=document.createElement('div');
					em.id=errorID;
					var newp=document.createElement('p');
					newp.appendChild(document.createTextNode(errorMsg))
					// clone and insert the error image
					newp.appendChild(errorIndicator.cloneNode(true));
					em.appendChild(newp);
					// find the submit button 
					for(var i=0;i<of.getElementsByTagName('input').length;i++)
					{
						if(/submit/i.test(of.getElementsByTagName('input')[i].type))
						{
							var sb=of.getElementsByTagName('input')[i];
							break;
						}
					}
					if(sb)
					{
						sb.parentNode.insertBefore(em,sb);
					}	
				} 
			}
			
			}//Closes the onsubmit listener
			
			function cf_isEmailAddr(str) 
			{
			    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
			}
		}
function checkPassword() {
	if (document.forms['frmMain'].txtPassword.value != document.forms['frmContact'].txtPassword1.value) {
		document.forms['frmMain'].txtPassword.select();
		document.forms['frmMain'].txtPassword1.value = "";
		alert('The entered passwords do not match. please re-enter the passwords.');
		return false;
	}
}