// Utilities for form verification

//   var legalUName = "/cr\d+u\d+/i";
//   var legalUName = /cr\d+u\d+/i;
   var legalUName = /cr\d+u[^\@]+\@[^\@]+/i;
   var legalPlName = /pl\d+u[^\@]+\@[^\@]+/i;
   var mailRE = /^[^\@]+\@[^\@]+$/;
   var engOnRE = /^[a-zA-Z0-9\_\-\.]+$/;
   var engMailOnRE = /^[a-zA-Z0-9\_\-\.\@]+$/;
   var numOnly = /^[0-9]+$/;

function checkLogin()
{
   courseTaken = false;
   toSend = true
   with  (document.letForm) {
   if (eMail.value) {
	   toSend = true;
	} else {
        toSend = false;
		errCode = 1
   } // if all fields
 //   Now to find out if there is a @ in the E-mails
   if (
        (
      (!mailRE.test(eMail.value)) || 
	  (!engMailOnRE.test(eMail.value))
	    ) && (toSend)) {
        toSend = false;
		errCode = 5
 }


if (toSend) {
 		if (!theGender[0].checked && !theGender[1].checked) {
          errCode = 9
          toSend = false
	   }
  } // toSend



   if (toSend) {
	   if (!didNotBefore.checked)
	   {
        toSend = false;
		errCode = 3
	   }
   } // toSend



   if (toSend) {
      return true
   } else {
      switch (errCode) {
	     case 1:
            alert ("Please fill the Password field")
			break;
	     case 3:
            alert ("You must confirm that you did not participated in this test before")
			break;
	     case 5:
            alert ("Illegal E-mail address")
            eMail.focus()
            eMail.select()
			break;
	     case 6:
            alert ("Please answer if you took A-levels in Economics")
			break;
	     case 7:
            alert ("Please select your major study")
			break;
	     case 8:
            alert ("You must select one of the years")
			break;
	     case 9:
            alert ("You must select one of the gender buttons (Male or Female)")
			break;
	  } // switch
	  return false
   }  // else to send
  
   } // with
} // checkLogin

function checkNum(theField, minVal, maxVal) 
{
//	alert(theField)
   theRetValue = true;
   if (theField.value && numOnly.test(theField.value))
   {
	   if (theField.value < minVal || theField.value > maxVal)
	   {
            theRetValue = false;
	        alert ("Integer should be in the [" + minVal + "," + maxVal + "] range")
            theField.value = ""
	        theField.select()
            errCode = 3
	   }
   } else {
	   alert ("Only Numeric values (integers) are allowed in this field")
       errCode = 2
       theField.value = ""
	   theField.select()
	   theField.focus()
       theRetValue = false;
   }
   document.retVal = theRetValue;
   return theRetValue
} // checkNum


function checkParam (theField, minVal, maxVal)
{
   toSend = true
   if (!theField.value)
   {  
       errCode = 1
       toSend = false
   } else {
	   if (!checkNum(theField, minVal, maxVal))
	   {
       errCode = 2
       toSend = false
	   }
   }
   if (toSend)
   {
	   return true
   } else {
       switch (errCode) {
	     case 1:
            alert ("You must enter value into this field")
	        theField.select()
	        theField.focus()
			break;
		 // case 2 is taken care by the numeric value utility
//	     case 2:
//            alert ("Only Numeric values (integers) are allowed in this field")
//            theField.value = ""
//	        theField.select()
//	        theField.focus()
//			break;
	  } // switch
	  return false
  }
}

function checkRadio (theField)
{
   toSend = true
		if (!theField[0].checked && !theField[1].checked) {
          errCode = 1
          toSend = false
	   }
      if (toSend)
      {
	      return true
      } else {
       switch (errCode) {
	     case 1:
            alert ("You must select one of the radio buttons (Yes or No)")
			break;
		 // case 2 is taken care by the numeric value utility
//	     case 2:
//            alert ("Only Numeric values (integers) are allowed in this field")
//            theField.value = ""
//	        theField.select()
//	        theField.focus()
//			break;
	  } // switch	    
	  return false
	 }
}

function checkRadioN (theField, numOfBtn)
{
   toSend = false;
	   for (var idx=0; idx < numOfBtn; idx++) {
	      if (theField[idx].checked) {
             toSend = true;

		  }
       } // for
      if (toSend)
      {
	      return true;
      } else {
            alert ("You must select one of the radio buttons");
	  return false;
	 }
}


