// Amended 26/01/2001 	G Hills 	Fix P206439. Check for invalid months.
// Amended 04/05/2001	G Hills		Fix P206440. Check for spaces in a field.

//Check that the string ctl does not contain spaces - P206440
function ContainsSpace(ctl, message) {
	if (!((ctl.search(/ /)) == -1)) {
			alert ("The " + message + " field may not contain spaces, please try again.")
			return (true);
		}else { 
			return (false);
			}
}

function isDate(ctl,message) {
	if (!(CheckDateFormat(ctl))) {
			alert ("The " + message + " field is not a valid date format or is an invalid date. Please try again.");
			return (true);
		}else { 
			return (false);
			}
}

function CheckNumber(ctl,message) {
	if (isNaN(ctl)) {
			alert ("The " + message + " field is not a valid numeric format. Please try again.");
			return (true);
		}else { 
			return (false);
			}
}

//Check that the length of string in <ctl> is a minimum of length <minlen>
function CheckMinLength(ctl,message,minlen) {
	if (ctl.length<minlen) {
			alert ("The " + message + " field should be a minimum of " + minlen + " characters long. Please try again.");
			return (true);
		}else { 
			return (false);
			}
}

//Check that Date2 is greater than Date 1
//if same=1 then we will reject if the dates are equal
//otherwise reject if 2 is greater than 1
function checkdates (date1,date2,same) {
	var a = new Array()
	var b = new Array()
	var d = new String(date1)

	a = d.split("/")
	d = new String(date2)
	b = d.split("/")
//Check the year first
//if Date2 year is greater then no need to check anything else
	if ((b[2]-0) > (a[2]-0)) return false
//if Date2 year is less than we can return an error
	if ((b[2]-0) < (a[2]-0)) return true

//The year is the same 	
//Check the month
//if Date2 month is greater then no need to check anything else
	if ((b[1]-0) > (a[1]-0)) return false
//If less than we can return an error
	if ((b[1]-0) < (a[1]-0)) return true
//Check the day
	if (same) {
		if ((b[0]-0) >= (a[0]-0)) return false
		return true
	}else{
		if ((b[0]-0) > (a[0]-0)) return false
		return true
	}
}

function CheckDateFormat(dstr) {
	var a = new Array()
	var d = new String(dstr)
	var day,month,year
		
	a = d.split("/")
//split up the string into 3 parts - dd/mm/yyyy
//First check we have three parts
	if (a.length != 3) return false;
//Check that we have no more than 2 digits in parts 1 & 2
//and exactly four digits in part 3
	if (a[0].length !=1 && a[0].length !=2) return false;
	if (a[1].length !=1 && a[1].length !=2) return false;
	if (a[2].length !=4) return false;
	
	day = a[0]-0
	month = a[1]-0
	year = a[2]-0
	//alert ("Day - " + day ) 
	//alert ("Month - " + month ) 
	//alert ("Year - " + year ) 
//Check for the leap year
	if (day ==29 && month==2 && year%4 !=0 && year != 2000) return false;
//Check the months entered are in the range 1-12. Fix for P206439.
	if (month < 1 || month > 12) return false;
//Check for correct day & month combinations
	if (day >=32 || day <=0) return false;
	if (day == 31){
		 if ((month == 2) || (month == 4) || (month == 6) ||(month == 9) || (month == 11)) return false;
	}
	if (day == 30){
		 if (month == 02) return false;
	}
	return true;
}

function isBlank (ctl,message) {
		if ( (ctl.length==0) || (ctl==null) ) {
			alert ("The " + message + " field is currently empty.\nYou need to enter some information for this field and try again.");
			return (true);
		}else { 
			return (false);
			}
}

	function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("The email address format is not valid. It should contain a user name, an @, a host name, and a domain or a domain/country code such as username@hostname.co.uk")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The email address username is not valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("The email address Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The email address domain name is not valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The email address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="The email address is missing a hostname!"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}
// -->

//Matts Stuff

function datesequal(date1,date2)
{
	var a = new Array()
	var b = new Array()
	var d = new String(date1)

	a = d.split("/")
	d = new String(date2)
	b = d.split("/")
//Check the year first
//if Date2 year is greater then we can return an error
	if ((b[2]-0) > (a[2]-0)) return true
//if Date2 year is less than we can return an error
	if ((b[2]-0) < (a[2]-0)) return true

//The year is the same 	
//Check the month
//if Date2 month is greater then we can return an error
	if ((b[1]-0) > (a[1]-0)) return true
//If less than we can return an error
	if ((b[1]-0) < (a[1]-0)) return true
//Check the day
	if ((b[0]-0) == (a[0]-0)) return false
	return true
}

function IsTime(strTime,strField)
 {
  var Timeerror = false;
  var strTestTime = new String(strTime);
    
  if (strTestTime.length > 5) 
  	Timeerror = true;
	
  if (Timeerror == false && strTestTime.indexOf(":",0) == 0) 
  	Timeerror = true;
  
  var nColonPlace = strTestTime.indexOf(":",1);
  if (Timeerror == false && ((parseInt(nColonPlace) + 3) < (strTestTime.length - 1) || (parseInt(nColonPlace) + 2) > (strTestTime.length - 1)))
   Timeerror = true;
  
  if (Timeerror == false && (parseInt(nColonPlace) == 2))
   {
   if (strTestTime.substr(0,2) > 23)
    {Timeerror = true;}
   if (Timeerror == false && strTestTime.substr(3,2) > 59)
    {Timeerror = true;}
   } 
  else if (Timeerror == false && (parseInt(nColonPlace) == 1))
   { 
   if (strTestTime.substr(3,2) > 59)
    {Timeerror = true;}
   }   
  if (Timeerror == true)
   {alert("You have entered an invalid Time for " + strField + ".  Please ensure that the time entered is correct and is in the HH:MM format");}
  return Timeerror;
 }
 
 function comparetime(time1,time2)
//Time time2 should be greater than time1
{
	var a = new Array()
	var b = new Array()
	var d = new String(time1)

	a = d.split(":")
	d = new String(time2)
	b = d.split(":")
//Check the hour first
//if time2 hour is less then we can return an error
	if ((b[0]-0) < (a[0]-0)) return true
//if time2 hour is greater then there is no error
	if ((b[0]-0) > (a[0]-0)) return false
	
//The hour is the same  	
//Check the minute
//If time2 minute is less than we can return an error
	if ((b[1]-0) < (a[1]-0)) return true
//If time2 minute is the same then there is an error
	if ((b[1]-0) == (a[1]-0)) return true
	return false
}


 function minussign (str,strField)
 {
 	var a = new String(str)
 
 	//Check to see whether the first character is a '-' sign
	if (a.substr(0,1) == "-")
		{alert("Negative values for " + strField + " are not valid.  Please re-enter."); return true;}
	else
		{return false;}	
 }
 
 
function isthereadecimal(str,strField)
{
//Test to see if there is a decimal point
var num = /\./

if (str.match(num) == null)
	{return false;}
else
	{alert("Please use only whole integers for "+strField); return true;}
}
// End of Matts stuff

