/*
	Holcim script file
	@author khanh pham
*/

/*
	get ServerPath
	return path_url 
*/
function getCurrentServerPath(){
	var URL = unescape(window.location.href);	// get current URL in plain ASCII
	var xstart = URL.lastIndexOf("/") + 1;

	var xend = URL.length
	//var hereName = URL.substring(xstart,xend)
	var herePath = URL.substring(0, xstart); // server path
	//return herePath;
	//return "/holcim/coding/";
	return "/web/";
}
/*
	ajax Login Handler
*/
//show_div: vung hien thi.
//param: thong so truyen vao.
function callAjaxHandler(url_path, show_div, param){
	//document.getElementById(show_div).innerHTML = document.getElementById("LoadingDiv").innerHTML;	
	var serverPath = getCurrentServerPath();
	url = serverPath+url_path;
	new Ajax.Updater(show_div, url, {
	evalScripts: true,
	method: 'post',
	parameters: param
	});
}
function setText(id, value, defaultValue){
	if (value == defaultValue){
		document.getElementById(id).value = "";
	} if( value == "" ) {
		document.getElementById(id).value = defaultValue;
	}
}

/*
	common check isBlank, isEmail, isNum ...
*/
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 isStrLenMin(str, lmin) {
	if(str.length >= lmin) return true;
	return false;
}
function isStrLenMax(str, lmax) {
	if(str.length <= lmax) 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 isNumeric(str){
	if( isNotBlank(str) ){
		if( !isNaN(str) ){
			return true
		} else {
			return false;
		}
	}
	return false;
}
function checkRegisStep1(){
	email   	= document.getElementById("registersEmail").value;
	//reEmail 	= document.getElementById("registersRetypeEmail").value;
	password 	= document.getElementById("registersPassword").value;
	rePassword	= document.getElementById("registersRetypePassword").value;
	//$accept		= document.getElementById("chkAccept").value;
	mess 		= "";
	if (!isEmail(email)){
		mess += "\nEmail của bạn không hợp lệ";
	} 
	// if (reEmail != email){
		// mess += "\nEmail của bạn không trùng nhau";
	// } 
	if (!isStrLenMin(password, 6)) {
		mess += "\nMật khẩu phải hơn 6 ký tự";
	} if (rePassword != password){
		mess += "\nMật khẩu của bạn không trùng nhau";
	}
//	if ($accept == 0){
//		mess += "\nBạn phải chấp nhận quy định của cungxaytoam.com.vn";
//	} 
	if (mess == ""){
		return true;
	} else {
		alert("Bạn thiếu những thông tin sau:"+mess);
		return false;
	}	
}
function checkRegisStep2(){
	$fullName 		= document.getElementById("fullName").value;
	//$nickName = document.getElementById("userName").value;
	//$telephone  	= document.getElementById("telephone").value;
	//$answer   	= document.getElementById("answer").value;
	//$capcha			= document.getElementById("recaptcha_response_field").value;
	$capcha			= document.getElementById("txtVercode").value;
	$mess	 	= "";
	 if (!isNotBlank($fullName)){
		  $mess += "\nNhập họ tên của bạn";
	 } //if (!isNotBlank($nickName)){
		//$mess += "\nNhập tên hiển thị của bạn";
	//} 
	// if (!isNumeric($telephone)){
		// $mess += "\nSố điện thoại phải là kiểu số";
	// }
	if (!isNotBlank($capcha)){
		$mess += "\nNhập mã xác nhận bên dưới";
	}
	if($mess == ""){
		return true;
	} else {
		alert("Bạn thiếu những thông tin sau:"+$mess);
		return false;
	}
}
function checkRegisStep3(){
	comName		=	document.getElementById("comName").value;
	comAdd		=	document.getElementById("comAdd").value;
	comPhone	=	document.getElementById("comPhone").value;
	//website		=	document.getElementById("website").value;
	jobtitle	=	document.getElementById("jobtitle").value;
	mess		=	"";
	if (!isNotBlank(comName)){
		mess	+=	"\n Nhập tên công ty";
	} if (!isNotBlank(comAdd)){
		mess	+=	"\n Nhập địa chỉ công ty";
	} if (!isNotBlank(comPhone)){
		mess	+=	"\n Nhập số điện thoại công ty";
	} /* if (!isNotBlank(website)){
		mess	+=	"\n Nhập website công ty";
	} */if (!isNotBlank(jobtitle)){
		mess	+=	"\n Nhập chuyên môn, chức vụ của bạn";
	} 
	if (mess == ""){
		return true;
	} else {
		alert("Bạn thiếu những thông tin sau:"+mess);
		return false;
	}
}
function checkExistEmail(value){
	if ( isNotBlank(value) ){
		url_path = "registers/checkExitsEmail/"+value;
		param = url_path;
		show_div = "processHandler";
		//alert(param);
		callAjaxHandler(url_path, show_div, param);
	}
}

function swapImage(id, source){
	document.getElementById(id).src = source;
}
function changeImageSource(fObject, tObject){
        document.getElementById(tObject).value = document.getElementById(fObject).value;
    }

function hideObject(id){
	   document.getElementById(id).style.display = "none";
}
function showObject(id){
    document.getElementById(id).style.display = "block";
}