

function email_check(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		 
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    
		    return false
		 }

 		 return true					
	}
function check_post_news(f) {
	
	var title = f.title.value;
	if (Trim(title) == "") {
		alert('Bạn hãy nhập tiêu đề bản tin!');
		f.title.focus();
		return false;
	}
	var inf = f.inf.value;
	if (Trim(inf) == "") {
		alert('Bạn hãy nhập nội dung tin!');
		f.inf.focus();
		return false;
	}
	
	category = f.category.value;
	if(category == '') {
		alert('Bạn hãy chọn danh mục !');
		f.category.focus();
		return false;
	}
	
	user_fullname = f.user_fullname.value;
	if(user_fullname == '') {
		alert('Bạn hãy nhập thông tin liên hệ !');
		f.user_fullname.focus();
		return false;
	}
	
	
	var user_email = f.user_email.value;
	if (Trim(user_email) == "") {
		alert('Bạn hãy nhập địa chỉ email của bạn!');
		f.user_email.focus();
		return false;
	}
	var user_email = f.user_email.value;
	if (!email_check(user_email))
	{
		alert("Bạn nhập sai kiểu Email. Hãy nhập lại !");
		f.user_email.focus();
		return false;
	}
	
	return true;
}
function check_user_fogot(f) {
	
	var email = f.email.value;
	if (Trim(email) == "") {
		alert('Bạn hãy nhập địa chỉ email của bạn!');
		f.email.focus();
		return false;
	}
	var email = f.email.value;
	if (!email_check(email))
	{
		alert("Bạn nhập sai kiểu Email. Hãy nhập lại !");
		f.email.focus();
		return false;
	}

	
	return true;
}

function check_user_login(f) {
	
	var user_email = f.user_email.value;
	if (Trim(user_email) == "") {
		alert('Bạn hãy nhập địa chỉ email của bạn!');
		f.user_email.focus();
		return false;
	}
	var user_email = f.user_email.value;
	if (!email_check(user_email))
	{
		alert("Bạn nhập sai kiểu Email. Hãy nhập lại !");
		f.user_email.focus();
		return false;
	}

	user_pass = f.user_pass.value;
	if(user_pass == '') {
		alert('Bạn hãy nhập vào mật khẩu');
		f.user_pass.focus();
		return false;
	}
	return true;
}
function check_send_mail_Form(f)
{				
		var email = f.to.value;
		if (Trim(email) == "") {
			alert('Bạn hãy nhập vào địa chỉ email!');
			f.to.focus();
			return false;
		}
		var email = f.to.value;
		if (!email_check(email))
		{
			alert("Bạn nhập sai kiểu Email. Hãy nhập lại !");
			f.to.focus();
			return false;
		}
		var subject = f.title.value;
		if (Trim(subject) == "") {
			alert('Bạn hãy nhập vào tiêu đề liên hệ!');
			f.title.focus();
			return false;
		}
		var message = f.content.value;
		if (Trim(message) == "") {
			alert('Bạn hãy nhập vào nội dung liên hệ !');
			f.content.focus();
			return false;
		}
		return true;
}
function check_user_Form(f) {
	
	var user_email = f.user_email.value;
	if (Trim(user_email) == "") {
		alert('Bạn hãy nhập địa chỉ email của bạn!');
		f.user_email.focus();
		return false;
	}
	var user_email = f.user_email.value;
	if (!email_check(user_email))
	{
		alert("Bạn nhập sai kiểu Email. Hãy nhập lại !");
		f.user_email.focus();
		return false;
	}

	user_pass = f.user_pass.value;
	if(user_pass == '') {
		alert('Bạn hãy nhập vào mật khẩu');
		f.user_pass.focus();
		return false;
	}
	lenght_pass = f.user_pass.length;
	if(lenght_pass <= 6) {
		alert('Bạn hãy nhập vào mật khẩu có số ký tự lớn hơn 6 ký tự');
		f.user_pass.focus();
		return false;
	}
	user_repass = f.user_repass.value;
	if (user_pass != user_repass) {
		alert('Bạn phải nhập xác nhận mật khẩu chưa đúng ! Hãy nhập lại.');
		f.user_repass.focus();
		return false;
	}
	var user_fullname = f.user_fullname.value;
	if (Trim(user_fullname) == "") {
		alert('Bạn hãy nhập vào họ tên đầy đủ!');
		f.user_fullname.focus();
		return false;
	}
	var user_address = f.user_address.value;
	if (Trim(user_address) == "") {
		alert('Bạn hãy nhập vào địa chỉ của bạn!');
		f.user_address.focus();
		return false;
	}
	var code = f.code.value;
	if (Trim(code) == "") {
		alert('Bạn hãy nhập vào mã an toàn !');
		f.code.focus();
		return false;
	}
	var code1 = f.code.value;
	var code_hide = f.code_hide.value;
	if (code1!=code_hide) {
		alert('Bạn chưa nhập đúng mã an toàn. Hãy nhập lại!');
		f.code.focus();
		return false;
	}
	

	return true;
}


function showStatus() {
		window.status='© Intervision company';
}
function checkTextBoxSearch (str) {
	var textVal = str.value;	
	if (Trim(textVal) == '') {
		alert(messSearch);
		str.focus();
		return false;
	}
	return true;
}
function checkPassword(str) {
	var textVal = str.value;	
	if (Trim(textVal) == '') {
		alert(messPassword);
		str.focus();
		return false;
	}
	return true;

}
function checkEmail(str,f){
	var textValue = f.str.value;
	var err=''
	if (Trim(textValue) == ''){
		err += messEmailInvalid;
		alert(err);
		str.focus();
		return false
	}
	if (textValue.indexOf('@') <= 0 || textValue.indexOf('@') == textValue.length -1){
		err += messEmailInvalid;		
	} else {
		var substr = textValue.substring(textValue.indexOf('@'),textValue.length);
		if (textValue.indexOf('.') <= 0) {
			err += messEmailInvalid;
		}
	}
	if (err != '') { 
		alert(err); 
		str.focus();
		return false;
	}
	return true;
}

function fnCategories(whereId){
		
	var url = "index.php?";
	var catId = GetPostVariable('cat_id');
	if (catId !=undefined) {
		url = url + "cat_id=" + catId;
	}
	var subCatId = GetPostVariable('sub_cat_id');
	

	if (subCatId !=undefined){
		url = url + "&sub_cat_id=" + subCatId;
	}	

	var isSub = GetPostVariable('is_sub');
	if(isSub !=undefined)  {
		url = url + "&is_sub=" + isSub;
	}

	var title = GetPostVariable('title');
	
	if (title !=undefined) {
		url = url + "&title=" + title;
	}
	
	var titleType = GetPostVariable('title_type');
	
	if (titleType !=undefined) {
		url = url + "&title_type=" + titleType;
	}
	var typePost = GetPostVariable('type_post');
	if (typePost !=undefined) {
		url = url + "&type_post=" + typePost;
	}
	
	url = url + "&where_id="+ whereId;
	
	document.location = url;
}	

/* IE and NS6+ */
/* begin no click right */
function clickIE() {
	if (document.all) {
		return false;
	}
}
function clickNS(e) {
	if (document.layers||(document.getElementById&&!document.all)) {
		if (e.which==2||e.which==3) {
			return false;
		}
	}
}

/*****************************/

function checkVote(f,num) {
	var len = f.vote.length;
	var checked = false;
	w = 500;
	h = 340;
	var left = (screen.availWidth/2) - (w/2);
	var top = (screen.availHeight/2) - (h/2);

	if(num == "1") {
		window.open('votes.php','vote','width='+w+',height='+h+',scrollbars=0,status=0, left='+left+', top='+top)
		return true;
		
	} else {
		for (var i = 0; i< len; i++) {
			if (f.vote[i].checked==true){
				window.open('votes.php?vote=' + f.vote[i].value,'vote','width='+w+',height='+h+',left='+left+',top='+top);
				return true;
			}
		}
	}
	alert("Xin hãy chọn mục tin mà bạn quan tâm nhất!");
	f.vote.focus;
	return false;
}

function isPicture(userfile) {
	if (userfile !=undefined) {
		var file = userfile.value;		
		if (file != '') {
			var len = file.length;
			k= file.indexOf('.');
			str = file.substring(k + 1,len) ;
			result = false;
			switch (str) {
				case "jpg":
					result = true;
					break;
				case "gif":
					result = true;
					break;
				case "png":
					result = true;
					break;
			}
			if (!result) {
				alert('Bạn chỉ có thể đăng với các tập tin: *.jpg,*.gif,*.png');
				return false;
			}
		}
	}
	return true;
}
	
	function checkPostForm(f) {				
		if (f.cateId  != undefined) {
			var subCatId = f.cateId.value;
			if (subCatId == 0) {
				alert('Xin bạn chọn mục tin');
				f.cateId.focus();
				return false;
			}
		}	
		var title = f.txtTittle.value;
		if (Trim(title) == "") {
			alert('Xin hãy nhập vào tiêu đề!');
			f.txtTittle.focus();
			return false;
		}
		var expire = f.txtStoDate.value;
		if(expire == "0") {
			alert('Xin hãy chọn số ngày lưu tin!');
			f.txtStoDate.focus();
			return false;
		}
		var body = f.txtContent.value;
		if (Trim(body) == "") {
			alert(messBody);
			f.txtContent.focus();
			return false;
		}
		
		if (!isPicture(f.userfile)) {
			return false;
		}	
		var whereId = f.txtCity.value;
		if (whereId == "0") {
			alert('Xin hãy chọn nơi rao!');
			f.txtCity.focus();
			return false;
		}
		return true;
	}
// Check Form Tin bat dong san

function checknewsForm(f) {				
		
		var title = f.title.value;
		if (Trim(title) == "") {
			alert('Xin hãy nhập vào tiêu đề!');
			f.title.focus();
			return false;
		}
		var body = f.brief.value;
		if (Trim(body) == "") {
			alert('Xin hãy nhập vào mô tả ngắn!');
			f.brief.focus();
			return false;
		}
		var body = f.content.value;
		if (Trim(body) == "") {
			alert('Xin hãy nhập vào mô tả ngắn!');
			f.content.focus();
			return false;
		}
		
		
		return true;
}
// Check Form Dich vu

function checkservisesForm(f) {				
		
		var title = f.title.value;
		if (Trim(title) == "") {
			alert('Xin hãy nhập vào tiêu đề!');
			f.title.focus();
			return false;
		}
		var brief = f.brief.value;
		if (Trim(brief) == "") {
			alert('Xin hãy nhập vào mô tả ngắn!');
			f.brief.focus();
			return false;
		}
		
		return true;
	}
// Check Form Dich vu

function checklinkForm(f) {				
		
		var title = f.title.value;
		if (Trim(title) == "") {
			alert('Xin hãy nhập vào liên kết !');
			f.title.focus();
			return false;
		}
		var body = f.content.value;
		if (Trim(body) == "") {
			alert('Vui lòng nhập tên website liên kết !');
			f.content.focus();
			return false;
		}
		
		return true;
	}
// Check Form Loai nha

function check_category_Form(f) {				
		
		var title = f.title.value;
		if (Trim(title) == "") {
			alert('Xin hãy nhập vào tiêu đề danh mục !');
			f.title.focus();
			return false;
		}
		
		
		return true;
	}
// Check Form thay doi mat khau

function check_change_pass_Form(f) {				
		
		var oldpassword = f.oldpassword.value;
		if (Trim(oldpassword) == "") {
			alert('Xin hãy nhập vào mật khẩu đang sử dụng !');
			f.oldpassword.focus();
			return false;
		}
		var newpassword = f.newpassword.value;
		if (Trim(newpassword) == "") {
			alert('Xin hãy nhập vào mật khẩu mới !');
			f.newpassword.focus();
			return false;
		}
		var retypepassword = f.retypepassword.value;
		if (Trim(retypepassword) == "") {
			alert('Xin hãy nhập vào mật khẩu xác nhận !');
			f.retypepassword.focus();
			return false;
		}
		//var newpassword = f.newpassword;
		//var retypepassword = f.retypepassword;
		if (newpassword != retypepassword) {
			alert('Bạn đã nhập sai xác nhận mật khẩu hãy kiểm tra lại !');
			f.retypepassword.focus();
			return false;
		}
		return true;
	}
	// check form house
		function checkPost_HouseForm(f) {				
		
		var select_category = f.select_category.value;
		if (Trim(select_category) == "") {
			alert('Xin hãy chọn loại nhà !');
			f.select_category.focus();
			return false;
		}
		var title = f.title.value;
		if(Trim(title)== "") {
			alert('Xin hãy nhập vào tên nhà !');
			f.title.focus();
			return false;
		}
		var select_zone = f.select_zone.value;
		if(Trim(select_zone)== "") {
			alert('Xin hãy chọn vị trí !');
			f.select_zone.focus();
			return false;
		}
		var bedroom = f.bedroom.value;
		if(Trim(bedroom)== "") {
			alert('Xin hãy nhập vào số phòng ngủ!');
			f.bedroom.focus();
			return false;
		}
		var bathroom = f.bathroom.value;
		if(Trim(bathroom)== "") {
			alert('Xin hãy nhập vào số phòng tắm !');
			f.bathroom.focus();
			return false;
		}
		var size = f.size.value;
		if(Trim(size)== "") {
			alert('Xin hãy nhập vào diện tích ngôi nhà !');
			f.size.focus();
			return false;
		}
		
		var body = f.brief.value;
		if (Trim(body) == "") {
			alert("Bạn phải nhập vào mô tả ");
			f.brief.focus();
			return false;
		}
		
		
		return true;
	}
/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
PURPOSE: Remove leading blanks from our string.
IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

function checkMailMess(f) {
	var body = f.txt_mail_mess.value;
	if (Trim(body) == '') {
		alert(messBody);
		f.txt_mail_mess.focus();
		return false;
	}
	return true;
}

function popup(p,w,h){
	var left = (screen.availWidth/2) - (w/2);
	var top = (screen.availHeight/2) - (h/2);
	window.open(p,'Open','width='+w+',height='+h+',scrollbars=0,status=0, left='+left+', top='+top)
}
function popup1(p,w,h){
	var left = (screen.availWidth/2) - (w/2);
	var top = (screen.availHeight/2) - (h/2);
	window.open(p,'Open','width='+w+',height='+h+',scrollbars=1,status=0, left='+left+', top='+top)
}
function checkMemberInfo(f){
	fullName = f.full_name.value;
	if(Trim(fullName) =='') {
		alert(messFullName );
		f.full_name.focus();
		return false;
	}
	return true;
}


function subCatChange(i,n,id,sessId) {
	postStr = '';
	if (n == 1){
		p = 4;
	}else if(n==2) {
		p = 10;
		postStr = '&post_id=' + id;
	}	

	deleteAnItem();	
	if (i != "0") {
		lenSub = eval("len" + i);
		for ( var j = 0; j < lenSub; j++){
			eval("document.frm.sub_cat_id_mobile.options[j]=arrCat" + i + "_" +  j);
		}
	}	
}
	
function deleteAnItem() {   	
	for (var i = 0; i <document.frm.sub_cat_id_mobile.length; i++){		
		document.frm.sub_cat_id_mobile.options[i]=null 
	}

	document.frm.sub_cat_id_mobile.length=0
		
}
function clickHelpSearch() {
	mess = '-------------------- HƯỚNG DẪN TÌM KIẾM --------------------\n\nVí dụ tìm từ = Nokia 8210\n+ Bạn cần gõ: Nokia 8210\n+ Xem chi tiết tìm kiếm click vào "Hỏi đáp" từ menu chính';
	alert(mess);
}

