// modified by Simon Lau Kian Lye 14 Jul 2000

// Functions available :
// ----------------------------------------------------------------------
// -----> checkCreditCardExpDate(myfield,myfield1))
// -----> checkCreditCardNumber(myfield,ccType)
// -----> days_between(date1,date2) // returns date2-date1
// -----> checkNRIC(nricfield) Returns true/false
// -----> validateCfrmPswd(myfield,myfield1)
// -----> compareDates(date1,date2) // equal=0 date1<date2=-1 date1>date2=1
// -----> validateMulipleSel(myfield,name)
// -----> validateImgFile(myfield,name)
// -----> validateFileExt(myfield,name)
// -----> validateUserName(myfield,name,min,max) 24 Jul 2000
// -----> validateRadio_Check(myfield,name) 19 Jul 2000
// -----> validateDropDown(myfield,name) 19 Jul 2000
// -----> textareaMax(myfield) 19 Jul 2000
// -----> RemoveBad(InStr)
// -----> validateString(myfield,name)
// -----> validateEmail(myfield,name)
// -----> validateNum(myfield,name)
// -----> validateFloat(myfield,name)
// -----> validateDate(myfield,name)
// -----> validateRange1_99(myfield,name)
// -----> validateRange1_9999(myfield,name)
// -----> validateRange0_99(myfield,name)
// -----> validateMoney(myfield,name)
// ----------------------------------------------------------------------

function checkCreditCardExpDate(myfield,myfield1)
{
	var result = true;
	var formValue = myfield.options[myfield.selectedIndex].value + "/" + myfield1.options[myfield1.selectedIndex].value;

	var elems = formValue.split("/");
	
	result = (elems.length == 2); // should be two components
	var expired = false;
	
	if (result)
	{
		var month = parseInt(elems[0]);
		var year = parseInt(elems[1]);
		
		if (elems[1].length == 2)
			year += 2000;
		
		var now = new Date();
		
		var nowMonth = now.getMonth() + 1;
		var nowYear = now.getFullYear();
		
		expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
	}
	
	if (expired)
		return ('   * Credit Card Expiration Date has expired.\n');

	return "";
}


function checkCreditCardNumber(myfield,ccType)
{
	var result = true;
 	var ccNum = myfield.value;

	if (!notNull(myfield.value) || !notBlank(myfield.value))
		return ('   * Credit Card No.\n');
 
  	if (result && (myfield.value.length>0))
 	{ 
 		if (!isDigits(ccNum))
 		{
 			return ('   * Please enter only numbers (no dashes or spaces) for the Credit Card No.\n');
		}	

		if (result)
 		{ 
 			
 			if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNum))
 			{
 				return ('   * Please enter a valid Credit Card No.\n');
			}	
		} 

	} 
	
	return "";
}

function LuhnCheck(str) 
{
  var result = true;

  var sum = 0; 
  var mul = 1; 
  var strLen = str.length;
  
  for (i = 0; i < strLen; i++) 
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;
    
  return result;
}

function validateCCNum(cardType,cardNum)
{
	var result = false;
	cardType = cardType.toUpperCase();
	
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);

	switch (cardType)
	{
		case "VISA","2":
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AMEX":
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MASTERCARD","3":
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISCOVER":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DINERS":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
	}
	return result;
}

function days_between(date1, date2) {
    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    vardate1 = new Date(date1); //#(NEW-PTcancel)
	vardate2 = new Date(date2); //#(NEW-PTcancel)
    var date1_ms = vardate1.getTime();
    var date2_ms = vardate2.getTime();

    // Calculate the difference in milliseconds
    var difference_ms = date2_ms - date1_ms;
    
    var days_d = difference_ms/ONE_DAY;
    if (days_d<1 && days_d>0)
    	return 0;
    else if (days_d<0 && days_d>-1)
    	return -1;
    else
	    // Convert back to days and return
    	return Math.round(difference_ms/ONE_DAY);
}


Date.prototype.getDateString = getDateString;
Date.prototype.getFullYear = getFullYear;

function dig(id,n) {
  return parseInt(id.charAt(n),10)
}

function checkNRIC(id) {
// Returns non-zero if error
	if (id.length != 9)
		return false;

	if ((id.charAt(0) != "S")&&(id.charAt(0) != "s")&&(id.charAt(0) != "T")&&(id.charAt(0) != "t")&&(id.charAt(0) != "F")&&(id.charAt(0) != "f")&&(id.charAt(0) != "G")&&(id.charAt(0) != "g"))
		return false;
	//alert(id.charAt(0));
	if ((id.charAt(0) == "S") || (id.charAt(0) == "s")||(id.charAt(0) == "T") || (id.charAt(0) == "t")) {
		// NRIC validation
		//alert("S validation");
		var S1 = dig(id,1)*2+dig(id,2)*7+dig(id,3)*6+dig(id,4)*5+dig(id,5)*4+dig(id,6)*3+dig(id,7)*2;
		if (id.charAt(0) == "T" || id.charAt(0) == "t") {
			S1 = S1 + 4;
		}
		var P = 10 - (S1 % 11);
		var S2 = "ABCDEFGHIZJ";
		var S3 = "abcdefghizj";

		if ((id.charAt(8) != S2.charAt(P))&&(id.charAt(8) != S3.charAt(P))) return false;

		if (id.charAt(1) < '0' || id.charAt(1) > '9' ||
		    id.charAt(2) < '0' || id.charAt(2) > '9' ||
		    id.charAt(3) < '0' || id.charAt(3) > '9' ||
		    id.charAt(4) < '0' || id.charAt(4) > '9' ||
		    id.charAt(5) < '0' || id.charAt(5) > '9' ||
		    id.charAt(6) < '0' || id.charAt(6) > '9' ||
		    id.charAt(7) < '0' || id.charAt(7) > '9' )
		{
		  return false ;
		}

		return true;
	}
	else {
		// FIN validation
		if ((id.charAt(0) == "F") || (id.charAt(0) == "f") || (id.charAt(0) == "G") || (id.charAt(0) == "g")) {
			//alert("F validation");
			var F1 = dig(id,1)*2+dig(id,2)*7+dig(id,3)*6+dig(id,4)*5+dig(id,5)*4+dig(id,6)*3+dig(id,7)*2;
			if (id.charAt(0) == "G" || id.charAt(0) == "g") {
				F1 = F1 + 4;
			}
			var P = 10 - (F1 % 11);
			var F2 = "KLMNPQRTUWX";
			var F3 = "klmnpqrtuwx";

			if ((id.charAt(8) != F2.charAt(P))&&(id.charAt(8) != F3.charAt(P))) return false;

			if (id.charAt(1) < '0' || id.charAt(1) > '9' ||
			    id.charAt(2) < '0' || id.charAt(2) > '9' ||
			    id.charAt(3) < '0' || id.charAt(3) > '9' ||
			    id.charAt(4) < '0' || id.charAt(4) > '9' ||
			    id.charAt(5) < '0' || id.charAt(5) > '9' ||
			    id.charAt(6) < '0' || id.charAt(6) > '9' ||
			    id.charAt(7) < '0' || id.charAt(7) > '9' )
			{
			  return false ;
			}

			return true;
		}
	}
}

function validateCfrmPswd(myfield,myfield1)
{
	if ((myfield.value=='')||(myfield1.value==''))
		return "";
	else
	{
		if (myfield.value==myfield1.value)
			return "";
		else
			return ('\n* Confirmation password differ from the new password\n');
	}
}

function compareDates(date1,date2){

	if ((isDate(date1.value)) && (isDate(date2.value)))
	{
		vardate1 = new Date(date1.value);
	   	vardate2 = new Date(date2.value);
	  	if(vardate1.getDateString() == vardate2.getDateString())
	    		return(0);
	 	else if(vardate1.getDateString() < vardate2.getDateString())
	  		return(-1);
	  	else
	   		return(1);
	}
	else
		return (2);
}
function compareDates2(date1,date2){

	if ((isDate(date1)) && (isDate(date2)))
	{
		vardate1 = new Date(date1);
	   	vardate2 = new Date(date2);
	  	if(vardate1.getDateString() == vardate2.getDateString())
	    		return(0);
	 	else if(vardate1.getDateString() < vardate2.getDateString())
	  		return(-1);
	  	else
	   		return(1);
	}
	else
		return (2);
}

function getDateString(){
   var dateStr;
   dateStr = "" + this.getFullYear();
   if (this.getMonth() < 9)
      dateStr += "0";
   dateStr +=  (this.getMonth() + 1);
   if (this.getDate() < 10)
      dateStr += "0";
   dateStr += this.getDate();
   return dateStr;
}

function getFullYear(){
   var year = this.getYear();
   if(year < 1000){
      year += 1900;}
   return year
}

function validateMulipleSel(myfield,name)
{	count=myfield.length;
	allValid=false;
	for (ic=0;ic<count;ic++)
	{
		if (myfield[ic].selected)
		{	allValid=true;
			break;
		}
	}
	if (!allValid)
		return (name+"\n");
	else
		return "";
}

function validateImgFile(myfield,name)
{
	checkStr=myfield.value;
	if (checkStr=='')
		return(name);
	else
	{
		var strExt=checkStr.substring(checkStr.indexOf('.')+1);

		if (strExt.toLowerCase()=='gif' || strExt.toLowerCase()=='jpg')
			return('');
		else
			return(name + ' invalid file format\n');
	}
}

function validateFileExt(myfield,name)
{
	checkStr=myfield.value;
	if (checkStr=='')
		return('');
	else
	{
		var strExt=checkStr.substring(checkStr.indexOf('.')+1);
		var FileExt = strExt.toLowerCase();
		var errmsg;

		switch (FileExt) {
			case 'pdf' 	: 	errmsg = '';
							break;
			default 	:	errmsg = name + ' invalid file format\n';
							break;
		}
		return errmsg;
	}
}

function validateUserName(myfield,name,min,max)
{
  	var allValid = true;

	if (!notNull(myfield.value)||! notBlank(myfield.value))
		allValid = false;

	if (min!="")
	{
		if (myfield.value.length<min)
			allValid = false;
	}

	if (max!="")
	{
		if (myfield.value.length>max)
			allvalid = false;
	}

	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
  	var checkStr = myfield.value;

  	for (i = 0;  i < checkStr.length;  i++)
  	{
    		ch = checkStr.charAt(i);
    		for (j = 0;  j < checkOK.length;  j++)
      			if (ch == checkOK.charAt(j))
				break;
    		if (j == checkOK.length)
    		{
      			allValid = false;
      			break;
    		}
  	}

	if (!allValid)
  		return (name+"\n");
  	else
  		return "";
}
function validateMulipleSel(myfield,name)
{	count=myfield.length;
	allValid=false;
	for (ic=0;ic<count;ic++)
	{
		if (myfield[ic].selected)
		{	allValid=true;
			break;
		}
	}
	if (!allValid)
		return (name+"\n");
	else
		return "";
}
function validateRadio_Check(myfield,name)
{	count=myfield.length;
	allValid=false;
	for (ic=0;ic<count;ic++)
	{
		if (myfield[ic].checked)
		{	allValid=true;
			break;
		}
	}
	if (!allValid)
		return (name+"\n");
	else
		return "";
}

function validateDropDown(myfield,name)
{
	if ((myfield.options[myfield.selectedIndex].value) == '')
		return (name+"\n");
	else
		return "";
}


function textareaMax(myfield, maxlimit)
{
	if (myfield.value.length > maxlimit) // if too long...trim it!
		myfield.value = myfield.value.substring(0, maxlimit);
}
// eg. <textarea name="" wrap="physical" cols="45" rows="5" onKeyDown="textareaMax(this.form.txtContent,255);" onKeyUp="textareaMax(this.form.txtContent,255);"></textarea>

function RemoveBad(InStr){
    InStr = InStr.replace(/\</g,"");
    InStr = InStr.replace(/\>/g,"");
    InStr = InStr.replace(/\"/g,"");
    InStr = InStr.replace(/\'/g,"");
    InStr = InStr.replace(/\%/g,"");
    InStr = InStr.replace(/\;/g,"");
    InStr = InStr.replace(/\(/g,"");
    InStr = InStr.replace(/\)/g,"");
    InStr = InStr.replace(/\&/g,"");
    InStr = InStr.replace(/\+/g,"");
    return InStr;
}

function validateString(myfield,name)
{
	if (notNull(myfield.value)&& notBlank(myfield.value))
		return "";
	else
		return (name+"\n");
}

function validateEmail(myfield,name)
{
	if (notNull(myfield.value)&& notBlank(myfield.value) && isEmail(myfield.value))
		return "";
	else
		return (name+"\n");
}

function validateNum(myfield,name)
{
	if (notNull(myfield.value)&& notBlank(myfield.value)&&isDigits(myfield.value))
		return "";
	else
		return (name+"\n");
}

function validateFloat(myfield,name)
{
	if (notNull(myfield.value)&& notBlank(myfield.value)&&isNumber(myfield.value))
		return "";
	else
		return (name+"\n");
}

function validateDate(myfield,name)
{
	if (notNull(myfield.value)&& notBlank(myfield.value)&&isDate(myfield.value))
		return "";
	else
		return (name+"\n");
}

function validateRange1_9999(myfield,name)
{
	if (isDigits(myfield.value) && isInRange(myfield.value,1, 9999))
		return "";
	else
		return (name+"\n");
}

function validateRange1_99(myfield,name)
{
	if (isDigits(myfield.value) && isInRange(myfield.value,1, 99))
		return "";
	else
		return (name+"\n");
}

function validateRange0_99(myfield,name)
{
	if (isDigits(myfield.value) && isInRange(myfield.value,0, 99))
		return "";
	else
		return (name+"\n");
}

function validateMoney(myfield,name)
{
	if (notNull(myfield.value)&& notBlank(myfield.value))
	{
		if(isMoney(myfield.value))
			return "";
		else
			return (name+"\n");
	}
	else
		return (name+"\n");
}

// end of Functions


//======================================================================
// Begin String Validation
//======================================================================
function notNull(str) {
	if (str.length == 0 )
		return false
	else
		return true
}

function notBlank(str) {
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ")
			return true
	}
	return false
}

function isSize(str, size) {
	if (str.length == size)
		return true
	else
		return false
}
//======================================================================
// End String Validation
//======================================================================

//======================================================================
// Begin Numeric Validation
//======================================================================
function isDigits(str) {
	var i
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if (mychar < "0" || mychar > "9")
			return false
	}
	return true
}

function isNumber(str) {
	numdecs = 0
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if ((mychar >= "0" && mychar <= "9") || mychar
			== ".") {
			if (mychar == ".")
				numdecs++
		}
		else
			return false
	}
	if (numdecs > 1)
		return false
return true
}

function isInRange(str, num1, num2) {
	var i = parseInt(str)
	return((i >= num1) && (i <= num2))

}

function isMoney(str)
{
	var tempStr;

	tempStr = str;

	tempStr = stripChars(tempStr, '$');

	return isNumber(tempStr);
}
//======================================================================
// End Numeric Validation
//======================================================================

//======================================================================
// Begin Formatting
//======================================================================
function stripNonDigits(str) {
	var i
	var newstring = ""
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i)
		if (isDigits(mychar))
			newstring += mychar
	}
	return newstring
}

function stripChars(str, chars) {
	var i
	var newstring = ""
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i)
		if (chars.indexOf(mychar) == -1)
			newstring += mychar
	}
	return newstring
}

// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripInitialWhitespace (s)
{   var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;

    return s.substring (i, s.length);
}
function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}

//======================================================================
// End Formatting
//======================================================================

//======================================================================
// Begin : Email Validation
//======================================================================
// isEmail (STRING s [, BOOLEAN emptyOK])
//
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if (isEmpty(s))
       if (isEmail.arguments.length == 1) return false;
       else return (isEmail.arguments[1] == true);

    // is s whitespace?
    if (isWhitespace(s)) return false;

    // is s containing special characters?
    if (s.indexOf('(')>=0) return false;
    if (s.indexOf(')')>=0) return false;
    if (s.indexOf('<')>=0) return false;
    if (s.indexOf('>')>=0) return false;
    if (s.indexOf(',')>=0) return false;
    if (s.indexOf(';')>=0) return false;
    if (s.indexOf(':')>=0) return false;
    if (s.indexOf('\\')>=0) return false;
    if (s.indexOf('"')>=0) return false;
    if (s.indexOf('[')>=0) return false;
    if (s.indexOf(']')>=0) return false;
    if (s.indexOf('#')>=0) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}
//======================================================================
// End : Email Validation
//======================================================================

//======================================================================
// Begin : Whitespace Validation
//======================================================================
var whitespace = " \t\n\r";
function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}
//======================================================================
// End : Whitespace Validation
//======================================================================

//======================================================================
// Begin : Date Validation
//======================================================================
// isYear (STRING s [, BOOLEAN emptyOK])
//
// isYear returns true if string s is a valid
// Year number.  Must be 2 or 4 digits only.
//
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isYear (s)
{   if (isEmpty(s))
       if (isYear.arguments.length == 1)
       		return false;
       else
       		return (isYear.arguments[1] == true);

    return ((s.length == 2) || (s.length == 4));
}

// isMonth (STRING s [, BOOLEAN emptyOK])
//
// isMonth returns true if string s is a valid
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isMonth (s)
{   if (isEmpty(s))
       if (isMonth.arguments.length == 1) return false;
       else return (isMonth.arguments[1] == true);
    return isInRange (s, 1, 12);
}

// isDay (STRING s [, BOOLEAN emptyOK])
//
// isDay returns true if string s is a valid
// day number between 1 and 31.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isDay (s)
{   if (isEmpty(s))
       if (isDay.arguments.length == 1) return false;
       else return (isDay.arguments[1] == true);
    return isInRange (s, 1, 31);
}

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   }
   return this
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

// daysInFebruary (INTEGER year)
//
// Given integer argument year,
// returns number of days in February of that year.

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day
// form a valid date.
//

function isDateCheck (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false;

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function isDate(DateStr)
{
	dateVal = DateStr
	var firstSlash = dateVal.indexOf("/")
	var lastSlash = dateVal.lastIndexOf("/")
	if (firstSlash != lastSlash)
	{
		var month = dateVal.substring(0,firstSlash);
		var day = dateVal.substring(firstSlash+1,lastSlash);
		var year = dateVal.substring(lastSlash+1);

		if (month.substring(0,1) == '0')
			month = month.substring(1,2);
		if (day.substring(0,1) == '0')
			day = day.substring(1,2);
		if (((day=="")||(month=="")) || (year==""))
			return false;
		else
			return(isDateCheck(year,month,day))
	}
	else
		return false;
}
//======================================================================
// End : Date Validation
//======================================================================

function isTime(timeStr,name,outMsg)
{
	//Note : valid time fomat hh:mm AM or hh:mm PM

	timeVal = timeStr.value;
	timeVal = timeVal.toUpperCase(timeVal);		//convert to upper case
	timeLength = timeVal.length;

	//check that colon is keyed
	charColon = timeVal.indexOf(":")+1;
	if (charColon == 0)
		return (name);

	//find the position of the blank space between time and unit AM/PM
	charSpaceUnit = timeVal.indexOf(" ")+1;
	if (charSpaceUnit == 0)
		return (name);

	//extract the value for minute
	numHour = timeVal.substring(0,charColon-1)					//charColon gives the exact position of the colon

	//check that the hour value before the colon is not blank
	if (numHour.length < 1){
		return (name);
	}

	//check that hour is within valid range
	if (numHour < 1 || numHour > 12)
		return (name);

	//check that hour is a numeric value, without zero at first position
	if (!(notNull(numHour)&& notBlank(numHour)&&isDigits(numHour)))
		return (name);

	//extract the value for minute
	numMin = timeVal.substring(charColon,charSpaceUnit-1)		//charColon gives the exact position of the colon

	//check that minute consists of 2 chars length
	if (numMin.length < 2 || numMin.length > 2)
		return (name);

	//check that the minute value after the colon is not blank and numeric
	if (!(notNull(numMin)&& notBlank(numMin)&&isDigits(numMin)))
		return (name);

	//check that minute is within valid range
	if (numMin > 59)
		return (name);

	//check that the chars after the blank space is AM/PM
	remainStr = (timeVal.substring(charSpaceUnit,timeLength));
	charTimeUnit = remainStr.substring(0,2)		//extract the first 2 chars for validation. substring starts with position 0
	if (charTimeUnit != "AM" && charTimeUnit != "PM")
		return (name);

	//check that there is no other chars after AM/PM
	charExtraChar = remainStr.substring(2,timeLength);		//extract any chars after the 2nd char
	if (charExtraChar != "")
		return (name);


	return "";
}

