/*
Author		: PhÃ­ Trá»ng Tuáº¥n
Version		
Description	: 
	Khi ngÆ°Æ¡Ã¬ sá»­ dá»¥ng Ä‘ang focus vÃ o má»™t textbox, text area, nháº¥n phÃ­m Enter,
	hÃ m sáº½ lÃ m sinh sá»± kiá»‡n onclick() cá»§a má»™t button mÃ  khÃ´ng cáº§n pháº£i click tháº³ng 
	vÃ o button Ä‘Ã³.
	Äáº·t hÃ m nÃ y vÃ o sá»± kiá»‡n onkeydown cá»§a textBox,textarea
Input:
	e			: window event
	btnSelectId	: chuá»—i, id cá»§a button sáº½ Ä‘Æ°á»£c lÃ m phÃ¡t sinh sá»± kiá»‡n onclick 
Output
	None
Example
	<input name="txtUserName"  onkeydown="fireButtonClick(event,'btnLogin')" />
	<input language="javascript" onclick="__doPostBack('btnLogin','')" id="btnLogin" type="button"/>
	NhÆ° tháº¿ khi áº¥n Enter táº¡i textbox "txtUserName" sáº½ lÃ m onclick button "btnLogin"
*/
function fireButtonClick(
	e,			
	btnSelectId
	){
	/*Trong IE thÃ¬ dÃ¹ng window.event, cÃ²n trong FireFox thÃ¬ 
	cáº§n truyá»n biáº¿n event vÃ o*/
	if(!e){
		e=window.event;
	}
	/*event.keyCode trong IE tÆ°Æ¡ng Ä‘Æ°Æ¡ng event.which trong FireFox*/
	
	if(e.which || e.keyCode)
	{
		if ((e.which == 13) || (e.keyCode == 13)) 	{
			var button = document.getElementById(btnSelectId);
			if(button && button.onclick){
				button.onclick();
				/*Begin: Khi áº¥n Enter do Ä‘Ã£ Ä‘á»ƒ fire btnSelectId.onClick();
				nÃªn cáº§n cháº·n sá»± kiá»‡n form submit ngáº§m Ä‘á»‹nh ( vá»‘n luÃ´n xáº£y ra khi áº¥n Enter
				khi Ä‘ang focus vÃ o má»™t text box
				*/
				if(e.preventDefault){
					e.preventDefault(isKeyPressEnter);//FireFox
				}
				else{ 
					e.returnValue =false;
				}
				/*End: Khi áº¥n Enter do Ä‘Ã£ Ä‘á»ƒ fire btnSelectId.onClick();
				nÃªn cáº§n cháº·n sá»± kiá»‡n form submit ngáº§m Ä‘á»‹nh ( vá»‘n luÃ´n xáº£y ra khi áº¥n Enter
				khi Ä‘ang focus vÃ o má»™t text box
				*/
			}
		
		}
	}

}
/*----------------------------------------------------------------------------------------------------*/
/*TuanPT: Dung ham nay de fix loi su dung nextSibling cho FireFox*/
function getNextSibling(startBrother){
  endBrother=startBrother.nextSibling;
  while(endBrother.nodeType!=1){
    endBrother = endBrother.nextSibling;
  }
  return endBrother;
}
/*----------------------------------------------------------------------------------------------------*/
/*TuanPT: Dung ham nay de fix loi su dung previousSibling cho FireFox*/
function getPreviousSibling(startBrother){
  endBrother=startBrother.previousSibling;
  while(endBrother&&endBrother.nodeType!=1){
    endBrother = endBrother.previousSibling;
  }

  return endBrother;
}
/*----------------------------------------------------------------------------------------------------*/
function getFirstChild(elm){
	
	if ( !elm.childNodes.length ){
		return null;
	}
	var iLen = elm.childNodes.length;
	for ( var i = 0; i < iLen; i++ ){
		if ( elm.childNodes[i].nodeType == 1 ){
			return elm.childNodes[i];
		}
	}
	return null;
}
/*----------------------------------------------------------------------------------------------------*/
function getQueryString() 
{
	var qsParm = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) 
	{
		var pos = parms[i].indexOf('=');
		if (pos > 0){
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	
	}
	return qsParm; 
}
/*----------------------------------------------------------------------------------------------------*/
function resizePage(){
	//alert(screen.availHeight);
//	alert(screen.availWidth);
	var trHeader = document.getElementById("trHeader");
	var trPadding= document.getElementById("trPadding");
	var trFooter= document.getElementById("trFooter");
	
	//if(trHeader==null||trPadding==null||trFooter==null)return;
	var nHeight;
	
	if(screen.availHeight>730){
		nHeight = Math.round(0.1*(screen.availHeight-736)+30);
		if(trHeader!=null){
			trHeader.style.height=  nHeight;	
		}
		nHeight = Math.round(0.25*(screen.availHeight-736)+1);
		if(trPadding!=null){
			trPadding.style.height=  nHeight;
		}
		nHeight = Math.round(0.1*(screen.availHeight-736)+58);
		if(trFooter!=null){
			trFooter.style.height=  nHeight;
		}
					
	}
	else{
		if(trHeader!=null){
			trHeader.style.height	=  30;
		}
		if(trPadding!=null){
			trPadding.style.height	=  1;
		}
		if(trFooter!=null){
			trFooter.style.height	=  58;
		}
	}	
	
	
}
/*----------------------------------------------------------------------------------------------------*/
function getURLParam(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
	return "no";
	else
	return results[1];
}
/*----------------------------------------------------------------------------------------------------*/
function connectYahoo(){
	
	var menuMain=document.getElementById("menuMain");
	var parent=menuMain.parentNode;
	var yahooMessenger=document.getElementById("yahooMessenger");

	yahooMessenger.style.height = parent.clientHeight-menuMain.clientHeight-1;
	var img=yahooMessenger.getElementsByTagName("IMG")[0];
	img.src="http://opi.yahoo.com/online?u=archiworldjsc&amp;m=g&amp;t=1";
}
/*----------------------------------------------------------------------------------------------------*/
function isSearchValidate(){
	//var form =document.forms["frmSearch"];
	//var inputSearch=form.getElementsByTagName("INPUT")[0];
	var inputSearch=document.getElementsByName('search')[0];
	if(inputSearch.value.length<5){
		alert("Chuỗi tìm kiếm gồm ít nhất 5 ký tự");
		inputSearch.focus();
		return false;
	}
	return true;
}
/*----------------------------------------------------------------------------------------------------*/
function trim(str) {
    var re = new RegExp("^\\s*|\\s*$","g");
    return str.replace(re, "");
}
/*-----------------------------------------------------------------------------------------------------------------------------*/
function trimLeft(str) {
    var re = new RegExp("^\\s*","");
    return str.replace(re, "");
}
/*-----------------------------------------------------------------------------------------------------------------------------*/
function trimRight(str) {
    var re = new RegExp("\\s*$","");
    return str.replace(re, "");
}
/*-----------------------------------------------------------------------------------------------------------------------------*/
//function CheckDateFormat check is the right format and validation of date field data
//input: a date string (e.g: 1/21/2003 month/day / year)
//output: variant: TRUE if valid and FALSE if not
//
//Added by Phi Trong Tuan 2008-01-24. Them tham so isDisplayTime
var hasError=0;
function CheckDateFormat(objDate, isDisplayTime ) 
{
	var strDate=objDate.value;
	if (strDate!="")
	{
		strDate = CompleteDateTime(strDate,isDisplayTime);
		var strValidDate=""
		if(isDisplayTime==null)
				isDisplayTime =false;
	
		var match;
		if(isDisplayTime){
			dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*(\d{1,2}):(\d{1,2})[ ]*$/;
	  	}
	  	else{
			dateregex=/^[ ]*[0]?(\d{1,2})\/[0]?(\d{1,2})\/(\d{4,})[ ]*$/;
	  	}
	  	match=strDate.match(dateregex);
		if (match)
		{ 
			var tmpdate;
	  		if(isDisplayTime)
			{
	  			tmpdate=new Date(match[3],parseInt(match[2])-1,match[1],match[4],match[5]);
			}
	  		else{
				tmpdate=new Date(match[3],parseInt(match[2])-1,match[1]);
	  		}
	  		
	    		if (	
				tmpdate.getDate()==parseInt(match[1]) && 
				tmpdate.getFullYear()==parseInt(match[3]) && 
				(tmpdate.getMonth()+1)==parseInt(match[2])&&
				(tmpdate.getHours()==parseInt(match[4])&&	tmpdate.getMinutes()==parseInt(match[5])||(isDisplayTime==false) )
			)
	    		{ 
				if(isDisplayTime){
					strValidDate= match[1] +"/" + match[2]+"/"+match[3] + " "+match[4]+ ":"+match[5];
				}
				else{
					strValidDate= match[1] +"/" + match[2]+"/"+match[3];
				}
	   		} 
	   	}
				
		if(strValidDate=="")
		{
			hasError=1
			strError="";
			if(isDisplayTime)
				strError= "Dữ liệu ngày tháng không hợp lệ. Bạn phải nhập ngày tháng theo định dạng Ngày/Tháng hoặc Ngày/Tháng/Năm hoặc Ngày/Tháng/Năm Giờ:Phút";  
			else
				strError= "Dữ liệu ngày tháng không hợp lệ. Bạn phải nhập ngày tháng theo định dạng Ngày/Tháng hoặc Ngày/Tháng/Năm";
			alert(strError);
			
			setTimeout(function(){objDate.focus();},1); 
			objDate.focus()
			return false;
		}
		else
		{
			hasError=0;
			objDate.value=strDate;		
			return true;
		}	
	}
}
/*-----------------------------------------------------------------------------------------------------------------------------*/
function CompleteDateTime(sDateInput,isDisplayTime	) 
{
	if(isDisplayTime==null)
	{
		isDisplayTime = false;
	}
	//Begin Xu ly cac ky tu space
	var re = new RegExp("^\\s*|\\s*$","g");
    	//xoa cac space o dau va o cuoi
    	sDateInput= sDateInput.replace(re, "");
    	//xoa cac space o xuat hien hon 2 lan  = 1 dau space xuat hien 1 lan
    	re = new RegExp("\\s{2,}","g");
    	sDateInput= sDateInput.replace(re, " ");
    	//End Xu ly cac ky tu space
    	
	if(sDateInput=="")
		return ""; //Khong co du lieu bo qua check;

    	var dateReg=/^(\d{1,2})\/(\d{1,2})*$/; 				//DD/MM
    	var dateReg1=/^(\d{1,2})\/(\d{1,2})\/(\d{4})*$/;	//DD/MM/YYYY
    	match=sDateInput.match(dateReg);
    	match1=sDateInput.match(dateReg1);
    	var sDay,sMonth,sYear,sResult;
    	var curDate = new Date();
    	sResult ="";
  	if (match){ 
  		if(match[1]<10){
  			sDay			="0"+new Number(match[1]).toString(10);
		 } 
		 else{
		 	sDay =+match[1];
		 }
  		if(match[2]<10)
		{
  			sMonth		="0"+new Number(match[2]).toString(10);
		 }else{
		 	sMonth = match[2];
		} 

		sDateInput = sDay+"/"+sMonth+"/"+curDate.getFullYear();
		if(isDisplayTime){
			sDateInput = sDateInput+" "+"00:00";
		}
		
	}else if(match1){
		 if(match1[1]<10){
  			sDay			="0"+new Number(match1[1]).toString(10);
		 } 
		 else{
		 	sDay =+match1[1];
		 }
  		if(match1[2]<10){
  			sMonth		="0"+new Number(match1[2]).toString(10);
		 }else{
		 	sMonth = match1[2];
		} 
		sYear= match1[3];
		sDateInput = sDay+"/"+sMonth+"/"+sYear;
		if(isDisplayTime){
			sDateInput = sDateInput+" "+"00:00";
		}
	}
	return sDateInput;
}
/*-----------------------------------------------------------------------------------------------------------------------------*/
/*
iConvertType=0
	Convert yyyyMMdd =>dd/MM/yyyy
*/
function convertFormatDate(sDate,iConvertType){
	if(iConvertType==0){
		re=/^(\d{4})(\d{2})(\d{2})*$/;
		var match=sDate.match(re);
		return match[3]+"/"+match[2]+"/"+match[1];	
	}
	
}
/*-----------------------------------------------------------------------------------------------------------------------------*/
