function areYouSure( type )
{
    return( confirm( "Are you sure you want to delete the " + type + "?" ) );
}

// I have purposely avoided using any javascript features that work differently across
// browsers, or that may not be available on some javascript enabled browsers.  
// Specifically, I am not using regular expressions, Float.NaN, and sparingly using arrays.
//
var PHONE_DELIMITERS=" ()-.";
var DIGITS_IN_US_PHONE_NUMBER=10;
var DIGITS="0123456789";

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

//returns true if the string s only contains characters that are
//in the string chars
function containsOnlyChars(s, chars)
{
  for (var i=0; i < s.length; i++)
  {
    var c=s.charAt(i);
    if (chars.indexOf(c) == -1) return false;
  }
  return true;
}

//removes any characters from s that are in chars
function strip(s, chars)
{
  var t="";
  for (i=0; i < s.length; i++)
  {
    var c=s.charAt(i);
    if (chars.indexOf(c) == -1) t+=c;
  }
  return t;
}

function isUSPhoneNumber(s)
{   
  var s2=strip(s, PHONE_DELIMITERS);
  return (containsOnlyChars(s2, DIGITS) && s2.length == DIGITS_IN_US_PHONE_NUMBER);
}

function isEMail(s)
{
  var i = 1;
  var sLength = s.length;

  // look for @
  while ((i < sLength) && (s.charAt(i) != "@")) {i++}

  if ((i >= sLength) || (s.charAt(i) != "@")) 
    return false;
  else 
    i += 2;

  // look for .
  while ((i < sLength) && (s.charAt(i) != ".")) {i++}

  // there must be at least one character after the .
  return ((i < sLength - 1) && (s.charAt(i) == ".")) 
}

//returns true iff s is between a and b
function isIntInRange(s,a,b)
{
  if (!isInteger(s)) return false;
  var num = parseInt(s,10);
  return ((num >= a) && (num <= b));
}

//returns true iff s is between a and b
function isFloatInRange(s,a,b)
{
  if (!isFloat(s)) return false;
  var num = parseFloat(s);
  return ((num >= a) && (num <= b));
}

//returns true iff s is an integer
function isInteger(s)
{   
  if (s.length > 0) 
  {
    var c=s.charAt(0);
    var i=0;
    if (c=='-') {
      i++;
    }
    var s2=s.substring(i);
    return containsOnlyChars(s2, DIGITS);     
  } 
  else 
  {
    return false;
  }
}

//returns true iff s is a float
function isFloat(s)
{
  var i=0;
  var seenDecimalPoint = false;

  if (s == "." || s == "-.") return false;
  if (s.charAt(0)=="-") {
    i++
  }
     
  // Search through string's characters one by one
  // until we find a non-numeric character.
  // When we do, return false; if we don't, return true.

  for (; i < s.length; i++)
  {   
    // Check that current character is number.
    var c = s.charAt(i);

    if ((c == ".") && !seenDecimalPoint) 
      seenDecimalPoint = true;
    else 
      if (DIGITS.indexOf(c)==-1) return false;
  }

  // All characters are numbers.
  return true;
}

//return true iff s is between a and b
function isLengthOK(s,a,b)
{
  return (s.length >= a && s.length <= b);
}

//Takes a Selection element as an argument, and returns true
//if it has between min and max options selected AND
//all the options selected are found in the string values.
//values is delimited with backticks, just because backticks aren't
//used too much.  This is kinda groce, but I wanted to avoid using arrays
//because they are buggy in certain browsers. 
function isSelectionOK(sel, min, max, values)
{
  if (sel.type=="text")
  {
    if (min <= 1 && max >=1) 
    {
      return (values.indexOf(sel.value) >= 0);
    }
    return false;
  }
  else if (sel.length > 1)
  {
    var numSelected=0;
    for (var i = 0; i < sel.length; i++) 
    {
      if (sel[i].checked || sel[i].selected) 
      {
        if (values.indexOf(sel[i].value)==-1) return false;
          numSelected++;
      }
    }
    return (numSelected >= min && numSelected <= max);     
  }
}

//return true iff sel is empty
function isSelectionEmpty(sel)
{
  if (sel.type=="text") 
  {
    return (sel.value=="");
  } 
  else if (sel.length > 1)
  {
    for (var i = 0; i < sel.length; i++)
    {
      if (sel[i].checked || sel[i].selected)
      {
        return false;
      }
    }
  } 
  return true;
}

//replace all the dashes with slashes
function dashToSlash(p_str) 
{
  var buf="";
  for (var i=0; i < p_str.length; i++) 
  {
    var c=p_str.charAt(i);
    if (c=="-") 
    {
      c="/";
    }
    buf+=c;
  }
  return buf;
}

//return the number of days in a given year in february
function daysInFebruary(p_year) {
  return  (((p_year % 4 == 0) && ( (!(p_year % 100 == 0)) || (p_year % 400 == 0) ) ) ? 29 : 28 );
}

//month is zero based
function daysInMonth(p_month, p_year) 
{
  if (p_month==1)
  {
    return daysInFebruary(p_year);
  } 
  else if (p_month==0 || p_month==2 || p_month==4 || p_month==6 || p_month==7 || p_month==9 || p_month==11) 
  {
    return 31;
  } 
  else 
  {
    return 30;
  }
  //int[] daysInMonth=new int[] {31,29,31,30,31,30,31,31,30,31,30,31};
}

//takes a string like "9:34 pm" and returns it as a date, or null if the 
//time is not valid
function getTime(p_time)
{
  var time=trim(p_time).toLowerCase()+"     ";
  if (time.charAt(1)==':') time="0"+time;
  var hour=time.substring(0,2);
  if (time.charAt(2)!=':') return null;
  var minute=time.substring(3,5);
  var rest=trim(time.substring(5));
  var am=false;
  var pm=false;
  if (rest=="a" || rest=="am") {
    am=true;
  } else if (rest=="p" || rest=="pm") {
    pm=true;
  } else if (rest=="") {
    //ok, not am, not pm, but ok
  } else {
    return null;
  }
  if (isIntInRange(hour,0,23) && isIntInRange(minute,0,59)) {
    var hourInt=parseInt(hour,10);
    var minuteInt=parseInt(minute,10);
    if (am || pm) {
      if (hourInt < 1 || hourInt > 12) {
        return null;
      }
      if (hourInt < 12 && pm) {
        hourInt+=12;
      } else if (hourInt==12 && am) {
        hourInt=0;
      }
    } 
    var date=new Date((hourInt*60+minuteInt)*60*1000);
    return date;
  }
  return null;
}

function fixYear(p_year) {
  var year=p_year;
  if (year < 100) {
    if (year > 70) {
      year=1900+year;
    } else {
      year=2000+year;
    }
  }
  return year;
}

function getYear(date) {
  var year=date.getYear();
  if (year < 500) {
    year=1900+year;
  }
  return year;
}

//returns a date object out of the month, day, and year, or returns
//null if any of them are invalid
function getDateFromParts(p_month, p_day, p_year, p_yearRequired) {
  if (p_year=="" && !p_yearRequired) {
    p_year=getYear(new Date())+"";
  }
  if (p_month.length < 1 || 
      p_month.length > 2 || 
      p_day.length < 1 || 
      p_day.length > 2 || 
      p_year.length < 2 ||
      p_year.length > 4 ||
      p_year.length == 3 ||
      (p_year.length==4 && (p_year.substr(0,2)!="19" && p_year.substr(0,2)!="20"))) {
    return null;
  }
  if (!isIntInRange(p_month,1,12) || !isIntInRange(p_day,1,31) || !isIntInRange(p_year,0,3000)) {
    return null;
  }
        
  var monthInt=parseInt(p_month,10);
  var dayInt=parseInt(p_day,10);
  var yearInt=parseInt(p_year,10);

  yearInt=fixYear(yearInt);
  var daysInMonth2=daysInMonth(monthInt-1, yearInt);
  if (dayInt > daysInMonth2) {
    return null;
  }
  var date=new Date(yearInt, monthInt-1, dayInt);
  return date;
}

//takes a string such as 2/14/44 and returns a date object, or
//null if it is not in an acceptable format
function getDate(p_date, p_yearRequired) {
  var date=null;
        
  var value=trim(dashToSlash(p_date));
  var i=value.indexOf('/');
  if (i >= 1) {
    var month=value.substring(0, i);
    var rest=value.substring(i+1);
    i=rest.indexOf('/');
    var day;

    var year;
    if (i >= 1 && i < rest.length-1) {
      day=rest.substring(0, i);
      year=rest.substring(i+1);
    } else {
      day=rest;
      year="";
    }
    return getDateFromParts(month, day, year, p_yearRequired);
  }
  return date;                    
}

function getDateAndTimeFromParts(p_month, p_day, p_year, p_time, p_yearRequired, p_timeRequired) {
  var dateAndTime=null;
  var date=getDateFromParts(p_month, p_day, p_year, p_yearRequired);
  if (date!=null) {
    if (p_timeRequired || trim(p_time)!="") {
      var time=getTime(p_time);
      if (time!=null) {
        dateAndTime=new Date(date.getTime()+time.getTime());
      }
    } else {
      dateAndTime=date;
    }    
  }
  return dateAndTime;
}

//takes a string of the form 1/33/00  3:28 pm and returns a Date object
//or null if the string can not be properly parsed.
function getDateAndTime(p_dateAndTime, p_yearRequired, p_timeRequired) {
  colonIndex=p_dateAndTime.indexOf(':');
  dateAndTime=null;
  if (colonIndex < 0 && !p_timeRequired) {
    dateAndTime=getDate(p_dateAndTime, p_yearRequired);
  } else if (colonIndex > 1) {
    timeIndex=colonIndex;
    while (timeIndex > 0 && 
           p_dateAndTime.charAt(timeIndex) != ' ') {
      timeIndex--;
    }
    dateStr=trim(p_dateAndTime.substring(0, timeIndex));
    timeStr=p_dateAndTime.substring(timeIndex+1);
    date=getDate(dateStr, p_yearRequired);
    time=getTime(timeStr);
    if (date!=null && time!=null) {
      dateAndTime=new Date(date.getTime()+time.getTime());
    }
  }
  return dateAndTime;
}

function trim(s) {
  var i=0;
  var len=s.length;
  while (i < len && s.charAt(i)==' ') i++;
  while (i < len && s.charAt(len-1)==' ') len--;
  return s.substring(i, len);
}

function isDateInRange(d,a,b)
{
  if (d==null) 
  {
    return false;
  } 
  else if (a==null && b==null)
  {
    return true;
  }
  else if (a==null)
  {
    return (d <= b);
  }
  else if (b==null)
  {
    return (d >= a);
  }
  else
  {
   return (d >= a && d <= b);
  }
}

function confirmForm()
{
  //the following variables must be set for this function to do anything:
  //
  //fun0="functionToCallWithArgs(oogie, 3, 8)"
  //err0="errorMessageToDisplay"
  //fun1=...
  //err1=...
  //funarraylen=2
  //
  //funarraylen holds the 'array' length
  //fun holds the function (and the arguments) to evaluate to determine if an 
  //error has occured or not.
  //err holds the error to display if the function returns false
  // whether the field is allowed to be empty

  var errMsgs="";
  for (var i=0; i < funarraylen; i++) 
  {
    if (!eval(eval("fun"+i)) )
    {
      if (i > 0) {errMsgs+="\n";}
      errMsgs+=eval("err"+i);
    }
  }
  if (errMsgs!="") 
  {
    alert(errMsgs);
    return false;
  } 
  else 
  {
    return true;
  }
}
// isSSN (STRING s [, BOOLEAN emptyOK])
// 
// isSSN returns true if string s is a valid U.S. Social
// Security Number.  Must be 9 digits.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
var digitsInSocialSecurityNumber = 9;
var defaultEmptyOK = false

function isSSN (s)
{   if (isEmpty(s)) 
       if (isSSN.arguments.length == 1) return defaultEmptyOK;
       else return (isSSN.arguments[1] == true);
    {
        var nexts = strip( s, "-" ) ;
        return (isInteger(nexts) && nexts.length == digitsInSocialSecurityNumber);
    }
}
// isZIPCode (STRING s [, BOOLEAN emptyOK])
// 
// isZIPCode returns true if string s is a valid 
// U.S. ZIP code.  Must be 5 or 9 digits only.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.  
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

var digitsInZIPCode1 = 5
var digitsInZIPCode2 = 9
function isZIPCode (s)
{  if (isEmpty(s)) 
       if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == digitsInZIPCode1) ||
             (s.length == digitsInZIPCode2)))
}


