/*
 * This function will not return until (at least)
 * the specified number of milliseconds have passed.
 * It does a busy-wait loop.
 */
function pause(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}


/*
 * This function sets up a javascript mailto:
 */
function mailTo(name, domain, options) {
 var mailToString = null;
 if ((name != null) && (domain != null) && (options != null)) {
  mailToString = "mailto:"+name+"@"+domain+"?"+options;
 }
 else if ((name != null) && (domain != null)) {
  mailToString = "mailto:"+name+"@"+domain;
 }

 if (mailToString != null) {
  window.location = mailToString;
 }
}


// return form object
function get_form() {
    //return document.forms[document.forms.length - 1]
    return document.forms[0]
}


// return reference to specified form field
function get_formfield(field) {
  return get_form().elements[field];
}


// set a list element to specified value
function set_list_value(name, value) 
{
  field = get_formfield(name);
  for (i=0; i < field.length; i++) 
  {
    if (field.options[i].value == value) 
    {
      field.selectedIndex = i;
      return i;
    }
  }
  alert("Item not found!");
  return -1;
}


// make_updater for calendar and formulator date fields
function make_updater(datefield) {
  updater = function (calendar) {
              var y = calendar.date.getFullYear()
              var m = calendar.date.getMonth() + 1
              var d = calendar.date.getDate()
              if (m < 10) m = "0" + m
              if (d < 10) d = "0" + d
              set_list_value('subfield_' + datefield + '_year', y)
              set_list_value('subfield_' + datefield + '_month', m)
              set_list_value('subfield_' + datefield + '_day', d)
              calendar.hide()
            }
  return updater
}



/*
 * Setup a calendar object
 *
 */
function calendar_setup(buttonid, formfield, statusfunc) {
  Calendar.setup({
    button         :    buttonid,  
    align          :    "tR",           
    singleClick    :    true,
    onClose        :    make_updater(formfield),
    disableFunc    :    statusfunc
  });
}


/*
 * Return True for invalid paydays
 */
function not_valid_payday(date, y, m, d) {
  dayofweek = date.getDay()

  switch(y) {
  case 2005:
    var holidays = {
      1 :  [1, 17],
      2 :  [21],
      5 :  [30],
      7 :  [4],
      9 :  [5],
      10 : [10],
      11 : [11,24],
      12 : [26]
    };
    break;
  case 2006:
    var holidays = {
      1 :  [2, 16],
      2 :  [20],
      5 :  [29],
      7 :  [4],
      9 :  [4],
      10 : [9],
      11 : [11, 23],
      12 : [25]
    };
    break;
  case 2007:
    var holidays = {
      1 :  [1, 15],
      2 :  [19],
      5 :  [28],
      7 :  [4],
      9 :  [3],
      10 : [8],
      11 : [12, 22],
      12 : [25]
    }
    break;
  }

  // disable weekends
  if (dayofweek == 0 || dayofweek == 6)
    return true;
  
  // disable holidays (month is zero based)
  holidaysthismonth = holidays[m+1];
  if (!holidaysthismonth) 
    return false;
  for (var i in holidaysthismonth) 
    if (holidaysthismonth[i] == d) 
      return true;

  // day is ok
  return false;
}

