function emailCheck (emailStr) {
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("電郵錯誤或未填寫，請再試");
  //alert("Email address seems incorrect or empty")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    alert("電郵錯誤或未填寫，請再試");
    //alert("The username doesn't seem to be valid.")
    return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("電郵錯誤或未填寫，請再試");
	        //alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
    alert("電郵錯誤或未填寫，請再試");
	//alert("The domain name doesn't seem to be valid.")
    return false
}


var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 ||
    domArr[domArr.length-1].length>4) {
   alert("電郵錯誤或未填寫，請再試");
   //alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

if (len<2) {
   alert("電郵錯誤或未填寫，請再試");
   //var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false
}
return true;
}

function checkTextEmpty(theTextField,msg)
{
	if (theTextField.value == "")
	{
	  alert(msg);
	  theTextField.focus();
		return true;
	}
	return false;
}

function checkTextValue(theTextField,msg)
{
	if(!isNAN(theTextField.value))
	{
	  if(parseInt(theTextField.value)<1 && parseInt(theTextField.value)>20)
	  {
	    alert(msg);
	    theTextField.focus();
	    return true;
	  }

	}
	else
	{
	   alert(msg);
	   theTextField.focus();
	   return true;
	 }

	 return false;
}

function checkEqual(theTextField1,theTextField2,msg)
{
	if (theTextField1.value != theTextField2.value)
	{
	  alert(msg);
	  theTextField1.value = "";
	  theTextField2.value = "";
	  theTextField1.focus();
		return true;
	}
	return false;
}

function checkdate(yyyy,mm,dd)
{
  // mm month, dd day, yyyy year
  var err = 0
  if (dd<1 || dd>31) err = 1
  if (mm<1 || mm>12) err = 1
  if (mm==4 || mm==6 || mm==9 || mm==11)
  {
    if (dd==31) err=1
  }

  if (mm==2)
  {
    var g=parseInt(yyyy/4)
    if (isNaN(g))
    {
      err=1
    }
    if (dd>29) err=1
    if (dd==29 && ((yyyy/4)!=parseInt(yyyy/4))) err=1
  }

  if (err==1)
  {
    alert('This is not correct date. Please select the date again!');
    return false;
  }
  else
  {
    return true;
  }
}

function checkRadioEmpty(theRadioField,msg)
{
  var count=0;
  if (isNaN(theRadioField.length))
  {
    if (theRadioField.checked == true)
    {
        count = 1;
    }
  }
  else
  {
    for (var i =0;i<theRadioField.length;i++)
    {
      if (theRadioField[i].checked== true)
      {
        count = 1;
      }
    }
  }

  if (count == 0)
  {
    alert(msg)
    if (isNaN(theRadioField.length))
    {
      theRadioField.focus();
    }
    else
    {
      theRadioField[0].focus();
    }
    return true;
  }
  return false;
}

function getRadioValue(theRadioField)
{
  var count=0;

  if (isNaN(theRadioField.length))
  {
    if (theRadioField.checked)
    {
      return theRadioField.value;
    }
  }
  else
  {
    for (var i =0;i<theRadioField.length;i++)
    {
      if (theRadioField[i].checked)
      {
        return theRadioField[i].value;
      }
    }
  }
  return false;
}

function setRadioEmpty(theRadioField)
{
  var count=0;
  if (isNaN(theRadioField.length))
  {
    theRadioField.checked = false;
  }
  else
  {
    for (var i =0;i<theRadioField.length;i++)
    {
      if (theRadioField[i].checked=="1")
      {
        theRadioField[i].checked = false;
        return true;
      }
    }
  }
  return true;
}

function checkSelectEmpty(theSelectedField,msg)
{
	if (theSelectedField.options[theSelectedField.selectedIndex].value == "" ||
	theSelectedField.options[theSelectedField.selectedIndex].value == 0)
	{
		alert(msg);
		theSelectedField.focus();
		return true;
	}
	return false;
}

function selectEmpty(theSelectedField)
{
  if (theSelectedField.options[theSelectedField.selectedIndex].value == "" ||
	theSelectedField.options[theSelectedField.selectedIndex].value == 0)
	{
		return true;
	}
	return false;
}

 function checkCheckBoxEmpty(theForm,fieldName,msg)
    {
      var count=0;

      for (i=0,n=theForm.elements.length;i<n;i++)
        if(theForm.elements[i].name.indexOf(fieldName) != -1)
        if (theForm.elements[i].checked)
        {
          count = 1;
        }
      if (count == 0)
      {
        alert(msg)
        //theCheckBoxField[0].focus();
        return true;
      }
      return false;
    }

    function checkCheckBoxValue(theForm,fieldName,theValue)
    {
      var count=0;

      for (i=0,n=theForm.elements.length;i<n;i++)
        if(theForm.elements[i].name.indexOf(fieldName) != -1)
        if (theForm.elements[i].checked)
        {
          if (theForm.elements[i].value == theValue)
            count = 1;
        }
      if (count == 0)
      {
        return false;
      }
      return true;
    }

function resetForm(theForm)
{
  theForm.reset;
}

// validate and format HKID
// input: HKID in any format
// output: return formatted hkid if valid, return false if invalid
// e.g. a1234567, A1234567, a123456 (7), a123456(7) --> A123456(7)
function formatHKID(input_hkid)
{
 input_hkid = input_hkid.toString();

 var alpha = /[A-Za-z]/;
 var numeric = /[0-9]/;
 var checkdigit_range = /[0-9aA]/;
 var hkid = "";
 var arr_hkid;
 var char1;
 char1 = "";
 var checkdigit, lastdigit, sum, result;

 for (var i=0; i<input_hkid.length; i++) {
  char1 = input_hkid.charAt(i);
  if (!(char1==" " || char1=="(" || char1==")"))
   hkid += char1;
 }

 arr_hkid = hkid.split("");

 if (arr_hkid.length == 9) {
  if (!(arr_hkid[0].match(alpha) && arr_hkid[1].match(alpha)))
   return false;
  if (!(arr_hkid[2].match(numeric) && arr_hkid[3].match(numeric) &&
      arr_hkid[4].match(numeric) && arr_hkid[5].match(numeric) &&
      arr_hkid[6].match(numeric) && arr_hkid[7].match(numeric)))
   return false;
  if (!arr_hkid[8].match(checkdigit_range))
   return false;

  arr_hkid[0] = arr_hkid[0].toUpperCase();
  arr_hkid[1] = arr_hkid[1].toUpperCase();

  sum = (arr_hkid[0].charCodeAt(0)-55)*9 +
        (arr_hkid[1].charCodeAt(0)-55)*8 +
         parseInt(arr_hkid[2],10)*7 +
   parseInt(arr_hkid[3],10)*6 +
   parseInt(arr_hkid[4],10)*5 +
   parseInt(arr_hkid[5],10)*4 +
   parseInt(arr_hkid[6],10)*3 +
   parseInt(arr_hkid[7],10)*2;
  lastdigit = arr_hkid[8].toString.toUpperCase();

  result = arr_hkid[0]+arr_hkid[1]+arr_hkid[2]+
    arr_hkid[3]+arr_hkid[4]+arr_hkid[5]+
    arr_hkid[6]+arr_hkid[7]+"("+arr_hkid[8]+")";
 // if (result==10) result="A";

 } else if (arr_hkid.length == 8) {
  if (!arr_hkid[0].match(alpha))
   return false;
  if (!(arr_hkid[1].match(numeric) && arr_hkid[2].match(numeric) &&
      arr_hkid[3].match(numeric) && arr_hkid[4].match(numeric) &&
      arr_hkid[5].match(numeric) && arr_hkid[6].match(numeric)))
   return false;
  if (!arr_hkid[7].match(checkdigit_range))
   return false;

  arr_hkid[0] = arr_hkid[0].toUpperCase();

  sum = (arr_hkid[0].charCodeAt(0)-64)*8 +
   parseInt(arr_hkid[1],10)*7 +
   parseInt(arr_hkid[2],10)*6 +
   parseInt(arr_hkid[3],10)*5 +
   parseInt(arr_hkid[4],10)*4 +
   parseInt(arr_hkid[5],10)*3 +
   parseInt(arr_hkid[6],10)*2;
  lastdigit = arr_hkid[7].toString().toUpperCase();
  result = arr_hkid[0]+arr_hkid[1]+arr_hkid[2]+
    arr_hkid[3]+arr_hkid[4]+arr_hkid[5]+
    arr_hkid[6]+"("+arr_hkid[7]+")";
 // if (result==10) result="A";

 } else {
  return false;
 }

 checkdigit = 11 - sum % 11;
 if (checkdigit.toString()=="10") checkdigit="A";
 if (checkdigit.toString()=="11") checkdigit="0";
 return ((checkdigit.toString()==lastdigit.toString()) ? result : false);
}

function clearForm(theForm)
{
  self.theForm.reset();
}

function validateForm(theForm)
{

  var name = theForm.name;
  if (checkTextEmpty(name,"請填寫姓名"))
  {
    return false;
  }

  var email = theForm.email;
  if (checkTextEmpty(email,"電郵錯誤或未填寫，請再試"))
  {
    return false;
  }

  if (!emailCheck(email.value))
  {
    email.focus();
    return false;
  }

  var tel = theForm.tel;
   if (checkTextEmpty(tel,"請填寫電話"))
   {
     return false;
   }



  return true;
}