﻿function trim(psValue)
{
	if(!psValue){ 
		psValue = "";
		return psValue;
	}
	return String(psValue).replace(/^\s*|\s*$/g,"");
}
function IsNumeric(psValue)
{
	var ValidChars = "0123456789.,-";
	var IsNumber = true;
	var lcChar;
	psValue = trim(psValue);//doi sang kiểu string
	if (psValue == "")
		IsNumber = false;
	else
		for (i = 0; i < psValue.length && IsNumber == true; i++) 
		{ 
			lcChar = psValue.charAt(i); 
			if (ValidChars.indexOf(lcChar) == -1) 
				IsNumber = false;
		}// EndOf For
	return IsNumber;
}// EndOf Function
function EncodeUrI(psData){
    try{
        return encodeURIComponent(psData);
    }catch(ex){
        return psData;
    }
}
function DecodeURI(psData){
    try{
        return decodeURIComponent(psData);
    }catch(ex){
        return psData;
    }
}
function DecodeHtm(psData){
    try{
        return trim(psData.replace('&nbsp;',' '));
    }catch(ex){
        return psData;
    }
}
function CheckAll(pobjForm,psName,pbCheck)
{//check all checkbox
//psName : tên checkbox trên grid cần đánh check
	var i, liCount,lsVal;
	psName = psName.toLowerCase();
	liCount = pobjForm.elements.length - 1;
	for (i = 0 ; i <= liCount; i++)
	{
		if (pobjForm.elements[i].type == "checkbox")
		{	
			lsVal = pobjForm.elements[i].name.toLowerCase();
			if (lsVal.indexOf(psName) >= 0)
				pobjForm.elements[i].checked = pbCheck;
		}//EndOf if
	}//EndOf for
}//EndOf function
function IsCheck(psName)
{//Test list radio button has select
//psName : tên radio button list cần test
	var i, liCount,lsVal, loList, lbCheck = false;
	try{
		loList = document.getElementsByName(psName);
		liCount = (loList ? loList.length : 0);
		for (i = 0 ; i < liCount; i++){
			if (loList[i].checked){	
				lbCheck = true;
				break;
			}
		}
	}catch(ex){}
	return lbCheck;
}//EndOf function
function HasCheckMe(pobjForm, psID)
{//test has check checkbox
//psID : id of checkbox in grid
	var i, liCount, lsVal, loList, lbCheck = false;
	psID = psID.toLowerCase();
	loList = pobjForm.elements;
	liCount = loList.length;
	for (i = 0 ; i < liCount; i++){
		if (loList[i].type == "checkbox"){	
			lsVal = loList[i].id.toLowerCase();
			if (lsVal.indexOf(psID) >= 0){
				if (loList[i].checked){	
					lbCheck = true;
					break;
				}
			}
		}//EndOf if
	}//EndOf for
	return lbCheck;
}//EndOf function
function IsEmail(psEmail){
	var lbRes = false;
	try{
		var lsTest, lrRE;
		lsTest = "^[_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{2,8})$";
		lrRE = new RegExp(lsTest);
		lbRes = lrRE.exec(psEmail);
	}catch(ex){}
	return lbRes;
}
function getCookie(c_name){
	try{
		if(document.cookie.length > 0){ 
			c_start = document.cookie.indexOf(c_name + "=");
			if(c_start != -1){ 
				c_start = c_start + c_name.length + 1;
				c_end = document.cookie.indexOf(";",c_start);
				if(c_end == -1) 
					c_end = document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			} 
		}
	}catch(ex){}
	return "";
}
function setCookie(c_name,value,expiredays)
{//expiredays : số ngày expire
	try{
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + expiredays);
		document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
	}catch(ex){}
}
function clearCookie(c_name){
	var exdate = new Date();
	exdate.setDate(exdate.getDate() - 1);
	document.cookie = c_name + '=;expires=' + exdate.toGMTString();
}
function LabelForCheckbox(psDiv) {
	try{
		var lDiv, larrChk, larrLbl, lintLen;
		lDiv = document.getElementById(psDiv);
		larrChk = lDiv.getElementsByTagName("input")
		larrLbl = lDiv.getElementsByTagName("label")
		lintLen = larrChk.length;
		for(i = 0; i < lintLen; i++){
			larrLbl[i].htmlFor = larrChk[i].id;
		}
	}catch(ex){}
}
function ShowListView(pstrKey,pstrIDCboRelate,psIDBox,psOwner){
	var ldivProv,lOwner;
	try{
		lOwner = document.getElementById(psOwner);//div containt list checkbox
		ListView(pstrKey,pstrIDCboRelate,lOwner);
		ldivProv = document.getElementById(psIDBox);//div containt div lOwner and label
		if(lOwner.innerHTML == '')
			ldivProv.className = 'hide';
		else
			ldivProv.className = '';
	}catch(ex){}
}
//////////////////////////////////////////////////////////
function showFrameDetail(displayBelowThisObject, psIDDetail, pReduce){
    try{
        //alert(displayBelowThisObject.style.checked);
	    var x = displayBelowThisObject.offsetLeft;
	    var y = displayBelowThisObject.offsetTop;//+ displayBelowThisObject.offsetHeight ;
	    // deal with elements inside tables and such
	    var parent = displayBelowThisObject;
	    while (parent.offsetParent) {
		    parent = parent.offsetParent;
		    x += parent.offsetLeft;
		    y += parent.offsetTop ;
	    }
	    // move the datepicker div to the proper x,y coordinate and toggle the visiblity
	    var DetailDiv = document.getElementById(psIDDetail);
	    DetailDiv.style.position = "absolute";
	    //DetailDiv.style.left = x + "px";
	    if(pReduce > 0)
	        y = y - pReduce;
	    DetailDiv.style.top = y + "px";
	    DetailDiv.className = '';
	    DetailDiv.style.zIndex = 10000;
	    document.getElementById('dvExpand').className = '';
	}catch(ex){}
}
function HideIt(psID){
    try{
        document.getElementById(psID).className = 'hide';
	}catch(ex){}
}
function HideListID(pListID, psPrefix) {
    try {
        var i, liCount, lArrID;
        lArrID = pListID.split(',');
        liCount = lArrID.length;
        if (!psPrefix)
            psPrefix = '';
        for (i = 0; i < liCount; i++) {
            HideIt(psPrefix + lArrID[i]);
        }
    } catch (ex) { }
}
//hiển thị / che control
function ShowMe(psID, pVisible, pClass){
	try{
		if(!pClass) pClass = '';
		document.getElementById(psID).className = (pVisible == true ? pClass : 'hide');
	}catch(ex){}
}
/// <summary>
/// focus control
/// </summary>
function Focus(psID){
	try{
		var lObj = document.getElementById(psID);
		lObj.focus(); lObj.select();
	}catch(ex){}
}
/// <summary>
/// Alert Message
/// </summary>
function AlertClient(psIDMsg, psMsg){
	try{
		document.getElementById(psIDMsg).innerHTML = psMsg;
		alert(psMsg);
	}catch(ex){}
}
/// <summary>
/// Alert & focus control
/// </summary>
function Alert_Focus(psID, psIDMsg, psMsg){
	try{
		AlertClient(psIDMsg, psMsg);
		Focus(psID);
	}catch(ex){}
}
/// <summary>
/// không cho PostBack về server
/// </summary>
function NotReturn(){
	try{
		window.event.returnValue = false;
	}catch(ex){}
}
/// <summary>
/// gán selectedindex cho combobox
/// </summary>
function GetListIndex(psVal, pCbo){
    var liLen, i;
    liLen = pCbo.length;
    pCbo.selectedIndex = -1;
    for(i = 0; i < liLen; i++){
		if (psVal == pCbo.options[i].value){
			pCbo.selectedIndex = i;
			break;
		}
	}//end for
}
/// <summary>
/// Xoá giá trị của control
/// </summary>
function ClearIt(psID){
	try{var lObj;
		lObj = document.getElementById(psID);
		switch(lObj.type){
		case 'select-one':
			lObj.options.length = 0;
			break;
		case 'checkbox':
		case 'radio':
			lObj.checked = false;
			break;
		default://hidden,text
			lObj.value = '';
			break;
		}
	}catch(ex){}
}
function ClearItExt(psID){
	try{var lObj;
		lObj = document.getElementById(psID);
		switch(lObj.type){
		case 'select-one':
			lObj.selectedIndex = -1;
			break;
		case 'checkbox':
		case 'radio':
			lObj.checked = false;
			break;
		default://hidden,text
			lObj.value = '';
			break;
		}
	}catch(ex){}
}
function ClearDiv(psID){
	try{
		document.getElementById(psID).innerHTML = '';
	}catch(ex){}
}
function ClearListID(pListID, psPrefix) {
    try {
        var i, liCount, lArrID;
        lArrID = pListID.split(',');
        liCount = lArrID.length;
        if (!psPrefix)
            psPrefix = '';
        for (i = 0; i < liCount; i++) {
            ClearIt(psPrefix + lArrID[i]);
        }
    } catch (ex) { }
}
/// <summary>
/// Gán giá trị của control
/// </summary>
function SetIt(psID, pVal){
    var lObj;
	try{
		lObj = document.getElementById(psID);
		if(lObj.type){
		    switch(lObj.type){
		    case 'text': case 'hidden':
			    lObj.value = pVal;
			    break;
			case 'checkbox': case 'radio':
			    lObj.checked = pVal;
			    break;
		    }
		}else //div, label, span
			lObj.innerHTML = pVal;
	}catch(ex){
	    alert('{SetIt} ID = ' + psID + ', ' + ex.message);
	}
}
function DisableIt(psID, pDisable){
	try{
		document.getElementById(psID).disabled = (pDisable ? true : false);
	}catch(ex){}
}
function CallPrint(psID){
	try{
		var prtContent = document.getElementById(psID);
		var WinPrint = window.open('','','letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
		WinPrint.document.write("<link href='../CSS/DuToan.css' rel='stylesheet' type='text/css' />");
		WinPrint.document.write(prtContent.innerHTML);
		WinPrint.document.close();
		WinPrint.focus();
		WinPrint.print();
		//if(!pKeep)
		WinPrint.close();
	}catch(ex){}
}
/// <summary>
/// Xoá edit row style của grid
/// </summary>
function RestoreStyle(psIDGrid){
	try{
		var i, lCount, lTab, lRow, lsCss;
		lTab = document.getElementById(psIDGrid);
		if(!lTab) return;
		lCount = lTab.rows.length;
		for(i = 1; i < lCount; i++){
			lsCss = lTab.rows[i].className;
			if(lsCss == "editem"){
				if(i % 2 == 0)
					lTab.rows[i].className = 'alitem';
				else
					lTab.rows[i].className = 'item';
				break;
			}
		}
	}catch(ex){}
}
function ConfirmDel(){
	if(confirm('Bạn thật sự muốn xóa dòng dữ liệu này ?') == false){
		try{
			NotReturn();
		}catch(ex){}
		return false;
	}
}
