﻿// Copy right by Nguyen Duc Minh (minhcon@yahoo.com) - Web Division - Viet Nam Datatelecommunication Company
// This is a demo version, Please make suggestions, Thank you very much.
var Data = new Array
	(
	new Array(226, 97, 259, 234, 101, 105, 244, 111, 417, 117, 432, 121, 194, 65, 258, 202, 69, 73, 212, 79, 416, 85, 431, 89),
	new Array(7845, 225, 7855, 7871, 233, 237, 7889, 243, 7899, 250, 7913, 253, 7844, 193, 7854, 7870, 201, 205, 7888, 211, 7898, 218, 7912, 221),
	new Array(7847, 224, 7857, 7873, 232, 236, 7891, 242, 7901, 249, 7915, 7923, 7846, 192, 7856, 7872, 200, 204, 7890, 210, 7900, 217, 7914, 7922),
	new Array(7849, 7843, 7859, 7875, 7867, 7881, 7893, 7887, 7903, 7911, 7917, 7927, 7848, 7842, 7858, 7874, 7866, 7880, 7892, 7886, 7902, 7910, 7916, 7926),
	new Array(7851, 227, 7861, 7877, 7869, 297, 7895, 245, 7905, 361, 7919, 7929, 7850, 195, 7860, 7876, 7868, 296, 7894, 213, 7904, 360, 7918, 7928),
	new Array(7853, 7841, 7863, 7879, 7865, 7883, 7897, 7885, 7907, 7909, 7921, 7925, 7852, 7840, 7862, 7878, 7864, 7882, 7896, 7884, 7906, 7908, 7920, 7924)
	);
var Str
var Lcd
var Mode
Mode=1
function ChangeMode(id)
{
	Mode=id
}
function ChangeVowel1(cc,ch)
{
	var fb
	fb=0
	for(i=0;(i<6); i++)  
		{
		for (j=0;(j<24); j++)
			{	
			if (cc==String.fromCharCode(Data[i][j]))
				{
					fb=1
					if(ch=='a'||ch=='e'||ch=='o'||ch=='A'||ch=='E'||ch=='O')
					{
						if(ch=='a'||ch=='e'||ch=='o'){if(j>11) j=j-12}
						else if(j<12)j=j+12
						if(ch=='a'||ch=='A')
							{
								if (j==1||j==13) return String.fromCharCode(Data[i][j-1])
								else 
									if(j==0||j==12) 
										{
											if(i==0) return ch+ch
											else return String.fromCharCode(Data[i][j+1])
										}
									else if(j==2||j==14)return String.fromCharCode(Data[i][j-2])
							}
						else if(ch=='o'||ch=='O')
								{
									if (j==7||j==19) return String.fromCharCode(Data[i][j-1])
									else 
										if(j==6||j==18) 
											{
												if(i==0) return ch+ch
												else return String.fromCharCode(Data[i][j+1])
											}
										else if(j==8||j==20) return String.fromCharCode(Data[i][j-2])
								}
								else
								{
									if (j==4||j==16) return String.fromCharCode(Data[i][j-1])
									else 
										if(j==3||j==15) 
											{
												if(i==0) return ch+ch
												else return String.fromCharCode(Data[i][j+1])
											}
								}
					}
					else if(ch=='w'||ch=='W')
							{
								if(ch=='w'){if(j>11) j=j-12}
								else if(j<12)j=j+12
								if(j==1||j==7||j==9||j==13||j==19||j==21)return String.fromCharCode(Data[i][j+1])
								else
									if (j==2||j==8||j==10||j==14||j==20||j==22)
										{
											if(i==0) 
												{
													if(j!=10&&j!=22)return String.fromCharCode(Data[i][j-1])+String.fromCharCode(Data[i][j-1])
													else return ch
												}
											else return String.fromCharCode(Data[i][j-1])
										}
									else if(j==0||j==6||j==12||j==18) return String.fromCharCode(Data[i][j+2])
							}
				}	
			}	
		}
	if(fb==0)
		{
		if (ch=='w'||ch=='W')
			{
				if(ch=='w') return cc+String.fromCharCode(432)
				else return cc+String.fromCharCode(431)
			}
		else if(ch=='d'||ch=='D')
				{
					if(ch=='d')
						{
							if(cc=='d'||cc=='D')return String.fromCharCode(273)
							else if(cc==String.fromCharCode(273)||cc==String.fromCharCode(272)) return 'dd'
						}
					else
						{
							if(cc=='d'||cc=='D')return String.fromCharCode(272)
							else if(cc==String.fromCharCode(273)||cc==String.fromCharCode(272)) return 'DD'
						}
				}
		}
	return cc+ch;
}
function CheckVowel(cc)
{
	for(i=0;(i<6); i++)  
		{
			for (j=0;(j<24); j++)
				{
					if(cc==String.fromCharCode(Data[i][j])) return i
				}
		}
	return -1
}
function CheckPriority(c1,c2)
{
	c1=c1.toLowerCase()
	c2=c2.toLowerCase()
	if((c1=='i'||c1=='y')&&(c2==String.fromCharCode(234))) return true
	else if((c1=='o')&&(c2=='a'||c2=='e'||c2==String.fromCharCode(226)||c2==String.fromCharCode(259))) return true
	else if((c1=='u')&&(c2=='y'||c2==String.fromCharCode(417)||c2==String.fromCharCode(234)||c2==String.fromCharCode(226))) return true
	else if(c1==String.fromCharCode(432)&&c2==String.fromCharCode(417)) return true
	else return false
}
function ChangeWovel2(cc,l,ch)
{
	Lcd=''
	for (j=0;(j<24); j++)
		{
			if(cc==String.fromCharCode(Data[l][j]))
				{
					if(((ch=='s')||(ch=='S'))&&(l!=1))return String.fromCharCode(Data[1][j])
					else if(((ch=='f')||(ch=='F'))&&(l!=2))return String.fromCharCode(Data[2][j])
					else if(((ch=='r')||(ch=='R'))&&(l!=3))return String.fromCharCode(Data[3][j])
					else if(((ch=='x')||(ch=='X'))&&(l!=4))return String.fromCharCode(Data[4][j])
					else if(((ch=='j')||(ch=='J'))&&(l!=5))return String.fromCharCode(Data[5][j])
					else {Lcd=ch;return String.fromCharCode(Data[0][j])}
				}
		}
}
function ChangeWord(Wd,ch)
{
	var c1
	var c2
	var c3
	wl=Wd.length
	c1=Wd.charAt(wl-1)
	cl=CheckVowel(c1)
	if((cl==-1)&&('tghmncp'.indexOf(c1)!=-1))
		{
			if(wl==1) return Wd+ch
			c2=Wd.charAt(wl-2)
			if(c1=='h'||c1=='H')
			{
				if((wl<3)||((c2!='t')&&(c2!='n')&&(c2!='c')&&(c2!='T')&&(c2!='N')&&(c2!='C'))) return Wd+ch
				wl=wl-2
			}
			else if(c1=='g'||c1=='G')
					{
						if((wl<3)||((c2!='n')&&c2!='N')) return Wd+ch
						wl=wl-2
					}
					else 
						{
							if(wl<2) return Wd+ch
							wl=wl-1
						}
			c1=Wd.charAt(wl-1)
			cl=CheckVowel(c1)
			if(cl==-1) return Wd+ch
		}
	if((wl>1)&&(cl==0))
		{
			c2=Wd.charAt(wl-2)
			l2=CheckVowel(c2)
			if(l2==-1) return Wd.substring(0,wl-1)+ChangeWovel2(c1,0,ch)+Wd.substring(wl)+Lcd
			else
				{
					if((wl>2)&&(l2==0))
						{
							if((CheckPriority(c2,c1))||((c2=='u'||c2=='U')&&(Wd.charAt(wl-3).toLowerCase()=='q')))
								return Wd.substring(0,wl-1)+ChangeWovel2(c1,cl,ch)+Wd.substring(wl)+Lcd
							else return Wd.substring(0,wl-2)+ChangeWovel2(c2,l2,ch)+Wd.substring(wl-1)+Lcd
						}	
					else
						{
							if(l2>0)
								{
									return Wd.substring(0,wl-2)+ChangeWovel2(c2,l2,ch)+Wd.substring(wl-1)+Lcd
								}
							else
								{
									if(CheckPriority(c2,c1))return c2+ChangeWovel2(c1,0,ch)
									else return ChangeWovel2(c2,l2,ch)+c1+Lcd
								}
						}
				}
		}
	else return Wd.substring(0,wl-1)+ChangeWovel2(c1,cl,ch)+Wd.substring(wl)+Lcd
}
function OnKeyPress(id)
{
	Str=id.value
}
function StrDiff(St)
{
	
	i=St.length	
		
	if(i>Str.length)
		{
			j=Str.length
			if(j>0)
				{
					for(i=0;i<=j;i++)
						if(St.substring(i,i+1)!=Str.substring(i,i+1)) return i+1
				}
			else return 1
		}
	return -1
}
function Mviet(id)
{
	if (Mode!=1) return
	fn=id.value
	if(fn!=Str)
	{
		di=StrDiff(fn)
		ch=fn.substring(di-1,di)
		if((di>1)&('sfrxjaeowdSFRXAEOWD'.indexOf(ch)!=-1))
			{
				cl=fn.substring(di-2,di-1)
				if('aeowdAEOWD'.indexOf(ch)!=-1)
					{
						id.value=fn.substring(0,di-2)+ChangeVowel1(cl,ch)+fn.substring(di)
					}
				else
					{
						if(di>6) id.value=fn.substring(0,di-7)+ChangeWord(fn.substring(di-7,di-1),ch)+fn.substring(di)
						else	id.value=ChangeWord(fn.substring(0,di-1),ch)+fn.substring(di)
					}
			}
		else if((di==1)&&(ch=='w'||ch=='W'))
				{
					if(ch=='w') id.value=String.fromCharCode(432)+fn.substring(1)
					else id.value=String.fromCharCode(431)+fn.substring(1)
				}
	}
	Str=id.value
}
//--------------------

function CheckDate(datefield,msg)
 {
	mdateval = datefield.value;
	if (mdateval != "")
	 {
		mday = mdateval.substring(0, mdateval.indexOf("/"));
		mmonth = mdateval.substring(mdateval.indexOf("/") + 1, mdateval.lastIndexOf("/"))
		myear = mdateval.substring(mdateval.lastIndexOf("/") + 1, mdateval.length);
		mdate = new Date (mmonth + "/" + mday + "/" + myear);
		cday = mdate.getDate();
		cmonth = mdate.getMonth() + 1;
		cyear = mdate.getYear();
		if (parseFloat(mday) != parseFloat(cday) || parseFloat(mmonth) != parseFloat(cmonth))
		  {			
			alert(msg);
			datefield.focus();
			//datefield.value = "";
			
			return;
		  }
		 if (myear.length!=4)
		  { 
		    alert('Bạn hãy nhập năm có dạng: yyyy');
		    datefield.focus();
		    //datefield.value = "";
   			return;
		  }
		 }	
  }
//--------
 	
 function CheckDateFT(datefrom,dateto)
  {
    var kt=true;
    mdatefrom = datefrom.value;
    mdateto = dateto.value;
  	if ((mdatefrom != "") && (mdateto != ""))
	 {
		mday = mdatefrom.substring(0, mdatefrom.indexOf("/"));
		mmonth = mdatefrom.substring(mdatefrom.indexOf("/") + 1, mdatefrom.lastIndexOf("/"))
		myear = mdatefrom.substring(mdatefrom.lastIndexOf("/") + 1, mdatefrom.length);
		mdate = new Date (mmonth + "/" + mday + "/" + myear);
		var Temp_datefrom = new Date(mdate);
		//----
		mday = mdateto.substring(0, mdateto.indexOf("/"));
		mmonth = mdateto.substring(mdateto.indexOf("/") + 1, mdateto.lastIndexOf("/"))
		myear = mdateto.substring(mdateto.lastIndexOf("/") + 1, mdateto.length);
		mdate = new Date (mmonth + "/" + mday + "/" + myear);		
    	var Temp_dateto = new Date(mdate);
    	var lngDtefrom = Temp_datefrom.getTime();
		var lngDteto = Temp_dateto.getTime();
 		if(lngDtefrom > lngDteto) 
    		{ alert('Không được phép  giá trị trường ĐẾN NGÀY > giá trị trường TỪ NGÀY');
		      dateto.focus();		      
		      kt=false;
       		  return kt;
		    }		    
	}	
	if ((mdatefrom == "") && (mdateto != ""))
	 {
	   alert('Bạn phải nhập vào trường TỪ NGÀY');
	   datefrom.focus();
 	   kt=false;
	   return kt;
	 }
	 if ((mdatefrom != "") && (mdateto == ""))
	 {
	   alert('Bạn phải nhập vào trường ĐẾN NGÀY');
	   dateto.focus();
	   kt=false;
       return kt;
	 }	 
	return kt;
}
//-----  	
function CheckData(wordfield,authorfield,datefromfield,datetofield,temp_form)
 { 
  if((wordfield.value=="")&&(authorfield.value=="")&&(datefromfield.value=="")&&(datetofield.value==""))
    {
     alert('Bạn chưa nhập thông tin cần tìm kiếm');
     wordfield.focus();
     return;     
    }
    var kq=CheckDateFT(datefromfield,datetofield);
    if(kq==true)
    {
      temp_form.submit();
    }
    return;
 }
 //---------
 	 
 function validate_number(ten)
     {
			if(isNaN(ten.value))
			{
				alert('Bạn phải nhập số');
				ten.value="";
				ten.focus();
			}
	}			 
	
////
 function verifyEmail(MainFrom,MainTo,form)
  {
 	checkMainFrom = MainFrom.value;
 	checkMainTo = MainTo.value;
 	if((checkMainFrom !="")||(checkMainTo !=""))
 	{
	  if ((checkMainFrom.indexOf('@') < 0) || ((checkMainFrom.charAt(checkMainFrom.length-4) != '.') && (checkMainFrom.charAt(checkMainFrom.length-3) != '.'))) 
	    {
			alert("Địa chỉ Email người gửi không hợp lệ .Xin hãy đánh lại");
			MainFrom.select();
			return; 
	    }
	  if ((checkMainTo.indexOf('@') < 0) || ((checkMainTo.charAt(checkMainTo.length-4) != '.') && (checkMainTo.charAt(checkMainTo.length-3) != '.'))) 
	    {
	   		alert("Địa chỉ Email người nhận không hợp lệ.Xin hãy đánh lại");
			MainTo.select();
			return; 
		}
	 }	
	 form.submit();
	 self.close();
  }	