//Last Update 1-12-2004
var defaultEmptyOK = false;

function uBreakContent(objText,sElementName)
{
  var FormLimit = 102399

  var TempVar = new String
  TempVar = objText.value
  
  //alert("total:" + TempVar.length)
  
  if (TempVar.length > FormLimit)
  { //alert("over limit")
    objText.value = TempVar.substr(0, FormLimit)
    TempVar = TempVar.substr(FormLimit)
    while (TempVar.length > 0)
    { var objTEXTAREA = document.createElement(sElementName)
      objTEXTAREA.name = objText.name
      objTEXTAREA.value = TempVar.substr(0, FormLimit)
      document.f1.appendChild(objTEXTAREA)

      TempVar = TempVar.substr(FormLimit)
    }
  }
}

function uValidator(sExp)
{  var lReturn = eval(sExp);
   if (lReturn)
	with (uValidator)
    {if (arguments.length > 1)
	    alert(arguments[1]);
	 if (arguments.length > 2)
		for(var n=2; n <= arguments.length;n++)
			eval(arguments[n]);
	}
return lReturn;
}

function uInRange(nNum, nA, nB)
{
return ((nNum >= nA) && (nNum <= nB));
}


function uTrim(str) {
   return str.replace(/(^\s*)|(\s*$)/g, "");
}

function uIsEmpty(str) {
   str = uTrim(str);
return ((str == null) || (str.length == 0));
}

function uIsLetter(chr) {
   return (((chr >= "a") && (chr <= "z")) || ((chr >= "A") && (chr <= "Z")));
}

function uIsDigit(chr) {
   return ((parseInt(chr) >= -9) && (parseInt(chr) <= 9));
}

function uIsWhitespace(str) {
   
    var whitespace = " \t\n\r";
    
    // Is str empty?
    
    if (uIsEmpty(str)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (var i = 0; i < str.length; i++)
    {   
        // Check that current character isn't whitespace.
        var chr = str.charAt(i);

        if (whitespace.indexOf(chr) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function uIsAlphabetic(str) {

   if (uIsEmpty(str)) 
       if (uIsAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (uIsAlphabetic.arguments[1] == true);
       
    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (var i = 0; i < str.length; i++)
    {   
        // Check that current character is letter.
        var chr = str.charAt(i);

        if (!uIsLetter(chr)) return false;
    }

    // All characters are letters.
    return true;
}

//=========================================================================

function uIsInteger(str)
{  var bNegative = false
   if (uIsEmpty(str))
       if (uIsInteger.arguments.length == 1) return defaultEmptyOK;
       else return (uIsInteger.arguments[1] == true);
       
    if (str.charAt(0) == "," || str.charAt(str.length-1) == ",") return false;

	if (str.length == 1 && str == "-") return false;	//only "-" is entered

    for (var i = 0; i < str.length; i++)
    {   
        var chr = str.charAt(i);
        
        if (chr != "-" && bNegative==false)
			if (!uIsDigit(chr) && chr != ",") return false;
		
		if (bNegative==true){
			chr = "-" + chr;
			if (!uIsDigit(chr) && chr != ",") return false;
			bNegative = false;
		}
		
		//if chr contains "-", set bNegative to true
		if (chr == "-") bNegative = true;
    }

    // All characters are numbers.
    return true;
}

//=========================================================================

function uIsFloat(str)

{   var j, seenDecimalPoint = false
    var decimalPointDelimiter = "."
        
    if (uIsEmpty(str)) 
       if (uIsFloat.arguments.length == 1) return defaultEmptyOK;
       else return (uIsFloat.arguments[1] == true);

    if (str == decimalPointDelimiter) return false;
    
    if (str.charAt(0) == "," || str.charAt(str.length-1) == ",") return false;
    
    for (var i = 0; i < str.length; i++)
    {   
        var chr = str.charAt(i);

        if ((chr == decimalPointDelimiter) && !seenDecimalPoint)
		{	seenDecimalPoint = true
			
			var j = i
			
			for (var a = j; a < str.length; a++)
				if (str.charAt(a) == ",") return false;
		}
		else
			if (!uIsDigit(chr) && chr != ",") return false;
    }
    
    // All characters are numbers.
    return true;
}

//=========================================================================

function uIsNumeric(str)
{	
	if (uIsEmpty(str)) 
       if (uIsNumeric.arguments.length == 1) return defaultEmptyOK;
       else return (uIsNumeric.arguments[1] == true);
       
	if (!uIsInteger(str) && !uIsFloat(str)) return false;

	return true;	
}

//=========================================================================
// uIsSignedInteger(STRING str [, BOOLEAN emptyOK])
// 
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.


function uIsSignedInteger(str)

{   if (uIsEmpty(str)) 
       if (uIsSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (uIsSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (uIsSignedInteger.arguments.length > 1)
            secondArg = uIsSignedInteger.arguments[1];

        // skip leading + or -
        if ( (str.charAt(0) == "-") || (str.charAt(0) == "+") )
           startPos = 1;    
        return (uIsInteger(str.substring(startPos, str.length), secondArg))
    }
}

function uIsIntegerInRange(str, a, b)
{   if (uIsEmpty(str)) 
       if (uIsIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (uIsIntegerInRange.arguments[1] == true);

    if (!uIsInteger(str, false)) return false;    

    var num = parseInt (str);
    return ((num >= a) && (num <= b));
}


//=========================================================================
// uIsNonnegativeInteger(STRING str [, BOOLEAN emptyOK])
// Returns true if string str is an integer >= 0.


function uIsNonnegativeInteger(str)
{   var secondArg = defaultEmptyOK;

    if (uIsNonnegativeInteger.arguments.length > 1)
        secondArg = uIsNonnegativeInteger.arguments[1];
    
    return (uIsSignedInteger(str, secondArg) && ( (uIsEmpty(str) && secondArg)  || (parseInt (str) >= 0) ) );
}

//=========================================================================
// uIsInRange returns true if string str is an integer 
// within the range of integer arguments a and b, inclusive.

function uIsStringInRange(str, a, b)
{   
    var inputLength = 0;
    var tempStr;
    
    tempStr = uTrim(str);

    inputLength = tempStr.length;

    return ((inputLength >= a) && (inputLength <= b));
}

//=========================================================================
// uIsZIPCode (STRING str [, BOOLEAN emptyOK])
// 
// uIsZIPCode returns true if string s is a valid 5 digits ZIP code.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.  

function uIsZIPCode(str)
{  if (uIsEmpty(str)) 
       if (uIsZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (uIsZIPCode.arguments[1] == true);

   return (uIsInteger(str) && (str.length == 5))
}

function uIsYear(str)
{   if (uIsEmpty(str)) 
       if (uIsYear.arguments.length == 1) return defaultEmptyOK;
       else return (uIsYear.arguments[1] == true);

    if (!uIsNonnegativeInteger(str)) return false;
    
    return (str.length == 4);
}

//=========================================================================
// uIsMonth (STRING str [, BOOLEAN emptyOK])
// 
// uIsMonth returns true if string str is a valid month between 1 and 12.

function uIsMonth(str)
{   if (uIsEmpty(str)) 
       if (uIsMonth.arguments.length == 1) return defaultEmptyOK;
       else return (uIsMonth.arguments[1] == true);
    
    return uIsIntegerInRange (str, 1, 12);
}

//=========================================================================
// uIsDay (STRING str [, BOOLEAN emptyOK])
// 
// uIsDay returns true if string str is a valid day number between 1 and 31.

function uIsDay(str)
{   if (uIsEmpty(str)) 
       if (uIsDay.arguments.length == 1) return defaultEmptyOK;
       else return (uIsDay.arguments[1] == true);   
    return uIsIntegerInRange (str, 1, 31);
}


//======================================================================================================================
// uDaysInFebruary (INTEGER year)
// 
// Given integer argument year, returns number of days in February of that year.


function uDaysInFebruary(year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.

    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}


//======================================================================================================================
// uIsDate (STRING year, STRING month, STRING day)
//
// uIsDate returns true if string arguments year, month, and day form a valid date.


function uIsDate(year, month, day)
{	
    var daysInMonth = new Array (13);
    daysInMonth[1] = 31;
    daysInMonth[2] = 29;   // must programmatically check this with function uDaysinFebruary
    daysInMonth[3] = 31;
    daysInMonth[4] = 30;
    daysInMonth[5] = 31;
    daysInMonth[6] = 30;
    daysInMonth[7] = 31;
    daysInMonth[8] = 31;
    daysInMonth[9] = 30;
    daysInMonth[10] = 31;
    daysInMonth[11] = 30;
    daysInMonth[12] = 31;


    // catch invalid years (not 4-digit) and invalid months and days.
    if (! (uIsYear(year, false) && uIsMonth(month, false) && uIsDay(day, false))) return false;

    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > uDaysInFebruary(intYear))) return false;

    return true;
}

//=========================================================================
//Compare a user date against a reference date (or current date if none given) and
//return the difference between these 2 dates in days.
function DaysBetweenDate(yr, mo, dy, sRefDate){
   var d, r, t1, t2, t3;            //Declare variables.
   
   //Milliseconds in a day, hour, or minute
   var MinMilli = 1000 * 60         //Initialize variables.
   var HrMilli = MinMilli * 60
   var DyMilli = HrMilli * 24
   
   t1 = Date.UTC(yr, mo - 1, dy)    //Get milliseconds since 1/1/1970.
   
   //If sRefDate is not provided, assign current date to t2
   if (sRefDate == "" || uIsEmpty(sRefDate)){
      d = new Date();                  //Create Date object.
      t2 = d.getTime();}               //Get current time in milliseconds.
   else
	  t2 = Date.parse(sRefDate)		  //Get milliseconds between sRefDate and 1/1/1970
   
   t3 = t1 - t2
   r = Math.round(t3 / DyMilli);
   
   //Return difference between supplied & sRefDate (or current) date.
   return(r);
}

//=========================================================================

function uIsCCDayMonth(sHiddenDate)
{	//Does a credit card expiry day and month exist. Hypens (-) indicates empty date.
	if (sHiddenDate.indexOf("-") != -1 || sHiddenDate == "") return false;
	
	return true
}

//=========================================================================

function uIsEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  
  return (!r1.test(str) && r2.test(str));
}

//=========================================================================

function uIsTime(timeStr) {
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.

var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

var matchArray = timeStr.match(timePat);
if (matchArray == null) {
alert("Time is not in a valid format.");
return false;
}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];

if (second=="") { second = null; }
if (ampm=="") { ampm = null }

if (hour < 0  || hour > 23) {
alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
return false;
}
if (hour <= 12 && ampm == null) {
if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
alert("You must specify AM or PM.");
return false;
   }
}
if  (hour > 12 && ampm != null) {
alert("You can't specify AM or PM for military time.");
return false;
}
if (minute<0 || minute > 59) {
alert ("Minute must be between 0 and 59.");
return false;
}
if (second != null && (second < 0 || second > 59)) {
alert ("Second must be between 0 and 59.");
return false;
}
return true
}

function uFN(expr, decPlace)
{
 if (isNaN(expr))
   {return "0." + "00000".substring(0,decPlace)}
 var str = "" + Math.round(eval(expr) * Math.pow(10,decPlace))
 while (str.length <= decPlace)
	{  str = "0" + str}
 var decPoint = str.length  - decPlace
return str.substring(0,decPoint) + "." + str.substring(decPoint,str.length)
}

function uFC(expr)
{ return uFN(expr,2)
}

function uReplace(expr,sSrc,sRplc)
{	temp = "" + expr
	while (temp.indexOf(sSrc)>-1)
	{	pos = temp.indexOf(sSrc)
		temp = "" + (temp.substring(0, pos) + sRplc + 
			temp.substring((pos + sSrc.length), temp.length))
	}
return temp
}

function trapNumKey()
{   var nKey = parseInt(event.keyCode)
	if (nKey!=46 && (nKey < 48) || (nKey > 57))
	{	event.returnValue = false
		return  }
}    
