var projectname = "/";
function sigIn() {
	window.location.href = projectname + "prc/?f=login";
}

function lD(ldd, txt) {
	if(typeof(txt) == 'undefined')
		document.getElementById(ldd).innerHTML = "<img src='" + projectname + "images/loading.gif'>";
	else
		document.getElementById(ldd).innerHTML = txt;
}

function _vL(idd) {
	return document.getElementById(idd).value;
}

function _sV(idd, vl) {
	document.getElementById(idd).value = vl;
}

function _sD(idd, vl) {
	document.getElementById(idd).disabled = vl;
}

function _fC(idd) {
	document.getElementById(idd).focus();
}

function _cK(idd, vl) {
	document.getElementById(idd).checked = vl;
}

function _yN(idd) {
	return document.getElementById(idd).checked;
}

function notNull(element) {
	if(element.value == "") {
		alert("Sorry not null!");
		element.focus();
	}
}

function siLent(ourl, trg) {
	if(typeof(ourl) != 'undefined') {
		if(trg == 1)
			window.open(ourl);
		else
			window.location.href = ourl;
	}
}

function checkAllRolMaster(idm) {
	//rp = _vL("read_" + idm);
	_cK("admin", false);
}

function checkPrivUser() {
	_cK("admin", false);
}

function checkAdmin() {
	am = _yN("admin");
	n = _vL("lm_id");
	for(ij = 0; ij < n; ij++) {
		idm = _vL("idm_" + ij);
		_cK("read_" + idm, am);
		_cK("write_" + idm, am);
		_cK("edit_" + idm, am);
		_cK("delete_" + idm, am);
		_cK("master_" + idm, am);
	}
}

function fullImg(img, w, h, mod, pr) {
	w += 50;
	h += 50;
	if(w > 1024)	w = 1024;
	if(h > 800)	 h = 800;
	url = pr + "viewfull.aspx?img=" + img + "&mod=" + mod;
	temp = "scrollbars=0, resizable=yes, width=" + w + ", height = " + h;
	window.open(url, "viewfull", temp);
}

function fullImgCom(id, w, h, mod, pr) {
	w += 50;
	h += 50;
	if(w > 1024)	w = 1024;
	if(h > 800)	 h = 800;
	url = pr + "viewfullcom.aspx?id=" + id + "&mod=" + mod;
	temp = "scrollbars=0, resizable=no, width=" + w + ", height = " + h;
	window.open(url, "viewfull", temp);
}


function viewVideo(video, w, h, mod, pr) {
	w += 50;
	h += 50;
	if(w > 1024)	w = 1024;
	if(h > 800)	 h = 800;
	url = pr + "viewvideo.aspx?video=" + video+ "&mod=" + mod;
	temp = "scrollbars=0, resizable=yes, width=" + w + ", height = " + h;
	window.open(url, "ViewVideo", temp);
}

function popupWin(url, u, w, h) {
	temp = "scrollbars=0, resizable=yes, width=" + w + ", height = " + h;
	window.open(url, "Gold", temp);
}

/*
var regExp = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF|.png|.PNG|.bmp|.BMP)$/;
    if (!regExp.test(document.getElementById("attach").value)) {
		alert("No accept!");
	} else
*/

///Kiem tra ngay thang hop le
//Dem so ky tu mark trong chuoi str
function countChar(str, mark) {
	var Count = 0;
	for(i = 0; i < str.length; i++)
		if (str.charAt(i) == mark) 
			Count++;
	return 	Count;
}
//Ham nay tra ve Ngay trong chuoi strDate duoc format thanh kieu so nguyen
function getDateToInt(strDate) {
	k = strDate.indexOf("/");			
	strDay = strDate.substring(0, k);
	return stringToInt(strDay);
}
//Ham nay tra ve Thang trong chuoi strDate duoc format thanh kieu so nguyen
function getMonthToInt(strDate) {
	i = strDate.lastIndexOf("/");
	j = strDate.lastIndexOf("/", i - 1) ;	
	strMonth = strDate.substring(j + 1, i);
	return stringToInt(strMonth);
}
//Ham nay tra ve Nam trong chuoi strDate duoc format thanh kieu so nguyen
function getYearToInt(strDate) {
	i = strDate.lastIndexOf("/");
	strYear = strDate.substring(i + 1, strDate.length);
	return stringToInt(strYear);
}
//
//Ham nay tra ve Ngay trong chuoi strDate duoc format dd/mm/yyyy H:i
function getDaytimeToInt(strDate) {
	k = strDate.indexOf("/");			
	strDay = strDate.substring(0, k);
	return stringToInt(strDay);
}
//Ham nay tra ve Thang trong chuoi strDate duoc format dd/mm/yyyy H:i
function getMonthtimeToInt(strDate) {
	i = strDate.indexOf("/");
	strMonth = strDate.substring(i + 1, i+3);
	return stringToInt(strMonth);
}
//Ham nay tra ve Nam trong chuoi strDate duoc format format dd/mm/yyyy H:i
function getYeartimeToInt(strDate) {
	i = strDate.lastIndexOf("/");
	strYear = strDate.substring(i + 1, i+5);
	return stringToInt(strYear);
}
//Ham nay tra ve Hour trong chuoi strDate duoc format format dd/mm/yyyy H:i
function getHourtimeToInt(strDate) {
	i = strDate.indexOf(":");
	strHour = strDate.substring(i-2, i);
	return stringToInt(strHour);
}
//Ham nay tra ve Minute trong chuoi strDate duoc format format dd/mm/yyyy H:i
function getMinutetimeToInt(strDate) {
	i = strDate.lastIndexOf(":");
	strMinute = strDate.substring(i+1, strDate.length);
	if(strMinute.length > 0)
		return stringToInt(strMinute);
	return -1;
}

//
function checkIntVal(val, low, high) {
	if (val <= high && val >= low) 
		return true;
	return false;
}
//
function stringToInt(str) {
	if(!stringIsNum(str))
		return 0 ;
	if(str.charAt(0) == '0') 
		return stringToInt(str.substring(1, str.length));
	else 
		if(str.length > 0) 
			return parseInt(str);
		 else 
			return (0);
}
//
function stringIsNum(val) {
	var str = new String(val);
	for (i=0;i<str.length;i++) 
		if (isNaN(parseInt(str.charAt(i))))
			return false;
	return true;
}
//Kiem tra chuoi theo chuan ton tai cua chuoi mark trong chuoi str
function checkExistVal(str, mark) {
	for(i = 0; i < str.length; i++)
		if (mark.indexOf(str.charAt(i)) < 0) 
			return false;
	return 	true;
}
//Lay nam hien tai
function getCurrentYear() {
	var objd = new Date();
	var currYear = objd.getYear();
	if(navigator.appName == "Netscape")
		currYear += 1900;
	return currYear;
}
// Kiem tra ngay theo chuan (dd/mm/yyyy)
function checkFormatDate(strDate, sb_year, se_year) {
	if(!checkExistVal(strDate, '1234567890/') || countChar(strDate, '/') != 2)
		return false;
	Year = getYearToInt(strDate);
	Month = getMonthToInt(strDate);
	Day = getDateToInt(strDate);
	if(typeof(sb_year) == 'undefined')
		sb_year =  getCurrentYear() - 1;
	if(typeof(se_year) == 'undefined')
		se_year =  getCurrentYear() + 222;
	if(!checkIntVal(Year, sb_year, se_year))
		return false;	
	if(!checkIntVal(Month, 1, 12))
		return false;	
	if(!checkIntVal(Day, 1, 31))
		return false;	
	if (Month == 4 || Month == 6 || Month == 9 || Month == 11 )
		if(Day > 30)
			return false;
	if(((Year % 4 == 0) && (Year %100 != 0)) || Year % 400 == 0) {
		if((Month == 2 ) && ( Day > 29))
			return false;
	} else
		if((Month == 2 ) && ( Day > 28))
			return false;
	return 	true;
}
//Kiem tra gio hop le
function checkFormatTime(strtime) {
	if(!checkExistVal(strtime, '1234567890:') || countChar(strtime, ':') != 1)
		return false;
	if(!checkIntVal(getHourtimeToInt(strtime), 0, 23))
		return false;
	if(!checkIntVal(getMinutetimeToInt(strtime), 0, 59))
		return false;
	return true;
}
//So sanh gio hop le
function compareTo(btime, etime) {
	bh = getHourtimeToInt(btime);
	bm = getMinutetimeToInt(btime);
	eh = getHourtimeToInt(etime);
	em = getMinutetimeToInt(etime);
	if(eh > bh) return true;
	if(eh == bh)
		if(em > bm) return true;
	return false;
}
//Chuyen dang ngay thang sang thang ngay
function changeDayMonthToMonthDay(sdate) {
	year = getYearToInt(sdate);
	month = getMonthToInt(sdate);
	day = getDateToInt(sdate);
	return month + "/" + day+ "/" + year;
}

//AddRemove
function addRemove(chose, list) {
	objc = document.getElementById(chose);
	objl = document.getElementById(list);
    var ll = objl.length;
	var lc = objc.length;
	var ok = true;
	var minSelect = -1;
	if(ll != 0) {
		var i = 0;
		while(i < ll) {
			if(objl.options[i].selected == true) {
				if (ok) {
					minSelect = i;
					ok = false;
				}
				opt = new Option();
				opt.text = objl.options[i].text;
				opt.value = objl.options[i].value;
				objc.options[lc++] = opt;
				objl.options[i] = null;
				--ll;
			} else 
				++i;
		}
	}
	if ((ll > 0) && (minSelect != -1)) {
		if(minSelect < ll)
			objl.options[minSelect].selected = true;
		else 
			objl.options[0].selected = true;
	}
}

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

//This function is used to check the validation of email address.
function isEmail(value) {
	/*
		Email Address's Format: username@subdomain.domain
		Email Address must be include 3 part:
			part 1: username
			part 2: @
			part 3: <domainname[.domainname,...]>.<domainname>
	*/
	if (value==null || value=="")	return true;
	if (value.indexOf(" ")>=0)		return false;

	var state, code, username, domain, amountOfDot, i;
	state = 1; username=''; domain=''; amountOfDot = 0;
	for (i=0; i<value.length; i++) {
		code = value.charAt(i);
		if (state==1) {
			if (code == "<" || code == ">" || code == "(" || code == ")"	) return false;
			else if (	code == "@"	)
				if (username == '') return false;
				else state = 3;
			username += code;
		}
		else if (state==3) {
			if ((code >= "0" && code <= "9")
					|| (code >= "A" && code <= "Z")
					|| (code >= "a" && code <= "z")
					|| code == "_"
					|| code == "-"
				) ;
			else if (code == ".")
				if (domain == '' || domain.charAt(domain.length-1) == '.') return false;
				else amountOfDot++;
			else return false;
			domain += code;
		}
	}
	if (state != 3) return false;
	if (domain == '' || domain.charAt(domain.length-1) == '.') return false;
	if (amountOfDot <1) return false;
	return true;
}

//This function is used to check the validation of telephone, fax number.
function isTelephone(phone) {
	var pattern = /^\+?[0-9 ()-.]+[0-9.]$/;
	var phoneval=trim(phone);
	if (!pattern.test(phoneval)) return false;
	return true;
}

function isNumber(nb) {
	var pattern = /^[0-9]+$/;
	var nb=trim(nb);
	if (!pattern.test(nb)) return false;
	return true;
}

/*===ADV===*/
var blkmember = {
	pageSize: 4,
	offset: 102,
	delay: 10,
	sliderObj: { },
	timeout: {},
	count: 0, 
	pos: 0,
	curPos: 0,
	
	init:function(newTileCount, newSliderObj){
		this.count = newTileCount;
		this.sliderObj = newSliderObj;
		this.sliderObj.style.height = this.offset + (newTileCount * this.offset) + 'px';
	},

	prev:function(){
		var curPage = (Math.ceil(Math.abs(this.pos / this.offset)) / this.pageSize) + 1;
		if (curPage > 1) {
			this.curPos = this.pos;
			this.pos += (this.offset * this.pageSize);
			this.timeout = setTimeout('blkmember.moveTiles(\'right\');',this.delay); 
		}
	},

	next:function(){
		var lastPage = (Math.ceil(this.count / this.pageSize));
		var curPage = (Math.ceil(Math.abs(this.pos / this.offset)) / this.pageSize) + 1;
		if (curPage < lastPage) {
			this.curPos = this.pos;
			this.pos = this.pos - (this.offset * this.pageSize);
			this.timeout = setTimeout('blkmember.moveTiles(\'left\');',this.delay); 
		}
	},
	
	moveTiles:function(direction) {
		var k = false;
		this.curPos = (direction == 'left')?this.curPos - (this.offset/4):this.curPos + (this.offset/4);
		k = (direction == 'left')?this.curPos >= this.pos:this.curPos <= this.pos;
		if (k)
		{
			this.sliderObj.style.top = this.curPos + "px";
			this.timeout = setTimeout('blkmember.moveTiles(\''+direction+'\');',this.delay); 
		} else {
			this.sliderObj.style.top = this.pos + "px";
		}
	}
}
