function checkLogin(f){
	check = checkEmail(f.email);
	if (check == false) {
		return false;
	}
	pass  = f.user_password.value;
	if (pass == "") {
		alert('Vui lòng nhập vào mật khẩu');	
		f.user_password.focus();
		return false;
	}
	return true;

}

function alphaNumeric(alphane){
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || alphaa =="-")
		  {
		  }
		else	{
			 return false;
		  }
		}
 	return true;
}
function checkEmpty(f,mess) {
	str = f.value;
	if(Trim(str) ==""){
		alert(mess);
		f.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 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 checkEmail(str){
	var textValue = str.value;
	var err=''
	if (textValue == ''){
		err += " Địa chỉ email không hợp lệ ";
		alert(err);
		str.focus();
		return false
	}
	if (textValue.indexOf('@') <= 0 || textValue.indexOf('@') == textValue.length -1){
		err += " Địa chỉ email không hợp lệ ";		
	} else {
		var substr = textValue.substring(textValue.indexOf('@'),textValue.length);
		if (textValue.indexOf('.') <= 0) {
			err += " Địa chỉ email không hợp lệ \n";
		}
	}
	if (err != '') { 
		alert(err); 
		str.focus();
		return false;
	}
	return true;
}

function checkFormRegister(f) {
	
	name = f.txtFullName.value;
	if (Trim(name) == '' ){
		alert('Vui lòng nhập họ tên');
		f.txtFullName.focus();
		return false;
	
	}	
	
	check = checkEmail(f.txtEmail);
	if (check == false) {
		return false;
	}
	txtPassword = f.txtPassword.value;
	if (txtPassword == "") {
		alert('Vui lòng nhập vào mật khẩu');
		f.txtPassword.focus();
		return false;
	}
	
	phone = f.txtTelephone.value;
	if (Trim(phone) == '') {
		alert('Vui lòng nhập số điện thoại');
		f.txtTelephone.focus();
		return false;		
	}
	securityCode = f.security_code.value;
	if(Trim(securityCode)== ''){
		alert('Vui lòng nhập mã an toàn');
		f.security_code.focus();
		return false;		
	}	
}

function checkFormForgotPassword(f) {
	check = checkEmail(f.email);
	if (check == false) {
			return false;
	}
	securityCode = f.security_code.value;
	if(Trim(securityCode)== ''){
		alert('Vui lòng nhập mã an toàn');
		f.security_code.focus();
		return false;		
	}	
	return true;
}

function checkFormPost(f) {

	domain = f.domain_name.value;
	if(Trim(domain) == ""){
		alert('Vui lòng nhập tên miền');
		f.domain_name.focus();
		return false;
	}
	domain = domain.toLowerCase();
	domain = domain.replace('http://','');
	domain = domain.replace('www.','');
		
	arr = domain.split('.');
	len = arr.length;
	if (len > 3 || len == 1) {
		alert('Tên miền này không hợp lệ');
		f.domain_name.focus();
		return false;		
	}
	if (len == 3) {
		domainStr = arr[0] + arr[1] + arr[2];
	} else {
		domainStr = arr[0] + arr[1];
	}
	
	if(!alphaNumeric(domainStr)) {
		alert('Tên miền này không hợp lệ');
		f.domain_name.focus();
		return false;		
	}
		
	price = f.price.value;
	if (Trim(price) == "") {
		alert('Vui lòng nhập giá');
		f.price.focus();
		return false;	
	}
	priceStatus = f.price_status.value; 
	
	if (priceStatus == "0") {
		alert('Vui lòng chọn trạng thái giá');
		f.price_status.focus();
		return false;	
	}

	
	
	catId = f.cat_id.value;
	if (catId== "0") {
		alert('Vui lòng chọn phân loại');
		f.cat_id.focus();
		return false;	
	}
	
	
	body = f.body.value;
	if (Trim(body) == "") {
		alert('Vui lòng nhập vào nội dung');
		f.body.focus();
		return false;	
	}
	
	typeof_web = f.type_of.value;
	if (Trim(typeof_web) == "0") {
		alert('Vui lòng chọn hình thức đấu giá');
		f.type_of.focus();
		return false;	
	}
	
	timeLimit = f.time_limit.value;
	if (timeLimit == "-1"){
		alert('Vui lòng chọn thời gian đấu giá');
		f.time_limit.focus();
		return false;
	}
	
	whereId = f.where_id.value;
	
	if(whereId == "0") {
		alert('Vui lòng chọn nội dung đăng');
		f.where_id.focus();
		return false;
	}

	securityCode = f.security_code.value;
	if(Trim(securityCode)== ''){
		alert('Vui lòng nhập mã an toàn');
		f.security_code.focus();
		return false;		
	}	
	
	return true;
}

function checkSelectAll(f,flag,name) {	
	var len = f.elements.length;
	for (var i = 0; i< len; i++){
		if (f.elements[i].name == 'chk_domain_id[]') {
			f.elements[i].checked = flag;
		}
	}	
	
}

function turnOnOff(f) {
	if (f.clickcontrol.checked==true){
		checkSelectAll(f,true);
	} else {
		checkSelectAll(f,false);

	}
}

function checkPostIdEmpty(f,name,str) {	
	var len = f.elements.length;
	for (var i = 0; i< len; i++){
		if (f.elements[i].name == 'chk_domain_id[]' && f.elements[i].checked==true) {
			f.del_lock.value=str;
			if (str=='delete') {
				return confirm('Bản tin này sẽ bị xóa?');
			} else if(str='lock') {
				return confirm('Bản tin này sẽ bị khóa');
			}
			
			return true;		
		}
	}	
	alert(' Xin vui long chon ban tin');	
	return false;
}

function checkDomainIdEmpty(f,name,str) {	
	var len = f.elements.length;	
	for (var i = 0; i< len; i++){
		if (f.elements[i].name == 'chk_domain_id[]' && f.elements[i].checked==true) {
			return confirm('Bản tin này sẽ bị xóa?');
		}
	}	
	alert('Vui lòng chọn bản tin');	
	return false;
}

function checkChangePassword(f){
	oldPass = f.old_password.value;
	if (oldPass == ""){
		alert('Vui lòng chọn mật khẩu cũ');
		f.old_password.focus();
		return false;
	}
	newPassword = f.new_password.value;
	if(newPassword == ""){
		alert('Vui lòng nhập mật khẩu mới');
		f.new_password.focus();
		return false;
	}
	newPassword1 = f.new_password1.value;
	if (newPassword1 != newPassword){
		alert('Nhập lại mật khẩu không đúng');
		f.new_password1.focus();
		return false;
	}
	return true;
}
function checkDomain(f) {
	domain = f.domain.value;
	if (Trim(domain) == ''){
		alert('Vui lòng nhập vào tên miền');
		f.domain.focus();
		return false;
	}
	return true;
}

function checkContactMember(f) {

	fullName = f.full_name.value;
	if (Trim(fullName) =="") {
		alert('Vui lòng nhập vào Họ tên');
		f.full_name.focus();
		return false;
	}
	telephone = f.telephone.value;
	if (Trim(telephone) == "") {
		alert('Vui lòng nhập số điện thoại');
		f.telephone.focus();
		return false;
	}		
	check = checkEmail(f.email);
	if (check == false) {
		return false;
	}
	
	address = f.address.value;
	if (Trim(address) == "") {
		alert('Vui lòng nhập địa chỉ');
		f.address.focus();
		return false;
	}	
	body = f.body.value;
	if (Trim(body) == "") {
		alert('Vui lòng nhập nội dung');
		f.body.focus();
		return false;		
	}

	securityCode = f.security_code.value;
	if(Trim(securityCode)== ''){
		alert('Vui lòng nhập mã an toàn');
		f.security_code.focus();
		return false;		
	}
	return true;
	
}
function checkContact(f) {
	fullName = f.full_name.value;
	if (Trim(fullName) =="") {
		alert('Vui lòng nhập vào Họ tên');
		f.full_name.focus();
		return false;
	}
	telephone = f.telephone.value;
	if (Trim(telephone) == "") {
		alert('Vui lòng nhập số điện thoại');
		f.telephone.focus();
		return false;
	}		
	check = checkEmail(f.email);
	if (check == false) {
		return false;
	}
	
	address = f.address.value;
	if (Trim(address) == "") {
		alert('Vui lòng nhập địa chỉ');
		f.address.focus();
		return false;
	}	
	body = f.body.value;
	if (Trim(body) == "") {
		alert('Vui lòng nhập nội dung');
		f.body.focus();
		return false;		
	}

	securityCode = f.security_code.value;
	if(Trim(securityCode)== ''){
		alert('Vui lòng nhập mã an toàn');
		f.security_code.focus();
		return false;		
	}
	return true;
	
}
function checkLoginPro(i) {
	if (i == 0) {
		alert('Để đăng dự án vui lòng đăng nhập vào hệ thống');
		return false;
	}
	return true;
}
function checkLoginForum(i,j) {
	if (i == 0) {
		alert('Để tham gia đấu thầu dự án vui lòng login');
		return false;
	} else if(j == 1) {
		alert('Dự án này đã được đóng lại');
		return false;	
	}
	return true;
}


function checkFormPostProject(f) {
	projectName = f.project_name.value;
	if (projectName == "") {
		alert('Vui lòng nhập vào tên dự án');
		f.project_name.focus();		
		return false;
	}	
	catId = f.cat_id.value;
	if (catId == "0") {
		alert('Vui lòng chọn chủng loại');
		f.cat_id.focus();
		return false;
	}
	
	limitDate =	f.limit_date.value;
	
	if (!checkNumber(limitDate)) {
		if (limitDate == "") {
			alert('Vui lòng nhập số ngày');
		} else {
			alert('Nhập ngày không hợp lệ');
		}
		f.limit_date.focus();
		return false;
	}
	
	desc = f.desc.value;
	if (desc == "") {
		alert('Vui lòng nhập vào phần mô tả dự án');
		f.desc.focus();
		return false;
	}	
	securityCode = f.security_code.value;
	if(Trim(securityCode)== ''){
		alert('Vui lòng nhập mã an toàn');
		f.security_code.focus();
		return false;		
	}
	
	
}
function checkPostRely (f) {
	price = f.project_price.value;
	if (price == 0) {
		alert('Vui lòng nhập giá cho dự án này');
		f.project_price.focus();
		return false;
	}	
}

function checkNumber(x){
	var anum=/(^\d+$)|(^\d+\.\d+$)/
	if (anum.test(x)) {
		result=true
	} else{
		//alert("Vui lòng nhập vào số hợp lệ")
		result = false
	}
	return result;
}
function checkFormPostForum(f) {

	price = f.project_price.value;
	
	if (!checkNumber(price)) {
		if (price == "") {
			alert('Vui lòng nhập vào giá cho dự án này');
		} else {
			alert('Giá không hợp lệ');
		}
		
		f.project_price.focus();
		return false;
	}
	
	finishTime = f.finish_time.value;
	if (!checkNumber(finishTime)) {
		if (finishTime == "") {
			alert('Vui lòng nhập thời gian hoàn thành');
		} else {
			alert('Ngày không hợp lệ');
		}
		f.finish_time.focus();
		return false;
	}		
	comment = f.comment.value;
	if (comment == "") {
		alert('Vui lòng nhập vào nội dung');
		f.comment.focus();
	}
	
}

