// JavaScript Document//*************************************************
// JavaScript Document
//*************************************************
// Created by: 			Deon Pheiffer
// Date:				3 April 2009
// Version:				1.5
// Version Comments: 	code is stuctured better and some
//						more functionality
//************************************************
//Main function to execute the following
//************************************************
function validation(ErrorCssClass, ConfirmCSSClass)
{
	
	//REWRITE THIS IN INTO FUNCTIONS
	var ErrorCssClass = ErrorCssClass;
	var ConfirmCSSClass = ConfirmCSSClass;

	//****************************************
	//SET THE PARAMTERES
	//****************************************
	this.setParameters = function (Input, Type, minLength, maxLength, Required, Message, DisplayMessage, DisplayMessageType) 
	{ 

			document.getElementById(DisplayMessage).innerHTML = "";
			
			//****************************************************************
			//Determine if the field is a Checkbox or Radio OR plain text box
			//****************************************************************
			if(Type == "chk" || Type == "radio")
				{
					booTxtInput = document.getElementById(Input).checked;
				}
			else
				{
					txtInput = document.getElementById(Input);
				}
			
			//*************************************************
			//Validation types
			//*************************************************
			var ValidateData = new Array;
			ValidateData[0] = MinLength(txtInput.value, minLength);
			ValidateData[1] = MaxLength(txtInput.value, maxLength);
			ValidateData[2] = CheckRequired(txtInput.value, Required);
			ValidateData[3] = validateOther(txtInput.value, Type)
			ValidateData[4] = SQLInjection(txtInput.value);
			var IsValid = this.checkForm(ValidateData)
			showMessage(IsValid, DisplayMessage, DisplayMessageType, Message, txtInput.value);

			return IsValid; 

	};
	

	//****************************************
	//Do total form Validation
	//****************************************
	this.checkForm = function (arrFields) 
	{ 
		//****************************************
		//Loop through the array of boolean values
		//****************************************
		for(x=0; x < arrFields.length; x++)
		{	
			if(!arrFields[x])
			  {return false; }
		}
		return true;
	};
	
	
	//****************************************
	//Do total form Validation
	//****************************************
	var showMessage = function(isValid, DivElement, showType, showMessage, Field) 
	{ 
	
		//**********************************************************
		//there are two diffrent types of validation, Alert and Div
		//**********************************************************
		if (!isValid){
			//THIS SECTION WILL SHOW IF AN ERROR HAS OCCURED
			//**********************************************
			switch (showType)
				{
					//************************************
					//Validate email address
					//************************************
					case 'alert':
						alert(showMessage);
					break;
					
					//show message in a Div element
					//*************************************
					case 'element':
						//**********************************
						//Decoration for the error Messages
						//**********************************
						document.getElementById(DivElement).setAttribute("class", ErrorCssClass);//For Most Browsers
						document.getElementById(DivElement).setAttribute("className", ErrorCssClass);//For IE; harmless to other browsers.

						//************************************
						//Show message
						//************************************
						document.getElementById(DivElement).innerHTML = showMessage;
					break;
					
					//show message in a Div element
					//*************************************
					case 'dynamic':
						//**********************************
						//Decoration for the error Messages
						//**********************************
						//Field.value = showMessage;
						//alert(this.ErrorMessageBackColor);
						//Field.style.backgroundColor = this.ErrorMessageBackColor;
					break;
					
					default:
					break;
				}
		}
		else
		{
			//THIS SECTION WILL SHOW IF AN ERROR HAS OCCURED
			//**********************************************
			switch (showType)
				{
					//************************************
					//Validate email address
					//************************************
					case 'alert':
						alert(showMessage);
					break;
					
					//show message in a Div element
					//*************************************
					case 'element':
						//**********************************
						//Decoration for the error Messages
						//**********************************
						document.getElementById(DivElement).setAttribute("class", ConfirmCSSClass);//For Most Browsers
						document.getElementById(DivElement).setAttribute("className", ConfirmCSSClass);//For IE; harmless to other browsers.

						//************************************
						//Show message
						//************************************
						document.getElementById(DivElement).innerHTML = "Correct";
					break;
					
					//show message in a Div element
					//*************************************
					case 'dynamic':
						//**********************************
						//Decoration for the error Messages
						//**********************************
						Field.value = showMessage;
						Field.style.backgroundColor = this.ErrorMessageBackColor;
					break;
					
					default:
					break;
				}
		}
	};	


	//*************************************************************
	//Validate Other information, Returns true OR false
	//**************************************************************
	var validateOther = function (txtValue, txtType) 
	{ 	

		if (txtType != "")
		{
			switch (txtType)
			{
				//************************************
				//Validate email address
				//************************************
				case 'email':
					
					if (txtValue.indexOf("@") == -1){ 
						return false;
						break;
					}
					if (txtValue.indexOf(".") == -1){ 
						return false;
						break;
					}
				
				break;

				//*****************************************
				//If submitted value needs to be an string
				//*****************************************
				case 'str':
					
					var numArr = new Array("1","2","3","4","5","6","7","8","9","0");
					//loop throught the items
					for (x in numArr){
						if (txtValue.indexOf(numArr[x]) != -1){
								return false;
								break;
							}
						}
					return true;
				
				break;
			
				//****************************************
				//If submitted value needs to be an number
				//****************************************
				case 'int':

					var VarArr = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
					for (y in VarArr){
						if (txtValue.indexOf(VarArr[y]) != -1){
							return false;
							break;
							}
					} 
					return true;
				
				break;

				
				//****************************************
				//Check if terms is required
				//****************************************
				case 'radio':

					if (txtValue != true){ 
						return false;
						break;
					}
				
				break;


				//****************************************
				//Check if terms is required
				//****************************************
				case 'chk':

					if (txtValue != true){ 
						return false;
						break;
					}
				
				break;

		
				//************************************
				//Default selection
				//************************************
				default:
					return true;
					break;
			}
		}
		return true;
	};
	
	
	
	//****************************************
	//Checks if the field is required
	//****************************************	
	var CheckRequired = function (txtValue, boolRequired) 
	{ 
		if(boolRequired)
		{
			if (txtValue.value == "" || txtValue == " ")
			{return false;}
		}
		return true;
	};



	//****************************************
	//check thats the max length does not exceed
	//****************************************	
	var MaxLength = function (txtValue, intLength) 
	{ 
		if (txtValue.length > intLength){
			return false;
		}
		
		return true;
	};	


	//****************************************
	//check thats the max length does not exceed
	//****************************************	
	var MinLength = function (txtValue, intLength) 
	{ 
		if (txtValue.length < intLength){
		return false;
		}
		
		return true;
	};	


	//****************************************
	//check thats the max length does not exceed
	//****************************************	
	var SQLInjection = function (txtValue) 
	{ 
		var arrBadInfo = new Array(">", "<", "<script>", "</script>",";","#","(",")","/","javascript","language","*", "script", "drop;");
		for (z in arrBadInfo){
		if (txtValue.indexOf(arrBadInfo[z]) != -1) {
			return false;
		break;}
		}
		
		return true;
	};	


}   
