var postExistJavaScript = ' » Tin của bạn đã đăng rồi\n » Không đăng liên tiếp cùng một nội dung\n » Bạn có thể đăng lặp lại bản tin này sau 15 phút\n » Nếu vi nhiều lần tất cả tin đăng sẽ bi xóa';
var postOk = "Tin của bạn đã được đăng";
var postNotOk = " » Đăng tin xảy ra lỗi\n » Xem hướng dẫn và quy định khi đăng tin";
var messFullName = "Vui lòng nhập họ và tên";
var messEmail = "Vui lòng nhập địa chỉ email";
var messBody = "Vui lòng nhập nội dung";
var messFullNameSender = "Vui lòng nhập tên người gởi";
var messEmailSender = "Vui lòng nhập email người gởi";
var messFullNameReceiver = "Vui lòng nhập tên người nhận";
var messEmailReceiver = "Vui lòng nhập email người nhận";
var messTelephone = "Vui lòng nhập số điện thoại";
var messSex = "Vui lòng chọn giới tính";
var messPassword = "Vui lòng nhập mật khẩu";
var messEmailInvalid = "Email không hợp lệ";
var messSearch = "Nhập vào nội dung tìm kiếm";


if(document.images)	{
	on = new Image;
	off = new Image;
	sel = new Image;
	on.src = 'images/imgBulletRedNew1.gif';
	off.src='images/imgBulletBlackNewNew1.gif';
	sel.src='images/imgBulletBlue.gif';
	
	smallOn = new Image;
	smallOff = new Image;
	
	smallOn.src ='images/imgBulletRedNew1.gif';
	smallOff.src ='images/imgBulletBlackNewNew1.gif';
	
	memOn = new Image;
	memOff = new Image;
	
	memOn.src = 'images/imgBulletRedNew1.gif';
	memOff.src = 'images/imgBulletBlackNewNew1.gif'; 
	
	folderOn = new Image;
	folderOff = new Image;
	folderOn.src = "images/imgFolderOpen1.gif";
	folderOff.src = "images/imgFolderOpen.gif";
	
}
    /*****************************************/
    function GetPostVariable(vName, vDef){
	var	str=location.href;
	var	pos=str.indexOf('?'.concat(vName).concat('='));

	if (pos==-1)
	{
		pos=str.indexOf('&'.concat(vName).concat('='));
		if (pos==-1) return vDef;
	}

	str=str.substring(pos + vName.length + 2);
	pos=str.indexOf('&');

	if (pos==-1)
	{
		pos=str.length;
	}

	if (pos > 0){
		str=str.substring(0, pos);
	}

	return str;
	}

    /*****************************************/

function checkChangePassword(f) {
	oldPassword = f.old_password.value;
	if(oldPassword == ''){
		alert('Vui lòng nhập mật khẩu củ');
		f.old_password.focus();
		return false;
	}
	newPassword1 = f.new_password1.value;
	if(newPassword1 == '') {
		alert('Vui lòng nhập mật khẩu mới');
		f.new_password1.focus();
		return false;
	}
	newPassword2 = f.new_password2.value;
	if (newPassword2 != newPassword1) {
		alert('Nhập lại mật khẩu mới không đúng');
		f.new_password2.focus();
		return false;
	}
}

function checkForm(f) {
	var user_email = f.user_email.value;
	var err=''
	if (Trim(user_email) == ''){
		err += messEmailInvalid;
		alert(err);
		f.user_email.focus();
		return false;
	}
	if (user_email.indexOf('@') <= 0 || user_email.indexOf('@') == user_email.length -1){
		err += messEmailInvalid;
	} else {
		var substr = user_email.substring(user_email.indexOf('@'),user_email.length);
		if (user_email.indexOf('.') <= 0) {
			err += messEmailInvalid;
		}
	}
	if (err != '') {
		alert(err);
		f.user_email.focus();
		return false;
	}
	var pass = f.user_password.value;
	if (Trim(pass) == '') {
		alert(messPassword);
		f.user_password.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){
	var textValue = 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 "bmp":
					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,*.bmp');
				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;
		}
		
		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;
	}
/*
==================================================================
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ừ = Nhà mới\n+ Bạn cần gõ: Nhà mới\n+ Tiếp đó bạn chọn "Mục" và "Địa điểm"';
	alert(mess);
}


