/*
  -------------------------------------------------------------------------------------------------

  : datedropdown_client.js
  : ben miller : ben@hyl.co.uk : http://digital.hyl.co.uk
  : 28 October 2004

  - bits and pieces required for date drop down control

  -------------------------------------------------------------------------------------------------
*/

/*
  churns existing day month and year drop down results through __DDDGetDate which
  returns a valid date. this is then passed back to the drop downs with __DDDSetDate.
*/
function __DDDValidateDay(fpat, ctrlID, fmID)
{
  var fm = (fmID==null)?document.forms[0]:document.forms[fmID];
  var d = __DDDGetDate(fpat, ctrlID, fm);
  if(d!=null)
  {
    __DDDSetDate(fpat, ctrlID, fm, d);
  }
}

/*
  changes the date if the day of the week changes.
  this is obviously restricted to the 7 dates of the week range.
*/
function __DDDWeekInfluence(fpat, ctrlID, fmID, ddEl)
{
  var fm = (fmID==null)?document.forms[0]:document.forms[fmID];
  var d  = __DDDGetDate(fpat, ctrlID, fm);
  if(d!=null)
  {
    var iDayOffset = ddEl.options[ddEl.selectedIndex].value - d.getDay();
    d.setDate(d.getDate()+iDayOffset);
    __DDDSetDate(fpat, ctrlID, fm, d);
  }
}

/*
  loops through date drop downs comparing with the format pattern array (fpat)
  as a result only the last drop down of each year, month or day is the one that influences the date.
  the return date is validated by js's native date object so that 31st September returns
  1st October or 29th February 2003 returns 1st March 2003.
*/
function __DDDGetDate(fpat, ctrlID, fm)
{
  var yr, mn, dt = 1;

  // textbox
  if(!isUndefined(fm.elements[ctrlID].type) && fm.elements[ctrlID].type == 'text')
  {
    var sD = fm.elements[ctrlID].value;
    if(sD.length != 8)
    {
      return null;
    }

    if(!isNaN(Date.parse(sD.substr(3, 2) + '/' + sD.substr(0, 2) + '/20' + sD.substr(6, 2))))
    {
      return new Date(parseInt(sD.substr(6, 2)) + 2000, Math.abs(sD.substr(3, 2))-1, Math.abs(sD.substr(0, 2)));
    }

    return null;
  }

  for(var i=0;i<fpat.length;i++)
  {
    el = isUndefined(fm.elements[ctrlID][0].type)?fm.elements[ctrlID]:fm.elements[ctrlID][i];

    if(el!=null)
    {
      switch(fpat[i])
      {
        case 'y':
        case 'yy':
        case 'yyyy':
        {
          yr = selectedValue(el);
          break;
        }
        case 'M':
        case 'MM':
        case 'MMM':
        case 'MMMM':
        {
          mn = selectedValue(el)-1;
          break;
        }
        case 'd':
        case 'dd':
        {
          dt = selectedValue(el);
          break;
        }
        default:
        {
          break;
        }
      }
    }
  }

  if(isNaN(Date.parse(mn + '/' + dt + '/' + yr)))
  {
    return null;
  }

  return new Date(yr, mn, dt);
}

/*
  loops through date drop downs comparing with the format pattern array (fpat) and
  assigning the corresponding value to each one from date object d.
*/
function __DDDSetDate(fpat, ctrlID, fm, d)
{
  // textbox
  if(!isUndefined(fm.elements[ctrlID].type) && fm.elements[ctrlID].type == 'text')
  {
    fm.elements[ctrlID].value = LZ(d.getDate()) + '/' + LZ(d.getMonth() + 1) + '/' + (d.getFullYear()+'').substr(2);
    return;
  }

  // dropdowns
  for(var i=0;i<fpat.length;i++)
  {
    el = isUndefined(fm.elements[ctrlID][0].type)?fm.elements[ctrlID]:fm.elements[ctrlID][i];

    if(el!=null)
    {
      switch(fpat[i])
      {
        case 'y':
        case 'yy':
        case 'yyyy':
        {
          el.selectedIndex = selectedValue(el, d.getFullYear());
          break;
        }
        case 'M':
        case 'MM':
        case 'MMM':
        case 'MMMM':
        {
          el.selectedIndex = selectedValue(el, d.getMonth()+1);
          break;
        }
        case 'd':
        case 'dd':
        {
          el.selectedIndex = selectedValue(el, d.getDate());
          break;
        }
        case 'ddd':
        case 'dddd':
        {
          el.selectedIndex = selectedValue(el, d.getDay());
          break;
        }
        default:
        {
          break;
        }
      }
    }
  }
}

/*
  utility that gets or sets the value of a dropdown form element
*/
function selectedValue(el, val)
{
  if(val!=null)
  {
    for(var i=0;i<el.options.length;i++)
    {
      if(el.options[i].value==val)
      {
        el.options[i].selected = true;
        return i;
      }
    }
  }
  return el.options[el.selectedIndex].value;
}