﻿// Ham lay chuoi , cat bo khoang trong phia trai
function LTrim(str){
	var i=0;
	var s = new String(str);
	while((i<s.length) && (s.charAt(i)==' ' || s.charAt(i)=='\r' || s.charAt(i)=='\t' || s.charAt(i)=='\n')) {i++;}
	return s.substr(i);
}
// Ham lay chuoi , cat bo khoang trong phia phai
function RTrim(str){
	var s = new String(str);
	var i = s.length-1;
	while((i>=0) && (s.charAt(i)==' ' || s.charAt(i)=='\r' || s.charAt(i)=='\t' || s.charAt(i)=='\n')) {i--;}
	return s.substr(0,i+1);
}
// Ham lay chuoi , cat bo khoang trong ca hai phia trai va phai
function Trim(str){
	return RTrim(LTrim(str));
}
// Ham Kiem Tra so Nguyen 1
function isDigit(c) {
	if((c=='0')||(c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||
		(c=='7')||(c=='8')||(c=='9')) 
		return true;
	else
		return false;
}
// Ham kiem tra so nguyen 2
function isPosInt(s) {
 	var n;
 	n = s.length
	 if(n==0) return false;
 	for(i=0;i<n;i++)
		if(!isDigit(s.charAt(i))) return false;
 	return true;
}
// Ham OutPut ra man hinh : Ex : Thu Ba, dd - mm - yyyy
function showDate() {
	var strdate;
	var dt = new Date();
	var strarrDay = new Array("Ch&#7911; nh&#7853;t", "Th&#7913; hai", "Th&#7913; ba", "Th&#7913; t&#432;", "Th&#7913; n&#259;m", "Th&#7913; s&aacute;u", "Th&#7913; b&#7843;y"); 
	var strarrMonth = new Array("01","02","03","04","05","06","07","08","09","10","11","12"); 
	
	var strThu = dt.getDay();
	strThu = strarrDay[strThu];
	var strDay=dt.getDate();
	if (strDay<10) strDay="0" + strDay
	var strMonth= dt.getMonth();
	strMonth= strarrMonth[strMonth] ;
	var strYears = dt.getYear();
	if (strYears<1900) strYears += 1900;
	strdate=strThu + ", ng&agrave;y " + strDay + " th&aacute;ng " + strMonth + " n&#259;m " + strYears
	window.document.write (strdate);
}
// Ham kiem tra co phai la ngay thang hop le hay khong ?
function isDate(dateStr) {
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // Kiem tra ky tu chuoi tren co ton tai hay khong
	if (dateStr == "") {
		return true; // Gia tri la Null
	}
	if (matchArray == null) {
		return false;
	}
	// Xu ly NGay
	day = matchArray[1];
	month = matchArray[3];
	year = matchArray[5];
	if (month < 1 || month > 12) { // Kiem tra tinh hop le cua Nam
		return false;
	}
	if (day < 1 || day > 31) {
		return false;
	}
	if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
		return false;
	}
	if (month == 2) { // Kiem tra thang 2 co 29 Ngay
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day == 29 && !isleap)) {
			return false;
		}
	}
	return true; // date la hop le
}
// Ham kiem tra E-mail co hop le hay khong ?
function CheckEmail(email) {
	if (email=='') {return false}
	if (email.indexOf('@')==-1) {return false}
	if (email.indexOf('.')==-1) {return false}
	var mark="<>?|:,[]~!#$%^&*()`'\\ /\"";
	for (i=0;i<mark.length;i++) {
		if (email.indexOf(mark.charAt(i))!=-1) {return false}
	}
	return true;
}
// Ham kiem tra So Phone co hop le hay khong ?
function IsPhoneNumber(v) {
	var s=new String(v);
	var reg=/[^0-9\s\(\)-]/;
	if(s.match(reg) || s.length<7 || s.length>12)
		return false;
	return true;
}
// Mo cua so moi khong co scrollbars
function page_window_open(url, w, h) {
	window.open(url, "_blank", "toolbar=auto,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,copyhistory=no,left=45,top=10, width="+w+",height="+h);
}
// Mo cua so moi co scrollbars
function page_open(url, w, h) {
	window.open(url, "_blank", "toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,copyhistory=no,left=225,top=25,width="+w+",height="+h);
}
function page_Print(url, w, h) {
	window.open(url, "_blank", "toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=no,copyhistory=no,left=225,top=25,width="+w+",height="+h);
}
// Mo cua so (trang) dang Dialog window
function page_dialog(url,w,h){
   window.showModalDialog(url,'','help:no;status:no;dialogHeight:'+h+'px; dialogWidth:'+w+'px');
}
function openPageView(url,w,h,l,t){
	window.open(url, "_blank","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=" + w + ',height=' + h + ',left=' + l + ',top=' + t + ',screenX=' + l + 'px,screenY=' + t + "px");
	return false;
}
function openPageViews(url,w,h,l,t){
	window.open(url, "_blank","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=" + w + ',height=' + h + ',left=' + l + ',top=' + t + ',screenX=' + l + 'px,screenY=' + t + "px");
	return false;
}
// Ham set mau chu, mau nen cho mot truong nao do
function setColor(objName, fg, bg) {
  if (objName.style) {
    objName.style.color = fg;
    objName.style.backgroundColor = bg;
  }
}
// Ham kiem tra mot control nao do co rong hoac bang khong hay khong ?
function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) || (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}
// Ham nay duoc su dung trong cac site xem hinh anh chi tiet
function viewimage(path,file) {
	var wnd = window.open('../viewimage.php?p='+path+'&f='+file,'viewimage','width=200,height=200,status=0,resizable=1,top='+(screen.availHeight/2-200)+',left='+(screen.availWidth/2-300));
	wnd.focus();
	return false;
}
/////////////////////////////////////////////////////
// Tro den mot hoac mot nhom doi tuong
function getElement(n) {
	if(document.all) {
		return document.all[n];
	} else if(document.getElementById){
		return document.getElementById(n);
	}
	return null;
}
// khoi phuc lai mau nen ban dau (tuc la mau ma truoc khi thay doi mau)
function restore_bgColor(o,c){
	var obj = getElement(o);
	obj.style.backgroundColor = c;
}
// Ham nay thong bao ra mot err goi den cho nguoi dung
// msg = "cau thong bao"
// o = Doi tuong gay ra err.
function display_error(msg,o){
	alert(msg);
	if(o==null) return true;
	o.focus();
	var obg = o.style.backgroundColor;
	o.style.backgroundColor='#FFAAAA';
	setTimeout("restore_bgColor('"+o.name+"','"+obg+"')",3000);
}
// Ham chon hoac bo chon tat ca cac check box trong form
// Goi Ham : onClick="checkAll(document.tenform.tenchkbox,this)"
function checkAll(checkname, exby) {
	for (i = 0; i < checkname.length; i++)
  	checkname[i].checked = exby.checked? true:false
}
// Ham Dinh Dang lai mot so
// vi du : Nhap : $1,234,56 ----> 123456
function filterNum(str) {
	re = /^\$|,/g;
	// remove "$" and ","
	return str.replace(re, "");
}
// Xu Ly Hinh Anh
function limitSizeImg(obj,w,h){ // Kiem Tra Be Rong, Cao
	var img;
	var v = obj.value;
	if(v!=''){
		img = new Image;
		img.src = v;
		berong = img.width;
		becao = img.height;
		if(berong > w){alert('Image can phai <= ' + w + ' !'); return false;}
		if(becao > h){alert('Image can phai <= ' + h + ' !'); return false;}
	}
	return true;
}
function ZoomOut(id){ // Phong Nho
	obj = document.getElementById(id);
	if(obj.width > 50){obj.width = obj.width / 2;}
}
function ZoomIn(id){ // Phong Lon
	obj = document.getElementById(id);
	if(obj.width < 400){obj.width = obj.width * 2;}
}
function GrayImage(id){ // Hinh Mau Trang Den
	obj = document.getElementById(id);
	obj.style.filter = "gray";
}
function ColorImage(id){ // Hinh co Mau
	obj = document.getElementById(id);
	obj.style.filter = "";
}
function setClosePage(){
		page_open('close.php','20','20');
}
function getval(url, v){
	document.location.href = url + "&trang=" + v;
}
