//-Lib JavaScript-------------------------------------------------------------------------------------
//-Author: Pham Tan Nhut
//----------------------------------------------------------------------------------------------------

//1. function isDigit(c) : Check if a character is a digit or not
function isDigit(c)
{
if((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||(c=='7')||(c=='8')||(c=='9'))
	return true;
else
	return false;
}

//----------------------------------------------------------------------------------------------------
//2. function isValidPhoneChar(c): Check if a character is valid for phone number.
//   Valid if it is a digit, "(" or ")"
function isValidPhoneChar(c)
{
if((c==' ')||(c=='.')||(c=='(')||(c==')')||(isDigit(c)))
	return true;
else
	return false;
}

//----------------------------------------------------------------------------------------------------
//3. Function TrimLeft(s): Remove all spaces at the beginning of a string
function trimLeft(s)
{
 var i; i=0;
 var n;
 n = s.length;
 while((i<n)&&(s.charAt(i)==' ')) i++;
	s = s.substring(i);
 return(s);
} 

//----------------------------------------------------------------------------------------------------
//4. Function trimRight(s): Remove all spaces at the end of a string
function trimRight(s)
{
 var n; n = s.length;
 var i;
 i = s.length-1;
 while((i>=0)&&(s.charAt(i)==' ')) i--;
	s = s.substring(0,i+1);
 return(s);
}

//----------------------------------------------------------------------------------------------------
//5. Function trimAll(s): Remove all leading and trailing spaces in a string
function trimAll(s)
{
 s = trimLeft(s);
 s = trimRight(s);
 return(s);
}  

//----------------------------------------------------------------------------------------------------
//6. Function isPosInt(s): check if a string is a valid positive integer
function isPosInt(s)
{
 var n; n = s.length
 if(n==0) return false;
 for(i=0;i<n;i++)
	if(!isDigit(s.charAt(i))) return false;
 return true;
}

//----------------------------------------------------------------------------------------------------
//7. Function isPosReal(s):check if a string is a valid positive real number or not(.as decimal number)
function isPosReal(s)
{
 var dot;
 s = trimAll(s);
 dot =0;
 for(i=0;i<s.length;i++)
	if(!isDigit(s.charAt(i))) 
		{
			if(s.charAt(i)=='.') 
				{
					dot++;
					if(i==s.length-1) return false;
					if(dot>1) return false;
				}
			else return false;	
		}
 return true;
}

//----------------------------------------------------------------------------------------------------
//7. Function isValidPhoneNumber(s): check if a string is a valid phone number
function isValidPhoneNumber(s)
{
 var n; n = s.length
 if(n==0) return false;
 for(i=0;i<n;i++)
	if(!isValidPhoneChar(s.charAt(i))) return false;
 return true;
}

//----------------------------------------------------------------------------------------------------
//8. Function isValidDate(strDate): check if a string is a valid date. The format of date is mm/dd/yyyy
function isValidDate(strDate)
{
 var m;
 var d;
 var y;
 var i1;
 var i2;
 
 strDate=trimAll(strDate);
 if(strDate=="") return false;
 i1 = strDate.indexOf("/")
 if(i1<0) return false;
 m = strDate.substring(0,i1)
 i2= strDate.indexOf("/",i1+1)
 if(i2<0) return false;
 d = strDate.substring(i1+1,i2)
 y = strDate.substring(i2+1)

 if((m=="")||(d=="")||(y=="")) return false;
 if((m==0)||(d==0)||(y==0)) return false;
 if(!isPosInt(m))
 	 return false;
 else
 	{	
	 m = parseInt(m);
	 if(m>12) return false;
 	}

 if(!isPosInt(y))
 	 return false;
 else
	{
	 y = parseInt(y)
	 if(y>9999) return false;
	}

 if(!isPosInt(d))
 	 return false;
 else
	{
	 d = parseInt(d)
	 if((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12))
		 if(d>31) return false;
 	 if((m==4)||(m==6)||(m==9)||(m==11))
		if(d>30) return false;

	 if(m==2)
		{
		 if(d>29) return false;
		 if((y%4)!=0) // not a leap year
		 	if(d>28) return false;
		}
	}

return true
}

//----------------------------------------------------------------------------------------------------
//9. Function isValidTime(strTime): check if a string is a valid time. The format of time is hh:mm:ss
function isValidTime(strTime)
{
 var h;
 var m;
 var s;
 var i1;
 var i2;
 
 strTime=trimAll(strTime);
 if(strTime=="") return false;
 i1 = strTime.indexOf(":")
 if(i1<0) return false;
 h = strTime.substring(0,i1)
 i2= strTime.indexOf(":",i1+1)
 if(i2<0) return false;
 m = strTime.substring(i1+1,i2)
 s = strTime.substring(i2+1)

 if((h=="")||(m=="")||(s=="")) return false;
 if(!isPosInt(h))
 	 return false;
 else
 	{	
	 h = parseInt(h);
	 if(h>23) return false;
 	}

 if(!isPosInt(m))
 	 return false;
 else
	{
	 m = parseInt(m)
	 if(m>59) return false;
	}

if(!isPosInt(s))
 	 return false;
 else
	{
	 s = parseInt(s)
	 if(s>59) return false;
	}

return true
}

////----------------------------------------------------------------------------------------------------
//10. Function isEmail(strEmail): check if an email address is valid (format only) 
function isEmail(strEmail)
{
 var intlen;
 var ctmp;
 strEmail = trimAll(strEmail);
 if(strEmail=='') return false;
 intlen=strEmail.length;
 if(intlen<5) return false;
 if(strEmail.indexOf('@')==-1) return false;
 if(strEmail.indexOf('.')==-1) return false;
 if(intlen - strEmail.lastIndexOf('.') -1 > 3) return false; 
 if((strEmail.indexOf("_")!=-1) && (strEmail.lastIndexOf("_") > strEmail.lastIndexOf("@"))) return false;
 if(strEmail.lastIndexOf(".") <= strEmail.lastIndexOf("@")+1)  return false;
 if(strEmail.indexOf("@")!=strEmail.lastIndexOf("@")) return false;
 if(intlen -1 == strEmail.lastIndexOf('.')) return false;
 if(strEmail.charAt(strEmail.indexOf('@')+1)=='.') return false;
 if(strEmail.indexOf(" ")!=-1) return false;
 if(strEmail.indexOf("..")!=-1) return false;
 
 strEmail=strEmail.toLowerCase();
 for(intcnt=0;intcnt<intlen;intcnt++)
	{
	 ctmp = strEmail.charAt(intcnt)
	 if((!isDigit(ctmp))&& ((ctmp>'z')||(ctmp<'a')) && (ctmp!='-') && (ctmp!='.') && (ctmp!='@') && (ctmp!='_')) return false;
	}

return true;
}

//----------------------------------------------------------------------------------------------------
//11. Function isZip(str): receive  a string, return true if it has a valid format of US 5 digits zip code.
function isZip(str)
{
 str=trimAll(str);
 if(str=='') return false;
 if(str.length!=5) return false;
 if(!isPosInt(str)) return false;
 return true;
}

//----------------------------------------------------------------------------------------------------
//12. Function getFileName(str): receive a full path file name, return only the file name
//    ex: input = C:\Windows\myfile.txt
//        output = myfile.txt
function getFileName(str)
{
 var bpos
 var filename
 if((str=='')||(str.indexOf("\\")==-1)) return(str);
 bpos = str.lastIndexOf("\\");
 filename = str.substring(bpos+1,str.length)
 return(filename);
}

//----------------------------------------------------------------------------------------------------
//13. Function getFileType(str): receive a full path file name, return only the file extension
//    ex:      input  = C:\Windows\myfile.txt
//             output = txt
function getFileType(str)
{
 var filename;
 var fileext;
 var dotpos;
 fileext ='';
 filename = getFileName(str);
 dotpos = filename.lastIndexOf(".");
 
 if(dotpos!=-1)
	{
		fileext = filename.substring(dotpos+1,filename.length);
		fileext = fileext.toLowerCase();
	}
 else
	{
		fileext = '';
	}
 return(fileext);
}
//----------------------------------------------------------------------------------------------------
//14.Caculate total days in a months
function GetDaysInMonth(strMonth, strYear)
{	
	var m ;
	var DaysInMonth;
	m = parseInt(strMonth);
	y = parseInt(strYear);
	/*if ((m == 1)||(m == 3)||(m == 5)||(m == 7)||(m == 8)||(m == 10)||(m == 12))
		{
			DaysInMonth = 31;

		}	
	if((m == 4)||(m == 6)||(m == 9)||(m == 11))
		{
			DaysInMonth = 30;

		}

	if (m == 2)
		{
		if ((y % 4) == 0)
			DaysInMonth = 29;
		else
			DaysInMonth = 28;
		}
return DaysInMonth;*/
	return m;
}