
function CheckEmail(emailStr) {
var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);

  if (matchArray==null) {
    alert("Invalid email address. Please check email address!");
    return false;
  }

var user=matchArray[1];
var domain=matchArray[2];

  for (i=0; i<user.length; i++) {
    if (user.charCodeAt(i)>127) {
      alert("Invalid charactor in email. Please check email address!");
      return false;
    }
  }
  for (i=0; i<domain.length; i++) {
    if (domain.charCodeAt(i)>127) {
      alert("Invalid email");
      return false;
    }
  }

  if (user.match(userPat)==null) {
    alert("Invalid email address. Please check email address!");
    return false;
  }

var IPArray=domain.match(ipDomainPat);

  if (IPArray!=null) {
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
        alert("Invalid email address. Please check email address!");
        return false;
      }
    }
    return true;
  }

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;

  for (i=0;i<len;i++) {
    if (domArr[i].search(atomPat)==-1) {
      alert("Invalid email address. Please check email address!");
      return false;
    }
  }

  if (checkTLD && domArr[domArr.length-1].length!=2 &&
     domArr[domArr.length-1].search(knownDomsPat)==-1) {
    alert("Invalid email address. Please check email address!");
    return false;
  }

  if (len<2) {
    alert("Invalid email address. Please check email address!");
    return false;
  }

  return true;
}
function ltrim(s)
{
	while(s.charAt(0)==' ')
	{
		s = s.substr(1);
	}
	return s;
}
function rtrim(s)
{
	while(s.charAt(s.length-1)==' ')
	{
		s = s.substr(0, s.length-1);
	}
	return s;
}
function trim(s)
{
	var rs = ltrim(s);
	rs = rtrim(rs);
	return rs;
}
function check_form_vn(o){
	o.cname.value = trim(o.cname.value);
	//o.phone.value = trim(o.phone.value);
	o.address.value = trim(o.address.value);
	if(o.cname.value==''){ alert("Vui long nhap ten!"); o.cname.focus(); return false;}
	//if(o.phone.value==''){ alert("Vui long cho biet so dien thoai!"); o.phone.focus(); return false;}
	//if(o.phone.value.length<7){alert("Vui long nhap du so dien thoai!"); o.phone.focus(); return false;}
	if(o.address.value == ""){alert("Vui long nhap dia chi!"); o.address.focus(); return false;}
	if(o.d.value==0){alert("Vui ḷng chon ngày gui hàng");o.d.focus();return false;}
	return true;
}
function check_form_en(o){
	o.cname.value = trim(o.cname.value);
	//o.phone.value = trim(o.phone.value);
	o.address.value = trim(o.address.value);
	if(o.cname.value==''){ alert("Please enter your name!"); o.cname.focus(); return false;}
	//if(o.phone.value==''){ alert("Please enter your number!"); o.phone.focus(); return false;}
	//if(o.phone.value.length<7){alert("Please check your number!"); o.phone.focus(); return false;}
	if(o.address.value == ""){alert("Please enter your address!"); o.address.focus(); return false;}
	if(o.d.value==0){alert("Please enter ship date!");o.d.focus();return false;}
	return true;
}

function check_contact_vn(o){
	o.cname.value = trim(o.cname.value);
	o.email.value = trim(o.email.value);
	o.message.value = trim(o.message.value);
	if(o.cname.value==''){ alert("Vui ḷng nhâp tên!"); o.cname.focus(); return false;}
	if(o.email.value==""){ alert("Vui ḷng cho biêt dia chi email!"); o.email.focus(); return false;}
	if(o.message.value==""){ alert("Vui long cho biêt nôi dung liên hê.!"); o.message.focus();return false;}
	return CheckEmail(o.email.value);
}
function check_contact_en(o){
	o.cname.value = trim(o.cname.value);
	o.email.value = trim(o.email.value);
	o.message.value = trim(o.message.value);
	if(o.cname.value==''){ alert("Please enter your name!"); o.cname.focus(); return false;}
	if(o.email.value==""){ alert("Please enter your email!"); o.email.focus(); return false;}
	if(o.message.value==""){ alert("Please Enter your Request or commnad!"); o.message.focus();return false;}
	return CheckEmail(o.email.value);
}

//check email
function isemail(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}
