﻿	 /*
	  * Kiem tra du lieu nhap vao co phai la ngay hop le khong
	  * Tham so truyen vao se la(year, month, day)
	  * Neu ham tra ve la True -> Ngay hop le,nguoc lai 
	  *                   False -> Ngay ko hop le
	  */
	function fncCheckValidDate(yyyy, mm, dd) {
		
		yyyy = parseInt(yyyy, 10);
		mm = parseInt(mm, 10);
		dd = parseInt(dd, 10);
		if ((yyyy < 1900) || (yyyy > 9999)) /*note-xanhbiec - kiem tra gioi han nam*/
		{
			return false;
		} 
		else if ((mm < 1) || (mm > 12)) /*note-xanhbiec - kiem tra gioi han thang*/
		{
			return false;
		} 
		else if ((dd < 1) || (dd > 31)) /*note-xanhbiec - kiem tra gioi han ngay*/
		{
			return false;
		} 
		else if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && (dd == 31)) /*xanhbiec - kiem tra lien he giua thang va ngay*/
		{
			return false;
		} 
		else if (mm == 2) /*note-xanhbiec - kiem tra nam nhuan*/
		{ 
			var isleap = (yyyy % 4 == 0 && (yyyy % 100 != 0 || yyyy % 400 == 0));					
			if (dd > 29 || (dd == 29 && !isleap)) 
			{
				return false;
			} 
			else 
			{
				return true;
			}
		} 
		else 
		{
			return true;
		}
	}
	/**********************************************************************************
	Description: chi dinh text cho phep nhap vao
	Parameter: e: Truyen su kiem phai sinh vao cho ham..o day la su kien keypress 
		validchars: la dang chuoi... 
		chi co cac tu trong chuoi nay moi duoc phep nhap vao textbox chi dinh
	**********************************************************************************/
	function fncKeyRestrict(e, validchars) 
	{
		var key='', keychar='';
		key = fncGetKeyCode(e);
		if (key == null) return true;
		keychar = String.fromCharCode(key);
		keychar = keychar.toLowerCase();
		validchars = validchars.toLowerCase();
		if (validchars.indexOf(keychar) != -1)
			return true;
		if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
			return true;
		return false;
	}
	/**********************************************************************************
	Description: nhan ve keycode
	Note: ham ho tro cho ham keyRestrict
	**********************************************************************************/
	function fncGetKeyCode(e)
	{
		if (window.event)
		   return window.event.keyCode;
		else if (e)
		   return e.which;
		else
		   return null;
	}
	/*[HUYNH THI HONG NGUYEN]*/
	/**********************************************************************************
	Description: Ham su dung show dialog voi cac tham so nhan vao
	Parameter:
	p_strUrl: duong dan url cua trang can hien thi kieu du lieu - string
	p_intTop, p_intLeft: chi dinh vi tri cua form
						 neu gia tri la rong --> se hien thi ngay giu man hinh kieu du lieu - integer
	p_intHeight,p_intWidth * do cao va rong cua form kieu du lieu - integer
	p_blnScrollBar: chi dinh co hien thi scrollbar hay khong kieu du lieu - boolean
	p_blnResizable: chi dinh co cho phep thay doi kich thuong form hay khong kieu du lieu - boolean
	**********************************************************************************/
	function fncShowDialog(p_strUrl, p_intTop, p_intLeft, p_intHeight, p_intWidth, p_blnScrollBar, p_blnResizable) 
	{
			var objArguments = window;		//Get arguments on dialog with window.dialogArguments
			var strScrollBar;
			var strResizable;
			var strFeatures = "";
			
			if (p_blnScrollBar) {
				strScrollBar = "yes";
			} else {
				strScrollBar = "no";
			}
				
			if (p_blnResizable) {
				strResizable = "yes";
			} else {
				strResizable = "no";
			}
				
			strFeatures += "dialogHeight: " + p_intHeight + "px; ";
			strFeatures += "dialogWidth: " + p_intWidth + "px; ";
			strFeatures += "dialogTop: " + p_intTop + "px; ";
			strFeatures += "dialogLeft: " + p_intLeft + "px; ";
			strFeatures += "scroll: " + strScrollBar + "; ";
			strFeatures += "resizable: " + strResizable + "; ";
			strFeatures += "status: Yes; ";
			strFeatures += "help: No;";
			
			return window.showModalDialog(p_strUrl, objArguments, strFeatures);
	}
		/**
		*
		*  UTF-8 data encode / decode
		*  http://www.webtoolkit.info/
		*
		**/
		var Utf8 = {
		// public method for url encoding
			encode : function (string) {
			string = string.replace(/\r\n/g,"\n");
			var utftext = "";
	
			for (var n = 0; n < string.length; n++) {
	
				var c = string.charCodeAt(n);
	
				if (c < 128) {
					utftext += String.fromCharCode(c);
				}
				else if((c > 127) && (c < 2048)) {
					utftext += String.fromCharCode((c >> 6) | 192);
					utftext += String.fromCharCode((c & 63) | 128);
				}
				else {
					utftext += String.fromCharCode((c >> 12) | 224);
					utftext += String.fromCharCode(((c >> 6) & 63) | 128);
					utftext += String.fromCharCode((c & 63) | 128);
				}
			}
			return utftext;
		},
	
		// public method for url decoding
		decode : function (utftext) {
			var string = "";
			var i = 0;
			var c = c1 = c2 = 0;
	
			while ( i < utftext.length ) {
	
				c = utftext.charCodeAt(i);
	
				if (c < 128) {
					string += String.fromCharCode(c);
					i++;
				}
				else if((c > 191) && (c < 224)) {
					c2 = utftext.charCodeAt(i+1);
					string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
					i += 2;
				}
				else {
					c2 = utftext.charCodeAt(i+1);
					c3 = utftext.charCodeAt(i+2);
					string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
					i += 3;
				}
			}
			return string;
		}	
	}
	/* Ham cat khoang trang, tham so la mot chuoi ky tu*/
	function Trim(str) {
		return str.replace(/^\s*|\s*$/, "");
	}
	
	/*--------------------------------------------------------------
	 [Summary]
		Name        isEmail
		Description Ham kiem tra du lieu nhap la email
	 [Value]
		Param		strEmail     string
		Param		control      object
		Param		StrMessage   string
		Return		true
		Return		false 
	 [Note]
		Gan tren su kien OnKeyPress
	------------------------------------------------------------------------*/
	function isEmail(strEmail)
	{	
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom=validChars + '+'
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=strEmail.match(emailPat)
		if (matchArray==null) 	{
			
			return false
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		if (user.match(userPat)==null) 
		{
			return false
		}
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) 
		{
			for (var i=1;i<=4;i++) 
			{
				if (IPArray[i]>255) 
				{
					return false;
				}
			}
			return true;
		}
		// Domain is symbolic name
		var domainArray=domain.match(domainPat);
		if (domainArray==null) 
		{		
			return false;
		}
		var atomPat=new RegExp(atom,"g");
		var domArr=domain.match(atomPat);
		var len=domArr.length;
		if (domArr[domArr.length-1].length<2 )//|| domArr[domArr.length-1].length>4) 
		{		
			return false;
		}
	
		// Make sure there's a host name preceding the domain.
		if (len<2) 
		{
			return false;
		}
	
		return true;
	}
	//Nguyeng Thanh Tri
	function IsNumber(Expression)
	{
		Expression = Expression.toLowerCase();
		RefString = "0123456789";
		
		if (Expression.length < 1)	return false;
		
		for (var i = 0; i < Expression.length; i++)
		{
			var ch = Expression.substr(i, 1);
			var a = RefString.indexOf(ch, 0);
			if (a == -1)		return false;
		}
		return true;
	}
	
	// Nguyen thanh Tri
	
	function Set_Cookie( name, value, expires, path, domain, secure ) 
	{
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		
		/*
		if the expires variable is set, make the correct 
		expires time, the current script below will set 
		it for x number of days, to make it for hours, 
		delete * 24, for minutes, delete * 60 * 24
		*/
		if ( expires )
		{
		expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		
		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
	}
	
	// this function gets the cookie, if it exists
	function Get_Cookie( name ) {
			
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) &&
		( name != document.cookie.substring( 0, name.length ) ) )
		{
		return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}
			
	// this deletes the cookie when called
	function Delete_Cookie( name, path, domain ) {
		if ( Get_Cookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
	/*TRAN PHI LONG 2007-09-20*/
	function btn_Logout_click(frmName)
	{	
		if(!confirm(str_mes_logout))
		{
			return false;
		}

		document.frm.EVENT.value = "LOGOUT";
		document.frm.action         = frmName;
		document.frm.submit();
	}

