/*
* to use on a page you must have a text input to set the value of the selected date
* to use on a page you must have a hidden input named nCals and it MUST have an id="nCals" set to the number of calendars on the page.
* you must have an iframe of the correct size named and id'd as CalFrame_n where _n is some number less than or equal to the value of nCals
* you must have a calendar image or text link to click on opening the calendar iframe
*
* an example is given below
*
* <input type="text" name="DepDate1" id="DepDate1" size="12" class="plannerTextBox" value="<CFOUTPUT>#request.DepDate1#</CFOUTPUT>" onClick="showCal2m('CalFrame1','DepDate1','DepDate1','f');">
* <a href="javascript:void(0);" id="callink1" onClick="showCal2m('CalFrame3','DepDate1','DepDate1','f');"><img src="/images/smallCal.gif" align="absmiddle" border="0" vspace="0" id="cal1"></a>
* <iframe style="display:none;position:absolute;z-index:100;vertical-align:bottom;align:left;" name="CalFrame1" id="CalFrame3" width="243" height="179" marginheight="0" marginwidth="0" noresize frameborder="0" scrolling="NO" src="/images/spacer.gif" NOTAB></iframe>
*/


function showCal2m(i,f,d,z){
  /*
    keys for input arguments
    i iframe to target
    f fieldname on form to set date
    d previous date field to start calendar from
    z is calendar type;  a is for all dates; f for forward dates only
  */
  closeAllCal();
  var c = document.getElementById(i);
  var p = document.getElementById(d);
  var calExt = '';
  if(z == 'a'){
    calExt = '_all';
  }else{
    calExt = '';
  }
  if (c!=null){
    if (p.value==''){
      c.src='/common/calendar2m' + calExt + '.cfm?i=' + i + '&f=' + f;
    }else{
      if(p.value=='mm/dd/yyyy'){
        p.value='';
        c.src='/common/calendar2m' + calExt + '.cfm?i=' + i + '&f=' + f;
      }else{
        c.src='/common/calendar2m' + calExt + '.cfm?i=' + i + '&f=' + f + '&Date=' + p.value;
      }
    }
    c.style.display = 'block';
  }
}

function showCal(i,f,d){//single calendar use above for double calendar
  var c = document.getElementById(i);
  var p = document.getElementById(d);
  if (c!=null){
    if (p.value==''){
      c.src='/common/calendar.cfm?i=' + i + '&f=' + f;
    }else{
      if(p.value=='mm/dd/yyyy'){
        p.value='';
        c.src='/common/calendar.cfm?i=' + i + '&f=' + f;
      }else{
        c.src='/common/calendar.cfm?i=' + i + '&f=' + f + '&Date=' + p.value;
      }
    }
    c.style.display = 'block';
  }
}

function closeAllCal(){
  var n = document.getElementById('nCals');
  for(var i=1; i <= n.value; i++){
    thisFrameED = 'CalFrameED' + i;
    thisFrameLD = 'CalFrameLD' + i;
    thisFrame = 'CalFrame' + i;
    closeCal(thisFrameED);
    closeCal(thisFrameLD);
    closeCal(thisFrame);
  }
}

function closeCal(i){
  var c = document.getElementById(i);
  if (c!=null){
    c.src='/images/spacer.gif';
    c.style.display = 'none';
  }
}

function setDate(i,f,d){
  document.getElementById(f).value=d;
  var n = document.getElementById('nCals');
  for(var x=1; x <= n.value; x++){
    thisFrame = 'CalFrame' + x;
    if(i==thisFrame){
      var tfield = document.getElementById('DepTime' + x);
      if(tfield!=null){
        setDefaultTime('DepTime' + x);
        tfield.focus();
      }
    }
    closeCal(i);
  }
}

function initCal(){
  closeAllCal()
}


function setDefaultTime(i){
  c = document.getElementById(i);
  if(c!=null){
    if(c.value==''){
      c.value='8:00';
    }
  }  
}

function setDefaultDate(i){
  c = document.getElementById(i);
  if(c.value==''){
    c.value='mm/dd/yyyy';
    c.select();
  }else{
    c.select();
  }
}
