//***********************************************************
//Momo Validation W3C Complient Script						*
//Recoded from Dave Watkins orignal validation code.		*
//Author: Warren Buckley									*
//Compiled: 17/01/05										*
//***********************************************************

function checkData(strFormName)
{

	var strMsg = '';
	var booSubmit = true;
	var booMissing = false;
	var booInvalidType = false;
	var booInvalidEmail = false;
	var booInvalidDomain = false;
	var booRequiredCheckbox = false;
	var booDefaultMessage = false;
	var booInvalidPassword = false;
	var objForm = document.getElementById(strFormName)
	var objItem
	var strPassLength = '5'
	
	
	for(i=0; i<objForm.length; i++)
	{
		//This is now inside the loop to check for each item.
		objItem = objForm.item(i);
		var ID = objItem.getAttribute('id')
		
		switch (objItem.type)
			{
				case 'text' : case 'password' : case 'textarea' : case 'file' : case 'select-one' :
							
					//**********************************************************************************************
					//SET ALL FIELDS BACKGROUND COLOR WHEN CORRECTLY VALIDATED, MAKE THIS MATCH TO THE MAIN CSS
					objItem.style.backgroundColor = '#1A1A1A';
					//objItem.style.borderRight = '3px solid #BFBFBF';
					//objItem.style.backgroundImage = 'url(../Assets/Form_Bg.gif)'
					objItem.style.color = 'white'
					//**********************************************************************************************
			}
		
		
		//********************************************************
		//Debug Line to display the ID for each form item.
		//alert(ID)
		//********************************************************
		
		//********************************************************
		//Debug Line to display the number that indexOf returns.
		//alert(ID.indexOf('Validate'))
		//********************************************************
		
		
		
		
	
		if(ID.indexOf('NoValidate')==0)
		{
			//****************************************************
			//DO NOTHING as this field is not being validated
			//alert('This field does not need to be validated')
			//****************************************************
			
			//******************************************************************
			//START CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			if(objItem.value.substring(0,6) == 'Enter ')
			{
				booDefaultMessage = true
								
				//************************************************************
				//THIS AREA BELOW styles the text field box
				//************************************************************
				
				objItem.style.backgroundColor = '#FF9900';
				//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Orange.gif)'
				
				//************************************************************
				//END STYLING
				//************************************************************
			}
			//******************************************************************
			// STOP CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			
		}
		
		if(ID.indexOf('NumberValidate')==0)
		{
			//*******************************************************************************************
			//DEBUG line to able to see if field is a number field that contains the ID NumberValidate
			//alert('NUMBER FIELD')
			//*******************************************************************************************	
			
			//******************************************************************
			//START CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			if(objItem.value.substring(0,6) == 'Enter ')
			{
				booDefaultMessage = true
					
					
				//************************************************************
				//THIS AREA BELOW styles the text field box
				//************************************************************
					
				objItem.style.backgroundColor = '#FF9900';
				//objItem.style.borderRight = '3px solid #FF9900';
				//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Orange.gif)'
				//objItem.style.color = '#F58C06'
					
				//************************************************************
				//END STYLING
				//************************************************************
				}
				//******************************************************************
				// STOP CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
				//******************************************************************
				
				else
				{
					var sVal = new String(objItem.value);
					sVal = sVal.replace(/ /gi, '');
					if (sVal == '' || isNaN(sVal) == true)
					{
						//************************************************************
						//THIS AREA BELOW styles the number field box
						//************************************************************
					
						objItem.style.backgroundColor = '#FFFFFF';
						//objItem.style.borderRight = '3px solid yellow';
						//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Yellow.gif)';
						
						//************************************************************
						//END STYLING
						//************************************************************
						booInvalidType = true
					}
				}
							
		}
		
		if(ID.indexOf('TextValidate')==0)
		{
			//*******************************************************************************************
			//DEBUG line to able to see if field is a textfield that contains the ID TextValidate
			//alert('TEXT FIELD')
			//*******************************************************************************************
			
			//******************************************************************
			//START CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			if(objItem.value.substring(0,6) == 'Enter ')
			{
				booDefaultMessage = true
				
				
				//************************************************************
				//THIS AREA BELOW styles the text field box
				//************************************************************
				
				objItem.style.backgroundColor = '#FF9900';
				//objItem.style.borderRight = '3px solid #FF9900';
				//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Orange.gif)';
				objItem.style.color = 'black'
				
				//************************************************************
				//END STYLING
				//************************************************************
			}
			//******************************************************************
			// STOP CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			else
			{
				if (objItem.value == '' || objItem.value == 0)
				{
				
					//************************************************************
					//THIS AREA BELOW styles the text field box
					//************************************************************
					
					objItem.style.backgroundColor = '#FFFFFF';
					objItem.style.color = 'black'
					//objItem.style.borderRight = '3px solid Green';
					//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Blue.gif)';
					
					//************************************************************
					//END STYLING
					//************************************************************
					
					booMissing = true
				}
			}
			
		}

		if(ID.indexOf('PasswordValidate')==0)
		{
			//*******************************************************************************************
			//DEBUG line to able to see if field is a textfield that contains the ID TextValidate
			//alert('PASS FIELD')
			//*******************************************************************************************
			
			//******************************************************************
			//START CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			
			if(objItem.value.substring(0,6) == 'Enter ')
			{
				
				booDefaultMessage = true
				
				
				//************************************************************
				//THIS AREA BELOW styles the text field box
				//************************************************************
				
				objItem.style.backgroundColor = '#FF9900';
				//objItem.style.borderRight = '3px solid #FF9900';
				//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Orange.gif)';
				//objItem.style.color = '#F58C06';
				
				//************************************************************
				//END STYLING
				//************************************************************
			}
			//******************************************************************
			// STOP CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			else
			{
						
				//************************************************************************************************
				// TO CHANGE THE MINIMUM LENGTH OF THE PASSWORD CHANGE THE VALUE.LENGTH NUMBER and var strPassLength
				//************************************************************************************************
				if (objItem.value == '' || objItem.value == 0 || objItem.value.length < strPassLength)
				{
					
					
				
					//************************************************************
					//THIS AREA BELOW styles the password field box
					//************************************************************
					
					objItem.style.backgroundColor = '#FFFFFF';
					objItem.style.color = 'black'
					
					//************************************************************
					//END STYLING
					//************************************************************
					
					booInvalidPassword = true
				}
			}
		}	
		
		
		if(ID.indexOf('FileValidate')==0)
		{
			//*******************************************************************************************
			//DEBUG line to able to see if field is a Filepath that contains the ID FileValidate
			//alert('FILE FIELD')
			//*******************************************************************************************
			
			if (objItem.value == ''|objItem.value == 0)
			{
			
				//************************************************************
				//THIS AREA BELOW styles the file field box
				//************************************************************
				
				objItem.style.backgroundColor = '#FFFFFF';
				objItem.style.color = 'black'
				//objItem.style.borderRight = '3px solid #FF9900';
				//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Orange.gif)';
				//objItem.style.color = '#F58C06';
				
				//************************************************************
				//END STYLING
				//************************************************************
								
				booMissing = true
			}
			
		}
		
		if(ID.indexOf('EmailValidate')==0)
		{
			//*******************************************************************************************
			//DEBUG line to able to see if field is a email that contains the ID EmailValidate
			//alert('EMAIL FIELD')
			//*******************************************************************************************
			
			
			//******************************************************************
			//START CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			if(objItem.value.substring(0,6) == 'Enter ')
			{
				booDefaultMessage = true
				
				
				//************************************************************
				//THIS AREA BELOW styles the text field box
				//************************************************************
				
				objItem.style.backgroundColor = '#FF9900';
				//objItem.style.borderRight = '3px solid #FF9900';
				//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Orange.gif)';
				//objItem.style.color = '#F58C06';
				
				//************************************************************
				//END STYLING
				//************************************************************
			}
			//******************************************************************
			// STOP CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			else
			{
				rExp = /\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,5}/gi;
				checkMail = new String(objItem.value)
				if (checkMail.search(rExp) == -1)
				{
					//************************************************************
					//THIS AREA BELOW styles the email field box
					//************************************************************
					objItem.style.backgroundColor = '#FFFFFF';
					objItem.style.color = 'black'
					//objItem.style.borderRight = '3px solid red';
					//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Red.gif)';
					//objItem.style.color = 'black';
					//objItem.style.fontWeight = 'bold';
					
					//************************************************************
					//END STYLING
					//************************************************************
					
					booInvalidEmail = true
				}
			}
			
			
			
		
		}
		
		if(ID.indexOf('DomainValidate')==0)
		{
			//*******************************************************************************************
			//DEBUG line to able to see if field is a domain that contains the ID DomainValidate
			//alert('DOMAIN FIELD')
			//*******************************************************************************************
			
			//******************************************************************
			//START CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			if(objItem.value.substring(0,6) == 'Enter ')
			{
				booDefaultMessage = true
				
				
				//************************************************************
				//THIS AREA BELOW styles the text field box
				//************************************************************
				
				objItem.style.backgroundColor = '#FF9900';
				//objItem.style.border = '1px solid #FF9900';
				//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Orange.gif)';
				//objItem.style.color = '#F58C06';
				
				//************************************************************
				//END STYLING
				//************************************************************
			}
			//******************************************************************
			// STOP CHECK FOR DEFAULT VALUE OF ENTER IN FIELD
			//******************************************************************
			else
			{
				rExp = /[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,5}/gi;
				checkMail = new String(objItem.value)
				if (checkMail.search(rExp) == -1)
				{
					//************************************************************
					//THIS AREA BELOW styles the Domain field box
					//************************************************************
					
					objItem.style.backgroundColor = '#FFFFFF';
					objItem.style.color = 'black'					
					//objItem.style.border = '1px solid green';
					//objItem.style.backgroundImage = 'url(../Assets/Form_Bg_Blue.gif)';
					
					//************************************************************
					//END STYLING
					//************************************************************
					
					booInvalidDomain = true
				}
			}
			
		}
	}
	
	
if (booInvalidType == true)
	{
		booSubmit = false;
		strMsg = 'You entered text or nothing into a required number field which has been marked yellow.\n'
	}

	if (booInvalidEmail == true)
	{
		booSubmit = false;
		strMsg = strMsg + 'You entered an invalid email address which has been marked white.\n'
	}

	if (booInvalidDomain == true)
	{
		booSubmit = false;
		strMsg = strMsg + 'You entered an invalid domain name which has been marked grey.\n'
	}
	
	if (booMissing == true)
	{
		booSubmit = false;
		strMsg = strMsg + 'You missed one or more fields which has now been marked white.\n'
	}
	
	if (booRequiredCheckbox == true)
	{
		booSubmit = false;
		strMsg = strMsg + 'You must tick the check boxes marked red.\n'
	}
	
	if (booDefaultMessage == true)
	{
		booSubmit = false;
		strMsg = strMsg + 'You did not change the text inside the field which has now been marked orange.\n'
	}
	
	if (booInvalidPassword == true)
	{
		booSubmit = false;
		strMsg = strMsg + 'You have not entered a password or have not entered at least ' + strPassLength + ' characters, which has now been marked green.' 
	}
		
	if (booSubmit == true)
	{
		return true;
	}
	else
	{
		alert(strMsg)
		return false;
	}

}

function postForm(strFormName)
{
	if (checkData(strFormName))
		document.getElementById(strFormName).submit();
	else
		return false;
}