/*
Function: empty(str) 
Description: check empty value
Input:
Process:
Output:
Sample code:
*/

function empty(str)
{
	str = new String(str);
	if(str.length==0) return 1;
		else return 0;
}

/*
Function: valid_email
Describle: check a string in form of an email address or not
Input: str
Process: 
Output: 
Sample code:
*/
function valid_email(str)
{
	str=new String(str);
	if(str.match('/\.\@/')) 
		return 0;
	if(str.match('/\@\./')) 
		return 0;
	if(str.match('/\.$/')) 
		return 0;
	if(str.match('/\.\./')) 
		return 0;
	if(!str.match('^[a-zA-Z0-9_\-]+[a-zA-Z0-9_\.\-]+@+[a-zA-Z0-9_\.\-]+$'))  
		return 0;
	return 1;
}

/*
Function: valid_string_pattern
Description: check the day is exist or not
Input: day,month,year as integer
Process:
Output: a day or not a day
Sample code:
*/
function valid_string_pattern(str,pattern){
	str = new String(str);
	if(!str.match(pattern))
		return 0;
	return 1;
}
/*
Function: valid_strstr
Description: 
Input:
Process:
Output:
Sample code:
*/

function valid_stdstr(str){
	str = new String(str);
	if(str.match('  ')) 
		return 0;
	if(str.match('^ ')) 
		return 0;
	if(str.match(' $')) 
		return 0;
	return 1;
}
/*
Function: valid_integer
Description: the string without special chars and start without number
Input:
Process:
Output:
Sample code:
*/
function valid_integer(str){
	str=new String(str);
	if(str.match('^[0-9]+$'))
		return 1;
	else
		return 0;
}
/*
Function: valid_numeric
Description: the string without special chars and start without number
Input:
Process:
Output:
Sample code:
*/

function valid_numeric(str){
	str=new String(str);
	var floatset="0123456789.";
	var tmp=-1;
	//so dau cham 0 co hoac chi co 1
	if(str.indexOf('.')!=str.lastIndexOf('.')) return 0;
	if(!str.match('^[0-9]|(\.)+$')) return 0;
	return 1;
}
/*
Function: valid_range
Description:
Input:
Process:
Output:
Sample code:
*/
function valid_range(str,minval,maxval){
	if(str<minval) return 0;
	if(str>maxval) return 0;
	return 1;
}
/*
Function: valid_minimum_value
Description:
Input:
Process:
Output:
Sample code:
*/
function valid_minimum_value(str,minval){
	if(str<minval) return 0;
	return 1;
}
/*
Function: valid_maximum_value
Description:
Input:
Process:
Output:
Sample code:
*/
function valid_maximum_value(str,maxval){
	if(str>maxval) return 0;
	return 1;
}
/*
Function: valid_maximum_value
Description:
Input:
Process:
Output:
Sample code:
*/
function valid_strlen(str,minlen,maxlen){
	str = new String(str);
	return (str.length>=minlen && str.length<=maxlen);
}
/*
Function: is_leap_year
Description: return the day in a month
Input:month, year
Process:
Output: how many days does the month have
Sample code:
*/
function is_leap_year(year){
	if((year%4==0)&&(year%100!=0)){
		return 1;
	}
	if(year%400==0) return 1;
	return 0;
}
/*
Function: get_day_in_month
Description: return the day in a month
Input:month, year
Process:
Output: how many days does the month have
Sample code:
*/
function get_day_in_month(month,year){
	switch(month){
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
			break;
		case 2:
			if(is_leap_year(year)){
				return 29;
			}
				else {
					return 28;
				}
		break;
	}
}
/*
Function: valid_date
Description: check the day is exist or not
Input: day,month,year as integer
Process:
Output: a day or not a day
Sample code:
*/
function valid_date(day,month,year){
	if((month<1)||(month>12)){
		return 0;
	}
	if(day<1) return 0;
	if(day>get_day_in_month(month,year)) return 0;
	return 1;		
}
/*
Function: is_strdate
Describle: is str formated as a valid date
Input: 
	str: date as string
		+dd-mm-YYYY and some
		+dd/mm/YYYY and some other 
	formated: dd/mm/YYYY or some	
Process:
Output:
Sample code:
*/			
function valid_date_string(str,format){
	var sign="-";
	if(str.indexOf('/')!=-1) sign='/';
	var set=str.split(sign);
	var setf=format.split(sign);
	if(set.length!=3) return 0;
	var day=0;
	var month=0;
	var year=0;
	for(var i=0;i<3;i++){
		tmp=setf[i];
			if(tmp.indexOf('d')!=-1){
				day=set[i];
			}
			if(tmp.indexOf('m')!=-1){
				month=set[i];
			}
			if(tmp.indexOf('Y')!=-1){
				year=set[i];
			}
	}
	return valid_date(day,month,year);
}

/*
Function: valid_inset
Description: str in set or not
Input: year
Process:
Output:
Sample code:
*/
function valid_inset(str,set)
{
	str = new String(str);
	set = new String(set);
  tmp = set.split("|");
	for(var i=0;i<tmp.length;i++){
	  if(str==tmp[i]) return 1;
	}
    return 0;
}
/*
Function: valid_outset
Description: str out set or not
Input: year
Process:
Output:
Sample code:
*/
function valid_outset(str,set)
{
   str = new String(str);
	 set = new String(set);
   tmp=set.split("|");
    for(var i=0;i<tmp.length;i++){
      if(str==tmp[i]) return 0;
    }
    return 1;
}
/*
Function: valid_data
Description: str out set or not
Input: year
Process:
Output:
Sample code:
*/

