
// Checks for the following valid date formats:
// 00:00 - 24:00
// by Paul Hinds
function isValidTime(objElement, strDescription){
 var strTime = objElement.value;
 var timePat = /[0-9]{2}:[0-9]{2}/;
 var wrong = false;
	// do tests
 wrong = !timePat.test(strTime);
 var intHrs = parseInt(strTime.substring(0,2));
 var intMins = parseInt(strTime.substring(3,5));
 wrong |= (intHrs>24);
 wrong |= (intMins>59);
 wrong |= (intHrs==24 && intMins>0); // N.B. 24:00 is allowed but 24:01 is not

 if(wrong)alert(strDescription + " error.\n\nInvalid Time. Please re-enter in the format 00:00");

 return !wrong;
}


function isValidDate(objElement, strDescription) {
// Checks for the following valid date formats:
// DD/MM/YY   DD/MM/YYYY   DD-MM-YY   DD-MM-YYYY
// Also separates date into month, day, and year variables
var datePat= /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
 while(objElement.value.charAt(0)==' ') objElement.value=objElement.value.substring(1,objElement.value.length);							                   // Trim objElement of leading spaces
 while(objElement.value.charAt(objElement.value.length-1)==' ')objElement.value=objElement.value.substring(0,objElement.value.length-1); // Trim objElement of trailing spaces
 var matchArray = objElement.value.match(datePat);        // is the format ok?
 if (matchArray == null) {
  alert(strDescription + " error.\n\n" + strDescription + " is not in a valid format.");
  return false;
 }
 var month = matchArray[3];                               // parse date into variables
 var day = matchArray[1];
 var year = matchArray[4];
 if (month < 1 || month > 12) {                           // check month range
  alert(strDescription + " error.\n\nMonth must be between 1 and 12.");
  return false;
 }
 if (day < 1 || day > 31) {
  alert(strDescription + " error.\n\nDay must be between 1 and 31.");
  return false;
 }
 if ((month==4 || month==6 || month==9 || month==11) && day==31) {
  alert(strDescription + " error.\n\nMonth "+month+" doesn't have 31 days!")
  return false
 }
 if (month == 2) {                                        // check for february 29th/leap year
  var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
  if (day>29 || (day==29 && !isleap)) {
   alert(strDescription + " error.\n\nFebruary " + year + " doesn't have " + day + " days!");
   return false;
  }
 }
 objElement.value = day + "/" + month + "/" + year;       // Put date into dd/mm/yy or dd/mm/yyyy format
 return true;                                             // date is valid
}

function isValidEmail(objElement, strDescription) {
 if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(objElement.value)){
  return true;
 } else {
  alert(strDescription + " error.\n\nInvalid E-mail Address. Please re-enter.");
  return false;
 }
}

function isDigit(num) {
	var string="1234567890 ";
	if (string.indexOf(num) != -1) {
		return true;
	}
	return false;
}

function isInteger(objElement, strDescription) {
 for (var i=0; i < objElement.value.length; i++) {
  if (!isDigit(objElement.value.charAt(i))) { 
   alert(strDescription + " error.\n\nNot a valid numeric value. Please re-enter.");
   return false; 
  }
 }
 return true;
}

function isNotNull(objElement, strDescription) {
 if (trim(objElement)==""){
  alert(strDescription + " error.\n\nThis field is currently blank.  Please enter value.");
  return false;
 }
 return true;
}

function trim(thingy) {
 while(''+thingy.value.charAt(0)==' ') thingy.value=thingy.value.substring(1,thingy.value.length);
 while(''+thingy.value.charAt(thingy.value.length-1)==' ') thingy.value=thingy.value.substring(0,thingy.value.length-1);
 return thingy.value;
}

function isDecimal(objElement, strDescription) {
 var dotcount = 0;
 for (var i=0; i < objElement.value.length; i++) {
  if (!isDigit(objElement.value.charAt(i))) {
    if (objElement.value.charAt(i) != '.') { 
       alert(strDescription + " error.\n\nNot a valid numeric value. Please re-enter.");
       return false;
    } else {
       dotcount = dotcount + 1;
    }
  }  
 }
 if (dotcount > 1) { 
       alert(strDescription + " error.\n\nOnly one decimal point is allowed. Please re-enter.");
       return false;
    }
 return true;
}