//'#################################################################################
//' 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);
			
			tabFocus(myTextbox);
			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
//' Actions :
//'		checks strText to ensure it contains only numbers
//'
//' Outputs :  TRUE if it is a number
//'			 FALSE if it is not a number
//'################################################################################# 
function isTextFieldNumber(strFormName, strTextFieldName, strMessage) {
	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')
	strData = trim(strData)
	var allowed = '0123456789'; // define valid characters
    for (var i=0; i< strData.length; i++) {
        if (allowed.indexOf(strData.charAt(i)) == -1) {
			if (strMessage !="")
			{
				alert(strMessage);
				
				tabFocus(myTextbox);
				eval(myTextbox.select())
				eval(myTextbox.focus())
			}
			return false;
        }
    }
    return true;
}

//'#################################################################################
//' Inputs :
//'
//' Actions :
//'
//' Outputs : 
//'################################################################################# 
function dateComparison(strFormName, strDateOne, strComparison, strDateTwo, strMessage)
{
	var dateoneDay = eval('document.' + strFormName + '.' + strDateOne + 'day.value');
	var dateoneMonth = eval('document.' + strFormName + '.' + strDateOne + 'month.value');
	var dateoneYear = eval('document.' + strFormName + '.' + strDateOne + 'year.value');

	var datetwoDay = eval('document.' + strFormName + '.' + strDateTwo + 'day.value');
	var datetwoMonth = eval('document.' + strFormName + '.' + strDateTwo + 'month.value');
	var datetwoYear = eval('document.' + strFormName + '.' + strDateTwo + 'year.value');

	var blnResult
	
	var myTextbox = eval('document.' + strFormName + '.' + strDateTwo)
	
	// Note : Javascript accepts dates from 0-11 not 1-12
	var dteBase = new Date(dateoneYear, dateoneMonth-1, dateoneDay)
	var dteCompare = new Date(datetwoYear, datetwoMonth-1, datetwoDay)
	
	if (eval('dteBase' + strComparison + 'dteCompare'))
		blnResult = true;
	else
		blnResult =  false;
	
	if (blnResult == true)
	{
		if (strMessage !="")
			alert(strMessage)
			
			tabFocus(myTextbox);
		return true;
	}
	else
		return false;

} 

//'#################################################################################
//' 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
//'		(sAllowChars)		: boolean which determines if the allowable text is allowable or not
//' 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, sAllowChars) {
	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')
	strData = trim(strData)
	var allowed = strAllowableText; // define valid characters
	var sComparison;
	
	if (sAllowChars)
		sComparison = "=="	
	else
		sComparison = "!="
		
	
    if (strData.length == 0)
	{
		if (!(strMessage==""))
		{
			alert(strMessage)
			
			tabFocus(myTextbox);
			eval(myTextbox.select())
			eval(myTextbox.focus())
		}
		return false;
	}
		
    for (var i=0; i< strData.length; i++) {
        if (eval('allowed.indexOf(strData.charAt(i))' + sComparison + '-1')) {
			if (!(strMessage==""))
			{
				alert(strMessage)
				
				tabFocus(myTextbox);
				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 that number is between min and max values (NOTE : you must ensure that the form 
//'		fields contain number values otherwise a javascript error will be thrown)
//'
//' 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 ((parseFloat(strData) < parseFloat(intMinValue)) || (parseFloat(strData) > parseFloat(intMaxValue)))
		{
			if (!(strMessage==""))
				alert(strMessage)
				
			tabFocus(myTextbox);
			eval(myTextbox.select())
			eval(myTextbox.focus())
			return false;
		}
	}
	else if (strNumType == 'I')
	{
		if ((parseInt(strData) < parseInt(intMinValue)) || (parseInt(strData) > parseInt(intMaxValue)))
		{
			if (!(strMessage==""))
				alert(strMessage)
				
			tabFocus(myTextbox);
			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
//'		(strNotAllowable)	: string of legal characters
//' Actions :
//'		checks strText to ensure it doesn't contain text specified in strNotAllowable
//'
//' Outputs :  TRUE if it is allowed
//'			 FALSE if it is not allowed
//'################################################################################# %>
function doesTextFieldAllow(strFormName, strTextFieldName, strMessage, strNotAllowable) {
	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')
	strData = trim(strData)
	var allowed = strNotAllowable; // define valid characters

    for (var i=0; i< strData.length; i++) {
        if (allowed.indexOf(strData.charAt(i)) != -1) {
			if (!(strMessage==""))
			{
				alert(strMessage)
				
				tabFocus(myTextbox);
				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
//' Actions :
//'		checks strText to ensure it doesn't contain a space character
//'
//' Outputs :  TRUE if it is allowed
//'			 FALSE if it is not allowed
//'################################################################################# %>

function noSpaceInFileName(strFormName, strTextFieldName, strMessage) {
	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')
	strData = trim(strData)

	var strSlash = "\\"; // define valid characters
	var strSpace = " ";
	var blnSlashFound;
	blnSlashFound=false;

    for (var i=(strData.length-1); i > -1; i--) 
		{
        if ((strSlash.indexOf(strData.charAt(i)) != -1) && (!blnSlashFound)) 
			{
			//dont go in here again
			blnSlashFound=true;
			for (var j=(strData.length-1); j > i; j--) 
				{
			    if (strSpace.indexOf(strData.charAt(j)) != -1) 
					{
					if (!(strMessage==""))
						{
						alert(strMessage)
						
						tabFocus(myTextbox);
						eval(myTextbox.select())
						eval(myTextbox.focus())
						}
					return false;
			    }
			}
		}
	}
    return true;	//else all ok
}


//'#################################################################################
//' 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 :
//'		checks strText to ensure it does contain an extension (.)
//'
//' Outputs :  TRUE if it is allowed
//'			 FALSE if it is not allowed
//'################################################################################# %>

function noExtentionOnFileNameOld(strFormName, strTextFieldName, strMessage) {
	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')
	strData = trim(strData)
	
	var bSlash = false
	var bDot = false
    for (var i=(strData.length-1); i > -1; i--) 
	{
		if (strData.charAt(i) == "\\" || strData.charAt(i) == "/")
			bSlash = true;
		if (strData.charAt(i) == ".")
			bDot = true;
	}
	
	if (bDot && bSlash)
		return true;
	
	if (!(strMessage==""))
		{
		alert(strMessage)
		
		tabFocus(myTextbox);
		eval(myTextbox.select())
		eval(myTextbox.focus())
		}
    return false;	//else not good
}

function noExtentionOnFileName(strFormName, strTextFieldName, strMessage) {
	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')
	strData = trim(strData)

	var strSlash = "\\/"; // define valid characters
	var strDot = ".";
	var blnSlashFound;
	blnSlashFound=false;

    for (var i=(strData.length-1); i > -1; i--) 
		{
        if ((strSlash.indexOf(strData.charAt(i)) != -1) && (!blnSlashFound)) 
			{
			//dont go in here again
			blnSlashFound=true;
			for (var j=(strData.length-1); j > i; j--) 
				{
			    if (strDot.indexOf(strData.charAt(j)) != -1) 
					{

					return true;
			    }
			}
		}
	}
	if (!(strMessage==""))
		{
		alert(strMessage)
		
		tabFocus(myTextbox);
		eval(myTextbox.select())
		eval(myTextbox.focus())
		}
    return false;	//else not good
}


//'#################################################################################
//' Inputs :
//'		(strFormName)		: the name of the current form
//'		(strTextboxNames)	: the common starting chars of the textbox names
//'		(textboxNumberStart): the number of the first textbox (appened to strTextboxNames above e.g. myBox1)
//'		(textboxNumberEnd): the number of the last textbox (appened to strTextboxNames above e.g. myBox5)
//'
//' Actions :
//'		for each textbox which begins with the name 'strTextboxNames' which appears in 
//'		the sequence between 'textboxNumberStart' and 'textboxNumberEnd' the textbox values
//'		are flipped around so that myBox1.value will appear in myBox5 and vice versa (etc.)
//'
//' Outputs : [none]
//'################################################################################# 
function flipTextboxValues(strFormName, strTextboxNames, textboxNumberStart, textboxNumberEnd)
{
	var objOrders;
	var myForm = eval('document.'+strFormName)
	objOrders = new Array(textboxNumberEnd)
	for (i=1;i<=textboxNumberEnd;i++)
		eval ('objOrders[i-1] = myForm.' + strTextboxNames + i + '.value')

	arrayElement = textboxNumberEnd
	
	for (i=1;i<=textboxNumberEnd;i++)
	{
		eval('myForm.' + strTextboxNames + i + '.value=objOrders[arrayElement-1]');
		arrayElement--;
	}
}


//'#################################################################################
//' 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 + '.' + intElementID + 'day.value');
	var myMonth = eval('document.' + strFormName + '.' + intElementID + 'month.value');
	var myYear = eval('document.' + strFormName + '.' + intElementID + 'year.value');
	
// ' check that all fields are not blank 
	if (!isTextFieldBlank_noFocus(strFormName, intElementID+'day', strMessage))
		return false;
	if (!isTextFieldBlank_noFocus(strFormName, intElementID+'month', strMessage))
		return false;
	if (!isTextFieldBlank_noFocus(strFormName, intElementID+'year', strMessage))
		return false;
// ' check that all fields contain only number 
	if (!doesTextFieldContain(strFormName, intElementID+'day', strMessage, "1234567890", true))
		return false;
	if (!doesTextFieldContain(strFormName, intElementID+'month', strMessage, "1234567890", true))
		return false;
	if (!doesTextFieldContain(strFormName, intElementID+'year', strMessage, "1234567890", true))
		return false;

// ' check day/month ranges 
	if (myDay<=0 || myDay >31)
	{dateBoxWarning(strFormName, intElementID+'day', strMessage)
	return false;}
		
	if (myMonth<=0 || myMonth >12)
	{dateBoxWarning(strFormName, intElementID+'month', strMessage)
	return false;}

	if (myYear <1000 || myYear>9999)
	{dateBoxWarning(strFormName, intElementID+'year', strMessage)
	return false;}

	if (!checkMonthLength(myMonth,myDay))
		{dateBoxWarning(strFormName, intElementID+'month', '')
		return false;}

	if (myMonth == 2)
		if (!checkLeapMonth(myMonth,myDay,myYear)) 
			{dateBoxWarning(strFormName, intElementID+'month', '')
			return false}

// ' if all is well return true  
	return true;
}

function isDateEntered(strFormName, intElementID)
{
	var myDay = eval('document.' + strFormName + '.' + intElementID + 'day.value');
	var myMonth = eval('document.' + strFormName + '.' + intElementID + 'month.value');
	var myYear = eval('document.' + strFormName + '.' + intElementID + 'year.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)
		
	//tabFocus(myTextbox);
	//eval(myTextbox.select())
	//eval(myTextbox.focus())
}


//'#################################################################################
//' Inputs :
//'
//' Actions :
//'
//' Outputs : 
//'################################################################################# 
function dateComparisonElement(strFormName ,intElementID, strCompDay, strCompMonth, strCompYear, strComparison, strMessage)
{
	var myDay = eval('document.' + strFormName + '.' + intElementID + 'day.value');
	var myMonth = eval('document.' + strFormName + '.' + intElementID + 'month.value');
	var myYear = eval('document.' + strFormName + '.' + intElementID + 'year.value');
	var myDayBox = eval('document.' + strFormName + '.' + intElementID + 'day')
	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)
			
		//tabFocus(myTextbox);
		//eval(myDayBox.select())
		//eval(myDayBox.focus())
		return false;
	}
	else
		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
//'
//' 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_noFocus(strFormName, strTextFieldName, strMessage)
{

	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')

	strData = trim(strData)	
	if (strData == "")
	{
		if (strMessage !="")
		{
			alert(strMessage)
			
			//tabFocus(myTextbox);
			//eval(myTextbox.select())
			//eval(myTextbox.focus())
		}
		return false;
	}
	else
		return true;

}

function isTextFieldEmail(strFormName, strTextFieldName, strMessage) {

	var myTextbox = eval('document.' + strFormName + '.' + strTextFieldName)
	var strData = eval('myTextbox.value')

    if (strData.search(/^\w+((-\w+)|(\.\w+)|(\&\w+)|(\'\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
        return true;
    else
    {
		if (strMessage > "")
			alert(strMessage)
			
		tabFocus(myTextbox);
		eval(myTextbox.select())
		eval(myTextbox.focus())
		return false;
	}
}
