var today = new Date();
var cal0;
var cal0Hidden = ""; 
var cal0SkyLightsHiddenMonthYear = ""; 
var cal0SkyLightsHiddenDay = ""; 
var disabledTxt = "---------------------------------------";
var maxDaysOut = 180;

function getObjInnerText (obj) 
{ 
    return (obj.innerText) ? obj.innerText : (obj.textContent) ? obj.textContent : ""; 
} 

/*
wherewefly destinations dual calendar
------------------------------------------------*/
function init() {

  cal0 = new YAHOO.widget.Calendar("cal0", "cal0Container");
  cal0.renderCellDefault = renderDefault;
  cal0.renderCellNotThisMonth = renderNotThisMonth;
  cal0.onChangePage = changePage0;
  cal0.onSelect = dateSelect0;
  cal0.buildShellFooter = buildFooter;
  cal0.customConfig = customize;
  cal0.setupConfig();
  cal0.render();
  
  // skylights compatible variables
  //cal0SkyLightsHiddenDay = $("sector_1_d");
  //cal0SkyLightsHiddenMonthYear = $("sector_1_m");
  //cal0Hidden = $("MARKET1_DATE");

  YAHOO.util.Event.addListener(cal0.oDomContainer.id.replace("Container", "Trigger"), "click", showCalendar0, cal0, true);
  YAHOO.util.Event.addListener(document.body, "click", offClick);
}

function renderDefault(workingDate, cell) {
  cell.innerHTML = ""; 
  var theDay;

  if (workingDate.getDate() < 10)
    theDay = "0" + this.buildDayLabel(workingDate);
  else
    theDay = this.buildDayLabel(workingDate);

  // if the date is before today, only display text, otherwise display a link
  // normally we'd use renderBodyCellRestricted, but it doesnt seem to facilitate 
  // blocking out a large set of dates
  var lastYear = new Date();
  lastYear.setFullYear(2007, 0, 0);
  
  if((workingDate < lastYear) || (workingDate > today))
  {
    cell.className = this.Config.Style.CSS_CELL_RESTRICTED;
    cell.appendChild(document.createTextNode(theDay));
  }
  else
  { 
    var link = document.createElement("a");

    link.href="javascript:void(null);" 
    link.name=this.id+"__"+workingDate.getFullYear()+"_"+(workingDate.getMonth()+1)+"_"+workingDate.getDate(); 

    link.appendChild(document.createTextNode(theDay));
    cell.appendChild(link); 
  }
}

function renderNotThisMonth(workingDate, cell) {
  cell.innerHTML = "";
  return YAHOO.widget.Calendar_Core.STOP_RENDER;
}

function customize() {
  // local
  this.Config.Locale.WEEKDAYS_SHORT = ["S", "M", "T", "W", "T", "F", "S"];

  // styles
  this.Config.Style.CSS_ROW_HEADER = "calendarHeader";
  this.Config.Style.CSS_HEADER  = "calendarHeader";
  this.Config.Style.CSS_HEADER_TEXT  = "calendarHeaderText";
  this.Config.Style.CSS_WEEKDAY_ROW  = "calendarWeekRow";
  this.Config.Style.CSS_CALENDAR  = "calendar";
  this.Config.Style.CSS_CELL_SELECTED = "cellSelected";

  // options
  this.Config.Options.NAV_ARROW_LEFT = "/i/calBack.jpg";
  this.Config.Options.NAV_ARROW_RIGHT = "/i/calForward.jpg";
  this.Config.Options.LOCALE_MONTHS = this.Config.Locale.MONTHS_SHORT;
}

function buildFooter() {
  var obj = this;
  var cid = obj.oDomContainer.id;
  var foot = document.createElement("TFOOT");
  var row = document.createElement("TR");
  var cell = document.createElement("TD"); cell.colSpan = 7;
  var link = document.createElement("a");
  var img = document.createElement("img");

  img.src = "/i/closePop.gif";
  link.href = "javascript:void(null);";
  link.onclick = function() {
    hide(cid);
  };
  link.appendChild(img);

  cell.appendChild(link); row.appendChild(cell); foot.appendChild(row); this.table.appendChild(foot);
}

function showCalendar0(e, obj) {

  var correctDate;
  if(this.getSelectedDates().length !=0) correctDate = this.getSelectedDates()[0];

  else correctDate = this.today;

  this.setMonth(correctDate.getMonth()); this.setYear(correctDate.getFullYear());

  var diff = Date.parse(correctDate.toString()) - Date.parse(this.today);
  
  if(this.pageDate.getMonth() == this.today.getMonth() && this.pageDate.getYear() == this.today.getYear()) {
    this.Config.Style.CSS_NAV_LEFT = "calnavH"; this.Config.Style.CSS_NAV_RIGHT = "calnavright";
  }
  else if(diff > (1000*60*60*24*maxDaysOut)) {
    this.Config.Style.CSS_NAV_LEFT = "calnavleft"; this.Config.Style.CSS_NAV_RIGHT = "calnavH";
  }
  else {
    this.Config.Style.CSS_NAV_LEFT = "calnavleft"; this.Config.Style.CSS_NAV_RIGHT = "calnavright";
  }
    this.Config.Style.CSS_NAV_LEFT = "calnavleft"; this.Config.Style.CSS_NAV_RIGHT = "calnavright";
  
  if(this.pageDate.getYear() == this.today.getYear() && this.pageDate.getMonth() == this.today.getMonth())
  {
    this.Config.Style.CSS_NAV_RIGHT = "calnavH";
  }
  this.render();

  $(this.oDomContainer.id).style.display = "block";
}


function hide(id) {
  $(id).style.display = "none";
}

function changePage0() {

  var currDate = this.today; var nextDate = this.pageDate;
  var diff = Date.parse(nextDate.toString()) - Date.parse(currDate.toString());

  if(diff <= 0) {
    this.Config.Style.CSS_NAV_LEFT = "calnavH"; this.Config.Style.CSS_NAV_RIGHT = "calnavright";
  }

  else if(diff > (1000*60*60*24*maxDaysOut)) {
    this.Config.Style.CSS_NAV_LEFT = "calnavleft"; this.Config.Style.CSS_NAV_RIGHT = "calnavH";
  }

  else {
    this.Config.Style.CSS_NAV_LEFT = "calnavleft"; this.Config.Style.CSS_NAV_RIGHT = "calnavright";
  }
    this.Config.Style.CSS_NAV_LEFT = "calnavleft"; this.Config.Style.CSS_NAV_RIGHT = "calnavright";
  
  //ensure not before the starting date of one year previous to today
  if(this.pageDate.getYear() <= 2007 && this.pageDate.getMonth() <= 0)
  {
    this.Config.Style.CSS_NAV_LEFT = "calnavH";
  }
  if(this.pageDate.getYear() == this.today.getYear() && this.pageDate.getMonth() == this.today.getMonth())
  {
    this.Config.Style.CSS_NAV_RIGHT = "calnavH";
  }
  
  this.render();
}

function initializePostBackDate(obj)
{
    hiddenCalField = document.getElementById("ctl00_MainSection_uxDepartureDate");
    
    var c = obj ? obj : this;
    var parsed = new Date(hiddenCalField.value);
    if(isNaN(parsed))
    {
        return;
    }   
    var day = parsed.getDay();
    var month = parsed.getMonth(); 
    
    var jbDate = c.Config.Locale.WEEKDAYS_MEDIUM[day] + ", " +
                c.Config.Locale.MONTHS_SHORT[month] + ", " +
                parsed.getDate() + ", " +
                parsed.getFullYear();
    $(c.oDomContainer.id.replace("Container", "Input")).innerHTML = jbDate;
    
    hide(c.oDomContainer.id);
}

function dateSelect0(obj) {
  var c = obj ? obj : this;
  var currDate = c.getSelectedDates()[0]; var parsed = Date.parse(currDate.toString());
  var jbDate = c.Config.Locale.WEEKDAYS_MEDIUM[currDate.getDay()] + ", " +
               c.Config.Locale.MONTHS_SHORT[currDate.getMonth()] + " " +
               currDate.getDate() + ", " +
               currDate.getFullYear();

  $(c.oDomContainer.id.replace("Container", "Input")).innerHTML = jbDate;

  // format date for skylights
  //cal0SkyLightsHiddenDay.value = currDate.getDate() < 10 ? "0" + currDate.getDate() : currDate.getDate();
  //var monthVal = (currDate.getMonth() + 1) < 10 ? "0" + (currDate.getMonth() + 1) : currDate.getMonth() + 1;
  //cal0SkyLightsHiddenMonthYear.value = monthVal + currDate.getFullYear().toString();
  hiddenCalField = document.getElementById("ctl00_MainSection_uxDepartureDate");

  hiddenCalField.value = (currDate.getMonth() + 1) + "/" + currDate.getDate() + "/" + currDate.getFullYear();
  
  hide(c.oDomContainer.id);
}

function reenableCal(cal) {
  var theInput = $(cal.oDomContainer.id.replace("Container", "Input"));
  if(theInput.innerHTML == disabledTxt || (theInput.getElementsByTagName("a")[0] && theInput.getElementsByTagName("a")[0].innerHTML == disabledTxt)) {
    theInput.style.color = "#4b90cd";
    theInput.innerHTML = "Date to return?";
    if(theInput.firstChild.nodeName.toLowerCase() == "a")
      theInput.firstChild.style.fontSize = "12px";
  }
}

function disableCal(cal, hid) {
  var theInput = $(cal.oDomContainer.id.replace("Container", "Input"));
  theInput.innerHTML = disabledTxt; theInput.style.color = "#cccccc";
  if(theInput.getElementsByTagName("a")[0]) theInput.getElementsByTagName("a")[0].style.color = "#cccccc";
  cal.clear(); $(hid).value = "";
  /* clear out return date if oneway flight - mK - 3/2/2007 */
  

  if(hid == 'MARKET2_DATE') {
  
  document.bBaf.sector_2_d.value = '';
  document.bBaf.sector_2_m.value = '';
  
  }
	var trigger = cal.oDomContainer.id.replace("Container", "Trigger");
}

function offClick(e) {
  if(!e) e = window.event;
  var caller = e.srcElement ? e.srcElement : e.target;
  var chain0 = false;
  var chain1 = false;
  
  while(caller) {
    if(caller.id == cal0.oDomContainer.id.replace("Container", "Root")) {
      chain0 = true;
      break;
    }
    caller = caller.parentNode;
  }

  if(!chain0) hide(cal0.oDomContainer.id);
}


