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("電郵錯誤或未填寫，請再試");
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    //alert("The username doesn't seem to be valid.")
    alert("電郵錯誤或未填寫，請再試");
    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("The domain name doesn't seem to be valid.")
	alert("電郵錯誤或未填寫，請再試");
    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>3) {
   //alert("The address must end in a three-letter domain, or two letter country.")
   alert("電郵錯誤或未填寫，請再試")
   return false
}

if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   alert("電郵錯誤或未填寫，請再試")
   return false
}
return true;
}

function checkTextEmpty(theTextField,msg)
{
	if (theTextField.value == "")
	{
	  alert(msg);
	  theTextField.focus();
		return true;
	}
	return false;
}

function getTextEmpty(theTextField)
{
	if (theTextField.value == "")
	{	  	  
	   return 1;
	}
	return 0;
}

function checkEqual(theTextField1,theTextField2,msg)
{
	if (theTextField1.value != theTextField2.value)
	{
	  alert(msg);
	  theTextField1.value = "";
	  theTextField2.value = "";
	  theTextField1.focus();
		return true;
	}
	return false;
}

function checkSelectEmpty(theSelectedField,msg)
{
	if (theSelectedField.options[theSelectedField.selectedIndex].value == "" ||
	theSelectedField.options[theSelectedField.selectedIndex].value == 0 ||
	theSelectedField.options[theSelectedField.selectedIndex].value == -1)
	{
		alert(msg);
		theSelectedField.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;
  for (var i =0;i<theRadioField.length;i++)
  {
    if (theRadioField[i].checked=="1")
    {
      count = 1;
    }
  }
  if (count == 0)
  {
    alert(msg)
    theRadioField[0].focus();
    return true;
  }
  return false;
}

function getRadioValue(theRadioField)
{
  var count=0;
  for (var i =0;i<theRadioField.length;i++)
  {
    if (theRadioField[i].checked=="1")
    {
      return theRadioField[i].value;
    }
  }
  return false;
}

function toVoid(){}

function checkTotalTextField(theForm,theVar,theStart,theEnd,theTotalValue,theMsg,toFocus)
{
  var count = 0;
  for (var j=theStart;j<=theEnd;j++)
  {
    var theField = theVar+j;
    for (var i = 0; i<theForm.elements.length; i++)
    {
      if ((theForm.elements[i].name == theField) && theForm.elements[i].value != "")
      {
        if (j == theStart)
        {
          var theStartField = theForm.elements[i];
        }
        count++;
      }
    }
  }
  if (count <= theTotalValue)
  {
    alert(theMsg);
    toFocus.focus();
    return true;
  }
  return false;
}

function checkForm(f)
{  
  var name = f.name
  if (checkTextEmpty(name,"姓名必須填寫"))
  {
	return false;
  }
  
   var people = f.people
  if (checkTextEmpty(people,"參加人數必須填寫"))
  {
	return false;
  }

  var tel = f.tel
  if (checkTextEmpty(tel,"電話必須填寫"))
  {
	return false;
  }
  
  if (!emailCheck(f.email.value))
  {
    f.email.focus();
    return false;	
  }	
   
  if (checkTextEmpty(f.address1,"通訊地址必須填寫"))
	  return false;

	

  return true;
}