/*	
* WEBSOL VALIDATION CODE
* COPYRIGHT 2003 WEBSOL LIMITED. NO PART OF THIS SOURCECODE MAY BE REPRODUCED IN ANY FORM WITHOUT THE WRITTEN PERMISSION OF WEBSOL LTD 
*
* CREATED BY: NEJG 16/JAN/2004
* LAST MODIFIED BY: NG 13/SEP/2004
* PATH: /JS/VALIDATOR.JS
* DESCRIPTION: THIS FILE IS USED THROUGHOUT THE SYSTEM FOR VALIDATING INPUT.
*/
/*****************************************************************************************************/
/*----------------------------START OF VALIDATOR FUNCTIONS AND VARIABLES-----------------------------*/	
/*****************************************************************************************************/	
	//GLOBAL VARIABLES
	
	//These are the names of the TD classes which the validator will switch
	var requiredClass = "requiredField";
	var requiredAlertClass = "requiredAlert";
	var unRequiredClass = "unRequired";
	var unRequiredAlertClass = "unRequiredAlert";
	
	//These are the alert messages to attach to the TDs
	//These can be overwritten in a page where the validators 
	//String prototype helper functions are over written
	var txt_reqLetAlert = "This input field cannot be left blank."
	var txt_reqDatAlert = "Select a date using the date popup box."
	var txt_reqNumAlert = "Enter a number (decimal places are allowed)."
	var txt_reqNumAlert1to4 = "Enter a number (between 1 and 9999)."
	var txt_reqEmlAlert = "Please only enter a valid email address."
	var txt_reqPasAlert = "Passwords must be at least six characters long and be alphanumeric."
	var txt_reqTmeAlert = "Please use the format 'HH:MM'."
	var txt_reqLgnAlert = "Logins must be at least four characters long and contain only letters, numbers and underscores."
	var txt_reqFlnAlert = "File Names must contain only letters, numbers and underscores."
	var txt_reqSelAlert = "Please select an item from the drop-down box."
	var txt_reqOthAlert = "Type something into the input field."
	//Make sure general session is selected and warning check with admin
	function checkCourseTypeInvigilated(courseTypeElement){
		if (courseTypeElement.options[courseTypeElement.selectedIndex].value==9) {
			document.getElementById('warnInvig').style.display='block';
			if (!document.getElementById('CertIntro').checked){
				alert ("'General Certificate Introduction' should be selected when course type is 'Invigilated Assessment Course'");
				return false;
			}
			else
				return true;
		}
		else {
			document.getElementById('warnInvig').style.display='none';
				return true;
		}
		
		
	}
	function isStrictValidation(tempNode)
	{
		var tempNodeTD = findRequiredTDForInput(tempNode);
		if(tempNode.value.trim().length < 1 && tempNodeTD.className.isUnrequired())
		{
			unhighlightField(tempNode);
			return false;
		}
		else return true;
	}
	
	function Validator(theForm)
	{
		var problemField = false;
		for (i=0;i<theForm.elements.length;i++)
		{
			var tempNode = theForm.elements[i];
			if(tempNode.name.substring(0,3).toUpperCase() == 'REQ' && isStrictValidation(tempNode))
			{
				switch(tempNode.tagName.toUpperCase())
				{
					case "INPUT":
						switch(tempNode.name.substring(3,6).toUpperCase()){
							case "LET":
								if(tempNode.type.toUpperCase() == "HIDDEN")
								{
									if(tempNode.value.stripHTML().length < 1)
									{
										problemField = true;
										highlightField(tempNode, txt_reqLetAlert);
									}else{
										unhighlightField(tempNode);
									}
								}else{
									if(tempNode.value.trim().length < 1)
									{
										problemField = true;
										highlightField(tempNode, txt_reqLetAlert);
									}else{
										unhighlightField(tempNode);
									}
								}
								break;
							case "DAT":	
								if(!tempNode.value.isDate())
								{
									problemField = true;
									highlightField(tempNode, txt_reqDatAlert);
								}else{
									unhighlightField(tempNode);
								}
								break;
							case "NUM":
								if(!tempNode.value.isNumeric())
								{
									problemField = true;
									highlightField(tempNode, txt_reqNumAlert);
								}else{
									unhighlightField(tempNode);
								}
								break;
							case "INT":
								if(!tempNode.value.isInteger())
								{
									problemField = true;
									highlightField(tempNode, txt_reqNumAlert1to4);
								}else{
									unhighlightField(tempNode);
								}
								break;
							case "EML":
								if(!tempNode.value.isEmail())
								{
									problemField = true;
									highlightField(tempNode, txt_reqEmlAlert);
								}else{
									unhighlightField(tempNode);
								}
								break;
							case "PAS":
								if(!tempNode.value.isPassword())
								{
									problemField = true;
									highlightField(tempNode, txt_reqPasAlert);
								}else{
									unhighlightField(tempNode);
								}
								break;
							case "TME":
								if(!tempNode.value.isTime())
								{
									problemField = true;
									highlightField(tempNode, txt_reqTmeAlert);
								}else{
									unhighlightField(tempNode);
								}
								break;
							case "LGN":
								if(!tempNode.value.isLogin())
								{
									problemField = true;
									highlightField(tempNode, txt_reqLgnAlert);
								}else{
									unhighlightField(tempNode);
								}
								break;
							case "FLN":
								if(!tempNode.value.isFileName())
								{
									problemField = true;
									highlightField(tempNode, txt_reqFlnAlert);
								}else{
									unhighlightField(tempNode);
								}
								break;
						}
						break;
					case "FILE":
					case "TEXTAREA":
						if(tempNode.value.trim().length < 1)
						{
							problemField = true;
							highlightField(tempNode, txt_reqOthAlert);
						}else{
							unhighlightField(tempNode);
						}
						break;
					case "SELECT":
						if(tempNode.multiple) tempNode.options[0].selected = false;
						if((!tempNode.multiple && tempNode.selectedIndex < 1) || (tempNode.multiple && tempNode.selectedIndex == -1))
						{
							problemField = true;
							highlightField(tempNode, txt_reqSelAlert);
						}else{
							unhighlightField(tempNode);
						}
						break;
				}
			}
		}
		if(problemField) alert("One or more of the fields has not been filled in correctly.\nIncorrect fields are highlighted red.\nYou can click on the name of the item for information about how to fill in the correct information.");
		return !problemField;
	}
	
	function findRequiredTDForInput(requiredFieldInputTag){
		//Just a pointer to the input node
		var tempNode = requiredFieldInputTag;
		try
		{
			//Get the TD of the inputNode
			while(tempNode.tagName != "TD" && tempNode.parentNode != null) tempNode = tempNode.parentNode;
			//if this isn't the requiedClass td try the previous sibling TD
			if(tempNode.className.isRequired()) return tempNode;
			if(tempNode.previousSibling != null){
				//siblingNodeFinder will be used to find a previous TD if it exists
				var siblingNodeFinder = tempNode.previousSibling;
				//search back to td or null then stop
				while(siblingNodeFinder != null && siblingNodeFinder.tagName != "TD"){
					siblingNodeFinder = siblingNodeFinder.previousSibling;
				}
				//if its not null it's td so give it to tempNode
				if(siblingNodeFinder != null) tempNode = siblingNodeFinder;
			}
			// if we still don't have the right TD try the last cousin TD
			if(tempNode.className.isRequired()) return tempNode;
			//Go up then...
			tempNode = tempNode.parentNode
			// ...back to...
			tempNode = tempNode.previousSibling
			 //... the previous TR
			while(tempNode.tagName != "TR" && tempNode.previousSibling != null){
				tempNode = tempNode.previousSibling
			}
			//then down one...
			tempNode = tempNode.lastChild;
			//And find the last TD this end
			while(tempNode.tagName != "TD" && tempNode.previousSibling != null)tempNode = tempNode.previousSibling
		}catch(e){
			alert("Sorry, Cas is experiencing some difficulties.\nPlease continue to use the other features of Cas.\n");//+requiredFieldInputTag.name);
			return null;
		}
		//We have run out of places to look so just return the node;
		if(tempNode.className.isRequired()) return tempNode;
		return null;
	}
	
	function highlightField(requiredFieldInputTag, alertMessage)
	{
		//Search for the Node if this is right change it otherwise we will have to ignore this case
		var tempNode = findRequiredTDForInput(requiredFieldInputTag);
		if(tempNode != null && tempNode.parentNode!= null)
		{
			switch (tempNode.className){
				case requiredClass:
					tempNode.className = requiredAlertClass;
					tempNode.onclick = function()
					{
						alert(alertMessage.toString());
					}
				break;
				case unRequiredClass:
					tempNode.className = unRequiredAlertClass;
					tempNode.onclick = function()
					{
						alert(alertMessage.toString());
					}
				break;
			}
		}
		return null;
	}
	
	function unhighlightField(requiredFieldInputTag)
	{
		//Search for the Node if this is right change it otherwise we will have to ignore this case
		var tempNode = findRequiredTDForInput(requiredFieldInputTag);
		if(tempNode != null && tempNode.parentNode!= null)
		{
			switch (tempNode.className){
				case requiredAlertClass:
					tempNode.className = requiredClass;
					tempNode.onclick = ""
				break;
				case unRequiredAlertClass:
					tempNode.className = unRequiredClass;
					tempNode.onclick = ""
				break;
			}
		}
		return null;
	}
	
	//**************************************************************
	//These are the helper functions used by the Validator
	//They (or just the Regexps can be overwritten by a page to 
	//change how that particular type is Validated.
	
	String.prototype.trim = function()
	{
		var trimmerString = this;
		trimmerString = trimmerString.replace(/^\s*(.*)/, "$1");
		//removed question mark to solve a Mac compatibility issue
		//trimmerString = trimmerString.replace(/(.*?)\s*$/, "$1");
		trimmerString = trimmerString.replace(/(.*)\s*$/, "$1");
		return trimmerString;
	}
	
	String.prototype.stripHTML = function()
	{
		return this.replace(/<[^>]*>/g, "").replace(/[\n\r]/g, "").replace(/&nbsp;/g, "").trim();
	}
	
	String.prototype.isNumeric = function()
	{
		return regexpNumbers.test(this.trim());
	}
	var regexpNumbers = /^\d{0,2}\.?\d{1,2}$/;
	
	String.prototype.isInteger = function()
	{
		return regexpInteger.test(this.trim());
	}
	var regexpInteger = /^\d{1,4}$/;
	
	String.prototype.isEmail = function()
	{
		return regexpEmail.test(this.trim());
	}
	var regexpEmail = /^.+@.+\..+$/;
	
	String.prototype.isPassword = function()
	{
		return (regexpPasswordLength.test(this.trim())
			&& regexpPasswordNumbers.test(this.trim())
			&& regexpPasswordLetters.test(this.trim()));
	}
	var regexpPasswordLength = /^\w{6,}$/
	var regexpPasswordNumbers = /\d+/
	var regexpPasswordLetters = /[a-z]+/i
	
	String.prototype.isLogin = function()
	{
		return (regexpLogin.test(this.trim()));
	}
	var regexpLogin = /^\w{4,}$/
	
	String.prototype.isFileName = function()
	{
		return (regexpFileName.test(this.trim()));
	}
	var regexpFileName = /^\w+$/
	
	String.prototype.isTime = function()
	{
		return regexpTime.test(this.trim()) && parseInt(this.substring(0,2)) < 24;
	}
	var regexpTime = /^[0-1]?[0-9]:[0-5][0-9]$/;
	
	String.prototype.isDate = function()
	{
		return regexpDate.test(this.trim());
	}
	var regexpDate = /^[0-3]?[0-9]\/[0-1]?[0-9]\/[1-2][0-9]{3}$/;
	
	String.prototype.isRequired = function()
	{
		return (this == requiredClass || this == unRequiredClass || 
					this == requiredAlertClass || this == unRequiredAlertClass);
	}
	
	String.prototype.isUnrequired = function()
	{
		return (this == unRequiredClass || this == unRequiredAlertClass);
	}
	
	//**************************************************************
	
/*****************************************************************************************************/
/*-----------------------------END OF VALIDATOR FUNCTIONS AND VARIABLES------------------------------*/	
/*****************************************************************************************************/	


function readObject(objectRead, typeName)
{
	if (typeof(objectRead) =="string") objectRead = document.getElementById(objectRead);
	if (typeof(objectRead) =="undefined") objectRead = document;
	var i ,j
	j = ""
	for(i in objectRead)
	{
		if(j.length > 100){
			alert(j);
			j = "";
		}
		if (typeof(typeName) != 'string' || typeof(objectRead[i]) == typeName){
			j += i + ": " + typeof(objectRead[i]); 
			if(typeof(objectRead[i])!= "function"){
				j += ": " + objectRead[i] + "\n";
			}
			else j+= "\n";
		}
	}
	alert(j);
}