
//--------calx.js
document.onclick = HCal

var FDate = 'mm/dd/yy';
var TDate = 'mm/dd/yy';
var MDate = '';
var CalS = '';
var CalE = '';

var FTime = '362';
var TTime = '362';


var cF=null;var cW=null;var g_tid=0;var g_cP,g_eD,g_eDP,g_dmin,g_dmax,g_dtsToBeSlct;
var g_FlowType;
var g_isDtRng;
var g_dispCalOnRightOfCtrl;
var nextFocus;var g_fNoCal=false;
//****************Culture Datetime Format Begin********************
var dtSep = "/";
var dayPos;
var monthPos;
var yearPos;
if (!window.cultureDateTimeFormat)
    var cultureDateTimeFormat ="mm/dd/yyyy";
if (!window.dtSep)
    var dtSep = "/";

if (!window.startDayOfWeek)
    var startDayOfWeek = 0;


function setDateParameters(dtfmt) {
    if (!window.dayPos || !window.monthPos || !window.yearPos) {
        cultureDateTimeFormat = dtfmt;
        var dtymdPos = cultureDateTimeFormat.toLowerCase().split(dtSep);
        if (dtymdPos.length == 3) {
            if (dtymdPos[0].indexOf("d") >= 0) dayPos = 0; else if (dtymdPos[0].indexOf("m") >= 0) monthPos = 0; else if (dtymdPos[0].indexOf("y") >= 0) yearPos = 0;

            if (dtymdPos[1].indexOf("d") >= 0) dayPos = 1; else if (dtymdPos[1].indexOf("m") >= 0) monthPos = 1; else if (dtymdPos[1].indexOf("y") >= 0) yearPos = 1;

            if (dtymdPos[2].indexOf("d") >= 0) dayPos = 2; else if (dtymdPos[2].indexOf("m") >= 0) monthPos = 2; else if (dtymdPos[2].indexOf("y") >= 0) yearPos = 2;

        }
    }
}
//****************Culture Datetime Format End********************

function getEventObj(e) { if (!e) e = window.event; return e; }

function stopBubble(e) { e = getEventObj(e); e.cancelBubble = true; if (e.stopPropagation) e.stopPropagation(); }

function CB() { stopBubble(event); }

function dispCalendar(elm, dt1, dt2, flowType, isDtRng, dispCalOnRightOfCtrl, showPassedDate) {

    g_FlowType = '';
    g_isDtRng = true;
    g_dispCalOnRightOfCtrl = true;

    if (dispCalOnRightOfCtrl != null)
        g_dispCalOnRightOfCtrl = dispCalOnRightOfCtrl;
    if (isDtRng != null)
        g_isDtRng = isDtRng;
    if (flowType != null)
        g_FlowType = flowType;

    var mindt = '';

    if (showPassedDate == null || showPassedDate == false) {
        mindt = (new Date()).toDateString();
    }
    if (dt1 != null && dt2 != null && GetTag(dt1) != null && GetTag(dt2) != null) {
        if (dt1 != null && GetTag(dt1) != null && elm != dt1)//case of from date
        {
            mindt = GetInputDate(GetTag(dt1).value); //mindate will be from date 
        }
    }

    if (mindt == cultureDateTimeFormat) mindt = '';

    var fromTo = '';

    if (dt1 != null && GetTag(dt1) != null) {
        fromTo = GetTag(dt1).value;
        if (GetTag(dt1).value == cultureDateTimeFormat) fromTo = '';
    }
    if (dt2 != null && GetTag(dt2) != null && g_isDtRng == true) {
        if (GetTag(dt2).value != cultureDateTimeFormat) {
            fromTo += "," + GetTag(dt2).value;
        }
    }
    //checking if not a valid date then setting current date as min date    
    if (isNaN(new Date(GetTag(dt1).value).getYear()))
        mindt = (new Date()).toDateString();

    showCalToSelectDate(elm, fromTo, mindt, '');
}
function dispDateRangeCalendar(elm, dt1, dt2, minDate, maxDate, flowType_) {
    g_FlowType = '';
    g_isDtRng = true;
    g_dispCalOnRightOfCtrl = true;

    if (flowType_ != null)
        g_FlowType = flowType_;

    var mindt = '', maxdt = '';
    if (minDate != null)
        mindt = minDate;
    if (maxDate != null)
        maxdt = maxDate;
    if (dt1 != null && GetTag(dt1) != null) {
        fromTo = GetTag(dt1).value;
        if (GetTag(dt1).value == cultureDateTimeFormat) fromTo = '';
    }
    if (dt2 != null && GetTag(dt2) != null && g_isDtRng == true) {
        if (GetTag(dt2).value != cultureDateTimeFormat) {
            fromTo += "," + GetTag(dt2).value;
        }
    }
    showCalToSelectDate(elm, fromTo, mindt, maxdt);
}
function GetTag(tagId) {
    return document.getElementById(tagId);
}

function showCalToSelectDate(obj, dtsToBeSlct, dtMin, dtMax) {
    var ctrl = GetTag(obj);
    CalS = dtMin; //set Minimum date
    CalE = dtMax;
    SC(ctrl, dtsToBeSlct);
}

function SC(el, dtsToBeSlct) {
    if (DE('calendar') == null) {
        return;
    }
    var id = el.id;
    var n = el.id;
    el.select();
    var temp = id;
    ShowCalendar(el, el, DE(temp), CalS, CalE, dtsToBeSlct);
}

function DE(el) {
    return GetTag(el);
}

function ShowCalendar(eP, eD, eDP, dmin, dmax, dtsToBeSlct) {

    SCal(eP, eD, eDP, dmin, dmax, dtsToBeSlct);

}

function SCal(cP, eD, eDP, dmin, dmax, dtsToBeSlct) {
    clearTimeout(g_tid);

    if (g_fNoCal) {
        g_fNoCal = false; return;
    }
    if (g_calShown && eD == g_eD) {
        //    return;
    }
    g_calShown = true;
    g_calCB = null;
    g_cP = cP;
    g_eD = eD;
    g_eDP = eDP;
    g_dmin = dmin;
    g_dmax = dmax;
    g_dtsToBeSlct = dtsToBeSlct;

    WaitCal();
}


function CancelCal() { clearTimeout(g_tid); if (!cF) cF = getObj('calendar'); cF.style.visibility = "hidden"; g_calShown = false; }
function WaitCal() {
    if (!cW)
        cW = frames['calendar'];

    if (null == cW || null == cW.g_fCL || false == cW.g_fCL) {
        g_tid = setTimeout("WaitCal()", 200);
    }
    else {
        if (!cF) cF = getObj('calendar');
        cF.style.visibility = "hidden";
        setTimeout("DoCal()", 1);
    }
}

function getObj(objID) {
    if (GetTag) { return GetTag(objID); }
    else if (document.all) { return document.all[objID]; }
    else if (document.layers) { return document.layers[objID]; }
}

function DoCal() {
    PosCal(g_cP);
    if (!cW) cW = frames['calendar'];
    cW.DoCal(g_eD, g_eDP, g_dmin, g_dmax, g_dtsToBeSlct, 2, g_FlowType, g_isDtRng);
}

function PosCal(cP) {
    var dB = document.body; var eL = 0; var eT = 0;
    if (!cF) cF = getObj('calendar');
    for (var p = cP; p && p.tagName != 'BODY'; p = p.offsetParent) { eL += p.offsetLeft; eT += p.offsetTop; }
    var eH = cP.offsetHeight; var dH = parseInt(cF.style.height); var sT = getScrollTop();
    if (g_dispCalOnRightOfCtrl) {
        if (eT - dH >= sT && eT + dH > getWinHeight() + sT) eT -= (dH - eH);
        cF.style.left = eL + 30 + cP.offsetWidth + 'px'; cF.style.top = eT + 'px';
    }
    else {
        if (eT - dH >= sT && eT + eH + dH > getWinHeight() + sT) eT -= dH; else eT += eH;
        cF.style.left = eL + 'px'; cF.style.top = eT + 'px';
    }
}

function getScrollTop() {
    if (document.documentElement.scrollTop) return document.documentElement.scrollTop;
    if (document.body.scrollTop) return document.body.scrollTop;
    if (window.pageYOffset) return window.pageYOffset;
    return 0;
}

function getWinHeight() {
    if (window.innerHeight) return window.innerHeight;
    if (document.documentElement.clientHeight) return document.documentElement.clientHeight;
    if (document.body.clientHeight) return document.body.clientHeight;
    return 0;
}

function SetNextFocus(e) { nextFocus = e; if (nextFocus) nextFocus.onfocus = CancelCal; }
function SetPrevFocus(e) { if (e) e.onfocus = CancelCal; }

function FGoNextFocus() { if (nextFocus) { nextFocus.focus(); return true; } return false; }

function CalSetFocus(e) { if (e) { g_fNoCal = true; e.focus(); setTimeout("EndCalFocus()", 200); } }
function EndCalFocus() { g_fNoCal = false; }

function CalDateSet(eInp, d, m, y, giveFocus) {
    eInp.value = GetFormattedDate(y, m, d);
    if (!giveFocus)
        CalSetFocus(eInp);

}

var g_calShown = false;
function SetCalShown(fcshown) { g_calShown = fcshown; }

var g_calCB;
function CalCallback() { if (g_calCB) g_calCB(); }
function SetCalendarCallback(cb) { g_calCB = cb; }

var rM = new Array(12); rM[0] = rM[2] = rM[4] = rM[6] = rM[7] = rM[9] = rM[11] = 31; rM[3] = rM[5] = rM[8] = rM[10] = 30; rM[1] = 28;
function GetMonthCount(m, y) { var c = rM[m]; if ((1 == m) && IsLY(y)) c++; return c; }
function IsLY(y) { if (0 == y % 4 && ((y % 100 != 0) || (y % 400 == 0))) return true; else return false; }
function DefYr(m, d) { var dt = new Date(); var yC = (dt.getYear() < 1000) ? 1900 + dt.getYear() : dt.getYear(); if (m < dt.getMonth() || (m == dt.getMonth() && d < dt.getDate())) yC++; return yC; }

function GetDowStart() { return startDayOfWeek; } function GetDtFmt() { return "mmddyy"; } function GetDtSep() { return "/"; }

function ShowCalSimp(fm, eD, eDP, dmin, dmax, dtsToBeSlct) {
    if (!dmin) dmin = '3/1/2005'; if (!dmax) dmax = '1/25/2006';
    if (fm) { SetNextFocus(objNext(fm, eD)); SetPrevFocus(objPrev(fm, eD)); }
    SCal(eD, eD, eDP, dmin, dmax, dtsToBeSlct);
}

function hideCalendar() {
    GetTag("calendar").style.display = "none";
    GetTag(actvEl).focus();
}

var zz, zv, d, fTSR;
d = new Date();
fTSR = 0;
zv = d.getTime();
zz = "&zz=" + zv;

var gBF = false;
function GoTo(u) { window.top.location = u + zz; }
function Go(u) { window.top.location = u; }

function BF() { gBF = true; }

function Foci(o) { if (!gBF && IsVis(o)) { o.focus(); } }

function IsVis(o) {
    if (!o || o.type == "hidden")
        return false;

    while (o && o.style && o.style.display != 'none') {
        o = o.parentNode;
    }
    return !o || !o.style;
}


function TEK(a, evt) {
    var keycode;
    if (window.event) { keycode = window.event.keyCode; evt = window.event; }
    else if (evt) { keycode = evt.which; }
    else { return true; }
    if (13 == keycode) { evt.cancelBubble = true; evt.returnValue = false; eval(a); }
}

function getObj(objID) {
    if (GetTag) { return GetTag(objID); }
    else if (document.all) { return document.all[objID]; }
    else if (document.layers) { return document.layers[objID]; }
}

function objNext(f, d) {
    var fFnd = false, el = f.elements, i = 0;
    for (; i < el.length; i++) {
        if ('hidden' != el[i].type && false == el[i].disabled && IsVis(el[i]) && fFnd) return el[i];
        if (d.id == el[i].id) fFnd = true;
    }
    return null;
}
function objPrev(f, d) {
    var fFnd = false, el = f.elements, i = el.length - 1;
    for (; i >= 0; i--) {
        if ('hidden' != el[i].type && false == el[i].disabled && IsVis(el[i]) && fFnd) return el[i];
        if (d.id == el[i].id) fFnd = true;
    }
    return null;
}

var actvEl;
function HCal() {
    if (GetTag('calendar')) {
        GetTag('calendar').style.visibility = "hidden";
    }
}

function setRdate(depID, retID) {
    setReturnDate(depID, retID, curTab, GetTag('rbOW').checked);
}

function setReturnDate(depID, retID, bookingType, isOneWayTrip) {
    if (bookingType == "F" && isOneWayTrip == true)
        return;

    if (GetTag(depID).value == "" || GetTag(depID).value == cultureDateTimeFormat)
        return;
    var curDt = GetInputDate(GetTag(depID).value);
    if (curDt != "Invalid Date" && curDt != "NaN") {
        if (bookingType == "H")
            curDt.setDate(curDt.getDate() + 2);
        else
            curDt.setDate(curDt.getDate() + 7);

        var retDt = GetInputDate(GetTag(retID).value);
        if (retDt == "Invalid Date" || retDt == "NaN" || (GetInputDate(GetTag(depID).value)) > retDt)
            GetTag(retID).value = GetFormattedDate(curDt.getFullYear(), (curDt.getMonth() + 1), curDt.getDate());
    }
}

//This function is used get date as per locale format
function GetFormattedDate(y, m, d) {

    var dt;
    if (window.dayPos != 'undefined' && window.dayPos != null && window.monthPos != 'undefined' && window.monthPos != null && window.yearPos != 'undefined' && window.yearPos != null) {
        //genrating First block of date
        if (dayPos == 0) dt = d + dtSep; else if (monthPos == 0) dt = m + dtSep; else dt = y + dtSep;
        //genrating Second block of date
        if (dayPos == 1) dt += d + dtSep; else if (monthPos == 1) dt += m + dtSep; else dt += y + dtSep;
        //genrating Third block of date
        if (dayPos == 2) dt += d; else if (monthPos == 2) dt += m; else dt += y;
    }
    else // otherwise use default format (mm/dd/yyyy) 
    {
        dt = m + dtSep + d + dtSep + y;
    }
    return dt;
}

//Check to see value is null or empty
function IsNullEmpt(val) {
    return (val == undefined || val == null || val.length == 0);
}


//this method will convert current date format to US date format for given date value
function getDateInUSFormat(t) {
    if (IsNullEmpt(t)) return null;
    t = t.replace(/\s+/g, "");
    if (t.match(/[^-|\d|\.|\/]/)) return null;
    var rgt = t.split(dtSep);
    for (var i = 0; i < rgt.length; i++) rgt[i] = parseInt(rgt[i], 10);
    if (!rgt[1]) return null;
    var m, d, y;

    if (!rgt[dayPos]) return null;
    m = rgt[monthPos]; d = rgt[dayPos]; y = rgt[yearPos];

    if (y < 100) y += 2000;
    if (y < 1601 || y > 4500 || m < 0 || (m-1) > 11 || d < 1 || d > GetMonthCount(m-1, y)) return null;
    return (m + dtSep + d + dtSep + y);
}

function GetInputDate(t)
{

    if(IsNullEmpt(t)) return null;
    t=t.replace(/\s+/g,"");
    if(t.match(/[^-|\d|\.|\/]/)) return null;
    var rgt=t.split(dtSep);
    for(var i=0;i<rgt.length;i++) rgt[i]=parseInt(rgt[i],10);
    if(!rgt[1]) return null;
    var m,d,y;

    if (!rgt[dayPos] || !rgt[monthPos] || !rgt[yearPos]) return null;
	m=rgt[monthPos];d=rgt[dayPos];y=rgt[yearPos];

    m-=1;if(y<100)y+=2000;
    if(y<1601||y>4500||m<0||(m-1)>11||d<1||d>GetMonthCount(m-1,y))return null;
    return new Date(y,m,d);
}

