function toUnicode(str1)
{
  var code, str2 , j=0;
  var len
  while(j<2)
   {
	len=str1.length
	str2=''
	for(var i=0;i<len;i++) 
	 {
      code=str1.charCodeAt(i);
      if(code<128) continue;
      str2 +=str1.substring(0,i) + '&#' + code + ';'
      str1=str1.substring(i+1,str1.length)
      len=str1.length
      i=0
     }
    str1=str2+str1
    j++;
   }
  return str1;
}

function DateVN()
{
		var now = new Date();
		var name_day = new Array("Chủ nhật","Thứ hai","Thứ ba","Thứ tư","Thứ năm","Thứ sáu","Thứ bảy");
		var name_month = new Array("01","02","03","04","05","06","07","08","09","10","11","12");
		currday = name_day[now.getDay()]+", ";
		currday += now.getDate()+"/" +name_month[now.getMonth()] + "/" + (now.getFullYear());		
		document.write(currday);	
};

function checkstatus(check, frm, n)
{
	if (check.checked)
	{	
		
		if (n == 1)
		{		
			frm.checkbox.checked = true;
		}
		else	
		{	
			for (var i = 0; i < n; i++)				
				frm.checkbox[i].checked = true;	
		}
	}			
	else		
	{	
		if (n == 1)
		{		
			frm.checkbox.checked = false;
		}
		else	
		{	
			for (var i = 0; i < n; i++)
				frm.checkbox[i].checked = false;	
		}
	}	
}

function checked_all(frmcheck)
{	
		frmcheck.checked = true;
}

function unchecked_all(frmcheck)
{	
		frmcheck.checked = false;
}
	
function openWindow(aselect)
{
	if (aselect[aselect.selectedIndex].value != -1)
		window.open("http://www."+aselect[aselect.selectedIndex].value);
}	

function explorer(id,b) 
{
	var i = 0;
	var found = false;
	var len = b.length;
	
	if (len > 1){
		while ((i < len) && !found)
		{
			if ((b[i].style.display == '') && (i != id))
			{			
				b[i].style.display = 'none';
				found = true;
			}
			else i++;
		}
		if (b[id].style.display =='')
			b[id].style.display = 'none';
		else b[id].style.display = '';
	}
	else{
		if (b.style.display =='')
			b.style.display = 'none';
		else b.style.display = '';
	}
}

function isEmpty(str)

{

	if (str != "")

		while (str.charAt(0) == " ")

			str = str.substr(1, str.length);

	return (str == "")? true : false;

}

function isDate(dat)
	{
		var i=dat.indexOf("/");
		var li=dat.lastIndexOf("/");

		var dd=dat.substr(0,i);
		var mm=dat.substring(i+1,li);
		var yy=dat.substr(li+1,4);
		
		if (dd<1 || dd>31) return false;
		if (mm<1 || mm>12) return false;
		if (yy<1900) return false;
		
		if (mm==2)
		{
			if (yy%4==0 && dd>29) return false;
			if (yy%4!=0 && dd>28) return false;
		}
		else
		{
			if (mm<8 && mm%2==0 && dd>30) return false;
			if (mm>7 && mm%2==1 && dd>30) return false;
		}

		return true;
	}


function Trim(str)
{

	if (str != "")

		while (str.charAt(0) == " ")

			str = str.substr(1, str.length);

	return str;

}



function inputNumber(number)

{

	var pattern = "0123456789 -().";

	

	if (len != 0)

	{

		var index = 0;

		var len = number.value.length;

		

		while ((index < len) && (len != 0))

			if (pattern.indexOf(number.value.charAt(index)) == -1)

			{

				if (index == len-1)

					number.value = number.value.substring(0, len-1);

				else if (index == 0)

					 	number.value = number.value.substring(1, len);

					 else number.value = number.value.substring(0, index)+number.value.substring(index+1, len);

				index = 0;

				len = number.value.length;

			}

			else index++;

	}

}



function inputPhoneFax(number)

{

	var pattern = "0123456789 -().";

	

	if (len != 0)

	{

		var index = 0;

		var len = number.value.length;

		

		while ((index < len) && (len != 0))

			if (pattern.indexOf(number.value.charAt(index)) == -1)

			{

				if (index == len-1)

					number.value = number.value.substring(0, len-1);

				else if (index == 0)

					 	number.value = number.value.substring(1, len);

					 else number.value = number.value.substring(0, index)+number.value.substring(index+1, len);

				index = 0;

				len = number.value.length;

			}

			else index++;

	}

}



function inputPrice(number)

{

	var pattern = "0123456789,.";

	

	if (len != 0)

	{

		var index = 0;

		var len = number.value.length;

		

		while ((index < len) && (len != 0))

			if (pattern.indexOf(number.value.charAt(index)) == -1)

			{

				if (index == len-1)

					number.value = number.value.substring(0, len-1);

				else if (index == 0)

					 	number.value = number.value.substring(1, len);

					 else number.value = number.value.substring(0, index)+number.value.substring(index+1, len);

				index = 0;

				len = number.value.length;

			}

			else index++;

	}

}

function doCheck(check, state, n)
{
	if (n == 1)
		check.checked = state;
	else
	{
		for (var i = 0; i < n; i++)
			check[i].checked = state;
	}
}



function isDelete(frm, n)

{

	var flag = false;



	if (n == 1)

		flag = frm.checkbox.checked;

	else

	{	

		var i = 0;		

		

		while ((i < n) && (!flag))

		{

			if (frm.checkbox[i].checked)

				flag = true;

			else i++;	

		}

	}	

	if(!flag)

	{

		alert("Xin vui long chon muc muon xoa!");

		return;

	}

	frm.submit();

}



function isEdit(frm, n)

{

	var flag = false;



	if (n == 1)

		flag = frm.checkbox.checked;

	else

	{	

		var i = 0;		

		

		while ((i < n) && (!flag))

		{

			if (frm.checkbox[i].checked)

				flag = true;

			else i++;	

		}

	}	

	if(!flag)

	{

		alert("Xin vui long chon muc muon sua!");

		return;

	}

	frm.submit();

}



function isEmail(email)

{

	if (email == "")

		return false;

	if (email.indexOf(" ") > 0)	

		return false;

	if (email.indexOf("@") == -1)	

		return false;

	if (email.indexOf(".") == -1)	

		return false;

	if (email.indexOf("..") != -1)	

		return false;

	if (email.indexOf("@") != email.lastIndexOf("@"))		

		return false;

		

	var len = email.length;

	

	if (email.lastIndexOf(".") == len-1)

		return false;

	

	var str = "0123456789abcdefghikjlmnopqrstuvwxyz-@._";

	

	for (var index = 0; index < len; index++)

		if (str.indexOf(email.charAt(index)) == -1)

			return false;

			

	var indexDot = 	email.indexOf(".");

	var indexAcsign = email.indexOf("@");

	

	if ((indexDot == indexAcsign-1) || (indexDot-1 == indexAcsign))

		return false;

					

	return true;

}



function chkConfirm(pwd, confirmpwd)

{

	if (pwd.value == "")

	{

		alert("Please choose a password.");

		pwd.focus();

		return false;

	}

	

	if (pwd.value.length < 6)

	{

		alert("Your new password must be at least 6 character.");

		pwd.focus();

		return false;

	}

	

	if ((confirmpwd.value == "") || (pwd.value != confirmpwd.value))

	{

		alert("Your password entries did not math.");

		confirmpwd.focus();

		return false;

	}	

	return true;

}



function chkcommonInfo(name, address, city, zip, country, phone, fax, email, isUser)

{

	if (isEmpty(name.value))

	{

		alert("Please enter your name!");

		name.focus();

		return false;

	}

	

	if (isEmpty(address.value))

	{

		alert("Please enter address!");

		address.focus();

		return false;

	}

	

	if (isEmpty(city.value))

	{

		alert("Please enter city!");

		city.focus();

		return false;

	}

	

	if (isEmpty(zip.value))

	{

		alert("Please enter zip/postal!");

		zip.focus();

		return false;

	}

	

	if (country.selectedIndex == 0)

	{

		alert("Please choose a country!");

		return false;

	}	

	

	if (isUser)

	{

		if (phone.value.length < 7)

		{

			alert("Invalid phone number, please try again");

			phone.focus();

			return false;

		}

		if (!isEmail(email.value))

		{

			alert("Invalid e-mail address, please try again");

			email.focus();

			return false;

		}		

	}

	else

	{

		if ((phone.value != "") && (phone.value.length < 7))

		{

			alert("Invalid phone number, please try again");

			phone.focus();

			return false;

		}

		if ((email.value != "") && (!isEmail(email.value)))

		{

			alert("Invalid e-mail address, please try again");

			email.focus();

			return false;

		}		

	}

	if ((fax.value != "") && (fax.value.length < 7))

	{

		alert("Invalid fax number, please try again");

		fax.focus();

		return false;

	}

	return true;

}



function chkCusInfo(frm, isChk, submitType)

{

	if (!chkcommonInfo(frm.customer, frm.address, frm.city, frm.zip, frm.country, frm.phone, frm.fax, frm.email, true))

	{

		if (submitType)

			return false;

		return;	

	}

	if (isChk)

	{

		if (submitType)

			return chkConfirm(frm.password, frm.confirmpassword);

		if (!chkConfirm(frm.password, frm.confirmpassword))

			return;

	}

	if (submitType)

		return true;

	frm.submit();	

}



function chkUserInfo(frm)

{

	if (!chkcommonInfo(frm.name, frm.address, frm.city, frm.zip, frm.country, frm.phone, frm.fax, frm.email, false))

	return;

	if (isEmpty(frm.username.value))

	{

		alert("Invalid user, please try again");

		frm.username.focus();

		return;

	}

	if (!chkConfirm(frm.password, frm.confirmpassword))

		return;

	frm.submit();	

}