/*'#################################################################################
' Inputs :
'		(strFormName)		: the name of the current form
'		(strTextFieldName)	: the name of the textbox being checked
'		(strMessage)		: message to display in alert box if textfield is blank
'
' Actions :
'		trims the textbox field to remove trailing or leading spaces
'		checks to see if the text field is blank
'		if blank an alert is displayed, the textbox is selected and focused
'
' Outputs : FALSE if the fields is blank
'			TRUE if the field is not blank
'################################################################################# */
function isTextFieldBlank(strFormName, strTextFieldName, strMessage)
{

	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')

	strData = trim(strData)	
	if (strData == "")
	{
		if (strMessage > "")
			{
			alert(strMessage)
			}
		eval(myTextbox.select())
		eval(myTextbox.focus())
		return false;
	}
	else
		return true;

}

/*'#################################################################################
' Inputs :
'		(strText)	: some text
'
' Actions :
'		trims the textbox field to remove trailing or leading spaces
'
' Outputs :  the trimmed string of text
'################################################################################# */
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

/*'#################################################################################
' Inputs :
'		(strFormName)		: the name of the current form
'		(strTextFieldName)	: the name of the textbox being checked
'		(strMessage)		: message to display in alert box if textfield is blank
'		(strAllowableText)	: string of legal characters
' Actions :
'		checks strText to ensure it contains only the characters specified by strAllowable Text
'
' Outputs :  TRUE if it is a number
'			 FALSE if it is not a number
'################################################################################# */
function doesTextFieldContain(strFormName, strTextFieldName, strMessage, strAllowableText) {
	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')
	strData = trim(strData)
	var allowed = strAllowableText; // define valid characters
    for (var i=0; i< strData.length; i++) {
        if (allowed.indexOf(strData.charAt(i)) == -1) {
			if (!(strMessage==""))
				alert(strMessage)
			eval(myTextbox.select())
			eval(myTextbox.focus())
			return false;
        }
    }
    return true;
}

/*'#################################################################################
' Inputs :
'		(strFormName)		: the name of the current form
'		(strTextFieldName)	: the name of the textbox being checked
'		(strMessage)		: message to display in alert box if textfield is blank
'		(intMinValue)		: the min value allowable (float or int)
'		(intMaxValue)		: the max value allowable (float or int)
'		(strNumType)		: flag of number type : F (float) I (integer)
' Actions :
'		checks to see that the value in text field specified is a number of type 'strNumType'
'		(if not it returns false), then it checks that number is between min and max values
'
' Outputs :  TRUE if number is between min and max
'			 FALSE if number is NOT between min and max
'################################################################################# */
function isTextFieldNumberBetween(strFormName, strTextFieldName, strMessage, intMinValue, intMaxValue, strNumType) {
	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')
	strData = trim(strData)
	
	if (strNumType == 'f')
	{
		if (doesTextFieldContain(strFormName, strTextFieldName, strMessage, '1234567890.'))
		{
			if ((parseFloat(strData) < parseFloat(intMinValue)) || (parseFloat(strData) > parseFloat(intMaxValue)))
			{
				if (!(strMessage==""))
					alert(strMessage)
				eval(myTextbox.select())
				eval(myTextbox.focus())
				return false;
			}
		}
		else
			return false;
	}
	else if (strNumType == 'i')
	{
		if (doesTextFieldContain(strFormName, strTextFieldName, strMessage, '1234567890'))
		{
			if ((parseInt(strData) < parseInt(intMinValue)) || (parseInt(strData) > parseInt(intMaxValue)))
			{
				if (!(strMessage==""))
					alert(strMessage)
				eval(myTextbox.select())
				eval(myTextbox.focus())
				return false;
			}
		}
		else
			return false;
	}
	return true;
}
/*
'#################################################################################
' Inputs :
'		(strFormName)		: the name of the current form
'		(intElementID)		: the id of the current form element
'		(strMessage)		: message to display if droplist is not selected
'
' Actions :
'		checks to see if the current droplist value equals the string value
'		specified by strEmpty (typically '' or '0')
'		if values match then and alert is shown and the droplist received focus
'
' Outputs : FALSE if the droplist value equals the strEmpty value
'			TRUE if the droplist value does not equal the strEmpty value
'################################################################################# */
function areTextFieldsDate(strFormName, intElementID, strMessage)
{
	var myDay = eval('document.' + strFormName + '.Day' + intElementID + '.value');
	var myMonth = eval('document.' + strFormName + '.Month' + intElementID + '.value');
	var myYear = eval('document.' + strFormName + '.Year' + intElementID + '.value');
	
/* ' check that all fields are not blank */
	if (!isTextFieldBlank(strFormName, 'Day'+intElementID, strMessage))
		return false;
	if (!isTextFieldBlank(strFormName, 'Month'+intElementID, strMessage))
		return false;
	if (!isTextFieldBlank(strFormName, 'Year'+intElementID, strMessage))
		return false;
/* ' check that all fields contain only number */
	if (!doesTextFieldContain(strFormName, 'Day'+intElementID, strMessage, "1234567890"))
		return false;
	if (!doesTextFieldContain(strFormName, 'Month'+intElementID, strMessage, "1234567890"))
		return false;
	if (!doesTextFieldContain(strFormName, 'Year'+intElementID, strMessage, "1234567890"))
		return false;

/* ' check day/month ranges */
	if (myDay<=0 || myDay >31)
	{dateBoxWarning(strFormName, 'Day'+intElementID, strMessage)
	return false;}
		
	if (myMonth<=0 || myMonth >12)
	{dateBoxWarning(strFormName, 'Month'+intElementID, strMessage)
	return false;}

	if (myYear <1000 || myYear>9999)
	{dateBoxWarning(strFormName, 'Year'+intElementID, strMessage)
	return false;}

	if (!checkMonthLength(myMonth,myDay))
		{dateBoxWarning(strFormName, 'Month'+intElementID, '')
		return false;}

	if (myMonth == 2)
		if (!checkLeapMonth(myMonth,myDay,myYear)) 
			{dateBoxWarning(strFormName, 'Month'+intElementID, '')
			return false}

/* ' if all is well return true */
	return true;
}

function isDateEntered(strFormName, intElementID)
{
	var myDay = eval('document.' + strFormName + '.Day' + intElementID + '.value');
	var myMonth = eval('document.' + strFormName + '.Month' + intElementID + '.value');
	var myYear = eval('document.' + strFormName + '.Year' + intElementID + '.value');

	if (myDay != '' || myMonth != '' || myYear != '')
		return true;
	else
		return false;
}

/*'#################################################################################
' Inputs :
'		(mm)		: a value between 1 and 12 representing the month
'		(dd)		: a value between 1 and 31 representing the day
'
' Actions :
'		checks to see that the number of days specified is legal for the month 
'		specified.  If not an error message is displayed with the name of the month
'		and the allowable number of days.
' Outputs : FALSE if the day value is not valid for month specified
'			TRUE if the date is valid
'################################################################################# */
function checkMonthLength(mm,dd) {
	mm = parseInt(mm)
	var months = new Array("","January","February","March","April","May","June","July","August","September","October","November","December")
	if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) && dd > 30) {
		alert(months[mm] + " has only 30 days.")
		return false
	} else if (dd > 31) {
		alert(months[mm] + " has only 31 days.")
		return false
	}
	return true
}

/*'#################################################################################
' Inputs :
'		(mm)		: a value between 1 and 12 representing the month
'		(dd)		: a value between 1 and 31 representing the day
'		(yyyy)		: a value between 1000 and 9999 representing the year
'
' Actions :
'		checks to see if the month of february for the specified year
'		is a leap year (i.e. contains 29 days)
' Outputs : FALSE if the day value is not valid for Feb
'			TRUE if the date is valid
'################################################################################# */
function checkLeapMonth(mm,dd,yyyy) {
	if (yyyy % 4 > 0 && dd > 28) {
		alert("February of " + yyyy + " has only 28 days.")
		return false
	} else if (dd > 29) {
		alert("February of " + yyyy + " has only 29 days.")
		return false
	}
	return true
}

/*'#################################################################################
' Inputs :
'		(strFormName)		: the name of the current form
'		(strElement)		: the name of the text box control
'		(strMessage)		: message to display if date incorrect (optional)
'
' Actions :
'		if strMessage is not blank and alert box is displayed 
'		the contents of strElement are then selected and given focus
'
' Outputs : n/a
'################################################################################# */
function dateBoxWarning(strFormName, strElement, strMessage)
{
	var myTextbox = eval('document.' + strFormName + '.' + strElement)
	if (strMessage !="")
		alert(strMessage)
	eval(myTextbox.select())
	eval(myTextbox.focus())
}


/*'#################################################################################
' Inputs :
'
' Actions :
'
' Outputs : 
'################################################################################# */
function dateComparison(strFormName ,intElementID, strCompDay, strCompMonth, strCompYear, strComparison, strMessage)
{
	var myDay = eval('document.' + strFormName + '.Day' + intElementID + '.value');
	var myMonth = eval('document.' + strFormName + '.Month' + intElementID + '.value');
	var myYear = eval('document.' + strFormName + '.Year' + intElementID + '.value');
	var myDayBox = eval('document.' + strFormName + '.Day' + intElementID)
	var blnResult

	// Note : Javascript accepts dates from 0-11 not 1-12
	var dteBase = new Date(myYear, myMonth-1, myDay)
	var dteCompare = new Date(strCompYear, strCompMonth-1, strCompDay)
	if (eval('dteCompare' + strComparison + 'dteBase'))
		blnResult = true;
	else
		blnResult =  false;
	
	if (blnResult == false)
	{
		if (strMessage !="")
			alert(strMessage)
		eval(myDayBox.select())
		eval(myDayBox.focus())
		return false;
	}
	else
		return true;

} 

/*'#################################################################################
' Inputs :
'
' Actions :
'
' Outputs : 
'################################################################################# */
function dateComparisonDP(strFormName ,intElementID1, intElementID2, strComparison, strMessage)
{
	var myDay1 = eval('document.' + strFormName + '.Day' + intElementID1 + '.value');
	var myMonth1 = eval('document.' + strFormName + '.Month' + intElementID1 + '.value');
	var myYear1 = eval('document.' + strFormName + '.Year' + intElementID1 + '.value');
	var blnResult

	// Note : Javascript accepts dates from 0-11 not 1-12
	var dteBase = new Date(myYear1, myMonth1-1, myDay1)

	var myDay2 = eval('document.' + strFormName + '.Day' + intElementID2 + '.value');
	var myMonth2 = eval('document.' + strFormName + '.Month' + intElementID2 + '.value');
	var myYear2 = eval('document.' + strFormName + '.Year' + intElementID2 + '.value');
	// Note : Javascript accepts dates from 0-11 not 1-12
	var dteCompare = new Date(myYear2, myMonth2-1, myDay2)
	
	if (eval('dteBase' + strComparison + 'dteCompare'))
		blnResult = false;
	else
		blnResult =  true;
	
	if (blnResult == false)
	{
		if (strMessage !="")
			alert(strMessage)
		return false;
	}
	else
		return true;

} 

/*'#################################################################################
' Inputs :
'
' Actions :
'   Postcode must conform to the following rules :
'	(1) The total length must be 6,7, or 8 characters, a gap (space character) must be included 
'	(2) The inward code, the part to the right of the gap, must always be 3 characters 
'	(3) The first character of the inward code must be numeric 
'	(4) The second and third characters of the inward code must be alpha 
'	(5) The outward code, the part to the left of the gap, can be 2,3, or 4 characters 
'	(6) The first character of the outward code must be alpha 
'	(7) No non-numeric/non alpha characters (except single space) allowed
' Outputs : 
'	Boolean
'################################################################################# */
function isTextFieldPostcode(strFormName, strTextFieldName, strMessage)
{
	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')
	var size, i;
	size = strData.length
	strData = strData.toUpperCase(); //Change to uppercase
	
	// ensure no non-number/alpha characters (except space)
	for(i=0;i<size;i++)
	{
		if (strData.charAt(i) != " ")
		{
			if ((strData.charAt(i) < "0" || strData.charAt(i) > "9") && (strData.charAt(i) < "A" || strData.charAt(i) > "Z"))
			{
			ShowMessageAndSetFocus(myTextbox, strMessage)
			return false;
			}
		}
	}
	
 while (strData.slice(0,1) == " ") //Strip leading spaces
  {strData = strData.substr(1,size-1);size = strData.length
  }
 while(strData.slice(size-1,size)== " ") //Strip trailing spaces
  {strData = strData.substr(0,size-1);size = strData.length
  }
 
 if (size < 6 || size > 8){ //Code length rule
  ShowMessageAndSetFocus(myTextbox, strMessage)
  return false;
  }
 if (strData.charAt(0) < "A"  || strData.charAt(0) > "Z"){ //leftmost character must be alpha character rule
  ShowMessageAndSetFocus(myTextbox, strMessage)
  return false;
  }
if (strData.charAt(size-3) < "0"  || strData.charAt(size-3) > "9"){ //first character of inward code must be numeric rule  
  ShowMessageAndSetFocus(myTextbox, strMessage)
  return false;
  }
  
 if (strData.charAt(size-2) < "A"  || strData.charAt(size-2) > "Z"){ //second character of inward code must be alpha rule
  ShowMessageAndSetFocus(myTextbox, strMessage)
  return false;
  }
 
 if (strData.charAt(size-1) < "A"  || strData.charAt(size-1) > "Z"){ //third character of inward code must be alpha rule
  ShowMessageAndSetFocus(myTextbox, strMessage)
  return false;
  }
  
 if (!(strData.charAt(size-4) == " ")){//space in position length-3 rule
  ShowMessageAndSetFocus(myTextbox, strMessage)
  return false;
   }
   
 count1 = strData.indexOf(" ");count2 = strData.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
  ShowMessageAndSetFocus(myTextbox, strMessage)
  return false;
  }
return true;
}


function ShowMessageAndSetFocus(objTextbox, sMessage)
{
objTextbox.select()
objTextbox.focus()
alert(sMessage)
}

/*'#################################################################################
' Inputs :
'		(strFormName)		: the name of the current form
'		(strTextField)		: the name of the textboxe containg the email address
'		(strMessage)		: message to display in alert box if textfield is blank
'
' Actions :
'		checks to see if the email address contains valid characters
'		if not an alert is displayed and the textbox is selected and focused
'
' Outputs : FALSE if the email address is invalid
'			TRUE if the email address is valid
'################################################################################# */
function isTextFieldEmail(strFormName, strTextFieldName, strMessage) {

	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')

	strData = trim(strData)	
    if (strData.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
    {
		ShowMessageAndSetFocus(myTextbox, strMessage)
		return false;
	}
}
