//�Ѻ onBlur 
//		��	str � input ���� date ������� 1 � ��Ѻ� � �͹ �   
//		��	finput � �Ẻͧ dmy,mdy,ymd,ydm ��
//		�� type � �Ẻͧ .���.���� ' T ' �� ' E '  �ҹ�
//		�� sym � �Ẻͧ�������͹�    xxxxxx     Linux Modification
//************************************CHECK DATE*******************************************//
function CheckDate(str,finput,type,sym){
st = new String ();
st = type;
tp=str;
str=str.value;
x = st.toUpperCase();
pattern = finput;

if ((x!="T") && (x!="E")) { 
					alert("Invalid Type");     //check type 
					tp.select();
					tp.focus();    

//************** "ymd " and "ydm""******************//

}else if ((pattern=="ydm")||(pattern=="ymd")){						
				
				temp = str;
				dd=temp.substr(8,2);
				mm=temp.substr(5,2);
				yy1=temp.substr(0,2);
				yy2=temp.substr(2,2);
				
				myArray1 = /(\d){2}/.exec(yy1);
				myArray2 = /(\d){2}/.exec(yy2);
				myArray3 = /(\d){2}/.exec(mm);
				myArray4 = /(\d){2}/.exec(dd);

/***************************************************************************************************/		
//**********[YYYY-MM-D]************//
alert(parseInt(dd.length));
if((myArray1!=null)&&(myArray2!=null)&&(myArray3!=null)&&(myArray4==null)){						
					
					yy1=temp.substr(0,2);
					yy2=temp.substr(2,2);
					mm=temp.substr(5,2);
					dd=temp.substr(8,1);
									
				if (dd!=0) {
				    dd = 0+dd;
					myArray1 = /(\d){2}/.exec(yy1);
					myArray2 = /(\d){2}/.exec(yy2);
					myArray3 = /(\d){2}/.exec(mm);
					myArray4 = /(\d){2}/.exec(dd);
				}

//**********[YYYY-M-DD]***********//

} else if ((myArray1!=null)&&(myArray2!=null)&&(myArray3==null)&&(myArray4==null)&&(dd.length!=0)) {														
					yy1=temp.substr(0,2);
					yy2=temp.substr(2,2);
					mm=temp.substr(5,1);
					dd=temp.substr(7,2);
										
				if (mm!=0) {
					mm = 0+mm;
					myArray1 = /(\d){2}/.exec(yy1);
					myArray2 = /(\d){2}/.exec(yy2);
					myArray3 = /(\d){2}/.exec(mm);
					myArray4 = /(\d){2}/.exec(dd);
				}

//************[YYYY-M-D]***************//

} else if ((myArray1!=null)&&(myArray2!=null)&&(myArray3==null)&&(myArray4==null)&&(dd.length==0)) {					
						dd=temp.substr(7,1);
						mm=temp.substr(5,1);
						yy1=temp.substr(0,2);
						yy2=temp.substr(2,2);
				if ((mm!=0)&&(dd!=0)) {
					dd= 0+dd;   mm = 0+mm;   
					myArray1 = /(\d){2}/.exec(yy1);
					myArray2 = /(\d){2}/.exec(yy2);
					myArray3 = /(\d){2}/.exec(mm);
					myArray4 = /(\d){2}/.exec(dd);
				} 

//************[YY-MM-D]***************//

} else if ((myArray1!=null)&&(myArray2==null)&&(myArray3==null)&&(myArray4==null)&&(temp.substr(7,1).length==0)&&(mm.length==2)){ 
					
					yy1=temp.substr(0,2);
					yy2=temp.substr(3,2);
					mm=temp.substr(6,1);
										
				if (mm!=0) {
				    mm = 0+mm;
					myArray1 = /(\d){2}/.exec(yy1);
					myArray2 = /(\d){2}/.exec(yy2);
					myArray3 = /(\d){2}/.exec(mm);
					myArray4 = "";
				}

//************[YY-M-DD]***************//
} else if ((myArray1!=null)&&(myArray2==null)&&(myArray3!=null)&&(myArray4==null)){

					yy1=temp.substr(0,2);
					yy2=temp.substr(3,1);
					mm=temp.substr(5,2);
				
				if (yy2!=0) {
					yy2 = 0+yy2;
					myArray1 = /(\d){2}/.exec(yy1);
					myArray2 = /(\d){2}/.exec(yy2);
					myArray3 = /(\d){2}/.exec(mm);
					myArray4 = "";
				}

//************[YY-M-D]***************//
} else if ((myArray1!=null)&&(myArray2==null)&&(myArray3==null)&&(myArray4==null)&&(mm.length==1)){

					yy1=temp.substr(0,2);
					yy2=temp.substr(3,1);
					mm=temp.substr(5,1);
				if ((yy2!=0)&&(mm!=0)) {
					mm= 0+mm;   yy2 = 0+yy2;  
					myArray1 = /(\d){2}/.exec(yy1);
					myArray2 = /(\d){2}/.exec(yy2);
					myArray3 = /(\d){2}/.exec(mm);
					myArray4 = "";
				}
//************[YY-MM-DD]***************//
} else if ((myArray1!=null)&&(myArray2==null)&&(myArray3==null)&&(myArray4==null)&&(temp.substr(7,1).length==1)&&(mm.length==2)){
					yy1=temp.substr(0,2);
					yy2=temp.substr(3,2);
					mm=temp.substr(6,2);
					if(mm!=00){
					myArray1 = /(\d){2}/.exec(yy1);
					myArray2 = /(\d){2}/.exec(yy2);
					myArray3 = /(\d){2}/.exec(mm);
					myArray4 = "";
					}
				}
if (dd.length==0) { tmp1=mm;  tmp2=dd; mm=yy2; dd=tmp1; yy2=tmp2;	}
if (pattern=="ydm") {	 tmp=dd;  dd=mm;  mm=tmp;	}	//change ymd to ydm//
/*************************************************************************************************/
if((dd==00)&&(mm==00)&&(yy1==00)&&(yy2==00)&&(myArray4!=null)){
		tp.value= str;
}
else{

if (temp.length>10){						//Check Length//
		alert("Invalid Length");	
			tp.select();
			tp.focus(); 
}else if((myArray1==null)||(myArray2==null)||(myArray3==null)||(myArray4==null)){   //Check Number//
		//alert("Invalid Input");
		//	tp.select();
		//	tp.focus();
}else  if ((dd>31)||(dd==00)) {						// Check Day //
		alert("Invalid Day"); 	 
		tp.select();
			tp.focus();
}else if ((mm>12)||(mm==00)) {  
		alert("Invalid Month");// Check Month //
		tp.select();
			tp.focus();
} else if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && ((dd > 30) || (dd == 00))){
		alert("Invalid Day");
		tp.select();
			tp.focus();
} else if (yy2.length==0){							// Form xx-xx-xx //
		if (pattern=="ydm") {	 tmp=dd;  dd=mm;  mm=tmp;	}	//change ymd to ydm//
		if ((yy1>=0)&&(yy1<=60)){						
				ch=25+yy1;   ch1=ch-543;			
				if (x=="E") {	ch=20+yy1;	ch1=ch; }    // check type Eng or Thai //
					//check day 28,29 of month 02 
				if ((mm==02)&&((ch1%4)==0)&&(dd>29)){
					alert("Invalid Day");  
					tp.select();
					tp.focus();
				} else if ((mm==02)&&((ch1%4)!=0)&&(dd>28)){
				alert("Invalid Day");
				tp.select();
				tp.focus();
				} else {
					// show  solution 
					tp.value=ch+sym+mm+sym+dd;
					
				}
		}else{ 	
				if (pattern=="ydm") {	 tmp=dd;  dd=mm;  mm=tmp;	}	//change ymd to ydm//
				ch=24+yy1;		ch1=ch-543;
				if (x=="E") { ch =19+yy1; ch1=ch; }		// check type Eng or Thai //
					//check day 28,29 of month 02
				if ((mm==02)&&((ch1%4)==0)&&(dd>29)){
				alert("Invalid Day");
				tp.select();
				tp.focus();
				} else if ((mm==02)&&((ch1%4)!=0)&&(dd>28)){
				alert("Invalid Day");
				tp.select();
				tp.focus();
				} else { 
					// show  solution 
					tp.value=ch+sym+mm+sym+dd;
					
				}
			 }
}else if(((yy2.length!=0))&&(x=="E")){		// Form xx-xx-xxxx type Eng//
				ch=yy1+yy2;  ch1 = ch;
				//check day 28,29 of month 02
				if ((mm==02)&&((ch1%4)==0)&&(dd>29)){
					alert("Invalid Day")
					tp.select();
					tp.focus();
				}else if ((mm==02)&&((ch1%4)!=0)&&(dd>28)){
					alert("Invalid Day");
					tp.select();
					tp.focus();
				} else { 
					// show  solution
					tp.value=ch+sym+mm+sym+dd;
					
				}
}else if (((yy2.length!=0))&&(x=="T")){		// Form xx-xx-xxxx type Thai//
				ch=yy1+yy2;  ch1 = ch-543;
				//check day 28,29 of month 02
				if ((mm==02)&&((ch1%4)==0)&&(dd>29)){
					alert("Invalid Day");
					tp.select();
					tp.focus();
				}else if ((mm==02)&&((ch1%4)!=0)&&(dd>28)){
					alert("Invalid Day"); 
					tp.select();
					tp.focus();
				} else { 
					// show  solution 
					tp.value=ch+sym+mm+sym+dd;
				}
} else {  
		alert("Invalid Year");			//end condition dmy mdy
		tp.select();
		tp.focus();
}																			

}

//***********************************************************************************************************//
//***********************************************************************************************************//

//[fuction "dmy " and "mdy"]//
}else if ((pattern== "dmy")||(pattern=="mdy")) {     
				temp = str;
				
				dd	=	temp.substr(0,2);               // cut string 
				mm	=	temp.substr(3,2);
				yy1	=	temp.substr(6,2);
				yy2	=	temp.substr(8,2);

				myArray1 = /(\d){2}/.exec(dd);			//check number match
				myArray2 = /(\d){2}/.exec(mm);
				myArray3 = /(\d){2}/.exec(yy1);
				myArray4 = /(\d){2}/.exec(yy2);

/************************************************************************************************************/

//**********[D-MM-YYYY]************//

if((myArray1==null)&&(myArray2==null)&&(myArray3!=null)&&(myArray4==null)&&(yy2.length!=0)){											
					dd=temp.substr(0,1);
					mm=temp.substr(2,2);
					yy1=temp.substr(5,2);
					yy2=temp.substr(7,2);
				
				if (dd!=0) {
				    dd = 0+dd;
					myArray1 = /(\d){2}/.exec(dd);
					myArray2 = /(\d){2}/.exec(mm);
					myArray3 = /(\d){2}/.exec(yy1);
					myArray4 = /(\d){2}/.exec(yy2);
				}

//**********[DD-M-YYYY]***********//

} else if ((myArray1!=null)&&(myArray2==null)&&(myArray3!=null)&&(myArray4==null)) {														
					dd=temp.substr(0,2);
					mm=temp.substr(3,1);
					yy1=temp.substr(5,2);
					yy2=temp.substr(7,2);
				if (mm!=0) {
					mm = 0+mm;
					myArray1 = /(\d){2}/.exec(dd);
					myArray2 = /(\d){2}/.exec(mm);
					myArray3 = /(\d){2}/.exec(yy1);
					myArray4 = /(\d){2}/.exec(yy2);
				}

//************[D-M-YYYY]***************//

} else if ((myArray1==null)&&(myArray2==null)&&(myArray3!=null)&&(myArray4==null)&&(yy2.length==0)) {					
						dd=temp.substr(0,1);
						mm=temp.substr(2,1);
						yy1=temp.substr(4,2);
						yy2=temp.substr(6,2);
				if ((mm!=0)&&(dd!=0)) {
					dd= 0+dd;   mm = 0+mm;   
					myArray1 = /(\d){2}/.exec(dd);
					myArray2 = /(\d){2}/.exec(mm);
					myArray3 = /(\d){2}/.exec(yy1);
					myArray4 = /(\d){2}/.exec(yy2);
				}

//************[D-MM-YY]***************//

} else if ((myArray1==null)&&(myArray2==null)&&(myArray3==null)&&(myArray4==null)&&(yy1.length!=0)){ 
					
					dd=temp.substr(0,1);
					mm=temp.substr(2,2);
					yy1=temp.substr(5,2);
				if (dd!=0) {
				    dd = 0+dd;
					myArray1 = /(\d){2}/.exec(dd);
					myArray2 = /(\d){2}/.exec(mm);
					myArray3 = /(\d){2}/.exec(yy1);
					myArray4 = "";
				}

//************[DD-M-YY]***************//
} else if ((myArray1!=null)&&(myArray2==null)&&(myArray3==null)&&(myArray4==null)){

					dd=temp.substr(0,2);
					mm=temp.substr(3,1);
					yy1=temp.substr(5,2);
				if (mm!=0) {
					mm = 0+mm;
					myArray1 = /(\d){2}/.exec(dd);
					myArray2 = /(\d){2}/.exec(mm);
					myArray3 = /(\d){2}/.exec(yy1);
					myArray4 = "";
				}

//************[D-M-YY]***************//
} else if ((myArray1==null)&&(myArray2==null)&&(myArray3==null)&&(myArray4==null)&&(yy1.length==0)){

					dd=temp.substr(0,1);
					mm=temp.substr(2,1);
					yy1=temp.substr(4,2);
				if ((mm!=0)&&(dd!=0)) {
					dd= 0+dd;   mm = 0+mm;   
					myArray1 = /(\d){2}/.exec(dd);
					myArray2 = /(\d){2}/.exec(mm);
					myArray3 = /(\d){2}/.exec(yy1);
					myArray4 = "";
				}
//************[DD-MM-YY]***************//
} else if ((myArray1!=null)&&(myArray2!=null)&&(myArray3!=null)&&(myArray4==null)){

					dd=temp.substr(0,2);
					mm=temp.substr(3,2);
					yy1=temp.substr(6,2);
					if(dd!=00){
					myArray1 = /(\d){2}/.exec(dd);
					myArray2 = /(\d){2}/.exec(mm);
					myArray3 = /(\d){2}/.exec(yy1);
					myArray4 = "";
					}
}
if (pattern=="mdy") {	 tmp=dd;  dd=mm;  mm=tmp;	}	//change dmy to mdy//

/*************************************************************************************************/
if((dd==00)&&(mm==00)&&(yy1==00)&&(yy2==00)&&(myArray4!=null)){
		tp.value= str;
}else{

if(temp.length>10){						//Check Length//
		alert("Invalid Length");	
		tp.select();
		tp.focus();
}else if((myArray1==null)||(myArray2==null)||(myArray3==null)||(myArray4==null)){   //Check Number//
		//alert("Invalid Input");
		//tp.select();
		//tp.focus();
}else  if ((dd>31)||(dd==00)) {						// Check Day //
		alert("Invalid Day");
		tp.select();
		tp.focus();
} else if ((mm>12)||(mm==00)) {  
		alert("Invalid Month");// Check Month //
		tp.select();
		tp.focus();
} else if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && ((dd > 30) || (dd == 00))){
		alert("Invalid Day");
		tp.select();
			tp.focus();
}else if (yy2.length==0){							// Form xx-xx-xx //
		if ((yy1>=0)&&(yy1<=60)){						
				ch=25+yy1;   ch1=ch-543;			
				if (x=="E") {	ch=20+yy1;	ch1=ch; }    // check type Eng or Thai //
					//check day 28,29 of month 02 
				if ((mm==02)&&((ch1%4)==0)&&(dd>29)){
					alert("Invalid Day");
					tp.select();
					tp.focus();
				} else if ((mm==02)&&((ch1%4)!=0)&&(dd>28)){
					alert("Invalid Day");
					tp.select();
					tp.focus();
				} else {
					// show  solution 
				if (pattern=="mdy") {	 tmp=dd;  dd=mm;  mm=tmp;	}
					tp.value=dd+sym+mm+sym+ch;
					
				}
		}else{ 											
				ch=24+yy1;		ch1=ch-543;
				if (x=="E") { ch =19+yy1; ch1=ch; }		// check type Eng or Thai //
					//check day 28,29 of month 02
				if ((mm==02)&&((ch1%4)==0)&&(dd>29)){
					alert("Invalid Day");
					tp.select();
					tp.focus();
				} else if ((mm==02)&&((ch1%4)!=0)&&(dd>28)){
					alert("Invalid Day");
					tp.select();
					tp.focus();
				} else { 
					// show  solution 
			if (pattern=="mdy") {	 tmp=dd;  dd=mm;  mm=tmp;	}
					tp.value=dd+sym+mm+sym+ch;
					  
				}
			 }
}else if(((yy2.length!=0))&&(x=="E")){		// Form xx-xx-xxxx type Eng//
				ch=yy1+yy2;  ch1 = ch;
				//check day 28,29 of month 02
				if ((mm==02)&&((ch1%4)==0)&&(dd>29)){
					alert("Invalid Day");
					tp.select();
					tp.focus();
				}else if ((mm==02)&&((ch1%4)!=0)&&(dd>28)){
					alert("Invalid Day");
					tp.select();
					tp.focus();
				} else { 
					// show  solution 
				if (pattern=="mdy") {	 tmp=dd;  dd=mm;  mm=tmp;	}
					tp.value=dd+sym+mm+sym+ch;
						
				}
}else if (((yy2.length!=0))&&(x=="T")){		// Form xx-xx-xxxx type Thai//
				ch=yy1+yy2;  ch1 = ch-543;
				//check day 28,29 of month 02
				if ((mm==02)&&((ch1%4)==0)&&(dd>29)){
					alert("Invalid Day");
					tp.select();
					tp.focus();
				}else if ((mm==02)&&((ch1%4)!=0)&&(dd>28)){
					alert("Invalid Day");
					tp.select();
					tp.focus();
				} else { 
					// show  solution 
				if (pattern=="mdy") {	 tmp=dd;  dd=mm;  mm=tmp;	}
					tp.value=dd+sym+mm+sym+ch;
						
				}
} else {  
		alert("Invalid Year");	
		tp.select();
		tp.focus();					//end condition dmy mdy
		}
}
} else { 
		alert("Invalid Form");
		tp.select();
		tp.focus();
	  }									//end condition dmy mdy ymd ydm 
	  
// bee modify

xdd = tp.value.substr(0, tp.value.indexOf('/'));
if(parseInt(xdd.length) > 2){
	alert("Invalid Day");
	tp.select();
	tp.focus();
	return;
}

tmp_mm = tp.value.substr(tp.value.indexOf('/')+1, tp.value.length);
xmm = tmp_mm.substr(0, tmp_mm.indexOf('/'));
if(parseInt(xmm.length) > 2){
	alert("Invalid Month");
	tp.select();
	tp.focus();
	return;
}

yy_tmp = yy1 + yy2;
if(parseInt(yy_tmp.length) == 1 || parseInt(yy_tmp.length) == 3){
	alert("Invalid Year");
	tp.select();
	tp.focus();
	return;
}
// end bee modify 
}         // end funtion 


///// Trim function Prototype //////
            function strltrim() {
                return this.replace(/^\s+/,'');
            }
            function strrtrim() {
                return this.replace(/\s+$/,'');
            }
            function strtrim() {
                return this.replace(/^\s+/,'').replace(/\s+$/,'');
            }
            String.prototype.ltrim = strltrim;
            String.prototype.rtrim = strrtrim;
            String.prototype.trim = strtrim;
//////////////////////////////////////////////
