// JavaScript Document
// valdiation js version 1.0.0 by NS
// COPYRIGHT FONDUE LTD 2008

//notes for validation a hidden paramter must be added to the form in use in the follwoing format
//<input name="name_valdMan" type="hidden" id="b_name_valdMan" value="Name must contain a value" /> the id is key as the first part denotes the name of thefiel to validate the _vald denoted type of validation
//the Man in this exaple denotes its a mandaorty field. The actual value denotes the customies error message to be returned. This does not have to be filled in as the system is capable of workign out a defulat error message but useful to make the message more friendly refer to the method at the bootom to see al current availble types. this validation will enable any methods to be added quite easily

// guidance on using methods which involve lengths of field etc like <input name="testLen_valdMaxCharLen" type="hidden" id="testLenM_valdMMaxCharLen5" value="req max length test field must comtain equal to or less than 5 characters and not be empty" />  - in this case you add a value to the end eg 5 which then tell the js to work with that eg depeign on the vald method it will either restrict or allow mimum chars of the given number
// TODO valiodation where one date cannot be more than another date, and various other comparing field etc

function validateForm(formName) {
var str = '';
var elem = document.getElementById(formName).elements;
var formPassed = true;
var errorMessage = '';

for(var i = 0; i < elem.length; i++)
{
	//only work with hidden parameters to see if any validation is needed
	if(elem[i].type == "hidden")
	{
		str += "<b>Type:</b>" + elem[i].type + "\n";
		var name = elem[i].id;
		var value = elem[i].value;
		var valIdentifier = identifyVal(name);

		//important get the actual form object that relates to the val field
		var valObj = document.getElementById(getValObj(name));
		

		//now we have the name of the type of validation required. the follwoign all methods will used depeing on what type is required
		if(valIdentifier=="valdMan")
		{
			formPassed = valMandatory(valObj.value);
			if(!formPassed)
			{
					//if it was a select box word the meassge differntly
				if(elem[i].type=="select-one")
					errorMessage = errorMessage + valObj.id + " must have a value selected" + "\n";
				else
				{
					//if custome message specified then use that otherwise use sytem defualt
					if(value.length>0)
						errorMessage = errorMessage + value + "\n";
					else
						errorMessage = errorMessage + valObj.id + " must contain a value" + "\n";
				}
			}
		}
		
		if(valIdentifier=="valdMNum")
		{
			formPassed=valManNumeric(valObj.value);
			if(!formPassed)
			{
				//if customer message specified then use that otherwise use sytem defualt
				if(value.length>0)
					errorMessage = errorMessage + value + "\n";
				else
				errorMessage = errorMessage + valObj.id + " must contain a numeric value" + "\n";		
			}
		}
		
		if(valIdentifier=="valdNum")
		{
			formPassed=valNumeric(valObj.value);
			if(!formPassed)
			{
				//if customer message specified then use that otherwise use sytem defualt
				if(value.length>0)
					errorMessage = errorMessage + value + "\n";
				else				
				errorMessage = errorMessage + valObj.id + " must contain a numeric value or left empty" + "\n";		
			}
		}
	
		if(valIdentifier=="valdEmail")
		{
			formPassed=valEmail(valObj.value);
			if(!formPassed)
			{
				//if customer message specified then use that otherwise use sytem defualt
				if(value.length>0)
					errorMessage = errorMessage + value + "\n";
				else
				errorMessage = errorMessage + valObj.id + " must not be empty and contain a valid email address" + "\n";
			}
		}
		
		if(valIdentifier=="valdMAlpha")
		{
			formPassed=valManAlpha(valObj.value);
			if(!formPassed)
			{
				//if customer message specified then use that otherwise use sytem defualt
				if(value.length>0)
					errorMessage = errorMessage + value + "\n";
				else
				errorMessage = errorMessage + valObj.id + " must not be empty and contain only letters" + "\n";
			}
		}
		
		if(valIdentifier=="valdAlpha")
		{
			formPassed=valAlpha(valObj.value);
			if(!formPassed)
			{
				//if customer message specified then use that otherwise use sytem defualt
				if(value.length>0)
					errorMessage = errorMessage + value + "\n";
				else
				errorMessage = errorMessage + valObj.id + " must contain only letters or be left empty" + "\n";
			}
		}
		
		if(valIdentifier=="valdFloat")
		{
			formPassed=valFloat(valObj.value);
			if(!formPassed)
			{
				//if customer message specified then use that otherwise use sytem defualt
				if(value.length>0)
					errorMessage = errorMessage + value + "\n";
				else
				errorMessage = errorMessage + valObj.id + " must be numeric and in the format ###.##" + "\n";
			}
		}
		
		//valdiation for string length will work slightly differently
		if(valIdentifier.match("valdMaxCharLen"))
		{
			
			var maxLenToVal = getStrippedVal(valIdentifier,"valdMaxCharLen",1);
			formPassed=valMaxCharLen(valObj.value, maxLenToVal);
			if(!formPassed)
			{
				//if customer message specified then use that otherwise use sytem defualt
				if(value.length>0)
					errorMessage = errorMessage + value + "\n";
				else
				errorMessage = errorMessage + valObj.id + " must have equal or less than "+maxLenToVal+ " characters or be left empty" + "\n";
			}
		}
		
		//valdiation for string length will work slightly differently
		if(valIdentifier.match("valdMMaxCharLen"))
		{
			
			var maxLenToVal = getStrippedVal(valIdentifier,"valdMMaxCharLen",1);
			formPassed=valMMaxCharLen(valObj.value, maxLenToVal);
			if(!formPassed)
			{
				//if customer message specified then use that otherwise use sytem defualt
				if(value.length>0)
					errorMessage = errorMessage + value + "\n";
				else
				errorMessage = errorMessage + valObj.id + " must have equal or less than "+maxLenToVal+ " characters and must not be left empty" + "\n";
			}
		}		
		

		//valdiation for string length will work slightly differently
		if(valIdentifier.match("valdMinCharLen"))
		{
			
			var minLenToVal = getStrippedVal(valIdentifier,"valdMinCharLen",1);
			formPassed=valMinCharLen(valObj.value, minLenToVal);
			if(!formPassed)
			{
				//if customer message specified then use that otherwise use sytem defualt
				if(value.length>0)
					errorMessage = errorMessage + value + "\n";
				else
				errorMessage = errorMessage + valObj.id + " must have equal or more than "+minLenToVal+ " characters or be left empty" + "\n";
			}
		}
		
		//valdiation for string length will work slightly differently
		if(valIdentifier.match("valdMMinCharLen"))
		{
			
			var minLenToVal = getStrippedVal(valIdentifier,"valdMMinCharLen",1);
			formPassed=valMMinCharLen(valObj.value, minLenToVal);
			if(!formPassed)
			{
				//if customer message specified then use that otherwise use sytem defualt
				if(value.length>0)
					errorMessage = errorMessage + value + "\n";
				else
				errorMessage = errorMessage + valObj.id + " must have equal or more than "+minLenToVal+ " characters and must not be left empty" + "\n";
			}
		}	

	}
	

		
} 

if(errorMessage !="")
{
	formPassed=false;
	alert("Your form has not been submitted please check the following\n --------------------------------------------------------------------------\n "  + errorMessage);
}
else
	formPassed=true;


return formPassed;
}

//function to get back the relating actula field object when a hidden field id is passsed in
function getValObj(name)
{
	var newName = name;
	if (name.match('_vald'))
	{	
		newName = name.split('_vald')[0];
	}
	else
	{
		
	}

	return newName;
}

//function which takes care of a matching val identifier if non found then return the name as it is
function identifyVal(name)
{
	var newName = name;
	if (name.match('_vald'))
	{
		var stringPos = name.indexOf('_vald');
		newName = name.substring(stringPos+1); // used to return everyhting after this prefix
	}
	else
	{
		
	}
	
	return newName;
}

//function to take in a word and then strip it of passed in char a bit like the ones above but more generic
//for other methods that may need this. the pos indicates whether you want the start of it or the end
//javscript split only uses 1 or 0;

function getStrippedVal(value,valToSearch,pos)
{
	var rtnName = value.split(valToSearch)[pos];
	return rtnName;
}


/** start of actual val methods **/

//method to check regardless of numeric ot text type there is a value in the field
function valMandatory(value)
{

	if (value==0)
		return false;
	else
		return true;

}

//method to validate a alpha only field and also it is a required field WITH NO SPACES
function valManAlpha(value)
{
	var numericExpression = /^[a-zA-z]+$/;
	if(value.match(numericExpression))
		return true;
	else
		return false;
	
}

//method to validate a alpha only field but not mandoratory but is a field WITH NO SPACES
function valAlpha(value)
{
	if(value==0)
	{
		return true;
	}
	else
	{
	var numericExpression = /^[a-zA-z]+$/;
	if(value.match(numericExpression))
		return true;
	else
		return false;
	}
}

//method to validate a numeric only field and also it is a required field
function valManNumeric(value)
{
	var numericExpression = /^[0-9]+$/;
	if(value.match(numericExpression))
		return true;
	else
		return false;
	
}

//same as above but can enter a blank value
function valNumeric(value)
{
	if(value==0)
	{
		return true;
	}
	else
	{
		var numericExpression = /^[0-9]+$/;
		if(value.match(numericExpression))
			return true;
		else
			return false
	}
}

//function to validate email
function valEmail(value) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = value;
   if(reg.test(address) == false) 
      return false;
	else
		return true;
   }
   
//function to validate float
function valFloat(value)
{	
	//we will allow blank entry to be added if the figure is not known
	//
	if(value=="")
		return true;
	
	var reg= /^[0-9]+([\.]?[0-9]+){0,1}$/;
	var valFloat = value;
   if(reg.test(valFloat)) 
      return true;
	else
	return false;
   }
   
//function to validate max length on any char type. as it is max this method will assume if no value is entered then it should pass validation
//takes in length to validate against
function valMaxCharLen(value, length)
{
	if(value=='')
		return true;
	
	var lenToVal = length;
	if(value.length > lenToVal)
		return false;
	else
		return true;
}

//same as above but requires an madatory entry
function valMMaxCharLen(value, length)
{
	if(value=='')
		return false;
	
	var lenToVal = length;
	if(value.length > lenToVal)
		return false;
	else
		return true;
}

//function to validate min length on any char type. as it is max this method will assume if no value is entered then it should pass validation
//takes in length to validate against
function valMinCharLen(value, length)
{
	if(value=='')
		return true;
	
	var lenToVal = length;
	if(value.length < lenToVal)
		return false;
	else
		return true;
}

//same as above but requires an madatory entry
function valMMinCharLen(value, length)
{
	if(value=='')
		return false;
	
	var lenToVal = length;
	if(value.length < lenToVal)
		return false;
	else
		return true;
}