<!--
// holds validations function for eTextBox control
// isRequired - provides mandatory field checks 
// noBlanks - provides check for blanks
// toLower & toUpper - provide functions to force case
// checkDataType - checks datatypes
//
//            dateTime            'date & time
//            dateOnly            'date, no time
//            dateOfBirth         '?
//            timeOnly            'time, no date
//            numeric             'number 0-9, with decimal & sign
//            positiveInteger     'number 0-9, no decimals, no sign
//            signedInteger       'number 0-9, no decimals
//            textOnly            'characters a-z, no numbers, no special characters.
//            alphaNumeric        'charcaters a-z, numbers 0-9 & no special characters  
//            printable			  'charcaters a-z, numbers 0-9 & printable special characters 
//            eMail               'valid email address, name@somewhere.com

 
var browserName = navigator.appName;
var browserVer  = parseInt(navigator.appVersion);

var NS4 = (document.layers) ? 1 : 0;
var IE4 = (document.all) ? 1 : 0;

function isRequired(curr_field)
 {
 var imageName = curr_field.id + '_IMG' ;
 var curr_image = document.getElementById(imageName) ;
	if ((browserName == 'Netscape' && browserVer >= 3) ||
		(browserName == 'Microsoft Internet Explorer' && browserVer >= 4)) {
			if (curr_field.value == '') {
			    curr_image.src = '../images/form_exclamationani.gif';
			} else {
			    curr_image.src = '../images/form_blank.gif';
			}
    }
}

//check the field doesn't contain blanks
function noBlanks(field)
{
  if (field.value.indexOf(' ') != -1)
  { 
  		alert("This field can not contain blanks");
		field.focus();
	}
}

//force case functions
function toUpper(txtField)
{
	txtField.value = txtField.value.toUpperCase()
}

function toLower(txtField)
{
	txtField.value = txtField.value.toLowerCase()
}
 
//check for enter key & ignore
function ignoreEnter() 
{ 
	if (event.keyCode == 13) 
		{ 
			event.cancelBubble = true; 
            event.returnValue = false; 
        } 
} 

function checkDataType(dataType, field)
{
	if (!field.value) 
	{
		return true;
	}
	else
	{
		switch(dataType)
		{
			case 'dateTime':						// dateTime            'date & time
			
				break ;
			case 'dateOnly':						// dateOnly            'date, no time
				if(chkdate(field,1)==false)
				{
					alert("The value of this field should be a date in the format DD/MM/YYYY");
					field.focus();
					return false;
				}
				break ;
			case 'timeOnly':						// timeOnly            'time, no date
				if(IsValidTime(field.value)==false)
				{
					alert("The value of this field should be a time in the format HH:MM");
					obj.focus();
					return false;
				}
				break ;
			case 'numeric':							// numeric             'number 0-9, with decimal & sign
				if(isNaN(field.value)==true)
				{
					alert("The value of this field should be numeric, i.e. only contain the numbers between 0 and 9, a plus/minus sign and/or a decimal point");
					field.focus();
					return false;
				}
				break ;
			case 'positiveInteger' :				// positiveInteger     'number 0-9, no decimals, no sign
				var valid = true ;
				if(isNaN(field.value)==true)
				{
					valid = false ;
				}
				else
				{
					if (Math.floor(field.value) != Math.abs(field.value))
					{
						valid = false ;
					}						
				}
				if (valid == false)
				{
					alert("The value of this field should be a positive integer, i.e. only contain the numbers between 0 and 9");
					field.focus();
					return false;
				}
				break ;
			case 'signedInteger':					// signedInteger       'number 0-9, no decimals
				var valid = true ;
				if(isNaN(field.value)==true)
				{
					valid = false ;
				}
				else
				{
					if (floor(field.value) != field.value)
					{
						valid = false ;
					}						
				}
				if (valid == false)
				{
					alert("The value of this field should be an integer, i.e. only contain the numbers between 0 and 9 and optionally a plus/minus sign");
					field.focus();
					return false;
				}
				break ;
			case 'textOnly':						// textOnly            'characters a-z, no numbers, no special characters.
				var c = "" ;
				for (var i = 0; i < field.value.length; i++) 
				{
					c = field.value.charAt(i).toLowerCase()
					if (c != " ")										// blank check elsewhere 
					{
						if (c < 'a' || c > 'z') 
						{
							alert("The value of this field should be Text, i.e. only contain the letters between A and Z");
							field.focus();
							return false;
						}
					}
				}	
				break ;
			case 'alphaNumeric':				// alphaNumeric        'charcaters a-z, numbers 0-9 & no special characters  
				var c = "" ;
				for (var i = 0; i < field.value.length; i++) 
				{
					c = field.value.charAt(i).toLowerCase()
					if (isAlphaNumeric(c) != true )
					{
						alert("The value of this field should be alphaNumeric, i.e. only contain the letters between A and Z and/or the numbers between 0 and 9 ");
						field.focus();
						return false;
					}
				}	
				break ;
		case 'printable':						// printable			  'charcaters a-z, numbers 0-9 & printable special characters 
				var checkText = "'~!@#%&_`-=;,./{}|:<>"				//remaining printable characters
				var c = "" ;
				for (var i = 0; i < field.value.length; i++) 
				{
					c = field.value.charAt(i).toLowerCase()
					if (isAlphaNumeric(c) != true)							// not alphaNumeric keep checking
					{		// check for regular expression special characters
						if (c != "\"" && c != "\\" && c != "\^" && c != "\$" && c != "\*" && c != "\+" && c != "\?" && c != "\." && c != "(" && c != ")" && c != "[" && c != "]") 
						{
						if (checkText.search(c) == -1) 
							{
								alert("The value of this field should be printable, i.e. only contain characters that can be printed");
								field.focus();
								return false;
							}	
						}
					}
				}	
				break ;
			case 'eMail':						// eMail               'valid email address, name@somewhere.com
				emailReg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
				if (!emailReg.test(field.value))
					{
						alert("The value of this field should be a valid email address, i.e. name@somewhere.com.au");
						field.focus();
						return false;
					}
				break;
		}
	}
}

function isAlphaNumeric(character)
{
	if (character != " ")										// blank check elsewhere 
	{
		if ((character < 'a' || character > 'z')  && (isNaN(character) == true))
		{
			return false;
		}
	}
	return true
}	

function IsValidTime(StrObj)
{
	var timeOk = true;

	if (StrObj.length > 0)
	{
		var timeArray = StrObj.match(/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5]\d$/);
		if (!timeArray)
		{
			if (StrObj != '24:00')
			{
				timeOk = false;
			}
		}
	}  
	return timeOk;
}

// Start Date check
function chkdate(objName,formatType) 
{
  //var strDatestyle = "US"; //United States date style
  var strDatestyle = "EU";  //European date style
  var strDate;
  var strDateArray;
  var strDay; 
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var booFound = false;
  var datefield = objName;
  var strSeparatorArray = new Array("-"," ","/",".");
  var intElementNr;
  var err = 0;
  var strMonthArray = new Array(12);

  strMonthArray[0] = "01";
  strMonthArray[1] = "02";
  strMonthArray[2] = "03";
  strMonthArray[3] = "04";
 
  strMonthArray[4] = "05";
  strMonthArray[5] = "06";
  strMonthArray[6] = "07";
  strMonthArray[7] = "08";
  strMonthArray[8] = "09";
  strMonthArray[9] = "10";
  strMonthArray[10] = "11";
  strMonthArray[11] = "12";
  strDate = datefield.value;


  if (strDate.length < 1) 
  {
    return true;
  }

  // Seperator check
  var seperatorArray = strDate.match(/(-|\/|\s)/);
  if (!seperatorArray)
  {
  	// No seperator found
    return false;
  } 

  for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
  {
    if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
    {
      strDateArray = strDate.split(strSeparatorArray[intElementNr]);
      if (strDateArray.length != 3) 
      {
        err = 1;
        return false;
      }
      else 
      {
        strDay = strDateArray[0];
        strMonth = strDateArray[1];
        strYear = strDateArray[2];
      }
      booFound = true;
    }
  }

  if (booFound == false) 
  {
    if (strDate.length > 5) 
    {
      strDay = strDate.substr(0, 2);
      strMonth = strDate.substr(2, 2);
      strYear = strDate.substr(4);
    }
  }
  
  // Sanity check on year
	if ((strYear.length > 4) || (strYear.length < 2) || (strYear.length == 3))
  {
    return false;    
  }
  else
  {
  	if (isNaN(strYear))
  	{
  		return false;
  	}
  }
	
  if (strYear.length == 2) 
  {
  	if (formatType == 1)
  	{
    	strYear = '20' + strYear;
    }
    else
    {
    	strYear = '19' + strYear;
    }
  }

  // Sets a Lower and Upper limit to what a Year can be
  var today = new Date()
  var todayYear=today.getYear()
  //Y2K bug fix
  if (todayYear < 1000)
	{
		todayYear+=1900
	}	
  
  var upperBoundYear = todayYear + 10
  var lowerBoundYear = todayYear - 150

  if (strYear < lowerBoundYear)
  {
	return false;
  }

  if (strYear > upperBoundYear)
  {
	return false;
  }


  // US style
  if (strDatestyle == "US") 
  {
    strTemp = strDay;
    strDay = strMonth;
    strMonth = strTemp;
  }

  intday = parseInt(strDay, 10);
  if (isNaN(intday)) 
  {
    err = 2;
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) 
  {
    for (i = 0;i<12;i++) 
    {
      if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
      {
        intMonth = i+1;
        strMonth = strMonthArray[i];
        i = 12;
      }
    }
    if (isNaN(intMonth)) 
    {
      err = 3;
      return false;
    }
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) 
  {
    err = 4;
    return false;
  }

  if (intMonth>12 || intMonth<1) 
  {
    err = 5;
    return false;
  }

  if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
  {
    err = 6;
    return false;
  }

  if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
  {
    err = 7;
    return false;
  }

  if (intMonth == 2) 
  {
    if (intday < 1) 
    {
      err = 8;
      return false;
    }
    if (LeapYear(intYear) == true) 
    {
      if (intday > 29) 
      {
        err = 9;
        return false;
      }
    }
    else 
    {
      if (intday > 28) 
      {
        err = 10;
        return false;
      }
    }
  }

  if (strDatestyle == "US") 
  {
    datefield.value = strMonthArray[intMonth-1] + " " + intday + " " + strYear;
  }
  else 
  {
    datefield.value = intday + "/" + strMonthArray[intMonth-1] + "/" + strYear;
  }
  return true;
}


function LeapYear(intYear) 
{
  if (intYear % 100 == 0) 
  {
    if (intYear % 400 == 0) 
    { return true; }
  }
  else 
  {
    if ((intYear % 4) == 0) 
      { return true; }
  }
  return false;
}
//  End Date Check



//-->