// Created By		:	SCadd	14/03/00
// Change History	:	DAtkins 29/05/01
//						Added the function ValidateDecimal
//						Skermode 05/03/04
//						Updated function ValidateDecimal to validate currency
//						PBromilow 30/03/04
//						Prevalidates input and makes the background red when an error
//						JJones	21/01/05
//						Added ValidateNumberWithDecimalPlaceControl function
//						Ian Hunt 17/02/2005
//						ValidateNumberWithDecimalPlaceControl now removes unnecessary deciaml points
// Purpose			:
// Perform validation checks on what users have entered into text fields


function Validate (oTextBox, strTextToValidate, bAlphas, bNumerics)
{
	var bValid = true;
	var strValidChars = '';
	var intTextCount = 0;
	var intValidCharsCount = 0;
	var strMessage = '';
	var mychar;
	
	if (bAlphas == 'true') {
		strValidChars = strValidChars + "abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ@,.-'&():/% _";
	}
	
	if (bNumerics == 'true') {
		strValidChars = strValidChars + "0123456789";
	}
	
	for (intTextCount = 0; intTextCount < strTextToValidate.length; intTextCount++) {
		mychar = strTextToValidate.charAt(intTextCount);
		for (intValidCharsCount = 0; intValidCharsCount < strValidChars.length; intValidCharsCount++)
			if (mychar == strValidChars.charAt (intValidCharsCount))
				break;
			if (intValidCharsCount == strValidChars.length)
			{
				bValid = false;
				break;
			}
	}
		
	if (bValid == false) {
		if ((bAlphas == 'true') && (bNumerics != 'true')) {
			strMessage = 'please only type letters';
		}
		else if ((bAlphas != 'true') && (bNumerics == 'true')) {
			strMessage = 'please only type numbers';
		} else {
			strMessage = 'please only type letters and numbers';
		}

		// Set background colour
		oTextBox.style.backgroundColor='#ffdddd'; // set colour	
		alert('Invalid Input, ' + strMessage + ' , please try again');
		oTextBox.focus();

		// Return a false state
		return false;
	} else {
		// Clear background colour as it is fine
		oTextBox.style.backgroundColor=''; // clear colour

		// Return a true state
		return true;
	}
	
}



function ValidateText (strTextToValidate, bAlphas, bNumerics)
{
	var bValid = true;
	var strValidChars = '';
	var intTextCount = 0;
	var intValidCharsCount = 0;
	var mychar;
	
	if (bAlphas == 'true')
	{
	strValidChars = strValidChars + "abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ@,.-'&():/% _";
	}
	
	if (bNumerics == 'true')
	{
		strValidChars = strValidChars + "0123456789";
		// if first character is  + or - then strip from test string
		if (strTextToValidate.charAt(0) == '-' || strTextToValidate.charAt(0) == '+')
		{
			strTextToValidate = strTextToValidate.substring(1);
		}
	}
	
	for (intTextCount = 0; intTextCount < strTextToValidate.length; intTextCount++)
	{
		mychar = strTextToValidate.charAt(intTextCount);
		for (intValidCharsCount = 0; intValidCharsCount < strValidChars.length; intValidCharsCount++)
			if (mychar == strValidChars.charAt (intValidCharsCount))
				break;
			if (intValidCharsCount == strValidChars.length)
			{
				bValid = false;
				break;
			}
			
	}		

	if (bValid == false)
	{			
		return false;
	}
	else
	{
	return true;
	}
	
}

function ValidateDecimal (strDecimalToValidate,strIsCurrency)
{
	var bValid = true;
	var strValidChars = "0123456789.";
	var numDecimalCount = 0;
	var numValidCharsCount = 0;
	var strChar;
	var numDecimalPoints = 0;
	var bTooManyDP = false;


	if (strIsCurrency && strDecimalToValidate.charAt(0) == '-')
	{
		strDecimalToValidate = strDecimalToValidate.substring(1,strDecimalToValidate.length)
	}

	for (numDecimalCount = 0; numDecimalCount < strDecimalToValidate.length; numDecimalCount++)
	{
		strchar = strDecimalToValidate.charAt(numDecimalCount);
		if (strchar == '.')
		{
			numDecimalPoints++ 
			if (numDecimalCount < strDecimalToValidate.length-3)
			{
				bTooManyDP = true;
			}
		}
		for (numValidCharsCount = 0; numValidCharsCount < strValidChars.length; numValidCharsCount++)						
			if (strchar == strValidChars.charAt(numValidCharsCount))
				break;
			if (numValidCharsCount == strValidChars.length)
			{
				bValid = false;
				break;
			}
			
	}		

	if (numDecimalPoints > 1 && strIsCurrency)
	{
		bValid = false;
	}
	if (bTooManyDP && strIsCurrency)
	{
		bValid = false;
	}	
	if (bValid == false)
	{			
		return false;
	}
	else
	{
	return true;
	}	
}


function ValidateNumberWithDecimalPlaceControl(poControl, pnumDecimalPlaces)
{

	/*
		This function will chop off any extraneous numbers after a predefined decimal place limit
		It does not round the decimal portion (currently)
		
		eg: input - 345.678 
			called with 	pnumDecimalPlaces = -1 returns 345.678
			called with 	pnumDecimalPlaces = 0 returns 345
			called with 	pnumDecimalPlaces = 2 returns 345.67
			called with 	pnumDecimalPlaces = 4 returns 345.678			
	*/
	
	var strNumber = new String(poControl.value);
	var strWholeNumberPortion = new String();
	var strDecimalPortion = new String();
	var re = new RegExp("[-+]?([0-9]*)(\.)?([0-9]+)?","ig");
	
	if (isNaN(strNumber))
	{
		poControl.style.backgroundColor='#ffdddd'; // set colour	
		alert('This is not a valid value for this numeric field, please try again');
		poControl.focus();
		return false;
	}
	else
	{
		//exec the regular expression		
		re.exec(strNumber);
		
		//$1 will now contain the whole number part and $3 will contain the decimal part
		strWholeNumberPortion = RegExp.$1;
		strDecimalPortion = RegExp.$3;
		
		//What limitation is placed on the decimal portion
		switch (pnumDecimalPlaces)
		{
			case -1 :
				//return everything
				if (strDecimalPortion.length ==0) 
				{
					poControl.value = strWholeNumberPortion;
				}
				else
				{
					poControl.value = strWholeNumberPortion + '.' + strDecimalPortion;
				}
				break;
			case 0 :
				poControl.value = strWholeNumberPortion;
				break;
			default :
				strDecimalPortion = strDecimalPortion.substring(0,pnumDecimalPlaces);
				poControl.value = strWholeNumberPortion + '.' + strDecimalPortion;
		}
		
		// Remove '.'
		if (poControl.value.substr(poControl.value.length - 1) == '.')
			poControl.value = poControl.value.substr(0, poControl.value.length - 1);
		
		// Clear background colour as it is fine
		poControl.style.backgroundColor=''; // clear colour
		return true;
	}
	
}
