if((l==null) || (l=='')) l=0;

//Begin check form
function verify(theForm)
{	for (i = 0; i < theForm.length; i++)
	{	if (theForm.elements[i].getAttribute("notnull") == 1)
		{	if (!NotEmptyValid(theForm.elements[i].value))
			{	alert(replaceAll(messages[l]['NOTNULL'], '{0}', theForm.elements[i].getAttribute("alt")));
				theForm.elements[i].focus();
				return false;
			}
		}
		
		if (theForm.elements[i].getAttribute("email") == 1)
		{	if (!EmailValid(theForm.elements[i].value))
			{	alert(messages[l]['INVALID_EMAIL']);
				theForm.elements[i].focus();
				return false;
			}
		}

		if (theForm.elements[i].getAttribute("int") == 1)//check for integer characters
		{	if (!IntValid(theForm.elements[i].value))
			{	alert(messages[l]['INVALID_INT']);
				theForm.elements[i].focus();
				return false;
			}
		}

		if (theForm.elements[i].getAttribute("float") == 1)
		{	if (!FloatValid(theForm.elements[i].value))
			{	alert(messages[l]['INVALID_FLOAT']);
				theForm.elements[i].focus();
				return false;
			}
		}

		if (theForm.elements[i].getAttribute("date") == 1)
		{	if (!dateValid(theForm.elements[i].value))
			{	alert(messages[l]['INVALID_DATE']);
				theForm.elements[i].focus();
				return false;
			} 
		}

		if (theForm.elements[i].getAttribute("time") == 1)
		{	if (!timeValid(theForm.elements[i].value))
			{	alert(messages[l]['INVALID_TIME']);
				theForm.elements[i].focus();
				return false;
			}
		}
	}
	return true;
}
/************************************************
DESCRIPTION:
	Validates that a string contains a valid email pattern.  
PARAMETERS: 
	str - String to be tested for validity   
RETURNS:
   True if valid, otherwise false.   
REMARKS:
	Accounts for email with country appended
	does not validate that email contains valid URL
	type (.com, .vn, .com.vn etc.) or valid country suffix.
*************************************************/
function EmailValid(str)
{	if (str=='') return true;
	
	var objRegExp = /^([a-z_][a-z_0-9\-]*)(([\.][a-z_0-9]+)*)@([a-z_][a-z_0-9\-]*)(([\.][a-z_0-9]+)*)([\.][a-z]{2,3})$/i;
  	//check for valid email
  	return objRegExp.test(str);
}

/************************************************
DESCRIPTION: 
	Validates that a string contains only valid integer number.
PARAMETERS:
	str - String to be tested for validity.
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
function IntValid(str)
{	if (str=='') return true;

	var objRegExp  = /(^-?\d\d*$)/; 
	//check for integer characters
	return objRegExp.test(str);
}

/******************************************************************************
DESCRIPTION:
	Validates that a string contains only valid numbers.
PARAMETERS:
	str - String to be tested for validity.
RETURNS:
	True if valid, otherwise false.
******************************************************************************/
function FloatValid(str)
{	if (str=='') return true;

	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
	//check for numeric characters
	return objRegExp.test(str);
}

/************************************************
DESCRIPTION:
	Validates that a string contains only
    valid dates with 2 digit month, 2 digit day, 
	4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy    
PARAMETERS:
   str - String to be tested for validity.
RETURNS:
   True if valid, otherwise false.
REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
function dateValid(str)
{	if (str=='') return true;

	var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
	//check to see if in correct format
  	if(!objRegExp.test(str))
	{	return false; //doesn't match pattern, bad date
  	}else
	{	var strSeparator = str.substring(2,3); //find date separator
		var arrayDate = str.split(strSeparator); //split date into month, day, year

		
		//create a lookup for months not equal to Feb.
		var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
							'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31};
							
		var intDay = parseInt(arrayDate[1],10);
	
		//check if month value and day value agree
		if(arrayLookup[arrayDate[0]] != null)
		{	if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0) return true; //found in lookup table, good date
		}
		
		var intMonth = parseInt(arrayDate[0],10);
		if (intMonth == 2)
		{ 	var intYear = parseInt(arrayDate[2]);
		   	if (intDay > 0 && intDay < 29)
			{	return true;
		   	}
		   	else if (intDay == 29)
			{	if ((intYear % 4 == 0) && (intYear % 100 != 0) || (intYear % 400 == 0))
				{	// year div by 4 and ((not div by 100) or div by 400) ->ok
				 	return true;
			 	}   
		   	}
    	}
  	}  
	return false; //any other values, bad date
}

/************************************************
DESCRIPTION:
	Validates that a string contains only valid time.
    Ex. HH:MM or HH:MM:SS or HH:MM:SS.mmm
PARAMETERS:
   str - String to be tested for validity.
RETURNS:
   True if valid, otherwise false.
*************************************************/
function timeValid(str)
{	if (str=='') return true;
	
	var objRegExp = /^((0?[0-9])|(1[0-9])|2[0-3]):[0-5]?\d(:[0-5]?\d(\.\d{1,3})?)?$/;
	//check to see if in correct format
  	return objRegExp.test(str);
}

/************************************************
DESCRIPTION:
	Validates that a string is not all blank (whitespace) characters.
PARAMETERS:
   str - String to be tested for validity
RETURNS:
   True if valid, otherwise false.
*************************************************/
function NotEmptyValid(str)
{	return (Trim(str)!='');
}
//End check form

function Trim(str)
{	return str.replace(/^\s*|\s*$/g, '');
}

function replaceAll(content, s1, s2)
{	return content.split(s1).join(s2); //content.replace((new RegExp(s1, 'g')), s2);
}

function isNull(str)
{	if(str==null) return true;

	var iLen = str.length;
	for (var i=0; i<iLen; i++)
	{	if (str.charAt(i)!= ' ') return false;
	}
	
	return true;
}

//Format number for Vietnam 1.230,50
function addCommas(nStr)
{	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function getObj(name) 
{   if (document.getElementById) { return document.getElementById(name); }
	else if (document.all)       { return document.all[name]; }
	else if (document.layers)    { return document.layers[name]; }
}