<!-- 
/******* File Name: forms.asp ******/

var True = true;
var False = false;
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var whitespace = " \t\n\r";	// whitespace characters
var defaultEmptyOK = false;
// Regular expressions
var reWhitespace = /^\s+$/; 
var reLetter = /^[a-zA-Z]$/;  
var reAlphabetic = /^[a-zA-Z]+$/; 
var reAlphanumeric = /^[a-zA-Z0-9]+$/;  
var reDigit = /^\d/; 
var reLetterOrDigit = /^([a-zA-Z]|\d)$/; 
var reInteger = /^\d+$/; // BOI -  one or more digits - EOI.
var reEmail = /^.+\@.+\..+$/; 

function isEmpty(s)		// Check whether string s is empty.
{   
	return ((s == null) || (s.length == 0))
}

function isWhitespace (s)		// Returns true if string s is empty or whitespace characters only.
{   
    return (isEmpty(s) || reWhitespace.test(s));
}

function RTrim(SourceStr)	// remove trailing blanks from string
{
	var str = new String(SourceStr);
	var i = str.length-1;
	while ((i > 0) && (str.charAt(i) == ' ')) {
		i-- ;
	}
	return str.substring(0, ++i);
}

function LTrim(SourceStr)	// remove leading blanks from string
{
	var str = new String(SourceStr);
	var i = 0;
	while ((str.charAt(i) == " ") && (i < str.length)) {
		i++;
	}
	return str.substring(i);
}

function oldTrim(SourceStr)	// remove leading or trailing blanks
{
	var str = new String(SourceStr);
	str = RTrim(LTrim(str));
	return str;
}

function Trim(str)		// remove leading or trailing blanks
{
	var i = 0;
	var j = str.length - 1;
	//var str = new String(SourceStr);
	while ((str.charAt(i) == ' ') && (i < str.length)) i++;	// LTrim
	while (j >= i && str.charAt(j) == ' ') j--;	// RTrim
	return str.substring(i, ++j);
}

function DotTrim(SourceStr)	// remove trailing periods from string
{
	var str = new String(SourceStr).replace(/\,/g ," ");	// replace commas with
	var i = 0;
	while ((str.charAt(i) == ' ') && (i < str.length)) i++;	// LTrim
	str = str.substring(i);
	var i = str.length - 1;
	while (i >= 0 && (str.charAt(i) == ' ' || str.charAt(i) == '.')) i--;	// RTrim
	return str.substring(0, ++i);
}

function Equal(Str1, Str2)	// test that two strings are equal when case is ignored
{ 
	return (String(Str1).toUpperCase() == String(Str2).toUpperCase()); 
}

function DatesEqual(Str1, Str2)		// test if two dates are equal
{ 
	return (Date.parse(Str1).toString() == Date.parse(Str2).toString()); 
}

function validateFld(Obj, fName)	// Require form field with non-blank value
{	
	//alert("validateFld " + Obj.name + " = " +Obj.value);
	if (Obj.value.length == 0 || Obj.value == null || Obj.value == "undefined")
	{
		alert("\""+ fName + "\" is required");
		Obj.focus();
		Obj.select();
		return false;
	}
	return true;
}

function validateSelect(Obj, fName)	// Require select field selection
{	
	//alert("validateSelect " + Obj.name + " = " + Obj.options[Obj.selectedIndex].value);
	if (Obj.options[Obj.selectedIndex].value.length == 0)
	{
		alert("\"" + fName + "\" is required");
		Obj.focus();
		return false;
	}
	return true;
}

function validateEmail(obj, fName, boptional) //******  check for valid email
{
	var boption = boptional;
	if (!boptional) boption = false;
	if (boption && obj.value.length == 0) 
		return true;
	//if (obj.value.length < 10) // need to replace this with better test
	if (!reEmail.test(obj.value)) // hopefully, this is the better test
	{
		if (boption) alert("\"" + fName + "\" is optional, but if entered, must be valid");
		else alert("Valid \"" + fName + "\" is required");
		obj.focus();
		obj.select();
		return false;
	}
	return true;
}

function comparePW(obj, obj2, fName) //******  Compare the two password entries
{
	if (obj.value != obj2.value)
	{
		alert('The two passwords are not the same');
		obj.focus();
		obj.select();
		return false;
	}
	return true;
}


var aMonth = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

function isLeap(year)	//******  check for leap year
{
	if ((year % 4) != 0) return false;	
	if ((year % 400) == 0) return true;
	return true;
}

function checkDayDate(year, month, dayDate)	//******  check for valid date
{
	if (Number(dayDate) < 29) return dayDate;
	switch (month)
	{
	case "Apr":
	case "Jun":
	case "Sep":
	case "Nov":
		//alert(month);
		if (dayDate > "30") return "30";
		break;
	case "Feb":
		//alert("Year " + year + ((isLeap(year))?" is ":" is not ") + "leap year");	
		if (isLeap(year)){
			if (dayDate > "29") return "29"}
		else{
			if (dayDate > "28") return "28"}
		return dayDate;
		break;
	}
	return dayDate;
}

function displayProps(obj) //  displayProps(object); useful for debugging
{
	var output = "";
	for (var prop in obj) {
		output +=  prop + " = " + obj[prop] + "<br>\n";
		}
	return output;
}

function displayCookies() //  Show the cookies associated with this document
{
	var sCookie = "Cookies: " + document.cookie + "<br>";
	return sCookie;
}

//******  Routines used to determine if the contents of a form have changed  *********
//  Example
// capture values in form after page is loaded
// var bb = new Array();
// bb = getFormElements(n)
//
// To determine if any fields have changed: 
// changeStatus = formChanges(n, bb, show) 
// Easiliy modified to return string containing all changed fields for audit trails.
// ************************************************************************

//******  Returns array with values of elements in document.forms[n] *****************

function getFormElements(n) 
{
	var b = new Array();
	var k = length;
	var s = "";
	//alert("getFormElements: " + document.forms[n].name + " has " + document.forms[n].length + " elements\n");
	with (document.forms[n]) 
	{
		for (j = 0; j < length; j++ ) 
		{
			switch (elements[j].type) 
			{
			case "select-one":
				b[j] = "selection:" + elements[j].selectedIndex;
				break;
			case "radio":
				b[j] = elements[j].value + ":" + elements[j].checked;
				break;
			case "checkbox":
				b[j] = elements[j].value + ":" + elements[j].checked;
				break;
			default:
				b[j] = elements[j].value;
				break;
			}
			s += elements[j].name + "=" + b[j] + "; ";		
		}
	}
	//alert("getFormElements: " + s);
	return b;
}

//******  Compare changes in document.forms[n] with values previously stored 
//******  n: form number; b: array of previous element values;
//******  a: array of true/false determines if element is to be checked;
//******  set show to true to display each change in an alert box

function getFormChanges(n, b, show, a) 
{
	var changed = false;
	var c = new Array();
	c = getFormElements(n);
	//alert("formChanges: " + document.forms[n].name + " has " + document.forms[n].length + " elements\n");
	
	with (document.forms[n]) 
	{
		for (j = 0; j < length; j++) 
		{
			bCheck = (a) ? a[j] : true;
			if (a[j] && (c[j] != b[j]) )
			{
				if (show)
					alert(elements[j].name + " changed from " + b[j] + " to " + c[j]);
				changed = true;
			}
		}
	}
	//if ( show && changed) alert("Form values have changed");
	return changed;
}

function skipTo(nURL) 
{
	document.location = nURL;
}

function askUserToSave() // Used by nextURL function in provider pages
{
	var sQuestion = "Data has changed.\n\nSelect \'OK\' to return to this form so you can save your changes.\n\nSelect \'Cancel\' to discard all changes made to this issue.\nIf you select \'Cancel\' you will lose any changes.";
	//return confirm(sQuestion);
	alert("Please save this issue.");
	return true;
}
//  -->
