function inputLen(intLen,theTextField,msg){
	var strInput = theTextField.value;
	if (strInput.length < intLen){
		alert(msg);
		theTextField.focus();
		return true;
	}
	return false;
}
function checkTextEmpty(theTextField,msg)
{
	if(trim(theTextField.value)== ""){
		 alert(msg); theTextField.focus(); return true;
	}

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

	return false;
}

function checkRadioEmpty(theRadioField,msg){


	for ( i = 0; i < theRadioField.length; i++){

		if(theRadioField[i].checked){
			return false;
		}
	}
	alert(msg);
	theRadioField[0].focus();
	return true;

}

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

function checkNumber(theTextField,msg){
	if(isNaN(theTextField.value)){
		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 checkChkEmpty(theForm,theChkField,message){
	var votelimit = 1;
	var isEmpty = false;
	var total = chkTotal(theForm,theChkField);


	if (total < votelimit)
	{
		isEmpty = true;
		alert(message);
		for (var i = 0; i<theForm.elements.length; i++){
			if(theForm.elements[i].name.indexOf(theChkField) > -1){
				theForm.elements[i].focus();
				break;
			}
      	}
	}
	return isEmpty;
}

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)
    return true;
  }
  return false;
}

function getCheckBoxValueEqual(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)
        {
          return true;
        }
      }
  return false;
}

function chkTotal(theForm,theChkField){

   var total = 0;
   for (var i = 0; i<theForm.elements.length; i++){
     if (theForm.elements[i].name.indexOf(theChkField) > -1)
      if (theForm.elements[i].checked)
        total++;
   }
   return total;
}

function checkSelectEmpty(theSelectedField,msg)
{
	if (theSelectedField.options[theSelectedField.selectedIndex].value == "" ||
	theSelectedField.options[theSelectedField.selectedIndex].value == -1)
	{
		alert(msg);
		theSelectedField.focus();
		return true;
	}
	return false;
}

function getSelectValue(theSelectField)
{
   return parseInt(Number(theSelectField.options[theSelectField.selectedIndex].value));
}

function trim(inputString) {

   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   /*while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }*/
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

// 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);
}
