/*
Form validation scripts used for checking user input
author: Remo Grand
date: 28/04/09
*/

	function changeClassNameHide(divName,className) // function to change the className of a page element
	{
		document.getElementById("newsBox1").className="newsBox";
		document.getElementById("newsBox2").className="newsBox";
		document.getElementById("newsBox3").className="newsBox";
		document.getElementById("newsBox4").className="newsBox";
		document.getElementById("newsBox5").className="newsBox";
		document.getElementById("newsBox6").className="newsBox";
		document.getElementById("newsBox7").className="newsBox";
		document.getElementById("newsBox8").className="newsBox";
		
		document.getElementById(divName).className=className;
	} // end of function showDiv
	


	function changeClassName(divName,className) // function to change the className of a page element
	{	
		document.getElementById(divName).className=className;
	} // end of function showDiv
	
	function agreeToTaC(enteredValue) // function to change the className of a page element
	{
		if(enteredValue.length == 0){
			document.getElementById("continueBtn").className="hideContinueBtn";
		} else {
			document.getElementById("continueBtn").className="showContinueBtn";
		}
		
	} // end of function agreeToTaC


        function emptyField(enteredValue,fieldName) // function to check if an input field is empty
	{
		if(enteredValue.length == 0) // if the entered value is is equal to 0 (the user hasnt input anything) send error message
		{
			document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/cross.gif' alt='' />Required";
			document.getElementById('err_' + fieldName).className="errMsg";
		}
		else // else send success message
		{
			document.getElementById('err_' + fieldName).innerHTML="";
		}
		return true;
	} // end of function emptyField
	
	
	
	
	function lettersOnly(enteredValue,fieldName) // function to check if an input field contains letters only
	{
		if(enteredValue.length == 0) // if the entered value is is equal to 0 (the user hasnt input anything) send error message
		{
			emptyField(enteredValue,fieldName);
		}
		else // else check that the value is letters only
		{
			var lettersExp = /^[a-zA-Z]+$/; // set the regular expression and store in a variable called lettersExp
			if(!lettersExp.test(enteredValue)) // test enteredValue with lettersExp to see if it is !NOT letters only, if true send error message
			{
				document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/cross.gif' alt='' />Letters only";
				document.getElementById('err_' + fieldName).className="errMsg";
			}
			else // else send success message
			{
				document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/tick.gif' alt='' />";
			}
			return true;
		}
	} // end of function lettersOnly
	
	
	
	
	function validEmail(enteredValue,fieldName) // function to check if an input field contains a valid email address
	{
		if(enteredValue.length == 0) // if the entered value is is equal to 0 (the user hasnt input anything) send error message
		{
			emptyField(enteredValue,fieldName);
		}
		else // else check that the value is valid characters only
		{
			var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/; // set the regular expression and store in a variable called emailExp
			if(!emailExp.test(enteredValue)) // test enteredValue with emailExp to see if it is !NOT valid characters only, if true send error message
			{
				document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/cross.gif' alt='' />Valid email please <br /> e.g. name@yourweb.com";
				document.getElementById('err_' + fieldName).className="errMsg";
			}
			else // else send success message
			{
				document.getElementById('err_' + fieldName).innerHTML="";
			}
			return true;
		}
	} // end of function validEmail
	
	
	
	
	function numbersOnly(enteredValue,fieldName) // function to check if an input field contains numbers only
	{
		if(enteredValue.length == 0) // if the entered value is is equal to 0 (the user hasnt input anything) send error message
		{
			emptyField(enteredValue,fieldName);
		}
		else // else check that the value is numbers only
		{
			var numbersExp = /^[0-9\(\)\-\s+]+$/; // set the regular expression and store in a variable called numbersExp
			if(!numbersExp.test(enteredValue)) // test enteredValue with numbersExp to see if it is !NOT numbers only, if true send error message
			{
				document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/cross.gif' alt='' />Numbers only";
				document.getElementById('err_' + fieldName).className="errMsg";
			}
			else // else send success message
			{
				document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/tick.gif' alt='' />";
			}
			return true;
		}
	} // end of function numbersOnly
	
	
	
	
	function selectCheck(formName,fieldName) // function to check if a selection has been made
	{
		if(document.forms[formName][fieldName].value == 'noselect') // if value is equal to 'noselect' the user hasnt made a selection so send error message
		{
			document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/cross.gif' alt='' />Required";
			document.getElementById('err_' + fieldName).className="errMsg";
		}
		else // else send success message
		{
			document.getElementById('err_' + fieldName).innerHTML="";
		}
	} // end of function selectCheck
	
	
	
	function filterProducts(enteredValue)// function to check if username allready exists in a database
	{
		getData('typeFilter.php?typeFilter='+enteredValue, 'productsContainer');
	}
	
	
	function checkDuplicateUsername(enteredValue,fieldName)// function to check if username allready exists in a database
	{
		if(enteredValue.length == 0)
		{
		    document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/cross.gif' alt='' />Required";
		    document.getElementById('err_' + fieldName).className="errMsg";
		}
		else
		{
		    getData('checkUsername.php?val='+enteredValue, 'err_'+fieldName);
		}
	}
	
	
		function checkDuplicateAdminname(enteredValue,fieldName)// function to check if username allready exists in a database
	{
		if(enteredValue.length == 0)
		{
		    document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/cross.gif' alt='' />Required";
		    document.getElementById('err_' + fieldName).className="errMsg";
		}
		else
		{
		    getData('checkAdminname.php?val='+enteredValue, 'err_'+fieldName);
		}
	}
        
        
        
        function checkDuplicateTeamname(enteredValue,fieldName)// function to check if username allready exists in a database
	{
		if(enteredValue.length == 0)
		{
		    document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/cross.gif' alt='' />Required";
		    document.getElementById('err_' + fieldName).className="errMsg";
		}
		else
		{
		    getData('checkTeamname.php?val='+enteredValue, 'err_'+fieldName);
		}
	}
        
        
        
        function pwLength(enteredValue,fieldName)// function to check the length of a password
	{
		if(enteredValue.length < 6)
		{
		    document.getElementById('err_' + fieldName).innerHTML="<img class='errorImg' src='images/cross.gif' alt='' />Must be 6+ characters";
		    document.getElementById('err_' + fieldName).className="errMsg";
		}
		else
		{
		    document.getElementById('err_' + fieldName).innerHTML="";
		}
	}
	
	
	
	
	function checkAllFields(formName) // function to check if all fields in a form are completed
	{
		for(i = 1; i < document.forms[formName].elements.length; i++) // setup a loop to loop through all the form elements
		{
			// get the type of the form element and store in a variable called type
			var type = document.forms[formName].elements[i].getAttribute('type');
			
			// if type is !NOT equal to submit and !NOT equal to reset do this
			if(type !== 'submit' && type !== 'reset' && type !== 'radio')
			{
				var classNames; // declare a variable called classNames to store classnames in
				var fieldId = document.forms[formName].elements[i].id; // get the ID of the input field
				var className = document.getElementById('err_' + fieldId).className; // use the ID of the input field to get the classname of the error element
			
				// add the classname to the variable classNames
				classNames += className;
			}
		}

		var errMsgExp = /errMsg/; // set a regular expression to test for the error message
		if(errMsgExp.test(classNames)) // if error message is found keep the submit button disabled
		{
			document.getElementById('submit').disabled=true;
			document.getElementById('submit').style.borderTopColor="#848484";
			document.getElementById('submit').style.borderRightColor="#ccc";
			document.getElementById('submit').style.borderLeftColor="#848484";
			document.getElementById('submit').style.borderBottomColor="#ccc";
			document.getElementById('submit').style.color="#ccc";
		}
		else // else enable the submit button
		{
			document.getElementById('submit').disabled=false;
			document.getElementById('submit').style.borderTopColor="#ccc";
			document.getElementById('submit').style.borderRightColor="#848484";
			document.getElementById('submit').style.borderLeftColor="#ccc";
			document.getElementById('submit').style.borderBottomColor="#848484";
			document.getElementById('submit').style.color="#000";
		}
	} // end of function checkAllFields
	
	
	
	
	function resetErr(formName) // function to reset all the error elements classNames and innerHTML and disable the submit button
	{
		for(i = 1; i < document.forms[formName].elements.length; i++) // setup a loop to loop through all the form elements
		{
			// get the type of the form element and store in a variable called type
			var type = document.forms[formName].elements[i].getAttribute('type');
			
			// if type is !NOT equal to submit and !NOT equal to reset do this
			if(type !== 'submit' && type !== 'reset' && type !== 'radio')
			{
				var fieldId = document.forms[formName].elements[i].name; // get the ID of the input field
			
				// reset all the error elements classNames and innerHTML and disable the submit button
				document.getElementById('err_' + fieldId).className="errMsg";
				document.getElementById('err_' + fieldId).innerHTML="*";
				document.getElementById('err_' + fieldId).style.color="#000";
				document.getElementById('submit').disabled="disabled";
				document.getElementById('submit').style.borderTopColor="#848484";
				document.getElementById('submit').style.borderRightColor="#ccc";
				document.getElementById('submit').style.borderLeftColor="#848484";
				document.getElementById('submit').style.borderBottomColor="#ccc";
				document.getElementById('submit').style.color="#ccc";
			}
		}
	} // end of function resetErr
	