/*******************************************************************************
MODIFICATION HISTORY

Created On: 7/30/2001 12 pm
Created By: Copied from the RRU project
Description: Include file for client side data validation




*******************************************************************************/

function replaceHTMLString(s)
{
	//s = s.replace(/'/g, "''");
	s = s.replace(/</g, "&lt;");
	s = s.replace(/>/g, "&gt;");
	return s;
}

// Pass a string value to test it for emptyness or all whitespace
function isEmpty(value)
{
	if (value == null || value == "")
		return true;
	else
	{
		// If any non-whitespace characters are found value is not empty
		var sValue = String(value);
		if (sValue.search(/\S/) == -1)
			return true;
		else
			return false;
	}
}

// field is the browser object to be validated
// sFieldName is the user friendly field name of the field object
function RequiredField(field, sFieldName)
{
	this.field = field;
	this.sFieldName = sFieldName;
}

// aRequiredFields must be an array of type RequiredField
function validateRequiredFields(aRequiredFields)
{
	var bError = false;
	var sErrorMessage = "Please fill in the required information:\n";
	// Loop through aRequiredFields checking whether each field is empty or not
	for (var i = 0; i < aRequiredFields.length; i++)
	{
		var field = aRequiredFields[i].field;
		if (field.type == "text" || field.type == "password" || field.type=="hidden" || field.type=="textarea")
		{
			if (isEmpty(field.value))
			{
				bError = true;
				sErrorMessage += aRequiredFields[i].sFieldName + "\n";
			}
		}
		else if (field.type == "select-one")
		{
			if (field.selectedIndex == -1 || field.options[field.selectedIndex].value == "")
			{
				bError = true;
				sErrorMessage += aRequiredFields[i].sFieldName + "\n";
			}
		}
		else
		{
			bError = true;
			sErrorMessage = "Functionality needs to be added to include/validation.js in function validateRequiredFields()";
		}
	}
	if (bError == true)
		return sErrorMessage;
	else
		return "";
}

//***********************************************************
//A function to check if checkString is a valid Email address
//***********************************************************

function checkEmail(checkString)
{
    var newstr = "";
    var at = false;
    var dot = false;

    // DO SOME PRELIMINARY CHECKS ON THE DATA

    // IF EMAIL ADDRESS HAS A '@' CHARACTER
    if (checkString.indexOf("@") != -1) {
      at = true;

    // IF EMAIL ADDRESS HAS A '.' CHARACTER
    } else if (checkString.indexOf(".") != -1) {
      dot = true;
    }
    // PARSE REMAINDER OF STRING
    for (var i = 0; i < checkString.length; i++) {
        ch = checkString.substring(i, i + 1)
        if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
                || (ch == "@") || (ch == ".") || (ch == "_")
                || (ch == "-") || (ch >= "0" && ch <= "9")) {
                newstr += ch;
                if (ch == "@") {
                    at=true;
                }
                if (ch == ".") {
                    dot=true;
                }
        }
    }
    if ((at == true) && (dot == true)) {
 //       return newstr;
 		return true;	
    }
    else {
//      // DISPLAY ERROR MESSAGE
//      alert ("Sorry, the email address you\nentered is not in the correct\nformat.");
//      return checkString;
		return false;
    }
}


function isValidDate(sDate)
{
	if (isEmpty(sDate))
		return true;
	else	
	{
		
		var re = / /g;
		sDate = sDate.replace(re, "");
		var days = [31,28,31,30,31,30,31,31,30,31,30,31];
   		var leap = 0;
		
		//'alert(sDate);
		//remove leading zeroes from dates like 01/01/2000
		if (sDate.charAt(0)=="0")
			sDate=sDate.substr(1);//remove the first 0
		if (sDate.charAt(sDate.indexOf("/")+1)=="0")
			sDate=sDate.substr(0, sDate.indexOf("/")+1) + sDate.substr(sDate.indexOf("/")+2);

		//add 20 or 19 to the year to make it 4 digits long
		var iYear = sDate.substring(sDate.lastIndexOf("/")+1,sDate.length);
		//alert(sDate.lastIndexOf("/"));
		//alert(sDate.length);
		//alert(iYear);
		if (iYear.length < 3 )
		{
			if (iYear > 30)
				sDate = sDate.substring(0,sDate.lastIndexOf("/")+1) + "19" + iYear.toString();
			else
				sDate = sDate.substring(0,sDate.lastIndexOf("/")+1) + "20" + iYear.toString();
		}
		
		//alert(sDate);
		
		// Date.parse only takes 4 digit year
		if (!isNaN(Date.parse(sDate)))
		{
			//alert(Date.parse(sDate));
			var d = new Date(Date.parse(sDate));
			var year = parseInt(d.getFullYear());
			var mon = parseInt(d.getMonth()+1); //getMonth returns 0-11
			var day = parseInt(d.getDate());
			/* alert(d);
			alert(year);
			alert(mon);
			alert(day); */
			if (year<1900 || year>2050)
				return false;
			
			if((mon > 12) || (mon < 1))
		   {
		      return false;
		   }
		 
		   if((mon == 2) && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
		   {
		     leap = 1;
		   }
		 
		   var d1 = days[mon-1] + leap;
		   if((day < 1) || (day > d1))
		   {
		      return false;
		   }

			//else
				//return true;
			// Date object is only valid for 1970 and later.
			// THis is to fix the SQL Server returning the default '1/1/1900' date 
			if (year == 1900)
				return true;
			else
			{
				return (((d.getMonth()+1) + "/" + d.getDate() + "/" + d.getFullYear()) == sDate);
			}
		}
		else
			return false;
	}//(!isNaN(Date.parse(sDate)));
}


// field is the browser object to be validated
// sFieldName is the user friendly field name of the field object
function FormField(field, sFieldName)
{
	this.field = field;
	this.sFieldName = sFieldName;
}

// aRequiredFields must be an array of type RequiredField
function validateDateFields(aDateFields)
{
	var bError = false;
	var sErrorMessage = "The following fields are not in correct date format (mm/dd/yyyy) or are invalid dates:\n";
	// Loop through aDateFields checking whether each field is empty or not
	for (var i = 0; i < aDateFields.length; i++)
	{
		var field = aDateFields[i].field;
		if (field.type == "text" || field.type == "hidden" )
		{
			if (!isValidDate(field.value))
			{
				bError = true;
				sErrorMessage += "\t\t" + aDateFields[i].sFieldName + "\n";
			}
		}
		
		else
		{
			bError = true;
			sErrorMessage = "Functionality needs to be added to include/validation.js in function validateDateFields() for " + field.type;
		}
	}
	if (bError == true)
		return sErrorMessage;
	else
		return "";
}

function isFloat (value)

{   if (isEmpty(value)) 
      return true;
    return (!isNaN(parseFloat(value)));
}

//this little function makes sure that the URl always starts with http://
function formatURL (sURL)
{
	if (sURL.value!=null && sURL.value!="")
	{
		var sNewURL=sURL.value;
		if (sURL.value.substr(0, 7)=="http://")
		{
			sNewURL=sURL.value.slice(7);
		}
		sURL.value= "http://" + sNewURL;
	}
}