﻿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 messkeyword = "Nhập vào nội dung tìm kiếm";



function checkChangePassword(f) {
	oldPassword = f.oldpassword.value;
	if(oldPassword == ''){
		alert('Vui lòng nhập mật khẩu củ');
		f.oldpassword.focus();
		return false;
	}
	newPassword = f.newpassword.value;
	if(newPassword == '') {
		alert('Vui lòng nhập mật khẩu mới');
		f.newpassword.focus();
		return false;
	}
	newPassword1 = f.newpassword1.value;
	if (newPassword1 != newPassword) {
		alert('Nhập lại mật khẩu mới không đúng');
		f.newpassword1.focus();
		return false;
	}
}

function checkForm(f) {
	var email = f.email.value;
	var err=''
	if (Trim(email) == ''){
		err += messEmailInvalid;
		alert(err);
		f.email.focus();
		return false;
	}
	if (email.indexOf('@') <= 0 || email.indexOf('@') == email.length -1){
		err += messEmailInvalid;
	} else {
		var substr = email.substring(email.indexOf('@'),email.length);
		if (email.indexOf('.') <= 0) {
			err += messEmailInvalid;
		}
	}
	if (err != '') {
		alert(err);
		f.email.focus();
		return false;
	}
	var pass = f.password.value;
	if (Trim(pass) == '') {
		alert(messPassword);
		f.password.focus();
		return false;
	}
	return true;
}

function checkTextBoxkeyword (str) {
	var textVal = str.value;	
	if (Trim(textVal) == '') {
		alert(messkeyword);
		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 checkPostForm(f) {
	if (f.sub_cat_id  != undefined) {
		subCatId = f.sub_cat_id.value;
		if (subCatId == 0) {
			alert('Vui lòng chọn mục tin');
			f.sub_cat_id.focus();
			return false;
		}
	}
	if(f.sub_cat_id_mobile != undefined  ) {
		subCatIdMobile = f.sub_cat_id_mobile.value;
		if(subCatIdMobile == 0){
			alert('Vui lòng chọn mục tin');
			f.sub_cat_id_mobile.focus();
			return false;
		}
	}	
	var title = f.subject.value;
	if (Trim(title) == "") {
		alert('Vui lòng nhập vào tiêu đề');
		f.subject.focus();
		return false;
	}
	var content = f.content.value;
	if (Trim(content) == "") {
		alert('Vui lòng nhập nội dung');
		f.content.focus();
		return false;
	}
	var sort = f.sort.value;
	if (sort == "0") {
		alert('Vui lòng chọn loại tin');
		f.sort.focus();
		return false;
	}
	var expire = f.expiredate.value;
	if(expire == "0") {
		alert('Vui lòng chọn số ngày đăng tin');
		f.expiredate.focus();
		return false;
	}
	var whereId = f.txt_where_id.value;
	if (whereId == "0") {
		alert('Vui lòng chọn nơi đăng');
		f.txt_where_id.focus();
		return false;
	}

	isFile = isPicture(f.userfile1);
	if (!isFile) {
		return false;
	}
	isFile = isPicture(f.userfile2);
	if (!isFile) {
		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));
}


