function changeStep (chapdiv, divcontent, chapmenu ,divmenu) {
	
	if(chapdiv && divcontent) {
		var divElm = document.getElementById(chapdiv).getElementsByTagName("div");		
		for (i=0; i<divElm.length; i++) {
			if(divElm[i].id == divcontent) {
				document.getElementById(divcontent).style.display = "block";
			} else {
				if(divElm[i].id){
					divElm[i].style.display = "none";
				}
			}
		}
	}
	var menuElm = document.getElementById(chapmenu).getElementsByTagName("a");
	for (i=0; i<menuElm.length; i++) {
		if(menuElm[i].id == divmenu) {
			document.getElementById(divmenu).className = "bh_menu_hover";
		} else {
			if(menuElm[i].id){
				menuElm[i].className = "bh_menu";
			}
		}
	}
}

function changeChapter (divchap, divmenu) {
	var chapElm = document.getElementById("chap_content").getElementsByTagName("div");
	for (i=0; i<chapElm.length; i++) {
		if(chapElm[i].id == divchap) {
			document.getElementById(divchap).style.display = "block";
		} else {
			if (chapElm[i].getAttribute("subid")){
				chapElm[i].style.display = "none";
			}
		}
	}
	var chapMenu = document.getElementById("chap_menu").getElementsByTagName("td");
	for (i=0; i<chapMenu.length; i++) {
		if(chapMenu[i].id == divmenu) {
			document.getElementById(divmenu).className = "bh_navigation_hover";
		} else {
			if(chapMenu[i].id){
				chapMenu[i].className = "bh_navigation";
			}
		}
	}
}

function changeTopStandard (divid, menuid) {
	var divElm = document.getElementById("top_standard").getElementsByTagName("div");
	for (i=0; i<divElm.length; i++) {
		if (divElm[i].id == divid) {
			document.getElementById(divid).style.display = "block";
		} else {
			if (divElm[i].id) {
				divElm[i].style.display = "none";
			}
		}
	}
	var menuElm = document.getElementById("top_menu").getElementsByTagName("div");
	for (i=0; i<menuElm.length; i++) {
		if (menuElm[i].id == menuid) {
			document.getElementById(menuid).className = "bh_menu_hover";
		} else {
			if (menuElm[i].id) {
				menuElm[i].className = "bh_menu";
			}
		}
	}
}
function reloadImage() {
	document.getElementById("verify_image").src = 'verifycode/verifycode.php';
}
function isNotBlank(str) {
	nochar="\n\r\t ";
	for(i=0;i<str.length;i++)
		if(nochar.indexOf(str.charAt(i))<0)
			return true;
	return false;
}
function isEmail(str) {
	if(isNotBlank(str)) {
		re=/^\w.+@\w+[.]\w+[\.]?[a-z,A-Z,0-9]+$/
		if(re.test(str)) return true;
		else {
				return false;
			 }
	} else return false;
}
function checkValidDownload(){
	var formElm = document.getElementById("frmDownload");
	var error = "";
	if(!isNotBlank(formElm.txtFullname.value)){
		error += "\nFullname is required";
	} if (!isEmail(formElm.txtEmail.value)) {
		error += "\nEmail is not valid";
	} if (!isNotBlank(formElm.txtCode.value)) {
		error += "\nCode is required";
	}
	if (error == "") {
		formElm.submit();
	} else {
		alert("Dữ liệu không hợp lệ! Hãy thử lại"+error);
	}
}
function checkValidSignup () {
	var frmElm = document.getElementById("frmSignup");
	var error = "";
	if (!isNotBlank(frmElm.txtUsername.value)) {
		error += "\nBạn chưa nhập tên đăng nhập";
	}
	if (!isNotBlank(frmElm.txtFullname.value)) {
		error += "\nBạn chưa nhập họ tên";
	} if (!isNotBlank(frmElm.txtPassword.value)) {
		error += "\nBạn chưa nhập mật khẩu";
	} if (isNaN(frmElm.txtLocalID.value) || (frmElm.txtLocalID.value) < 0) {
		error += "\nBạn chọn tỉnh thành của mình";
	} if (!isNotBlank(frmElm.txtCode.value)) {
		error += "\nBạn chưa nhập mã an toàn";
	} if (!isEmail(frmElm.txtEmail.value)) {
		error += "\nEmail của bạn không hợp lệ";
	} if(frmElm.txtConfirm.value != frmElm.txtPassword.value) {
		error += "\nMật khẩu của bạn không trùng nhau";											   
	}  
	if (error == "" ){
		frmElm.submit();
	} else {
		alert ("Dữ liệu của bạn không hợp lệ! Hãy thử lại"+error);
	}
}
function checkValidContact () {
	var frmElm = document.getElementById("frmContact");
	var error = "";
	if (!isNotBlank(frmElm.txtFullname.value)) {
		alert ("Bạn chưa nhập họ tên");
		frmElm.txtFullname.focus();
		return false;
	} if (!isEmail(frmElm.txtEmail.value)) {
		alert ("Email của bạn không hợp lệ");
		frmElm.txtEmail.focus();
		return false;
	} if(!isNotBlank(frmElm.txtContent.value)) {
		alert ("Bạn chưa nhập nội dung cần gởi");
		frmElm.txtContent.focus();
		return false;								   
	}  if(!isNotBlank(frmElm.txtCode.value)) {
		alert ("Bạn chưa nhập mã an toàn");
		frmElm.txtCode.focus();
		return false;										   
	}  
}
function confirmLogout () {
	ask = confirm("Bạn có muốn thoát quyền sử dụng?");
	if(ask == true) {
		window.location = "ajax/process_signout.php";
	}
}

function changAnswers(ansid, quesid){
	var divElm = document.getElementById("answer_list").getElementsByTagName("div");
	for (i=0; i<divElm.length; i++) {
		if(divElm[i].id == ansid) {
			document.getElementById(ansid).style.display = "block";
		} else {
			if(divElm[i].id) {
				divElm[i].style.display = "none";
			}
		}
	}
	var quesElm = document.getElementById("questions").getElementsByTagName("div");
	for (i=0; i<quesElm.length; i++) {
		if(quesElm[i].id == quesid) {
			document.getElementById(quesid).className = "qna_ques_hover";
		} else {
			if(quesElm[i].id) {
				quesElm[i].className = "qna_ques";
			}
		}
	}
}
function changeTab (divid, menu) {
	var chapElm = document.getElementById("blog_content").getElementsByTagName("div");
	for (i=0; i<chapElm.length; i++) {
		if(chapElm[i].id == divid) {
			document.getElementById(divid).style.display = "block";
		} else {
			if (chapElm[i].getAttribute("subid")){
				chapElm[i].style.display = "none";
			}
		}
	}
	
	var chapMenu = document.getElementById("tab_menu").getElementsByTagName("td");
	for (i=0; i<chapMenu.length; i++) {
		if(chapMenu[i].id == menu) {
			document.getElementById(menu).className = "bh_navigation_hover";
		} else {
			if(chapMenu[i].id){
				chapMenu[i].className = "bh_navigation";
			}
		}
	}
	if (divid == "common_ques") {
		//alert("hello");
		document.getElementById("askimg").src = "images/qna_ask_03_active.jpg";
		document.getElementById("thaoluanimg").src = "images/qna_thaoluan_03.jpg";
	} else if(divid == "thaoluanforum") {
		
		document.getElementById("askimg").src = "images/qna_ask_03.jpg";
		document.getElementById("thaoluanimg").src = "images/qna_thaoluan_active_03.jpg";
	}
}

function highLightDate(id) {
	div = document.getElementById("calendar").getElementsByTagName("div");
	for (i=0; i<div.length; i++) {
		if(div[i].id == id) {
			div[i].className = "calendar_highlight";
		} else {
			div[i].className = "";
		}
	}
}

function onScrollHandler1(divid) {
	divElm = document.getElementById(divid);
	var posY = divElm.scrollTop;
	if (posY < 750) {
		changeStep('', '', 'step_menu_chap1', 'chap1_step1_link');
	}
	if (posY > 750 && posY < 1200) {
		changeStep('', '', 'step_menu_chap1', 'chap1_step2_link');
	}
	if (posY > 1200 && posY < 2100) {
		changeStep('', '', 'step_menu_chap1', 'chap1_step3_link');
	}
	if (posY > 1500 && posY < 2300) {
		changeStep('', '', 'step_menu_chap1', 'chap1_step4_link');
	}
	if (posY > 2300 && posY < 2500) {
		changeStep('', '', 'step_menu_chap1', 'chap1_step5_link');
	}
	if (posY > 2500 && posY < 4500) {
		changeStep('', '', 'step_menu_chap1', 'chap1_step6_link');
	}
	if (posY > 4500 && posY < 5500) {
		changeStep('', '', 'step_menu_chap1', 'chap1_step7_link');
	} 
	if (posY > 5500) {
		changeStep('', '', 'step_menu_chap1', 'chap1_step8_link');	
	}
}
function onScrollHandler2(divid) {
	divElm = document.getElementById(divid);
	var posY = divElm.scrollTop;
	if (posY < 700) {
		changeStep('', '', 'step_menu_chap2', 'chap2_step1_link');
	}
	if (posY > 700 && posY < 1000) {
		changeStep('', '', 'step_menu_chap2', 'chap2_step2_link');
	}
	if (posY > 1000 && posY < 1080) {
		changeStep('', '', 'step_menu_chap2', 'chap2_step3_link');
	}
	if (posY > 1080 && posY < 1310) {
		changeStep('', '', 'step_menu_chap2', 'chap2_step4_link');
	}
	if (posY > 1310 && posY < 1800) {
		changeStep('', '', 'step_menu_chap2', 'chap2_step5_link');
	}
	if (posY > 1800 && posY < 2050) {
		changeStep('', '', 'step_menu_chap2', 'chap2_step6_link');
	}
	if (posY > 2050 && posY < 2550) {
		changeStep('', '', 'step_menu_chap2', 'chap2_step7_link');
	}
	if (posY > 2550) {
		changeStep('', '', 'step_menu_chap2', 'chap2_step8_link');
	}
}

function onScrollHandler3(divid) {
	divElm = document.getElementById(divid);
	var posY = divElm.scrollTop;
	
	if (posY < 120) {
		changeStep('', '', 'step_menu_chap3', 'chap3_step1_link');
	}
	if (posY > 120 && posY < 290) {
		changeStep('', '', 'step_menu_chap3', 'chap3_step2_link');
	}
	if (posY > 290) {
		changeStep('', '', 'step_menu_chap3', 'chap3_step3_link');
	}
}
function changeMenu () {
	var URL = unescape(window.location.href);
	var xstart = URL.lastIndexOf("/") + 1;

	var xend = URL.lastIndexOf(".php");
	var hereName = URL.substring(xstart, xend);
	if (xend == -1) {
		hereName = "home";
	}
	

	switch (hereName) {
		case "index":
			hereName = "home";
			break;
		case "build_house":
			hereName = "buildhouse";
			break;
		case "house_sample": case "house_sample_detail": 
			hereName = "housesample";
			break;
		case "holcim_top_standard": case "page2": case "page3": case "page4":
			hereName = "cimentholcim";
			break;
		case "showlist":
			hereName = "news";
			break;
		case "nhansach":
			hereName = "camnang";
			break;
		case "thaoluan":case "qna":
			hereName = "qna";
			break;
	}
	
	var divElm = document.getElementsByTagName("img");

	for (i=0; i<divElm.length; i++) {
		if (divElm[i].id == hereName) {
			divElm[i].onmouseover = null;
			divElm[i].onmouseout = null;
			source = "images/"+hereName+"_active.jpg";
			divElm[i].src = source;
		} else {
			if(divElm[i].id) {
				divElm[i].src = "images/"+divElm[i].id+".jpg";
			}
		}
	}
}

function onMouseOver(id, source){
	document.getElementById(id).src = source;
}
function onMouseOut(id, source){
	document.getElementById(id).src = source;
}
function printPage(){
	var divElm = document.getElementById('chap_menu').getElementsByTagName("td");
	for(i=0; i<divElm.length; i++){
		if(divElm[i].className == "bh_navigation_hover"){
			mid = divElm[i].id;
		}
	}
	page = "";
	switch (mid) {
		case "chap1_link":
		page = "lapkehoach.html";
		break;
		case "chap2_link":
		page = "chonvatlieu.html";
		break;
		case "chap3_link":
		page = "tienhanhxaynha.html";
		break;
		case "chap4_link":
		page = "nghiemthu.html";
		break;
	}
	showPopup(page, 974, 768);
}
function showPopup(source, width, height){
	height = 768;
	myWindow = window.open(source, "mywindow", "status=0, menubar=0, resizable=0, width="+width+", height="+height+", scrollbars=1");
	xPos = (screen.width-width)/2;
	yPos  = screen.height/4
	myWindow.moveTo(xPos, yPos);
	//myWindow.print();
}
function confirmInformation (uid) {
	alert("Cảm ơn bạn đã đăng ký nhận sách. Vui lòng kiểm tra lại địa chỉ của bạn để nhận sách.");
	createPopupInfo(uid, 500, 200);
}
function hienthiPopup(source, width, height){
	div = document.createElement("div");
	text = "<form id=\"confirmLoginDownload\"><table background=\"images/housesample_bg.jpg\" width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\" bgcolor=\"#ffffff\" style=\"border: 1px solid #DDDCDC; \">"+
			"<tr>"+
		   " <td colspan=\"2\" align=\"right\" class=\"search_header\" ><a onclick=\"removeObject('showPopup');\" style=\"cursor: pointer;\">X</a></td>"+
		  "</tr>"+
		  "<tr>"+
		   " <td colspan=\"2\" align=\"center\" class=\"search_header\">Thông tin của bạn </td>"+
		  "</tr>"+
		  "<tr>"+
			"<td width=\"160\" align=\"right\" class=\"label\">Tên đăng nhập: </td>"+
			"<td width=\"320\"><label> <input name=\"txtEmail\" type=\"text\" class=\"textfield\" id=\"txtEmail\" value=\"\" />"+
			"</label></td>"+
		  "</tr>"+
		 " <tr>"+
			"<td width=\"160\" align=\"right\" class=\"label\">Mật khẩu: </td>"+
			"<td width=\"320\"><label><input name=\"txtPassword\" type=\"password\" class=\"textfield\" id=\"txtPassword\" value=\"\" />"+
			"</label></td>"+
		 " </tr>"+
		"  <tr>"+
			"<td align=\"right\" class=\"label\">&nbsp;</td>"+
			"<td><input name=\"btnYes\" type=\"button\" class=\"hs_button\" id=\"btnYes\" value=\"Đăng nhập\" onclick=\"SigninHandler('confirmLoginDownload');\" />"+
			"&nbsp;"+
			  "<input name=\"btnNo\" type=\"reset\" class=\"hs_button\" id=\"btnNo\" value=\"Huỷ bỏ\" />"+
		  " </td>"+
		 " </tr>"+
		 "<tr>"+
		   " <td colspan=\"2\" align=\"center\" class=\"forgotpass\"><a href='signin.php' class=\"forgotpass\">Đăng ký</a> tài khoản mới</td>"+
		  "</tr>"+
		  "<tr>"+
		   " <td colspan=\"2\" align=\"center\" class=\"search_header\">&nbsp;</td>"+
		  "</tr>"+
		"</table></form>";
		
	div.innerHTML = text;
	div.id = "showPopup";
	xPos = (screen.width-width)/2;
	yPos  = screen.height/2;
	div.style.left = xPos+"px";
	div.style.top = yPos+"px";
	div.style.width = width+"px";
	document.getElementsByTagName("body")[0].appendChild(div);
}
function removeObject(id){
	Element.remove(id);
}
function createPopupInfo(uid, width, height){
	myWindow = window.open("confirmInfo.php?uid="+uid+"", "mywindow", "status=0, menubar=0, resizable=0, width="+width+", height="+height+", scrollbars=1");
	xPos = (screen.width-width)/2;
	yPos  = screen.height/4
	myWindow.moveTo(xPos, yPos);
}
function changePassword(){
	var frmElm = document.getElementById('frmChangePass');
	error = "";
	if (frmElm.txtOldPass.value == "") {
		error += "\nNhập mật khẩu của bạn";
	} if (frmElm.txtNewPass.value == "") {
		error += "\nNhập mật khẩu mới của bạn";
	} if ((frmElm.txtConfirmNew.value) != (frmElm.txtNewPass.value)) {
		error += "\nMật khẩu mới không trùng nhau";	
	}
	if (error == "") {
		frmElm.submit();
	} else {
		alert("Thông tin của bạn không hợp lệ. Hãy thử lại"+error);
	}
	
}
function showPopupImage(source, width, height){
	height = 600;
	myWindow = window.open(source, "mywindow", "status=0, menubar=0, resizable=1, width="+width+", height="+height+", scrollbars=1");
	xPos = (screen.width-width)/2;
	yPos  = screen.height/4
	myWindow.moveTo(xPos, yPos);
	//myWindow.print();
}
function showPropertiesImage(uid, id, image, real){
	imgElm = document.getElementById("gallery").getElementsByTagName("img");
	for (i=0; i<imgElm.length; i++) {
		if (imgElm[i].id == id) {
			imgElm[i].className = "hs_image_active";
		} else {
			imgElm[i].className = "hs_image";
		}
	}
	if (uid == "") {
		realLink = "alert('Bạn đăng nhập để xem hình thật.'); window.location = 'login.php'";
		downLink = "javascript: alert('Bạn đăng nhập để tải hình này về máy.'); javascript: window.location = 'login.php'";
		target = "_self";
	} else {
		realLink = "showPopupImage('popUpImage.php?img="+real+"&type=real', 800, 600)";
		downLink = real;
		target = "_blank";
	}
	//text = "<div style=\"padding-bottom: 5px;\"><img src = \"images/hinhnho.png\" align=\"middle\" /> <a onclick=\"showPopupImage('popUpImage.php?img="+image+"&type=big', 800, 600)\" class=\"bh_menu\" style=\"text-decoration:underline;\">Xem hình lớn</a></div>";
	text = "<div style=\"padding-bottom: 5px;\"><img src = \"images/hinhlon.png\" align=\"middle\" /> <a class=\"bh_menu\" style=\"text-decoration:underline;\" onclick=\""+realLink+"\">Xem hình thật</a></div>";
	text += "<div style=\"padding-bottom: 5px;\"><img src = \"images/taive.png\" align=\"middle\" /> <a target=\""+target+"\" href=\""+downLink+"\" class=\"bh_menu\" style=\"text-decoration:underline;\">Tải về</a></div>";
	text += "<div style=\"padding-bottom: 5px;\" class=\"news_content\"><em>Bạn click phải chuột chọn Save Target As để tải về.</em></div>";
	document.getElementById("functionImage").innerHTML = text;
	
}
