function validate_numeric(string) {
	var validFormatRegExp = /^\d*(\.\d+)?$/;
	var isValid = validFormatRegExp.test(string);
	return isValid;
}
function validate_alphanumericnospace(string) {
	var invalidCharactersRegExp = /[^a-z\d]/i;
	var isValid = !(invalidCharactersRegExp.test(string));
	return isValid;
}
function validate_email(email) {
	var validFormatRegExp = /^\w(\.?\w)*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;
	var isValid = validFormatRegExp.test(email);
	return isValid;
}
function validate_date(day, month, year) {
	var isValid = true;
	var enteredDate = new Date();
	enteredDate.setFullYear(year, month, day);
	if (enteredDate.getDate() != day) isValid = false;
	return isValid;
}
function getradio(rad) {
	var val = "";
	if (rad) {
		if (rad.length==undefined){
			if (rad.checked) {
				val=rad.value;
			}
		} else {
			for (i=0;i<rad.length;i++) {
				if (rad[i].checked) {
					val=rad[i].value;
					break;
				}
			}
		}
	}
	return val;
}

function OpenConfirm(url, msg) {
	var w = window.confirm(msg);
	if (w) window.location.href=url;
}
function OpenLink(url, target) {
	if (target!=null && target!="") {
		if (target=="_blank") {
			window.open(url);
		} else {
			window.open(url, target);
		}
	} else {
		window.location.href=url;
	}
}
function OpenWinSize(path, width, height) {
	vitriWidth=width+40;
	vitriHeight=height+40;
	vitriTop=(screen.availHeight-vitriHeight)/2;
	vitriLeft=(screen.availWidth-vitriWidth)/2;
	w=window.open(path,'','top='+vitriTop+', left='+vitriLeft+', width='+vitriWidth+', height='+vitriHeight+', scrollbars=0');
	w.focus();
}

function CheckConfig(frm, msg) {
	if (frm.config_name.value.length==0) {
		alert("Config name is invalid!");
		frm.config_name.focus();
	} else {
		w = window.confirm(msg);
		if (w) frm.submit();
	}
}
function CheckPost(frm, msg) {
//	if (ste.viewSource) { ste.toggleSource(); }
//	document.getElementById(ste.id).value = ste.getContent();
//	if (stf.viewSource) { stf.toggleSource(); }
//	document.getElementById(stf.id).value = stf.getContent();
//	nicEditors.findEditor('intro').saveContent();
//	nicEditors.findEditor('content').saveContent();

	if (frm.title.value=="") {
		alert("Title is invalid!");
		frm.title.focus();
	} else {
		w = window.confirm(msg);
		if (w) frm.submit();
	}
}
function CheckPhoto(frm, msg) {
	var filename = frm.image.value;
	var pos = filename.lastIndexOf(".");
	var ext = filename.substr(pos+1, filename.length-pos-1);
	var filetype = "jpg,gif,png,swf";

	if (frm.id.value=="" && frm.image.value=="") {
		alert("Browse an image!");
		frm.image.focus();
	} else if (frm.image.value!="" && filetype.indexOf(ext)==-1) {
		alert("Filetype is " + filetype);
		frm.image.focus();
	} else {
		w = window.confirm(msg);
		if (w) frm.submit();
	}
}
function CheckAttach(frm, msg, filetype) {
	var filename = frm.uploadfile.value;
	var pos = filename.lastIndexOf(".");
	var ext = filename.substr(pos+1, filename.length-pos-1);

	if (frm.title.value=="") {
		alert("Title is invalid!");
		frm.title.focus();
	} else if (frm.id.value=="" && frm.uploadfile.value=="" && frm.file.value=="") {
		alert("Browse a file or enter a url!");
		frm.file.focus();
	} else if (frm.uploadfile.value!="" && filetype.indexOf(ext)==-1) {
		alert("Filetype is " + filetype);
		frm.file.focus();
	} else {
		w = window.confirm(msg);
		if (w) frm.submit();
	}
}
function CheckComment(frm, msg) {
	if (frm.title.value=="") {
		alert("Enter a title!");
		frm.title.focus();
	} else if (frm.yourname.value=="") {
		alert("Enter your name!");
		frm.yourname.focus();
	} else if (frm.email.value.length==0 || !validate_email(frm.email.value)) {
		alert("Your email is invalid!");
		frm.email.focus();
	} else if (frm.numberrandom.value.length<6) {
		alert("Enter code beside!");
		frm.numberrandom.focus();
	} else {
		w = window.confirm(msg);
		if (w) frm.submit();
	}
}

function CheckUser(frm, msg) {
	if (frm.id.value=="" && frm.username.value.length==0) {
		alert("Vui lòng nhập Tên đăng nhập!");
		frm.username.focus();
	} else if (frm.fullname.value.length==0) {
		alert("Nhập Họ và Tên đầy đủ!");
		frm.fullname.focus();
	} else if (frm.password.value!="" && frm.password.value!=frm.repassword.value) {
		alert("Mật khẩu nhập lại không giống nhau!");
		frm.repassword.focus();
	} else {
		w = window.confirm(msg);
		if (w) frm.submit();
	}
}
function ResetForm(frm) {
	w = window.confirm("Reset form?");
	if (w) frm.reset();
}
function CheckContact(frm, msg) {
	if (frm.yourname.value.length==0) {
		alert("Enter your name!");
		frm.yourname.focus();
	} else if (frm.address.value.length==0) {
		alert("Enter your company\'s address!");
		frm.address.focus();
	} else if (frm.email.value.length==0 || !validate_email(frm.email.value)) {
		alert("Your email is invalid!");
		frm.email.focus();
	} else if (frm.content.value.length==0) {
		alert("Enter your content!");
		frm.content.focus();
	} else if (frm.numberrandom.value.length<6) {
		alert("Enter code below!");
		frm.numberrandom.focus();
	} else {
		w = window.confirm(msg);
		if (w) {
			frm.submit();
		}
	}
}

function CaptureMouse(divmenu, divname) {
	var $jm = jQuery.noConflict();
	var os = $jm('#'+divname).offset();
	var xx = os.left;
	var yy = os.top;

	var w1 = $jm('#'+divmenu).width();
	var w2 = $jm('#'+divname).width();
	var hh = $jm('#'+divname).height();
	
	$jm('body').mousemove(function(e) {
		if ((e.pageX < xx)
		|| (e.pageY > yy + hh)
		|| (e.pageY < yy - 46)
		|| (e.pageX > xx + w1 && e.pageY > yy - 46 && e.pageY < yy)
		|| (e.pageX > xx + w2 && e.pageY < yy + hh)) {
			$jm('body').unbind("mousemove");
			$jm('#'+divname).slideUp("fast");
		}
	}); 
}
function SlideSubmenu(divmenu, divname) {
	var $jm = jQuery.noConflict();
	var w1 = $jm('#'+divmenu).width();
	var w2 = $jm('#'+divname).width();
	var h2 = $jm('#'+divname).height();
	var ww = (w2>w1) ? w2 : w1;
//	alert(w1 + ", " + w2 + ", " + ww);
	if (h2>10)
	$jm('#'+divname).width(ww).slideDown("fast", function() {
		CaptureMouse(divmenu, divname);
	}); 
}

function ChangeImage(obj, img) {
	obj.src = img.src;
}
function ViewMore(id) {
	var $ja = jQuery.noConflict();
	$ja('#divMore_' + id).html('<p align="center"><img src="images/loading.gif" border="0"></p>');
	$ja.ajax({
		type: "GET",
		url: "index.php",
		data: "m=ajax&f=post&id=" + id,
		success: function(html){
			$ja('#divMore_' + id).hide().html(html).slideDown("slow", function() {
				$ja('#divPhotoMore_' + id + ' a').lightBox();
			});
		}
	});
}
function CheckAllCheckbox(obj) {
	var $jc = jQuery.noConflict();
	var checked = $jc('#' + obj).is(':checked');
	$jc("INPUT[type='checkbox']").attr('checked', checked);
}

function ShowNews(listNews, curNews) {
	var $jh = jQuery.noConflict();
	maxnews = listNews.length;
	if (maxnews>0) {
		caption = listNews[curNews][0];
		link    = listNews[curNews][1];
		$jh('#divHot').html('<a class="home" href="'+link+'">' + caption + '</a>');
		$jh('#divHot').fadeIn("slow").delay(5000).slideUp("slow", function() {
			curNews++;
			if (curNews>=maxnews) curNews=0;
			ShowNews(listNews, curNews);
		});
	}
}

function PrintContent(divname) { 
   var sOption="toolbar=yes,location=no,directories=yes,menubar=yes, scrollbars=yes, width=590, height=700, left=100, top=25"; 
   var sWinHTML = document.getElementById(divname).innerHTML;
   var winprint=window.open("","wPrint",sOption); 
       winprint.document.open(); 
       winprint.document.write('<html><LINK href="templates/ketoan1a/styles/printable.css" rel=Stylesheet>');
	   winprint.document.write('<body><div style="width:560">'); 
       winprint.document.write(sWinHTML);          
       winprint.document.write('</div></body></html>');
       winprint.document.close(); 
       winprint.focus();
       //winprint.print();
}
function ShowAttach(idparent, id) {
	var $jf = jQuery.noConflict();
	var cnt = $jf('#divFile_' + idparent + '_' + id).html();
	if (cnt=="") {
		$jf('#divFile_' + idparent + '_' + id).html('<p align="center"><img src="images/loading.gif" border="0"></p>');
		$jf.ajax({
			type: "GET",
			url: "index.php",
			data: "m=ajax&f=attach&t=" + idparent + "&id=" + id,
			success: function(html){
				$jf('#divFile_' + idparent + '_' + id).hide().html(html).slideDown("slow");
			}
		});
	} else {
		SkipDownload(idparent, id);
	}
}
function SkipDownload(idparent, id) {
	var $jf = jQuery.noConflict();
	$jf('#divFile_' + idparent + '_' + id).slideUp("slow", function() {
		$jf('#divFile_' + idparent + '_' + id).html("");
		window.location.href= 'download.php?t=' + idparent + '&id=' + id;
	});
}
function CheckDownload(frm) {
	if (frm.yourname.value.length==0) {
		alert("Enter your name!");
		frm.yourname.focus();
	} else if (frm.youremail.value.length==0 || !validate_email(frm.youremail.value)) {
		alert("Your email is invalid!");
		frm.youremail.focus();
	} else if (frm.numberrandom.value.length<6) {
		alert("Enter code below!");
		frm.numberrandom.focus();
	} else {
		var idparent = frm.t.value;
		var id = frm.id.value;
		var yourname = frm.yourname.value;
		var youremail = frm.youremail.value;
		var numberrandom = frm.numberrandom.value;
		
		var $jf = jQuery.noConflict();
		$jf('#divFile_' + idparent + '_' + id).html('<p align="center"><img src="images/loading.gif" border="0"></p>');
		$jf.ajax({
			type: "GET",
			url: "index.php",
			data: "m=ajax&f=attach&t=" + idparent + "&id=" + id + "&act=submit&yourname=" + yourname + "&youremail=" + youremail + "&numberrandom=" + numberrandom,
			success: function(html){
				$jf('#divFile_' + idparent + '_' + id).hide().html(html).slideDown("slow");
			}
		});
	}
}
// ==========================================
// SLIDER
// ==========================================
function SetUpScroller(container, childname, w, h) {
	var $jsc = jQuery.noConflict();
	var inuse = false;
	var num = $jsc('#' + container).children('.boxHomeBanner').length;
	var maxW = num * w;
	if (num>5) {
		var t = setInterval(function() {
			moveScroller('left');
		}, 3000);
	}
	function moveScroller(d) {
		for (var i=0; i<num; i++) {
			var l = parseInt($jsc('#' + childname + i).css('left'));
			if(isNaN(l)) l = 0;
			if (l <= -w) {
				l += maxW;
				$jsc('#' + childname + i).css('left', l);
			}
			var m = (d=='left') ? l-w : l+w;
			$jsc('#' + childname + i).animate({ 'left': m+'px' }, 'slow');
		}
	}
}
function SetUpSlider(container, btnLeft, btnRight, w, h) {
	var $js = jQuery.noConflict();
	var inuse = false;
	var num = $js('#' + container).children('.boxHomePost').length;
	var maxH = - num * h + h;
	if (num>1) {
		$js('#' + btnLeft).bind('click', function() {
			if(inuse===false) {
				inuse = true;
				moveSlider('right');
			}
		});
		$js('#' + btnRight).bind('click', function() {
			if(inuse===false) {
				inuse = true;
				moveSlider('left');
			}
		});
		var t = setInterval(function() {
			if (inuse===false) {
				inuse = true;
				moveSlider('left');
			}
		}, 5000);
	}
	function moveSlider(d) {
		var l = parseInt($js('#' + container).css('top'));
		if(isNaN(l)) l = 0;
		var m = (d=='left') ? l-h : l+h;
		if (m>0) m = maxH;
		if (m<maxH) m = 0;
		$js('#' + container).animate({ 'top': m+'px' }, 'slow', function() { inuse=false; });
	}
}
// ==========================================
// SLIDESHOW
// ==========================================
function slideshow(container, time) {
	var $js = jQuery.noConflict();
	var Max = $js('#' + container + ' div').length;
	var num = 0;
	$js('#' + container + 'div:first').addClass("active");
	if (Max>1) {
		var t = setInterval(function() { slideSwitch();}, time);
	}
	function slideSwitch() {
		var $active = $js('#' + container + ' div:eq(' + num + ')');
		$active.addClass("last-active");
		num++;
		if (num>Max-1) num = 0;
		var $next = $js('#' + container + ' div:eq(' + num + ')');
		$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});
	}
}


