/*
'------------------------------------------------------------------
'Developer      Date            Remark
'---------      -----------     ------
'Kelmen			28/Aug/2001		Revised for Nescape browser.
'cptan			07/Mar/2002		Check for positive number (positive_number)
'cptan			12/Mar/2002 	Email validation
'cptan			12/Mar/2002 	Special Character checking
'khleong		09/Sep/2003		Allowing Alphabet Only
'------------------------------------------------------------------
*/
var mc_strAttr_Mandatory		= 'mandatory';
var mc_strAttr_Numeric			= 'numeric';
var mc_strAttr_MaxLength		= 'maxlength';
var mc_strAttr_Date				= 'date';
var mc_strAttr_NoComma			= 'nocomma';
var mc_strAttr_Description		= 'description';
var mc_strAttr_FormatCurrency	= 'formatcurrency';
var mc_strAttr_Tel				= 'telformat';
var mc_strAttr_AlphabetOnly		= 'alphabetonly';


var mc_strAttr_Positive_Numeric	= "positive_numeric";
var mc_strAttr_Special_Description	= 'special_description';  // temporary

//=====================================
function gfstr_Form_Validate(v_objForm)
{
	var strErrMsg;
	var lngIdx;
	var vntErrMsg;
	
	strErrMsg = '';

	with(v_objForm)
	{	
		for (lngIdx = 0; lngIdx < elements.length; lngIdx++)
		{
			vntErrMsg = mfbln_Form_Validate(elements[lngIdx], null);
			
			if (vntErrMsg != null)
			{
				strErrMsg = MergeWithDelimeter(strErrMsg, '\n', vntErrMsg);
				vntErrMsg = null;
			}
		}
	}

	if (strErrMsg == '')
		return null;
	else
		return strErrMsg;

}

//=======================================
function gfvoid_Form_EnableAll(v_objForm)
{
	EnableElems(v_objForm);
}

//-----------------------------------------------------
function mfbln_Form_Validate(v_objHTML, v_strEntryType)
{
	var vntDescription, vntFormatCurrency, vntMaxLength, vntDate, vntInteger;
	var strErrMsg;
	var lngMaxLength;
	var lngIndx;
	var lngCounter=0;
	var strdummyvalue;					
	
	strErrMsg = '';
	
	with(v_objHTML)
	{
		vntDescription = gfvnt_getAttribute(v_objHTML, mc_strAttr_Description);
		if (vntDescription == null) vntDescription = '';
		
		
		//Validation : Mandatory
		if (gfvnt_getAttribute(v_objHTML, mc_strAttr_Mandatory) != null)
		{
			if (IsBlank(value))
				strErrMsg = vntDescription + ' is a Mandatory field!';
		}
		
		//Validate : Special Character checking
		if (strErrMsg == '')
		{	
			if ((v_objHTML.type == 'text' || v_objHTML.type == 'textarea') && (gfvnt_getAttribute(v_objHTML, mc_strAttr_Special_Description) != true))
			{	
				if (Trim(value) != '' && (gfvnt_getAttribute(v_objHTML, mc_strAttr_Numeric) != true) && (gfvnt_getAttribute(v_objHTML, mc_strAttr_Positive_Numeric) != true) && (gfvnt_getAttribute(v_objHTML, mc_strAttr_FormatCurrency) != true))
				{
					if (toAlphaNumber(value)==false)
					{
						if (Trim(vntDescription) == '')
						{
							//strErrMsg = "Special Character is not allowed";
							strErrMsg ="";
						}
						else
						{
							strErrMsg = "Special Character is not allowed for " + vntDescription;
							strErrMsg="";
						}
					}
				}
			}		 
		}
			
		//Validation : Numeric
		if (strErrMsg == '')
		{
			if (gfvnt_getAttribute(v_objHTML, mc_strAttr_Numeric) != null)
			{
				if (!IsBlank(value))
				{
					if (IsNumber(value))
					{
						vntFormatCurrency = gfvnt_getAttribute(v_objHTML, mc_strAttr_FormatCurrency);
						if (vntFormatCurrency != null)
						{
							strErrMsg = Validate_FormatCurrency(vntFormatCurrency, CNum(value));
							if (strErrMsg != '')
								strErrMsg = 'Please enter ' + vntDescription + ' in ' + strErrMsg + ' format!';
							vntFormatCurrency = null;
						}
					}
					else
						strErrMsg = vntDescription + ' is a Numeric field!';
				}
			}
		}
		
		

		//Validation : Numeric - positive
		if (strErrMsg == '')
		{
			if (gfvnt_getAttribute(v_objHTML, mc_strAttr_Positive_Numeric) != null)
			{
				if (!IsBlank(value))
				{
					if (IsNumber(value))
					{	
						vntFormatCurrency = gfvnt_getAttribute(v_objHTML, mc_strAttr_FormatCurrency);
						if (vntFormatCurrency != null)
						{
							strErrMsg = Validate_FormatCurrency(vntFormatCurrency, CNum(value));
							if (strErrMsg != '')
								strErrMsg = 'Please enter ' + vntDescription + ' in ' + strErrMsg + ' format!';
							vntFormatCurrency = null;
						}
						// check for positive number		
						if (CNum(value) < 0)
							strErrMsg  = 'Please enter positive value in ' + vntDescription ;
						
						if (value[0]== '-')
							strErrMsg  = 'Please enter positive value in ' + vntDescription ;
					}
					else
						strErrMsg = vntDescription + ' is a Numeric field!';
				}
			}
		}
		
		//Validation : telephone - No
		if (strErrMsg == '')
		{
			if (gfvnt_getAttribute(v_objHTML, mc_strAttr_Tel) != null)
			{
				if (!IsBlank(value))
				{
					if (value.search(/[^0-9-]/) != -1)
					{
						strErrMsg = vntDescription + ' is invalid telephone format field!';
					}
					
					
					for (lngIndx=0; lngIndx < value.length; lngIndx ++ )
					{
						if (value[lngIndx] == '-')
						{
							lngCounter ++;
						}
					}
					if (lngCounter > 1) 
					{
						strErrMsg  = 'Please enter single dash for telephone No format in ' + vntDescription ;
					}
					
				}
			}
		}


		//Validation : MaxLength of TextArea
		if (strErrMsg == '')
		{
			vntMaxLength = gfvnt_getAttribute(v_objHTML, mc_strAttr_MaxLength);
			if (vntMaxLength != null)
			{
				lngMaxLength = CNum(vntMaxLength);
				if (value.length > lngMaxLength)
					strErrMsg = vntDescription + ' maximum length is ' + lngMaxLength + ' characters!';
					
				vntMaxLength = null;
			}
		}


		//Validation : Date
		if (strErrMsg == '')
		{
			vntDate = gfvnt_getAttribute(v_objHTML, mc_strAttr_Date);
			if (vntDate != null)
			{
				//vntDate = null;
			}
		}
		
		//Validation : Alphabet Only
		if (gfvnt_getAttribute(v_objHTML, mc_strAttr_AlphabetOnly) != null)
		{
			if(gfvnt_getAttribute(v_objHTML, mc_strAttr_AlphabetOnly) == true)
			{
				var iCharCode;
				var iErr = 0;
				var i = 0;
				
				if (!IsBlank(value))
				{
					for(i=0; i< value.length && iErr==0; i++)
					{
						iCharCode = value.charCodeAt(i);
						if(!((iCharCode >= 65 && iCharCode <= 90) || (iCharCode >= 97 && iCharCode <= 122) || iCharCode == 32))
							iErr = 1;
					}
					if(iErr != 0)
						strErrMsg = vntDescription + ' is an Alphabet Only field!';
					
				}
			}
		}
				
	}
	

	
	if (strErrMsg == '')
		return null;
	else
		return strErrMsg;
}