﻿/*-------------------------- Some JavaScript utilities --------------------------------

This file contains source code of some utilities that are used usually  in JavaScript 
programming:
- writeDate: write current date to browser
- isDigit : test if a character is a digit or not.
- trimLeft : cut leading blank spaces of a string.
- trimRight: cut trailing blank spaces of a string.
- trimAll: cut all leading and trailing blank spaces of a string
- isPosInt: test if a string has the format of a positive integer number or not.
- isPosReal: test if a string has the format of a positive real number or not. The decimal 
			 separator must be ".".
- isValidDate: test if a string has the format of a valid date or not.
- isValidDeliveryDate: check if a string is a valid date and after current date. The format of date is dd/mm/yyyy
- isValidTime: check if a string is a valid time. The format of time is hh:mm:ss
- isEmail: test if a string is a valid e-mail address or not
- isZip: test if a string has the format of a US zip code or not.
- getFileName: return the file name form the full file name.
- getFileExt: return the file extenstion from the full file name.
/*--------------------------------Implementation ---------------------------------------*/

/*
writeDate
Write current datetime to browser
*/
function writeDate()
{
today = new Date(); 
weekday = today.getDay();
if (weekday == 6) document.write('Thứ bảy');
if (weekday == 0) document.write('Chủ Nhật');
if (weekday == 1) document.write('Thứ hai');
if (weekday == 2) document.write('Thứ ba');
if (weekday == 3) document.write('Thứ tư');
if (weekday == 4) document.write('Thứ năm');
if (weekday == 5) document.write('Thứ sáu');
document.write(', ');
date = today.getDate(); 
document.write(date); 
month = today.getMonth();
if (month == 0) document.write('-01');
if (month == 1) document.write('-02');
if (month == 2) document.write('-03');
if (month == 3) document.write('-04');
if (month == 4) document.write('-05');
if (month == 5) document.write('-06');
if (month == 6) document.write('-07');
if (month == 7) document.write('-08');
if (month == 8) document.write('-09');
if (month == 9) document.write('-10');
if (month == 10) document.write('-11');
if (month == 11) document.write('-12'); 
year=today.getYear();
if (year <= 1999) 
document.write ('-',1900+year);
else document.write ('-',year);
}

/*
isDigit
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;
}

/*
 trimLeft
 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);
} 

/*
 trimRight
 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);
}

/* 
 trimAll
 Remove all leading and trailing spaces in a string
*/
function trimAll(s)
{
 s = trimLeft(s);
 s = trimRight(s);
 return(s);
}  

/*
 isPosInt
 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;
}

/*
 isPosReal
 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;
}

/*
 isValidDate
 check if a string is a valid date. The format of date can be specified 
 1- is mm/dd/yyyy
 2- is dd/mm/yyyy
 3 -is mm/dd/yy
 4 -is dd/mm/yy 	
*/
function isValidDate(strDate,intFormatDate)
{
 var m;
 var d;
 var y;
 var i1;
 var i2;
 
 if((intFormatDate!=1)&&(intFormatDate!=2)&&(intFormatDate!=3)&&(intFormatDate!=4)) return false;

 strDate=trimAll(strDate);
 if(strDate=="") return false;
 i1 = strDate.indexOf("/")
 if(i1<0) return false;
 
 if((intFormatDate==1)||(intFormatDate==3))
 	m = strDate.substring(0,i1);
 else
	d = strDate.substring(0,i1);

  
 i2= strDate.indexOf("/",i1+1)
 if(i2<0) return false;

 if((intFormatDate==1) || (intFormatDate==3))
	d = strDate.substring(i1+1,i2);
 else
	m = strDate.substring(i1+1,i2);
 
 y = strDate.substring(i2+1)

 if((m=="")||(d=="")||(y=="")) return false;
 if((m==0)||(d==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((y>=100)&&(y<1900)) 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
}

/*
 isValidDeliveryDate
 check if a string is a valid date and after current date. The format of date is dd/mm/yyyy
*/
function isValidDeliveryDate(strDate)
{
  
 strDate=trimAll(strDate);
 if (! isValidDate(strDate))
 {
	return false;
 }
 else
 {
	var c_date = new Date();
	var c_m = parseInt(c_date.getMonth()) + 1;
	var c_d = parseInt(c_date.getDate());
	var c_y = parseInt(c_date.getYear());
	
	var i1 = strDate.indexOf("/");
	var d_d = parseInt(strDate.substring(0,i1));
	var i2= strDate.indexOf("/",i1+1);
	var d_m = parseInt(strDate.substring(i1+1,i2));
	var d_y = parseInt(strDate.substring(i2+1));
	
	if (d_y > c_y)
	{
		return true;
	}
	else
	{
		if (d_y < c_y)
		{
			return false;
		}
		else
		{
			if (d_m > c_m)
			{
				return true;
			}
			else
			{
				if (d_m < c_m)
				{
					return false;
				}
				else
				{
					if (d_d > c_d)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
		}
	}
 }

return true;
}


/*
 isValidTime
 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((h==0)||(m==0)||(s==0)) 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
}


/*
 isEmail
 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	;
}

/*
 isZip
 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;
}

/*
getFileName
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);
}

/* 
 getFileType
 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);
}

function openWindow(szURL)
{
	var szLinkedURL;
	if (szURL.substr(0, 7) !="http://")
		szLinkedURL = "http://" + szURL
	else
		szLinkedURL = szURL;
	open(szLinkedURL);
}

function viewImage(szURL)
{
	open(szURL, "ViewImage", "toolbar=no,width=536,height=380,screenX=200,screenY=200,directories=no,status=no,scrollbars=yes,resize=no,menubar=no");
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

