﻿/********************************************************
Author: Lang Uy Tin
Lastupdate: 10 JUN 2010
Email: uytin4u@yahoo.co.uk
*********************************************************/

function fncCreateDateObject(targetID, sourceID) {
    if ($("#" + sourceID).length <= 0) {
        return;
    }
    var curdate = $("#" + sourceID)[0].value;
    var cdate = "";
    if (curdate.trim() == "") {
        cdate = new Date();
    } else {
        cdate = new Date(curdate);
    }
    var strhtml = "";
    strhtml += "<Select class=\"tnsearchform\" id='" + sourceID + "1' onchange=\"fncChangeDateObject(this, '" + sourceID + "', 1);\">";
    strhtml += fncCreateDayOption(cdate);
    strhtml += "</Select>";
    strhtml += " <Select class=\"tnsearchform\" id='" + sourceID + "2' onchange=\"fncChangeDateObject(this, '" + sourceID + "', 2);\">";
    strhtml += fncCreateMonthOption(cdate, $('#' + sourceID).datepicker("option", "monthNamesShort"));
    strhtml += "</Select>";
    strhtml += " <Select class=\"tnsearchform\" id='" + sourceID + "3' onchange=\"fncChangeDateObject(this, '" + sourceID + "', 3);\">";
    strhtml += fncCreateYearOption(cdate);
    strhtml += "</Select>";
    $("#" + targetID).html(strhtml);
}
function fncCreateDayOption(curdate) {
    var curmonth = curdate.getMonth() + 1;
    var beginDate = new Date(curmonth + "-1-" + $.datepicker.formatDate('yy', curdate));
    var imonth = curmonth;
    var result = "";
    var iday = 1;
    while (curmonth == imonth) {
        var selected = "";
        if (iday == curdate.getDate()) {
            selected = " selected ";
        }
        result += "<option " + selected + " value='" + $.datepicker.formatDate('dd', beginDate) + "'>" + $.datepicker.formatDate('dd', beginDate) + "</option>";
        iday++;
        beginDate.setDate(iday);
        curmonth = beginDate.getMonth() + 1;
    }
    return result;
}
function fncCreateMonthOption(curdate, monthNames) {
    var curmonth = curdate.getMonth() + 1;
    var imonth = 0;
    var result = "";
    for (imonth = 1; imonth < 13; imonth++) {
        var selected = "";
        if (imonth == curmonth) {
            selected = " selected ";
        }
        result += "<option " + selected + " value='" + ((imonth > 9) ? "" : "0") + imonth + "'>" + monthNames[imonth-1] + "</option>";
    }
    return result;
}
function fncCreateYearOption(curdate) {
    var curyear = curdate.getFullYear();
    var passyear = 2;
    var futureyear = 3;
    var i = curyear - passyear;
    var result = "";
    for (; i < curyear + futureyear; i++) {
        var selected = "";
        if (i == curyear) {
            selected = " selected ";
        }
        result += "<option " + selected + " value='" + i + "'>" + i + "</option>";
    }
    return result;
}

function fncChangeDateObject(sender, sourceID, key) {
    var sday = $("#" + sourceID + "1")[0];
    var smonth = $("#" + sourceID + "2")[0];
    var syear = $("#" + sourceID + "3")[0];
    var i;
    var result = "";
    i = smonth.selectedIndex;
    var nummonth = smonth.options[i].value
    result += nummonth;
    result += "/";
    i = sday.selectedIndex;
    result += sday.options[i].value;
    result += "/";
    i = syear.selectedIndex;
    result += syear.options[i].value;
    var curdate = new Date(result);
    if (curdate.getMonth() + 1 != nummonth * 1) {
        curdate.setMonth(nummonth * 1 - 1);
        curdate.setDate(1);
    }

    var minDate = $("#" + sourceID).datepicker("option", "minDate");
    var flag = false;
    if (curdate.compareTo(minDate) < 0) {
        curdate = minDate;
        flag = true;
    }
//    var maxdate = new Date(minDate.getFullYear() + 4,
//                            minDate.getMonth(),
//                            minDate.getDate());

//    if (curdate.compareTo(maxdate) > 0) {
//        curdate = maxdate;
//        flag = true;
//    }

    //$("#" + sourceID)[0].value = result;
    $('#' + sourceID).datepicker("setDate", $.datepicker.formatDate('mm/dd/yy', curdate));
    if (key == 3) {
        flag = true;
    }
    if (key > 1 || flag) {
        sday.options.length = 0;
        $(sday).html(fncCreateDayOption(curdate));
        if (flag) {
            smonth.options.length = 0;
            $(smonth).html(fncCreateMonthOption(curdate, $('#' + sourceID).datepicker("option", "monthNamesShort")));

            syear.options.length = 0;
            $(syear).html(fncCreateYearOption(curdate));
        }

    }

}



/***************************Init javascript************/
//function fncInitDatePicker() {

//    $('#Departure').datepicker({
//        numberOfMonths: 1,
//        duration: '',
//        closeText: 'Close',
//        showOn: 'button',
//        buttonImage: '../images/calendar.gif',
//        buttonText: 'Choose',
//        buttonImageOnly: true,
//        showButtonPanel: true,
//        maxDate: '+4y',
//        minDate: new Date(),
//        onSelect: function(dateText, inst) {
//            fncCreateDateObject("spDeparture", "Departure");
//        }
//    });

//    $('#Arrival').datepicker({
//        numberOfMonths: 1,
//        duration: '',
//        closeText: 'Close',
//        showOn: 'button',
//        buttonImage: '../images/calendar.gif',
//        buttonText: 'Choose',
//        buttonImageOnly: true,
//        showButtonPanel: true,
//        maxDate: '+4y',
//        minDate: new Date(),
//        onSelect: function(dateText, inst) {
//            fncCreateDateObject("spArrival", "Arrival");
//        }
//    });

//    fncCreateDateObject("spDeparture", "Departure");
//    fncCreateDateObject("spArrival", "Arrival");


//}


/******************************html********************/
//    <link href="<%=Url.Content("~/Content/custom-theme/jquery-ui-1.8.2.custom.css")%>" rel="stylesheet" type="text/css" />
//    <script type="text/javascript" src="<%=Url.Content("~/scripts/jquery-1.3.2.js")%>"></script>
//    <script type="text/javascript" src="<%=Url.Content("~/scripts/jquery-ui.min.js")%>"></script>
//    <script type="text/javascript" src="<%=Url.Content("~/scripts/libAddins.js")%>"></script>
//    <script type="text/javascript" src="<%=Url.Content("~/scripts/LUTDateObject.js")%>"></script>
    
// 
//body onload="fncInitDatePicker();"
//                                <div id="divDeparture">
//                                    <span id="spDeparture"></span>
//                                    <%= Html.Hidden("Departure"
//                                    , String.Format("{0:MM/dd/yyyy}", Model.Departure)
//                                    ) %>
//                                </div>

//                                <div id="divArrival">
//                                    <span id="spArrival"></span>
//                                    <%= Html.Hidden("Arrival"
//                                    , String.Format("{0:MM/dd/yyyy}", Model.Arrival)
//                                    ) %>
//                                </div>


