
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 checkEmail(str){
	var textValue = str.value;
	var err=''
	if (textValue == ''){
		err += "Địa chỉ E-mail không hợp lệ";
		alert(err);
		str.focus();
		return false
	}
	if (textValue.indexOf('@') <= 0 || textValue.indexOf('@') == textValue.length -1){
		err += "Địa chỉ E-mail không hợp lệ ";		
	} else {
		var substr = textValue.substring(textValue.indexOf('@'),textValue.length);
		if (textValue.indexOf('.') <= 0) {
			err += "Địa chỉ E-mail không hợp lệ \n";
		}
	}
	if (err != '') { 
		alert(err); 
		str.focus();
		return false;
	}
	return true;
}

function checkNewletter(f)
{
	if (!checkEmail(f.txtletter)) {
	return false
	}
	return true;
}
/**/
// kiem tra thong tin khach hang lien he
function checkRegister (f) {
	companyName = f.txthoten.value;
	if (Trim(companyName) == "") {
		alert('Vui lòng nhập họ tên');
		f.txthoten.focus();
		return false;
	}
	
	address = f.txtdiachi.value;
	if (Trim(address) == "") {
		alert('Vui lòng nhập địa chỉ liên hệ');
		f.txtdiachi.focus();
		return false;
	}
	if (!checkEmail(f.txtemail)) {
		return false
	}
	
	catId = f.txtyeucau.value;
	if (catId == "") {
		alert('Vui lòng nhập nội dung liên hệ');
		f.txtyeucau.focus();
		return false;
	}
	
	return true;	
}
function checkRegisters (f) {
	
	catId = f.txtyeucau.value;
	if (catId == "") {
		alert('Vui lòng nhập nội dung liên hệ');
		f.txtyeucau.focus();
		return false;
	}
	
	return true;	
}
// kiem tra thong tin khach hang dang ky dat hang
function checkRegisterCart(f)
{
	if (!checkEmail(f.txtemail)) {
		return false
	}
	
	catId = f.txtpass.value;
	if (catId == "") {
		alert('Vui lòng nhập mật khẩu');
		f.txtpass.focus();
		return false;
	}
	
	return true;	
}
// kiem tra thong tin khi thay doi mat khau
function checkChangePass(f)
{
	companyName = f.txtpassold.value;
	if (Trim(companyName) == "") {
		alert('Vui lòng nhập lại mật khẩu cũ');
		f.txtpassold.focus();
		return false;
	}
	
	address = f.txtpassnew.value;
	if (Trim(address) == "") {
		alert('Vui lòng nhập mật khẩu mới');
		f.txtpassnew.focus();
		return false;
	}
	
	catId = f.txtconfirm.value;
	if (catId == "") {
		alert('Vui lòng nhập lại mật khẩu mới');
		f.txtconfirm.focus();
		return false;
	}
	if (address != catId)
	{
		alert('Mật khẩu mới không trùng nhau');
		f.txtpassnew.focus();
		return false;
	}
	return true;
}
// kiem tra thong tin khi tim kiem san pham
function checkSearch(f)
{
	companyName = f.cmbgroup.value;
	if (Trim(companyName) == "0") {
		alert('Vui lòng chọn tên nhóm sản phẩm');
		f.cmbgroup.focus();
		return false;
	}
	catId = f.txtsearch.value;
	if (catId == "") {
		alert('Vui lòng nhập từ khóa tìm');
		f.txtsearch.focus();
		return false;
	}
	
	return true;	
}
// kiem tra thong tin khach hang hieu chinh dia chi lien he
function checkCustomer(f)
{
	companyName = f.txthoten.value;
	if (Trim(companyName) == "") {
		alert('Tên thành viên không được rỗng');
		f.txthoten.focus();
		return false;
	}
	catId = f.txtdiachi.value;
	if (catId == "") {
		alert('Địa chỉ liên lạc không rỗng');
		f.txtdiachi.focus();
		return false;
	}
	catdt= f.txtdt.value;
	if (catdt == "") {
		alert('Điện thoại liên lạc không rỗng');
		f.txtdt.focus();
		return false;
	}
	return true;
}
// // kiem tra thong tin dang nhap thanh vien
function checkLogin(f)
{
	companyName = f.txtusername.value;
	if (Trim(companyName) == "") {
		alert('Vui lòng nhập tên đăng nhập');
		f.txtusername.focus();
		return false;
	}
	catId = f.txtpass.value;
	if (catId == "") {
		alert('Vui lòng nhập mật khẩu');
		f.txtpass.focus();
		return false;
	}
	
	return true;
}
// kiem tra thong tin dat hang neu la khach hang cũ
function checkInfo (f) {
	companyName = f.txtngaygh.value;
	var dt = new Date() ;
  	var dt_year = dt.getFullYear() ;

	var dt_month = dt.getMonth() + 1 ;

	var dt_day = dt.getDate() ;
	if (dt_day < 10)
		dt_day ="0"+dt_day;
	if 	(dt_month < 10)
		dt_month = "0"+dt_month;
  _s = dt_day+"/"+dt_month+"/"+dt_year;
	if (Trim(companyName) == "") {
		alert('Vui lòng chọn ngày giao hàng');
		f.txtngaygh.focus();
		return false;
	}
	
	var date = companyName;
	var arr = date.split('/');
	
	
	if (arr[0] <= dt_day && arr[1] <= dt_month && arr[2] <= dt_year )//1
	{
		alert('Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
		f.txtngaygh.focus();
		return false;
	}
	if (arr[0] > dt_day && arr[1] < dt_month && arr[2] <= dt_year )//1
	{
		alert('Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
		f.txtngaygh.focus();
		return false;
	}
	if (arr[0] >= dt_day && arr[1] >= dt_month && arr[2] < dt_year )//1
	{
		alert('Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
		f.txtngaygh.focus();
		return false;
	}
	/*if (arr[0] >= dt_day && arr[1] >= dt_month && arr[2] < dt_year )//1
	{
		alert('4Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
	}*/
	/*if ((arr[0] < dt_day && arr[1] <= dt_month && arr[2] <= dt_year))//1
	{
		alert('1Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
	}
	if ((arr[0] > dt_day && arr[1] < dt_month && arr[2] <= dt_year ))//2
	{
		alert('2Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
	}
	if ((arr[0] > dt_day && arr[1] >= dt_month && arr[2] < dt_year ))//3
	{
		alert('3Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
	}
	if ((arr[0] > dt_day && arr[1] >= dt_month && arr[2] < dt_year ))//4
	{
		alert('4Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
	}
	if ((arr[0] < dt_day && arr[1] > dt_month && arr[2] < dt_year ))//4
	{
		alert('5Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
	}
	if ((arr[0] < dt_day && arr[1] < dt_month && arr[2] > dt_year ))//4
	{
		alert('6Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
	}*/
	/*if (arr[1] < dt_month )
	{
		alert('Vui lòng chọn ngày giao hàng phải sau ngày hiện tại');
	}*/
	
	var alen = f.elements.length;
	var isChecked = true;
		for (var i = 0 ; i < alen ;  i++)
		{
			if(f.elements[i].type == "checkbox")
			{
				if(f.elements[i].name == "rdoaddcus")
				{
					if (f.elements[i].checked == true)
					{
						
						address = f.txtreceiver.value;
						if (Trim(address) == "") {
						alert('Vui lòng nhập họ tên người nhận');
						f.txtreceiver.focus();
						return false;
						}
						telephone = f.txtdeaddress.value;
						if (Trim(telephone) == "") {
							alert('Vui lòng nhập địa chỉ người nhận');
							f.txtdeaddress.focus();
							return false;
						}
						contactPerson = f.txtdephone.value;
						
						if (Trim(contactPerson) == "") {
							alert('Vui lòng nhập số điện thoại người nhận');
							f.txtdephone.focus();
							return false;
						}
					}
				}
			}
		}
	//if (f.rdoaddcus[0].checked == true)
	//{
		/*address = f.txtreceiver.value;
		if (Trim(address) == "") {
		alert('Vui lòng nhập họ tên người nhận');
		f.txtreceiver.focus();
		return false;
		}
		telephone = f.txtdeaddress.value;
		if (Trim(telephone) == "") {
			alert('Vui lòng nhập địa chỉ người nhận');
			f.txtdeaddress.focus();
			return false;
		}
		contactPerson = f.txtdephone.value;
		
		if (Trim(contactPerson) == "") {
			alert('Vui lòng nhập số điện thoại người nhận');
			f.txtdephone.focus();
			return false;
		}*/
	//}

	return true;	
}
// kiem tra thong tin dat hang neu la khach hang moi
/**/
function checkInfoNew (f)
{
	
	
	companyName = f.txthoten.value;
	if (Trim(companyName) == "") {
		alert('Vui lòng nhập họ tên');
		f.txthoten.focus();
		return false;
	}
	txtemail = f.txtemail.value;
	if (!checkEmail(f.txtemail)) {
		return false
	}
	address = f.txtdiachi.value;
	if (Trim(address) == "") {
		alert('Vui lòng nhập nhập địa chỉ');
		f.txtdiachi.focus();
		return false;
	}
	telephone = f.txtdt.value;
	if (Trim(telephone) == "") {
		alert('Vui lòng nhập số điện thoại');
		f.txtdt.focus();
		return false;
	}
	
	

	return true;	
}

/**/
function checkContact (f)
{
	companyName = f.txtName.value;
	
	if (Trim(companyName) == "") {
		alert('Vui lòng nhập họ tên');
		f.txtName.focus();
		return false;
	}
	txtemail = f.txtEmail.value;
	if (!checkEmail(f.txtEmail)) {
		return false
	}
	address = f.txtaddress.value;
	if (Trim(address) == "") {
		alert('Vui lòng nhập nhập địa chỉ');
		f.txtaddress.focus();
		return false;
	}
	telephone = f.txtOther.value;
	if (Trim(telephone) == "") {
		alert('Vui lòng nhập nội dung liên hệ');
		f.txtOther.focus();
		return false;
	}

	return true;	
}


