/*
- isDigit : test it a character is a digit or not.
- trimLeft : cut leading blank spaces of a string.
- trimRight: cut trailing blank spaces of a string.
- trim: 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. Format :mm/dd/yyyy
- 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.
- getFileType: return the file extenstion from the full file name.
*/
/*
 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=trim(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
}

/*
 isEmail
 check if an email address is valid (format only) 
*/
function isEmail(strEmail)
{
 var intlen;
 var ctmp;
 strEmail = trim(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 < 0) 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	;
}
/* 
 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 getFileName(str)
{
	var temp,extpost,fname;
	temp = str;
	extpost = temp.lastIndexOf("\\");
	if(extpost != -1)
	{
		fname = temp.substring(extpost + 1,temp.length);
		fname = fname.toLowerCase();
	}
	else
	{
		fname = '';
	}
	return(fname);
}
/*
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;
}

/*
 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 = trim(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;
}

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

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

/*
 isZip
 receive  a string, return true if it has a valid format of US 5 digits zip code.
*/
function isZip(str)
{
 str=trim(str);
 if(str=='') return false;
 if(str.length!=5) return false;
 if(!isPosInt(str)) return false;
 return true;

}
/*
	receive two format date string, return 1 if first string greater than other,
	else return 0
	- type = 1 : Set Valid regitration date.
	- type = 2 : Set Advanced/Early ReG. range date.
*/
function compareDate(str1,str2,type)
{
 var m1;
 var d1;
 var y1;
 var m2;
 var d2;
 var y2;
 var i1;
 var i2; 
 var s1;
 var s2;
 strDate1=trim(str1);
 if(strDate1=="") return false;
 i1 = strDate1.indexOf("/")
 if(i1<0) return false; 
 m1 = strDate1.substring(0,i1);
    
 i2= strDate1.indexOf("/",i1+1)
 if(i2<0) return false;
 d1 = strDate1.substring(i1+1,i2);

 y1 = strDate1.substring(i2+1)

 strDate2=trim(str2);
 if(strDate2=="") return false;
 i1 = strDate2.indexOf("/")
 if(i1<0) return false; 
 m2 = strDate2.substring(0,i1);
    
 i2= strDate2.indexOf("/",i1+1)
 if(i2<0) return false;
 d2 = strDate2.substring(i1+1,i2);
 

 y2 = strDate2.substring(i2+1)
 
 if(parseInt(y1)==parseInt(y2))
 {	
	if(parseInt(m1)<parseInt(m2))
	{
		return false;
	}
	else if(parseInt(m1)==parseInt(m2)) 
	{
		if(parseInt(type)==1)
		{
			if(parseInt(d2)!=1)
			{
				return false;
			}
			else if(parseInt(d1)<=parseInt(d2))
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else if(parseInt(d1)<=parseInt(d2))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}
else 
{
	if(parseInt(y1)>parseInt(y2))	
	{

		return true;
	}
	else
	{

		return false;
	}	

 }		
}

//--------------------------------------------------------------
// Function:    isLeapYear(iYear)
// Description: Tests the iYear argument to see whether it is
//              a leap year.
// Author:      Steve Crane
//--------------------------------------------------------------
function isLeapYear(iYear) {
  return (iYear % 4 ? 0 : iYear % 100 ? 1 : iYear % 400 ? iYear == 200 ? 1 : 0 : 1);
}

/*   DateSub
	input : DateInput is date form string("mm/dd/yyyy"); interval is a number.
	output: DateOutput = DateInput - interval. 
*/
function DateSub(DateInput,interval)
{
	var date1 = new Date(DateInput);		
	var d1 = parseInt(date1.getDate());	
	var m1 = parseInt(date1.getMonth()) + 1;
	var y1 = parseInt(date1.getYear());	
	var inter1 = d1 - interval;	
	if (inter1 <=0)
	{
		while(inter1<=0)
		{
			if(m1==1)
			{
				d1 = 31 + inter1;
				m1 = 12;
				y1 = y1 - 1;
			}
			else if(m1==2 || m1==4 || m1==6 || m1==8 || m1==9 || m1==11)
			{
				d1 = 31 + inter1;
				m1 = m1 - 1;
			}
			else if (m1==3)
			{
				if(isLeapYear(y1))
				{
					d1 = 29 + inter1;				
				}
				else
				{
					d1 = 28 + inter1;
				}
				m1 = m1 - 1;				
			}		
			else
			{
				d1 = 30 + inter1;
				m1 = m1 - 1;
			}
			inter1 = d1;
		} // end while	
	}
	else
	{		
		d1 = d1 - interval;
	}
	DateOutput = m1 + "/" + d1 + "/" + y1;
	return(DateOutput);
}

