// Format number (num) to (dec) decimal places
function RealNumberFormat(dec)
{
   this.dec    = dec;
   this.format = utilities_realnumberformat;
}

function utilities_realnumberformat(num) {

 if (typeof(num) == "undefined") num = "0";

  var i;
  var fneg = '';
  var fnum = '';
  var fdec = '';
  var dec = this.dec;
  
    if(num < 0){
    num = num * -1;
    fneg = '-';
  }
  
  num = num.toString();
  
  // remove leading zeroes
  
  var startIndex = 0;
  for (startIndex = 0; startIndex < num.length; startIndex++) {
       if (num.charAt(startIndex) != "0") { break;}
  }
  
  // if not format 00.nnnn strip zeroes and not the end of string
  
  if ((num.charAt(startIndex) != ".") && (startIndex != num.length)) {num = num.substring(startIndex);}
  
  if(dec > 0) {
    i = num.indexOf(".");
    if(i > 0) {
      i = i + 1;
      if (i + dec > num.length) {
        num = num + '0000000000';
      }
      fdec = '.' + num.substr(i, dec);
    }
    else {
           fdec = '0000000000';
           fdec = '.' + fdec.substr(0, dec);
    }
  }
  
  num = parseInt(num).toString();
  
  for(i = num.length; i >= 0; i = i - 1) {
  
    fnum = num.substr(i, 1) + fnum;
    if(((Math.floor((num.length - i)/3)) * 3) == num.length - i){
       fnum = ',' + fnum;
    }
  }
  
  fnum = fnum.substr(0, fnum.length - 1);
  
  if(fnum.substr(0, 1) == ',') {
  
    fnum = fnum.substr(1, fnum.length - 1);
  }
  
  return fnum + fdec + fneg;
}

function DollarFormat()
{
   this.format = utilities_dollarformat;
}
  
function utilities_dollarformat(num)
{  
   realNumber = new RealNumberFormat(2);
   
   if (typeof(num) == "undefined") num = "0";
   
   return "$" + realNumber.format(num);  
}

function utilities_unformatDollar(num) {

  var i;
  var n;
  var unum = '';
  num = num.toString();
  
  for(i = 0; i < num.length; i++) {
      n = num.substr(i, 1);
      if(n != ' ' && n != ',' && n != '$'){
         unum = unum.toString() + n.toString();
    }
  }
  return unum;
}

function StarFormat()
{
   this.format = starformat;
}
  
function utilities_starformat(num)
{     
   return "*" + num.toString();
}

function StripAlphaFilter() {

   this.validKey   = utilities_stripAlphaValidKey;
   this.validValue = null;
}

function utilities_stripAlphaValidKey(keyCode) {

   // NS Mac

   if ((document.layers) && (navigator.appVersion.indexOf("Mac") != -1)) {
	  return true;
   }

   if ((keyCode >= 48 && keyCode <= 57)  || keyCode == 46 ||	
	   (keyCode >= 96 && keyCode <= 105) || 
	   (keyCode >= 35 && keyCode <= 40)  || keyCode == 8  || keyCode == 9) {
		
	   return true;
   }
   else
   {	  
	   return false;
   }
}

function DecimalAlphaFilter() {

   this.validKey   = utilities_decimalAlphaValidKey;
   this.validValue = utilities_decimalAlphaValidValue;
}

function utilities_decimalAlphaValidKey(keyCode) {

   // NS Mac

   if ((document.layers) && (navigator.appVersion.indexOf("Mac") != -1)) {
	  return true;
   }

   if ((keyCode >= 48 && keyCode <= 57)  || keyCode == 46 ||	
	   (keyCode >= 96 && keyCode <= 105) || 
	   (keyCode >= 35 && keyCode <= 40)  || keyCode == 8  || keyCode == 9 || keyCode == 190 || keyCode == 110) {

	    return true;
   }
   else
        return false;
}

function utilities_decimalAlphaValidValue( obj )
{
	   return utilities_isValidDecimal( obj, 0, 999999999.99, 2);
}

function DateAlphaFilter() {

   this.validKey   = utilities_dateAlphaValidKey;
   this.validValue = null;
}

function utilities_dateAlphaValidKey(keyCode) {

   // NS Mac

   if ((document.layers) && (navigator.appVersion.indexOf("Mac") != -1)) {
	  return true;
   }

   if ((keyCode >= 48 && keyCode <= 57)  || keyCode == 46 ||	
	   (keyCode >= 96 && keyCode <= 105) || 
	   (keyCode >= 35 && keyCode <= 40)  || keyCode == 8  || keyCode == 9 || keyCode == 191) {
		
	   return true;
   }
   else
   {	  
	   return false;
   }
}
// Format number (num) to (dec) decimal places
function utilities_formatNumber(num, dec) {

  var i;
  var fneg = '';
  var fnum = '';
  var fdec = '';
    if(num < 0){
    num = num * -1;
    fneg = '-';
  }
  num = num.toString();
  if(dec > 0) {
    i = num.indexOf(".");
    if(i > 0) {
       i = i + 1;
       if(i + dec > num.length) {
          num = num + '0000000000';
       }
       fdec = '.' + num.substr(i, dec);
    }
    else {
          fdec = '0000000000';
          fdec = '.' + fdec.substr(0, dec);
    }
  }
  
  num = parseInt(num).toString();
  for(i = num.length; i >= 0; i = i - 1) {
    fnum = num.substr(i, 1) + fnum;
    if(((Math.floor((num.length - i)/3)) * 3) == num.length - i){
      fnum = ',' + fnum;
    }
  }
  fnum = fnum.substr(0, fnum.length - 1);
  if(fnum.substr(0, 1) == ',') {
    fnum = fnum.substr(1, fnum.length - 1);
  }
  return fnum + fdec + fneg;
}

// Strip any formating from number (num)

function utilities_unformatNumber(num) {
  var i;
  var n;
  var unum = '';
  num = num.toString();
  for(i = 0; i < num.length; i = i + 1) {
    n = num.substr(i, 1);
    if(n != ' ' && n != ',' && n != '$' && n != '.'){
       unum = unum.toString() + n.toString();
    }
  }
  return unum;
}

// Un-Format date String

function utilities_getDate( dateString) {
    
  var daysInMonth            = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
  var daysInMonthForLeapYear = new Array(31, 29, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
        	  
  dateString = dateString.valueOf();
  
  var day;
  var month;
  var year;
  
  var split = dateString.split("/");
  
  //day   = parseInt(split[0]);

  day   = eval(split[0]);
  if (isNaN(day) || (day <= 0)) { return null; }
  
  //month = parseInt(split[1]);

  month = eval(split[1]);
  if (isNaN(month) || (month < 0) || (month > 12)) { return null; }
  
  //year  = parseInt(split[2]);

  year  = eval(split[2]);
  if (isNaN(year) || (year <= 0)) return null;
    
  var isLeap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    
  if (isLeap == true) {
    
     if (day > daysInMonthForLeapYear[month - 1]) { return null;}
  }
  else {

     if (day > daysInMonth[month - 1]) { return null; }
  }
      
  // as javascript date object uses zero based months for some reason
  if (month > 0) month = month - 1;

  var theDate = new Date(year, month, day);
  
  return theDate;
}

// Strip any formating from number (num)

function utilities_unformatNumber(num) {
  var i;
  var n;
  var unum = '';
  num = num.toString();
  for(i = 0; i < num.length; i = i + 1) {
    n = num.substr(i, 1);
    if(n != ' ' && n != ',' && n != '$' && n != '.'){
      unum = unum.toString() + n.toString();
    }
  }
  return unum;
}


function utilities_stripAlpha(evt, allowedChar) {
	// NS Mac

	if ((document.layers) && (navigator.appVersion.indexOf("Mac") != -1)) {
		return true;
	}
		
	if (allowedChar != null || typeof(allowedChar) != "undefined") {
	    
	    if ((allowedChar == ".") && (evt.keyCode == 190)) return true;
	    if ((allowedChar == "/") && (evt.keyCode == 191)) return true;
	}

  	if ((evt.keyCode >= 48 && evt.keyCode <= 57) || evt.keyCode == 46 ||	
		(evt.keyCode >= 96 && evt.keyCode <= 105) || 
		(evt.keyCode >= 35 && evt.keyCode <= 40)  || evt.keyCode == 8  || evt.keyCode == 9) {
		// evt.keyCode == 16 || is shift button

        window.status = evt.keyCode;
		return true;
	}
    else
	{ window.status = evt.keyCode;
	  return false;
	}
}

function utilities_isValidDateObject( Value) {

 var daysInMonth            = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
 var daysInMonthForLeapYear = new Array(31, 29, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);

 if ((Value == null) || (typeof(Value) == "undefined")) return false;
 
 day   = Value.getDate();
 month = Value.getMonth() + 1;
 year  = Value.getFullYear();
 
 if (month < 1 || month > 12) { // check month range
 
     alert("Month must be between 1 and 12.");
     return false;
 }

 var isLeap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    
 if (isLeap == true) {
    
     if (day > daysInMonthForLeapYear[month - 1]) { alert("As this is a leap year, days cannot be greater than " + daysInMonthForLeapYear[month - 1]); return false;}
 }
 else {

    
     if (day > daysInMonth[month - 1]) { alert("Days is greater than the " + daysInMonth[month - 1] + " in this month." ); return false; }
 }


 return true;
}

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY


function utilities_isValidDate( Value) {

 var dateStr    = Value.toString();
 
 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

 var matchArray = dateStr.match(datePat); // is the format ok?
 
 if (matchArray == null) {
 
    //alert(dateStr + " Date is not in a valid format.")
    return false;
 }

 day   = matchArray[1]; // parse date into variables
 month = matchArray[3];
 year  = matchArray[4];

 var daysInMonth            = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
 var daysInMonthForLeapYear = new Array(31, 29, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
 
 if (month < 1 || month > 12) { // check month range
 
     alert("Month must be between 1 and 12.");
     return false;
 }

 var isLeap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    
 if (isLeap == true) {
    
     if (day > daysInMonthForLeapYear[month - 1]) { alert("Days cannot be greater than " + daysInMonthForLeapYear[month - 1] + " for this month"); return false;}
 }
 else {

    
     if (day > daysInMonth[month - 1]) { alert("Days cannot be greater than " + daysInMonth[month - 1] + " for this month." ); return false; }
 }

 return true;
}

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

function utilities_isValidDateEntry( Value) {

 var dateStr    = Value.toString();
 
 var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

 var matchArray = dateStr.match(datePat); // is the format ok?
 
 if (matchArray == null) {
 
    alert(dateStr + " Date is not in a valid format.")
    return false;
 }

 day = matchArray[1]; // parse date into variables
 month = matchArray[3];
 year = matchArray[4];
 
 // uses lower range limit of 0 amd upper 11 as javascript date object is zero indexed
 
 if (month < 0 || month > 11) { // check month range
 
     alert("Month must be between 1 and 12.");
     return false;
 }
 
 if (day < 1 || day > 31) {
 
     alert("Day must be between 1 and 31.");
     return false;
 }
 
 if ((month==3 || month==5 || month==8 || month==10) && day > 30) {
 
      alert("Month "+(month+1)+" doesn't have "+day+" days!")
      return false;
 }
 
 if ((month==0 || month==2 || month==4 || month==6 || month==7 || month==9 || month==11) && day > 31) {
 
      alert("Month "+(month+1)+" doesn't have "+day+" days!")
      return false;
 }
 
 if (month == 1) { // check for february 29th
 
     var isLeap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

     if (isLeap == true) {

         if (day > 29) {

	     alert("February " + year + " doesn't have " + day + " days!");
             return false;
         }
     }
     else {
            if (day > 28) {
     
                alert("February " + year + " doesn't have " + day + " days!");
                return false;
         }
     }
 }

 return true;
}

function utilities_dateAdd( interval, number, date)
{
//  if (!utilities_isValidDate(date.toString())) return null;

  var theDate = new Date(date);
  
  var milliSecsSinceEpoch = theDate.valueOf(); 
  
  var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
  var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
        	
  // check that the start parameter is a valid Date. 
    
  if ( isNaN (milliSecsSinceEpoch)) return null ;
    
  // check that an interval parameter was not numeric. 
    
  if ( interval.charAt(0) == null) return null ;
      
  // what kind of add to do? 
    
  switch (interval.charAt(0))
  {
      case 'y':
           
           theDate.setFullYear(theDate.getFullYear() + number);
           milliSecsSinceEpoch = theDate.valueOf();
           
      break;
      
      case 'm': 
           
           var months = theDate.getMonth(); 
           theDate.setMonth( months + number);
           
           //for (i = 0 ; i < number; i++) {
           //    
           //    if (months > 11) {
           //        months = 0;
           //        var year = theDate.getYear();
           //        year++;
           //        theDate.setYear(year);
           //    }
           //    
           //    theDate.setMonth(++months);
           //    
           //}
           
           milliSecsSinceEpoch = theDate.valueOf();
           
      break ;
            
      case 'd': 
      
           milliSecsSinceEpoch += parseInt(number * 86400000) ;
           
      break ;
            
      case 'H': 
      
           milliSecsSinceEpoch += parseInt(number * 3600000 ) ;
           
      break ;
            
      case 'M':
      
           milliSecsSinceEpoch += parseInt(number * 60000 ) ;
           
      break ;
            
      case 'S':
      
           milliSecsSinceEpoch += parseInt(number * 1000 ) ;
           
      break ;
                
      default:
          
          milliSecsSinceEpoch = 0;
           
      break;
    }
    
    return new Date(milliSecsSinceEpoch);
}

function utilities_dateDiff( interval, startDate, endDate) {

  //var milliSecondsSinceEpoch = date.valueOf(); 
  
  var theStartDate = new Date(startDate);
  var theEndDate   = new Date(endDate);
  
  var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
  var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
    
  var iOut = 0;
        
//  if (!utilities_isValidDate(startDate)) return null;
//  if (!utilities_isValidDate(endDate)) return null;
    
  // Create 2 error messages, 1 for each argument. 
    
  var milliSecStartDate = theStartDate.valueOf();
  var milliSecsEndDate  = theEndDate.valueOf();
    	
  // check that the start parameter is a valid Date. 
    
  if ( isNaN (milliSecStartDate) || isNaN (milliSecsEndDate) ) return null ;
    
  // check that an interval parameter was not numeric. 
    
  if ( interval.charAt(0) == null ) return null ;
        
  var number = milliSecsEndDate - milliSecStartDate ;
    
  switch (interval.charAt(0))
  {
      case 'y':
      
           var year = theStartDate.getFullYear(); 
           
           while(theStartDate.valueOf() < theEndDate.valueOf()) {
               
               year++;
               theStartDate.setFullYear( year);
               iOut++;
           }
      
      break;
                  
      case 'm': 
           
           var months = 0;      
           while(theStartDate.valueOf() < theEndDate.valueOf()) {
             
               months = theStartDate.getMonth(); 
               theStartDate.setMonth( months+1 );
                    
               if (theStartDate.valueOf() <= theEndDate.valueOf()) iOut++;
            }
                         
      break ;
            
      case 'd': 
      
           iOut = parseInt(number / 86400000) ;
           
      break ;
            
      case 'H': 
      
           iOut = parseInt(number / 3600000 ) ;
           
      break ;
            
      case 'M':
      
           iOut = parseInt(number / 60000 ) ;
           
      break ;
            
      case 'S':
      
           iOut = parseInt(number / 1000 ) ;
           
      break ;
            
      default:
      
        return null ;
    }
    
    return iOut ;
}

function utilities_check2DimArray( array)
{
  if ((typeof(array) != "undefined") && (typeof(array[0][0]) != "undefined")) {
     return true;
  }
    
  return false;
}

function utilities_create2DimArray(rows, cols)
{
  var array = new Array(rows);
  for (i = 0; i < rows; i++) 
      array[i] = new Array(cols);
      
  return array;
}


//////////////////////////////////////////////////////////////////////////////////////

var callBackTimeOut  = 10;

var checkValidIntegerTimer = null;
var checkValidDateTimer    = null;
var checkValidDecimalTimer = null;
var checkValidFractionTimer = null;
var theOldValue            = null;

function utilities_checkIntegerRange( obj, oldValue, min, max)
{     
      
   //if (obj == null) return false;
   //if (oldValue == null) return false;
   //if (min == null) return false;
   //if (max == null) return false;
   
   //if (typeof(obj) == "undefined") return false;
   //if (typeof(oldValue) == "undefined") return false;
   //if (typeof(min) == "undefined") return false;
   //if (typeof(max) == "undefined") return false;
  
   //if (isNaN(min) == true)       return false;
   //if (isNaN(max) == true)       return false;
   
   
   var matchArray = null;
   var IntegerPat = /\D{1,}/; //matches any non-digit
   
   var OldValueStr = oldValue.toString();
   
   matchArray = OldValueStr.match(IntegerPat); // is the format ok?
   if (matchArray != null) oldValue = "";
      
   var IntegerStr  = obj.value.toString();
   if (IntegerStr == "") {
       clearTimeout(checkValidIntegerTimer);
       checkValidIntegerTimer = null;
       return;
   }
   
   matchArray = IntegerStr.match(IntegerPat); // is the format ok?
 
   if (matchArray != null) {
   
       if (obj.value != oldValue) obj.value = oldValue;
 //      obj.focus();
       clearTimeout(checkValidIntegerTimer);
       checkValidIntegerTimer = null;
       return;
   }
      
   var checkIntValue = parseInt(obj.value, 10);
   
   if (isNaN(checkIntValue) == true) return false;
   
   if (checkIntValue >= min && checkIntValue <= max) {
   
       clearTimeout(checkValidIntegerTimer);
       checkValidIntegerTimer = null;
       return;
   }
  
   if (obj.value != oldValue) obj.value = oldValue;
      
 //  obj.focus();
 
   clearTimeout(checkValidIntegerTimer);
   checkValidIntegerTimer = null;
   
   
   return;
}

function utilities_isValidInteger( obj, min, max)
{
    if (checkValidIntegerTimer != null) { return false; }
    
    checkIntegerObj = obj;
    theOldValue     = obj.value;
    theMinInteger   = min;
    theMaxInteger   = max;
        
    checkValidIntegerTimer= setTimeout("utilities_checkIntegerRange( checkIntegerObj, theOldValue, theMinInteger, theMaxInteger)", callBackTimeOut);
    
    return (checkValidIntegerTimer != null) ? true : false;
}

function utilities_isValidDateFormat( obj)
{
    if (checkValidDateTimer != null) { return false; }
    
    checkDateObj = obj;
    theOldValue  = obj.value;
        
    if (checkValidDateTimer == null) checkValidDateTimer= setTimeout("utilities_checkValidDate( checkDateObj, theOldValue)", callBackTimeOut);
    
    return (checkValidDateTimer != null) ? true : false;
}

function utilities_checkValidDate( obj, oldValue)
{
   //if (obj == null) return false;
   //if (oldValue == null) return false;
   
   //if (typeof(obj.value) == "undefined") return false;
   //if (typeof(oldValue) == "undefined") return false;
      
   var dateValue = obj.value;
      
   if (dateValue == "") {
   
       clearTimeout(checkValidDateTimer);
       checkValidDateTimer = null;
       return;
   }
   
   if (utilities_isValidDate( dateValue) == true)  {
   
       clearTimeout(checkValidDateTimer);
       checkValidDateTimer = null;
       return;
   }
       
   if (utilities_isValidDate( oldValue) == false) oldValue = "";    
   
   obj.value = oldValue;
       
   //obj.focus();
   
   clearTimeout(checkValidDateTimer);
   checkValidDateTimer = null;
   
   return;
}
  
function utilities_checkDecimalRange( obj, oldValue, min, max, precision)
{   
   //if (obj == null) return false;
   //if (oldValue == null) return false;
   //if (min == null) return false;
   //if (max == null) return false;
   //if (precision == null) return false;
   
   //if (typeof(obj) == "undefined") return false;
   //if (typeof(oldValue) == "undefined") return false;
   //if (typeof(min) == "undefined") return false;
   //if (typeof(max) == "undefined") return false;
   //if (typeof(precision) == "undefined") return false;
  
   //if (isNaN(min) == true)       return false;
   //if (isNaN(max) == true)       return false;
   //if (isNaN(precision) == true) return false;
   
   var matchArray  = null;
   var DecimalPat  = /^[0-9]+\.[0-9]{1,2}$|^[0-9]+$|^[0-9]+\.$/i;   // mmmm.nn or mmmm. or mmmm
      
   var OldValueStr = oldValue.toString();
   matchArray = OldValueStr.match(DecimalPat); 
   if (matchArray == null) oldValue = "";
   
   var DecimalStr  = obj.value.toString();
   if (DecimalStr == "") {
	   clearTimeout(checkValidDecimalTimer);
       checkValidDecimalTimer = null;
       return;
   }
   
   matchArray = DecimalStr.match(DecimalPat); // is the format ok?
   if (matchArray == null) {
       
       obj.value = oldValue;
       clearTimeout(checkValidDecimalTimer);
       checkValidDecimalTimer = null;
       return;
   }
     
   var checkDecimal = eval(obj.value); //parseInt(obj.value, 10);
   
   if (isNaN(checkDecimal) == true)  {
	   clearTimeout(checkValidDecimalTimer);
       checkValidDecimalTimer = null;
       return;
   }
     
   if (checkDecimal < min || checkDecimal > max) { obj.value = oldValue; }
   
   clearTimeout(checkValidDecimalTimer);
   checkValidDecimalTimer = null;
            
   return;
}

function utilities_isValidDecimal( obj, min, max, precision)
{
    if (checkValidDecimalTimer != null) { return false; }
    
    checkDecimalObj        = obj;
    theOldValue            = obj.value;
    theMinDecimal          = min;
    theMaxDecimal          = max;
    theDecimalPrecision    = precision;
    
    checkValidDecimalTimer = setTimeout("utilities_checkDecimalRange( checkDecimalObj, theOldValue, theMinDecimal, theMaxDecimal, theDecimalPrecision)", callBackTimeOut);
    
    return (checkValidDecimalTimer != null) ? true : false; 
}

function utilities_checkFraction( obj, oldValue)
{   
   //if (obj == null) return false;
   //if (oldValue == null) return false;
      
   //if (typeof(obj) == "undefined") return false;
   //if (typeof(oldValue) == "undefined") return false;
      
   var matchArray  = null;
   //var FractionPat  = /^[0-9]{1,}\/[1-9]{1,}$|^[0-9]{1,}\/$|^[0-9]{1,}$/i;   // 1/2, 1/3, 1
   var FractionPat  = /^[0-9]{1,}\/[1-9]{1}[0-9]{0,}$|^[0-9]{1,}\/[1-9]{1,}$|^[0-9]{1,}\/$|^[0-9]{1,}$/i;   // 1/2, 1/3, 1
      
   var OldValueStr = oldValue.toString();
   matchArray = OldValueStr.match(FractionPat); 
   if (matchArray == null) oldValue = "";
   
   var FractionStr  = obj.value.toString();
   if (FractionStr == "") {
       clearTimeout(checkValidFractionTimer);
       checkValidFractionTimer = null;
       return;
   }
   
   matchArray = FractionStr.match(FractionPat); // is the format ok?
   if (matchArray == null) { obj.value = oldValue; }
         
   clearTimeout(checkValidFractionTimer);
   checkValidFractionTimer = null;
            
   return;
}

function utilities_isFraction( obj)
{   
   if (obj == null) return false;
   if (typeof(obj) == "undefined") return false;
         
   var matchArray  = null;
   //var FractionPat  = /^[0-9]{1,}\/[1-9]{1,}$|^[0-9]{1,}$/i;   // 1/2, 1/3, 1
   var FractionPat  = /^[0-9]{1,}\/[1-9]{1}[0-9]{0,}$|^[0-9]{1,}\/[1-9]{1,}$|^[0-9]{1,}$/i;   // 1/2, 1/3, 1
      
   var FractionStr  = obj.toString();
   if (FractionStr == "") return false;
   
   matchArray = FractionStr.match(FractionPat); // is the format ok?
   if (matchArray == null)      
       return false;
               
   return true;
}

function utilities_isValidFraction( obj)
{
    if (checkValidFractionTimer != null) { return false; }
    
    checkFractionObj       = obj;
    theOldValue            = obj.value;
       
    checkValidFractionTimer = setTimeout("utilities_checkFraction( checkFractionObj, theOldValue)", callBackTimeOut);
    
    return (checkValidFractionTimer != null) ? true : false; 
}

////////////////////////////////////////////////////////////////////

function utilities_getDivStyle(divname) {

  var style;
  if (isDOM) { style = document.getElementById(divname).style; }
  else { style = isIE ? document.all[divname].style
                     : document.layers[divname]; } // NS4
  return style;
}

function utilities_isElementVisible(divname) {

  return (utilities_getDivStyle(divname).visibility == 'visible') ? true : false;
}

function utilities_showElement(divname) {

  utilities_getDivStyle(divname).visibility = 'visible';
}

function utilities_hideElement(divname) {

  utilities_getDivStyle(divname).visibility = 'hidden';
}

function utilities_raiseElement(divname) {

  utilities_getDivStyle(divname).zIndex = 100;
}

function utilities_lowerElement(divname) {

  utilities_getDivStyle(divname).zIndex = 0;
}

function utilities_putBehindElement(divname, behindElt) {

  utilities_getDivStyle(divname).zIndex = utilities_getDivStyle(divname).zIndex - 1;
}

function utilities_isElementHidden(divname) {

  return (utilities_getDivStyle(divname).visibility == 'hidden') ? true : false;
}

function utilities_bringToFront (showTab, hideTab, showPanel, hidePanel, positionerName) {

  utilities_showElement(showPanel);
  utilities_hideElement(hidePanel);
  utilities_setPosition(showPanel, positionerName); 
  
  utilities_hiLite(showTab);
  utilities_unHilite(hideTab);
}

function utilities_bringToFrontEx(showTab, hideTab1, hideTab2, showPanel, hidePanel1, hidePanel2, positionerName) {

  utilities_setPosition(showPanel,  positionerName);
  utilities_setPosition(hidePanel1, positionerName);
  utilities_setPosition(hidePanel2, positionerName);

  utilities_showElement(showPanel);
  utilities_hideElement(hidePanel1);
  utilities_hideElement(hidePanel2);
  
  utilities_putBehindElement(positionerName, showPanel);
  utilities_raiseElement(showPanel);
  utilities_lowerElement(hidePanel1);
  utilities_lowerElement(hidePanel2);
  
  //utilities_setPosition(showPanel, positionerName); 
  
  utilities_hiLite(showTab);
  utilities_unHilite(hideTab1);
  utilities_unHilite(hideTab2);
}

function utilities_getAbsPos(positionerName, which) {

 var iPos = 0;
 var tmpPos = 0;
 
 var elt = document.getElementById(positionerName);
 
 while(elt != null)
 {
      tmpPos = elt["offset" + which];
      iPos += elt["offset" + which];
      elt = elt.offsetParent;
 }
 
 return iPos;
}

function utilities_setPosition( showPanel, positionerName) {

 var positioner;
 
 if (isIE) {
 
     positioner = document.all[positionerName];
 } else {
 
  if (isDOM) {
  
      positioner = document.getElementById(positionerName);
  } else {
  
    // not IE, not DOM (probably NS4)
    // if the positioner is inside a netscape4 layer this will *not* find it.
    // I should write a finder function which will recurse through all layers
    // until it finds the named image...
    
    positioner = document.images[positionerName];
  }
 }
  
 utilities_getDivStyle(showPanel).left   = utilities_getAbsPos(positionerName, "Left");
 utilities_getDivStyle(showPanel).top    = utilities_getAbsPos(positionerName, "Top");
}

function utilities_hiLite(divName){
	utilities_getDivStyle(divName).backgroundColor = '#eeeeee'; //'#d3d3d3';
	utilities_getDivStyle(divName).top=2;
}

function utilities_unHilite(divName){
	utilities_getDivStyle(divName).backgroundColor = '#d3d3d3'; //'#eeeeee'
	utilities_getDivStyle(divName).top=0;
}

function utilities_insertHTML(divName, html)
{
    var lyr = document.getElementById(divName);
    
    if (lyr == null || typeof(lyr) != "object") return;
    
    if (at.ie) {
	    lyr.innerHTML=html;
	}
	else if(at.ns4)
	{
		    lyr.document.open() 
		    lyr.document.write(html);
		    lyr.document.close();
		    lyr.captureEvents(Event.MOUSEUP);
	}
	else if (at.ns5 || at.ns7) {
		     while (lyr.hasChildNodes()) {lyr.removeChild(lyr.firstChild);}
		     var r = lyr.ownerDocument.createRange();
		     r.selectNodeContents(lyr);
		     r.deleteContents();
		     var df = r.createContextualFragment(html);
		     lyr.appendChild(df);
	}
}

function utilities_isWeekEnd( date)
{
    return ((date.getDay() == 0) || (date.getDay() == 6)) ? true : false;
}

/////////////////////////////////////////////////////

//function utilities_calcNumDaysUTI(startDate, endDate)
//{
//  var numDaysUTI = 0;
//  
//  if (endDate.valueOf() >= startDate.valueOf()) {
//    
//    numDaysUTI = utilities_dateDiff("d", startDate, endDate);
//    if (numDaysUTI >= 0) { numDaysUTI++;}
//    
//  }
//  
//  return numDaysUTI;
//}

function utilities_calcNumDaysUTI( startDate, endDate)
{
    var calcNumDaysUTI = 0;
    
    if (startDate.valueOf() > endDate.valueOf()) {
    
        calcNumDaysUTI = utilities_dateDiff("d", endDate , startDate);
        
        if (calcNumDaysUTI > 0) {
            calcNumDaysUTI = calcNumDaysUTI + 1;
        }
        else {
            calcNumDaysUTI = 0;
        }
    }
    else {
        if (startDate.valueOf() == endDate.valueOf()) {
            calcNumDaysUTI = 1;
        }
        else {
            calcNumDaysUTI= 0;
        }
    }
    
    return calcNumDaysUTI;
}

function utilities_calcNumDays( startDate, endDate)
{
  var tempDays = utilities_dateDiff("d", startDate, endDate);
  return (tempDays > 0) ? tempDays : 0;
}

function utilities_isWorkDay( date, region)
{  
  if (utilities_isWeekEnd( date) == true) { return false; }
  
  if (utilities_isPublicHoliday(date, region) == true) { return false; }
 
}

function utilities_getNextWorkDate( date, region)
{   
  while (utilities_isWorkDay(date, region) == false) {
        date = utilities_dateAdd("d", 1, date);
  }
  
  return date;
}

function utilities_isPublicHoliday( date, region) 
{
  var oHolidays = new Holidays();
  
  return oHolidays.find(date, region);
}

function utilities_getDateString( date)
{ 
  return date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
}

function utilities_maxDate( date1, date2)
{
  if (date1.valueOf() > date2.valueOf()) {
      return date1;
  }
  
  return date2;
  
}

var formatDateTimer = null;
var dateFormatObj   = null;
var theOldDateFormatValue = null;

function DateFormatFilter() {

   this.validKey   = utilities_dateFormatValidKey;
   this.validValue = utilities_dateFormat;
   
   // control key is all non alphanumeric keys
   
   this.isControlKey = utilities_isControlKey;
   
   this.controlKeyFlag = false;
}

function utilities_dateFormat( obj)
{   
    // if control key also return true so that this gets passed onto
    // keyboard handler
    
    if (this.isControlKey() == true) { return true; }
    if (formatDateTimer != null) { return false; }
    
    dateFormatObj = obj;
    theOldDateFormatValue = obj.value;
        
    if (formatDateTimer == null) formatDateTimer= setTimeout("utilities_makeDateFormat( dateFormatObj, theOldDateFormatValue)", callBackTimeOut);
    
    return (formatDateTimer != null) ? true : false;
}

function utilities_makeDateFormat( obj, oldValue)
{        
   var dateString = obj.value;
   
   var newValue = new String("");
     
   var strippedDate = dateString.replace(/(\/)+/g, '');
   
   for ( i = 0; i < strippedDate.length; i++) {
         if ((i == 2) || (i == 4)) {
               newValue += "/";
         }
         newValue += strippedDate.charAt(i);
   }
      
   if ( newValue.length > 10) { newValue = oldValue; }
    
   obj.value = newValue;
   
   clearTimeout(formatDateTimer);
   formatDateTimer = null;
   
   return;
}

function utilities_isControlKey()
{
	return this.controlKeyFlag;
}

function utilities_dateFormatValidKey(keyCode) {

   // NS Mac

   if ((document.layers) && (navigator.appVersion.indexOf("Mac") != -1)) {
	  return true;
   }
   
   // 37 = right arrow, 39 = left arrow
   
   this.controlKeyFlag = true;
   
   window.status = keyCode;
   
   // if numeric
     
   if ((keyCode >= 48) && (keyCode <= 57)) {
		
	   this.controlKeyFlag = false;
	   return true;
   }
   
   // if keypad numeric
     
   if ((keyCode >= 96) && (keyCode <= 105)) {
		
	   this.controlKeyFlag = false;
	   return true;
   }
         
   // if delete key and backspace
   
   if ((keyCode == 46) || (keyCode == 8)) {
		
	   this.controlKeyFlag = false;
	   return true;
   }
   
   // arrow keys ,  are ok, but are control keys
   
   if ((keyCode == 37) || (keyCode == 39) ) {
		  
	   return true;
   }
   
   return false;
}

// The following two functions are not used.

function utilities_saveCaret( elem)
{
   if ( elem.isTextEdit) {
        elem.caretPos = document.selection.createRange();
   }
}

function utilities_getCaretPos( elem)
{
   if ( elem.isTextEdit && elem.caretPos )
   {
        var bookmark = "~";
        var orig     = elem.value;
        var caretPos = elem.caretPos;
        caretPos.text = bookmark;
        var i = elem.value.search( bookmark );
        window.status = "Caret is a character " + i;
        elem.value = orig + "x";   
      
     //   elem.value = utilities_reformatDate(elem.value);
   }
}

function FormatDateFilter() {

   this.validKey   = null;
   this.validValue = null;
}


function utilities_reformatDate( value)
{   
   var blankDateArray  = [" ", " ", "/", " ", " ", "/", " ", " ", " ", " "];
   var blankDateString = "  /  /    "; 
      
   var dateString = value;
   
   if (dateString == "") { dateString = blankDateString; }
     
   var split = dateString.split("/");
   
   var cursorPos = document.selection.createRange().duplicate();
          
   var compositeDate = new String("");
   
   if (typeof(split[0]) != "undefined") { compositeDate += split[0]; }
   if (typeof(split[1]) != "undefined") { compositeDate += split[1]; }
   if (typeof(split[2]) != "undefined") { compositeDate += split[2]; }
      
   var j = 0;
   for ( i = 0; i < blankDateArray.length; i++) {
         if (blankDateArray[i] != "/") { blankDateArray[i] = compositeDate.charAt(j++); }
   }

   var newValue = new String("");
   
   for ( i = 0; i < blankDateArray.length; i++) { newValue += blankDateArray[i];}
   
   return newValue;
}
