﻿//------------------------------------------------------------------------
function validEmail(obj) {
	var s = obj.value;
	for (var i=0; i<s.length; i++)
		if (s.charAt(i)==" "){
			return false;
		}
	var elem, elem1;
	elem=s.split("@");
	if (elem.length!=2)	return false;

	if (elem[0].length==0 || elem[1].length==0)return false;

	if (elem[1].indexOf(".")==-1)	return false;

	elem1=elem[1].split(".");
	for (var i=0; i<elem1.length; i++)
		if (elem1[i].length==0)return false;
	return true;
}
//------------------------------------------------------------------------
function testSpaceBar(obj){
	if(obj.value=="")return false;
	else{		
		var s = obj.value;
		var temp = s.split(" ");
		var str = "";
		for(var i=0; i<temp.length; i++)str=str + temp[i];
		if(str==""){
			obj.value = str.substring(0,str.length);
			return false;
		}
	}//else
	return true;
}
//------------------------------------------------------------------------
function validDate(obj) {
	var s = obj.value;
	var unicode;
	for(var i=0;i<s.length; i++){
		unicode = s.charCodeAt(i);
		if(unicode<48 || unicode>57)
			if (unicode!=47)return false;
	}
	if(s.indexOf("/")==-1)return false;

	var elem=s.split("/");
	if(s.length!=0 && (elem[1].length==0 || eval(elem[1])<1 || eval(elem[1])>12))return false;

	var DaysPerMonth;
	switch(eval(elem[1])){
		case 4:
		case 6:
		case 9:
		case 11:{
			DaysPerMonth=30;
			break;}
		case 2:{ 
			if((elem[2]%4 == 0) && ((elem[2]%100 != 0) || (elem[2]%400 == 0)))
				DaysPerMonth=29
			else
				DaysPerMonth=28
			break;
		}
		default:
			DaysPerMonth=31
	}
	if(s.length!=0 && (elem[0].length==0 || eval(elem[0])<1 || eval(elem[0])>eval(DaysPerMonth)))return false;
	if(s.length!=0 && (elem[2].length==0 || eval(elem[2])<1900 || eval(elem[2])>3000))return false;
	return true;
}
//------------------------------------------------------------------------
function isNumber(obj) {
	var s = obj.value;
	s = s.toString();
	if (s.length == 0)
		return false;
	for (var n = 0; n < s.length; n++)
		if (s.substring(n, n+1) < "0" || s.substring(n, n+1) > "9")
			return false;

	return true;
}
//------------------------------------------------------------------------------------------
function validCheckBox(obj){
	var isChecked = false;
	for(var i=0; i<obj.length; i++)
		if (obj[i].checked==true)isChecked = true;
	if(isChecked==false)return false;
	else return true;
}
//------------------------------------------------------------------------------------------
function MM_openBrWindow(theURL,winName,features) { //v2.0
	var mywindow = window.open(theURL,winName,features);
	if (window.focus) {mywindow.focus()}
	return false;
}
//------------------------------------------------------------------------------------------
function createDate()
{
	var mymonth = eval(document.book.bookmonth.value);
	var myyear = eval(document.book.bookyear.value);
	var maxday;
	var selectedday = document.book.bookday.selectedIndex;
	switch(mymonth)
	{
		case 2:
			if((myyear%400)==0)
				maxday = 29;
			else
				maxday = 28;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			maxday = 30;
			break;
		default:
			maxday = 31;
			break;
	}
	document.book.bookday.options.length = 0;	
	for(i=0; i<maxday; i++)
	{
		var option = new Option(i+1,i+1);
		document.book.bookday.options.add(option,maxday);
	}
	if(selectedday>=maxday)
		document.book.bookday.selectedIndex=maxday-1;
	else
		document.book.bookday.selectedIndex=selectedday;
}
//------------------------------------------------------------------------------------------
function changeImage(divId, divImg, divAlt)
{
	var divImage = document.getElementById(divId);
	divImage.innerHTML = '<span style="width: 150px; border: 1px solid #C0C0C0; padding: 2px; margin-top: 10px; margin-bottom: 10px; float:left;"><span style="display: block; background: #FFFFFF url(&quot;images/' + divImg + '&quot;) center center no-repeat; overflow: hidden;" width="150px" height="100px"><img src="layout/blank.gif" width="150px" height="100px" alt="' + divAlt + '"/></span></span>';
}
//------------------------------------------------------------------------------------------
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}
//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 
//------------------------------------------------------------------------------------------
function viewCatalogue(divid,divid1,divid2,divid3)
{
    if(!divid) return;
    divid = document.getElementById(divid);
    divid1 = document.getElementById(divid1);
    divid2 = document.getElementById(divid2);
    divid3 = document.getElementById(divid3);
    if(!divid) return;
    divid.style.display = "";
    if(!divid1) return;
    divid1.style.display = "none";
    if(!divid2) return;
    divid2.style.display = "none";
    if(!divid3) return;
    divid3.style.display = "none";
}

//------------------------------------------------------------------------------------------
function openform(textbox,textboxsm,smstyle)
{
	newwin=window.open("/upload/?textbox="+textbox+"&textboxsm="+textboxsm+"&smstyle="+smstyle, "upload", "toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=450,height=100")
}
//------------------------------------------------------------------------------------------
function getfilename(textbox,strFilename,textboxsm,strSMFilename){
	textbox = document.getElementById(textbox);
	textboxsm = document.getElementById(textboxsm);
	if(!textbox) return;
	textbox.value = strFilename;
	if(!textboxsm) return;
	textboxsm.value = strSMFilename;
}
//-------------------------------------------------------------------------------------------
function doSubmit(){
	var post = document.post;
	if(!testSpaceBar(post.tieude)){
		alert("Vui lòng nhập tiêu đề liên hệ.");
		post.tieude.focus();
		return false;
	}
	if(!testSpaceBar(post.noidung)){
		alert("Vui lòng cho biết nội dung liên hệ.");
		post.noidung.focus();
		return false;
	}
	if(!testSpaceBar(post.hoten)){
		alert("Vui lòng nhập họ tên của bạn.");
		post.hoten.focus();
		return false;
	}
	if(!validEmail(post.email)){
		alert("Vui lòng nhập nhập email của bạn.");
		post.email.focus();
		return false;
	}
	post.action="contact.php";
}
//-------------------------------------------------------------------------------------------
function getPass(){
	if(!testSpaceBar(dangky.username)){
		alert("Vui lòng nhập tên đăng nhập.");
		dangky.username.focus();
		return false;
	}
	if(!testSpaceBar(dangky.email)){
		alert("Vui lòng nhập email liên hệ.");
		dangky.email.focus();
		return false;
	}
}
//-------------------------------------------------------------------------------------------
function logIn(){
	var login = document.login;
	if(!testSpaceBar(login.username)){
		alert("Vui lòng nhập tên đăng nhập.");
		login.username.focus();
		return false;
	}
	if(!testSpaceBar(login.password)){
		alert("Vui lòng nhập mật khẩu.");
		login.password.focus();
		return false;
	}
}
//-------------------------------------------------------------------------------------------
function logIn1(){
	var login = document.login1;
	if(!testSpaceBar(login.username)){
		alert("Vui lòng nhập tên đăng nhập.");
		login.username.focus();
		return false;
	}
	if(!testSpaceBar(login.password)){
		alert("Vui lòng nhập mật khẩu.");
		login.password.focus();
		return false;
	}
}
//-------------------------------------------------------------------------------------------
function doEnquiry(){
	var post = document.post;
	if(!testSpaceBar(post.hoten)){
		alert("Vui lòng nhập họ và tên của bạn.");
		post.hoten.focus();
		return false;
	}
	if(!testSpaceBar(post.diachi)){
		alert("Vui lòng nhập địa chỉ liên hệ.");
		post.diachi.focus();
		return false;
	}
	if(!testSpaceBar(post.dienthoai)){
		alert("Vui lòng nhập điện thoại liên hệ.");
		post.dienthoai.focus();
		return false;
	}
	if(!validEmail(post.email)){
		alert("Vui lòng nhập nhập email của bạn. Email phải có dạng: abc@tencongty.com");
		post.email.focus();
		return false;
	}
	post.action="enquiry.php";
}
//-------------------------------------------------------------------------------------------
function doReg(){
	var frmpost = document.frm;
	if(!testSpaceBar(frmpost.cmsusername)){
		alert("Vui lòng nhập tên đăng nhập.");
		frmpost.cmsusername.focus();
		return false;
	}
	if(!testSpaceBar(frmpost.cmspassword)){
		alert("Vui lòng nhập mật khẩu truy cập. Mật khẩu truy cập cần ít nhất 4 ký tự.");
		frmpost.cmspassword.focus();
		return false;
	}
	if(frmpost.cmspassword.value.length<4){
		alert("Mật khẩu truy cập cần ít nhất 4 ký tự.");
		frmpost.cmspassword.focus();
		return false;
	}	
	if(!testSpaceBar(frmpost.cmsrepassword)){
		alert("Vui lòng nhập lại xác nhận mật khẩu truy cập.");
		frmpost.cmsrepassword.focus();
		return false;
	}	
	if(frmpost.cmspassword.value!=frmpost.cmsrepassword.value){
		alert("Mật khẩu và xác nhận mật khẩu không giống nhau, vui lòng kiểm tra lại.");
		frmpost.cmsrepassword.focus();
		return false;
	}	
	if(!validEmail(frmpost.cmsemail)){
		alert("Địa chỉ email không hợp lệ, vui lòng kiểm tra lại.");
		frmpost.cmsemail.focus();
		return false;
	}
	if(!testSpaceBar(frmpost.cmsten)){
		alert("Vui lòng nhập tên của bạn.");
		frmpost.cmsten.focus();
		return false;
	}
	if(!testSpaceBar(frmpost.cmsdiachi)){
		alert("Vui lòng nhập địa chỉ liên hệ.");
		frmpost.cmsdiachi.focus();
		return false;
	}
	if(!testSpaceBar(frmpost.cmstinhthanh)){
		alert("Vui lòng chọn tỉnh/thành phố.");
		frmpost.cmstinhthanh.focus();
		return false;
	}
	if(!testSpaceBar(frmpost.cmsdienthoai)&&!testSpaceBar(frmpost.cmsdidong)){
		alert("Vui lòng cho biết điện thoại hoặc điện thoại di động liên hệ.");
		frmpost.cmsdienthoai.focus();
		return false;
	}	
}
//-------------------------------------------------------------------------------------------
function doUpdate(){
	var frmpost = document.frm;
	if(!validEmail(frmpost.cmsemail)){
		alert("Địa chỉ email không hợp lệ, vui lòng kiểm tra lại.");
		frmpost.cmsemail.focus();
		return false;
	}
	if(!testSpaceBar(frmpost.cmsten)){
		alert("Vui lòng nhập tên của bạn.");
		frmpost.cmsten.focus();
		return false;
	}
	if(!testSpaceBar(frmpost.cmsdiachi)){
		alert("Vui lòng nhập địa chỉ liên hệ.");
		frmpost.cmsdiachi.focus();
		return false;
	}	
	if(!testSpaceBar(frmpost.cmstinhthanh)){
		alert("Vui lòng chọn tỉnh/thành phố.");
		frmpost.cmstinhthanh.focus();
		return false;
	}	
	if(!testSpaceBar(frmpost.cmsdienthoai)&&!testSpaceBar(frmpost.cmsdidong)){
		alert("Vui lòng cho biết điện thoại hoặc điện thoại di động liên hệ.");
		frmpost.cmsdienthoai.focus();
		return false;
	}	
}
//------------------------------------------------------------------------------------------
function openform(textbox,textboxsm,smstyle)
{
	newwin=window.open("/upload/?textbox="+textbox+"&textboxsm="+textboxsm+"&smstyle="+smstyle, "upload", "toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=450,height=100")
}
//------------------------------------------------------------------------------------------
function getfilename(textbox,strFilename,textboxsm,strSMFilename){
	textbox = document.getElementById(textbox);
	textboxsm = document.getElementById(textboxsm);
	if(!textbox) return;
	textbox.value = strFilename;
	if(!textboxsm) return;
	textboxsm.value = strSMFilename;
}
//------------------------------------------------------------------------------------------
function doPost(){
	var post = document.frm;
	if(!testSpaceBar(post.cmstitle)){
		alert("Vui lòng nhập tên khóa học.");
		post.cmstitle.focus();
		return false;
	}
	if(!validCheckBox(post.cmsnganhnghe)){
		alert("Vui lòng chọn ít nhất 1 lĩnh vực ngành nghề cho khóa học.");
		post.cmsnganhnghe[0].focus();
		return false;
	}
	if(!testSpaceBar(post.cmsmota)){
		alert("Vui lòng nhập thông tin mô tả về khóa học.");
		post.cmsmota.focus();
		return false;
	}
}
//-------------------------------------------------------------------------------------------
function CountLeft(field, count, max) {
	if (field.value.length > max)
	field.value = field.value.substring(0, max);
	else
	count.value = max - field.value.length;
}
function CountLeftNS(field, max) {
	if (field.value.length > max)
		field.value = field.value.substring(0, max);
}
//-------------------------------------------------------------------------------------------
function del(id_del) {
	document.delfrm.iddel.value = id_del;
	document.delfrm.action=".?view=del"
	if (confirm("Bạn có chắc là xóa tin giao dịch này không?"))
		document.delfrm.submit();
}
//-------------------------------------------------------------------------------------------
function getPass(){
	if(!testSpaceBar(document.getpass.email)){
		alert("Vui lòng nhập email đã đăng ký.");
		document.getpass.email.focus();
		return false;
	}
	if(!validEmail(document.getpass.email)){
		alert("Vui lòng nhập chính xác địa chỉ email.");
		document.getpass.email.focus();
		return false;
	}
}
//-------------------------------------------------------------------------------------------
function doChangePass(){
	var frmpost = document.frm;
	if(!testSpaceBar(frmpost.oldpassword)){
		alert("Vui lòng nhập mật khẩu truy cập hiện tại.");
		frmpost.oldpassword.focus();
		return false;
	}
	if(!testSpaceBar(frmpost.cmspassword)){
		alert("Vui lòng nhập mật khẩu truy cập. Mật khẩu truy cập cần ít nhất 4 ký tự.");
		frmpost.cmspassword.focus();
		return false;
	}
	if(frmpost.cmspassword.value.length<4){
		alert("Mật khẩu truy cập cần ít nhất 4 ký tự.");
		frmpost.cmspassword.focus();
		return false;
	}	
	if(!testSpaceBar(frmpost.cmsrepassword)){
		alert("Vui lòng nhập lại xác nhận mật khẩu truy cập.");
		frmpost.cmsrepassword.focus();
		return false;
	}	
	if(frmpost.cmspassword.value!=frmpost.cmsrepassword.value){
		alert("Mật khẩu và xác nhận mật khẩu không giống nhau, vui lòng kiểm tra lại.");
		frmpost.cmsrepassword.focus();
		return false;
	}	
}

