﻿// JScript File

//  Queue the start of the Calendar stuff...
var oldLink = null;

var arivCal = null;
var deptCal = null;

var today = new Date(); 
var departureEpochDate = 0;
var arrivalEpochDate = 0;                            
var deptcount = 0;
var arivcount = 0;

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

var hiddenDepartureEle = null;
var hiddenArrivalEle = null;

var validationArrivalFunction = null;
var validationDestinationFunction = null;

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.desroy();

  _dynarch_popupCalendar = null;
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showDeptCalendar(id, format, showAtEle, hiddenEle, validationDestinationFunction) {
    var el = document.getElementById(id);
    if (deptCal != null) {
        deptCal.refresh();
        deptCal.hide();                 // so we hide it first.
    } 
    else {
        deptCal = new Calendar(1, null, setDepartureDate, closeHandler);
        deptCal.setRange(1900, 2070);        // min/max year allowed.
        deptCal.setDisabledHandler(isDepartureDisabled);
        deptCal.create();
    }
    deptCal.setDateFormat(format);    // set the specified date format
    deptCal.parseDate(el.value);      // try to parse the text in field
    deptCal.sel = el;                 // inform it what input field we use

    showAtEleID = document.getElementById(showAtEle)
    deptCal.validationDestinationFunction = validationDestinationFunction;
    
    if (hiddenEle != "") {
        hiddenDepartureEle = document.getElementById(hiddenEle);
    }

    deptCal.showAtElement(showAtEleID, "Br");        // show the calendar
    deptCal.refresh();
    
    return false;
}

function showArivCalendar(id, format, showAtEle, hiddenEle, validationArrivalFunction) {
    var el = document.getElementById(id);
    arivCal = new Calendar(1, null, setArrivalDate, closeHandler);
    arivCal.setRange(1900, 2070);        // min/max year allowed.
    arivCal.setDisabledHandler(isDepartureDisabled);
    arivCal.create();
    arivCal.setDateFormat(format);    // set the specified date format
    arivCal.parseDate(el.value);      // try to parse the text in field
    arivCal.sel = el;                 // inform it what input field we use

    showAtEleID = document.getElementById(showAtEle)
    arivCal.validationArrivalFunction = validationArrivalFunction;
    
    if (hiddenEle != "") {
        hiddenArrivalEle = document.getElementById(hiddenEle);
    }
    
    arivCal.showAtElement(showAtEleID, "Br");        // show the calendar
    arivCal.refresh();
    return false;
}

function showStockCalendar(id, format, showAtEle, hiddenEle, validationArrivalFunction, noYear) {
    var el = document.getElementById(id);
    var startYear = 1900;
    var endYear = new Date().getFullYear() + 2;
    
    if (noYear == true) {
		startYear = new Date().getFullYear();
		endYear = startYear;
    }
    
    stockCal = new Calendar(1, null, setArrivalDate, closeHandler);
    stockCal.setRange(startYear, endYear);        // min/max year allowed.
    // arivCal.setDisabledHandler(isDepartureDisabled);
    stockCal.create();
    stockCal.setDateFormat(format);    // set the specified date format
    stockCal.parseDate(el.value);      // try to parse the text in field
    stockCal.sel = el;                 // inform it what input field we use

    showAtEleID = document.getElementById(showAtEle);
    stockCal.validationArrivalFunction = validationArrivalFunction;
    
    if (hiddenEle != "") {
        hiddenStockEle = document.getElementById(hiddenEle);
    }
    
    stockCal.showAtElement(showAtEleID, "Br");        // show the calendar
    stockCal.refresh();
    
    if (noYear == true) {
		// hacks to remove Year selection from UI
		stockCal.title.style.visibility = 'hidden';
		stockCal._nav_py.style.display = 'none';
		stockCal._nav_now.colSpan = stockCal.title.colSpan;
		stockCal._nav_ny.style.display = 'none';
    }
    
    return false;
}

function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function isDepartureDisabled(date) {                      
    if (date.getTime() < today.getTime() ){
        return true;
    }
}
function isArrivalDisabled(date) {
   
    if (date.getTime() < today.getTime() ){
        return true;
    }
    return false;
}   

function setArrivalDate(cal, date){
    var arivDate = cal.date;
    // var params = cal.params;
    arrivalEpochDate = arivDate.getTime();
    arrivalRealDate = arivDate.getDate() + "/" + (arivDate.getMonth() + 1) + "/" + arivDate.getFullYear();
    if (hiddenArrivalEle) {
        hiddenArrivalEle.value = arrivalRealDate;
    }    
    cal.sel.value = date;
    cal.refresh();
    
    if (cal.dateClicked){
        cal.callCloseHandler();    
        if (cal.validationArrivalFunction){
           cal.validationArrivalFunction();
        }        
    }
}

function setDepartureDate(cal, date){
    var deptDate = cal.date;
    departureEpochDate = deptDate.getTime();
    departureRealDate = deptDate.getDate() + "/" + (deptDate.getMonth() + 1) + "/" + deptDate.getFullYear();
    //alert(departureEpochDate);
    if (hiddenDepartureEle) {
        hiddenDepartureEle.value = departureRealDate;
    }
    cal.sel.value = date;
    // alert(deptcount);
    deptcount=0;
    cal.refresh();
    if (cal.dateClicked){
        cal.callCloseHandler(); 
        if (deptCal.validationDestinationFunction){
           deptCal.validationDestinationFunction();
        }             
    }
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(0, null, flatSelected);

  // hide week numbers
  cal.weekNumbers = false;

  // We want some dates to be disabled; see function isDisabled above
  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showCalendar" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  cal.create(parent);

  // ... we can show it here.
  cal.show();
}

function checkDateForm(formElement, doSubmit){ 
    hiddenArrivalDate = document.getElementById("hiddenArrivalDate");
    ArrivalDate = arrayToDate(hiddenArrivalDate.value);    
    hiddenDepartureDate = document.getElementById("hiddenDepartureDate");
    DepartureDate = arrayToDate(hiddenDepartureDate.value);
    if (ArrivalDate < DepartureDate){    
        alert("The date you have selected to leave on is before the arrival date.  Please check your arrival/leaving dates.");
        return false;
    }
    else {
        if (doSubmit){
            formName = formElement.form.name;
            doc = document.forms[formName];
            doc.submit();
        }
    }
    return true;
}

function arrayToDate(valueToConvert){
    // This expects ONLY 3 element date
    tempSlashDate = valueToConvert;
    splitDate = tempSlashDate.split("/");
    tempDate = new Date(splitDate[2], splitDate[1], splitDate[0]);
    return tempDate.getTime();
}

function calendarInit(){
    window.onload = function(){

        var arrivalImageTrigger = document.getElementById('arrival_image_trigger');
        var arrivalInputTrigger = document.getElementById('ArrivalDateInput');
        
        var departureImageTrigger = document.getElementById('departure_image_trigger');
        var departureInputTrigger = document.getElementById('DepartureDateInput');
        
        if (arrivalInputTrigger){
            arrivalInputTrigger.onclick = function(){                                    
                arrivalImageTrigger.onclick();
            }
        }
        if (departureInputTrigger){
            departureInputTrigger.onclick = function(){                                    
                departureImageTrigger.onclick();
            }
        }
    }
}