﻿//------------------------------------------------------------------------
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 doSubmite(){
	var post = document.post;
	if(!testSpaceBar(post.tieude)){
		alert("Please enter subject.");
		post.tieude.focus();
		return false;
	}
	if(!testSpaceBar(post.noidung)){
		alert("Please enter content.");
		post.noidung.focus();
		return false;
	}
	if(!testSpaceBar(post.hoten)){
		alert("Please enter your full name.");
		post.hoten.focus();
		return false;
	}
	if(!validEmail(post.email)){
		alert("Please enter your email address.");
		post.email.focus();
		return false;
	}
	post.action="contacte.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(){
	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 ShowHideMenu(divmenu, divicon)
{
    if(!divmenu) return;
    divmenu = document.getElementById(divmenu);
    //divicon = document.getElementById(divicon);
    if(!divmenu) return;
    if(divmenu.style.display == "none")
    {
        divmenu.style.display = "";
        //divicon.style.backgroundImage = "url('/images/bullet-red-open.gif')";
    }
    else
    {
        divmenu.style.display = "none";
        //divicon.style.backgroundImage = "url('/images/bullet-red.gif')";
    }
}

