﻿$(document).ready(function() {
    // var sprefix = "#ctl00_cpfooter_search_";
    var sprefix = '#' + $('#hidsepgid').val();
    var setoday = $('#hdtoday').val();
    
    $(sprefix + "datepicker").datepicker({
        dateFormat: 'dd/mm/yy',
        showOn: "button",
        buttonImage: "/images/img/sno-date.gif",
        buttonImageOnly: true,
        //changeMonth: true,
        //changeYear: true,
        buttonText: '',
        defaultDate: '01/12/2011',
        minDate: setoday,
        maxDate: '31/01/2013',
        gotoCurrent: true,
        showOtherMonths: true,
        firstDay: 1,
        beforeShowDay: function(date) {
            var avail = true;
            var month = date.getMonth() + 1;
            if (month >= 5 && month < 12) {
                avail = false;
            }
            return [avail, ''];
        },
        onSelect: function(date) {
            selectDateFrom(date);
        }
    });

    function selectDateFrom(date) {
        var d1 = parseInt(date.substring(0, 2));
        var d2 = date.substring(3);
        $(sprefix + 'ddlMonth').val(d2);

        var month = parseInt(d2.substring(0, 2));
        var year = parseInt(d2.substring(3));
        var daysInMonth = new Date(year, month, 0).getDate();
        bindDay($(sprefix + 'ddlDay'), daysInMonth, month);
        $(sprefix + 'ddlDay').val(d1);
    }
    $(sprefix + 'ddlDay').change(function() {
        var day = parseInt($(sprefix + 'ddlDay').val());
        var date = $(sprefix + 'ddlMonth').val();

        if (day < 10)
            $(sprefix + "datepicker").val('0' + day + '/' + date);
        else
            $(sprefix + "datepicker").val(day + '/' + date);
    });

    $(sprefix + 'ddlMonth').change(function() {
        var dateStr = $(sprefix + 'ddlMonth').val();
        var month = parseInt(dateStr.substring(0, 2));
        var year = parseInt(dateStr.substring(3));
        var daysInMonth = new Date(year, month, 0).getDate();
        var day = parseInt($(sprefix + 'ddlDay').val());
        bindDay($(sprefix + 'ddlDay'), daysInMonth, month);

        if (0 != $(sprefix + 'ddlDay option[value=' + day + ']').length) {
            $(sprefix + 'ddlDay').val(day);
        }
        else {
            day = parseInt($(sprefix + 'ddlDay').val());
            $(sprefix + 'ddlDay').val(day);
        }
        if (day < 10)
            $(sprefix + "datepicker").val('0' + day + '/' + dateStr);
        else
            $(sprefix + "datepicker").val(day + '/' + dateStr);

    });

    bindDay = function(dayselect, len, semonth) {
        dayselect.empty();
        var thismonth = parseInt(setoday.substring(3, 5));
        var startday = 1;
        if (thismonth == semonth) {
            startday = parseInt(setoday.substring(0, 2));
        }
        for (var i = startday; i <= len; i++)
            dayselect.append("<option value=\"" + i + "\">" + i + "</option>");
    }

    GetSnoCountriesList = function() {
        var params = { 'id': 1, 'method': 'getCountriesAndSkiAreas', 'params': { /* void */} };
        $.ajax({
            url: "/snosearch.ashx",
            data: JSON.stringify(params),
            contentType: 'application/json',
            type: "POST",
            dataType: 'json',
            success: function(a) {
                bindSnoCountries(a);
            }
        });
    }

    bindSnoCountries = function(countryarray) {
        var $optcountries = $("#seplcountry optgroup[label='Countries']");
        var $optskiareas = $("#seplcountry optgroup[label='Ski Areas']");
        var $hfccv = $(sprefix + 'hfCC').val();

        $optcountries.empty(); $optskiareas.empty();
        var arr = countryarray.result;
        var vexisted = false;
        for (var i = 0; i < arr.length; i++) {
            var key = arr[i].DestinationCode;
            if (key == $hfccv) {
                vexisted = true;
            }

            if (key.length > 0 && key.substr(0, 1) == "1") {
                $optskiareas.append("<option value=\"" + key + "\">" + arr[i].DestinationName + "</option>");
            }
            else {
                $optcountries.append("<option value=\"" + key + "\">" + arr[i].DestinationName + "</option>");
            }
        }
        if (($hfccv != "0") && (vexisted || $hfccv == "10")) {
            $('#seplcountry').val($hfccv);
            GetResortList($hfccv);
        }
    }

    GetResortList = function(desCode) {
        var params = { 'id': 1, 'method': 'getResorts', 'params': { 'DestinationCode': desCode} };
        $.ajax({
            url: "/snosearch.ashx",
            data: JSON.stringify(params),
            contentType: 'application/json',
            type: "POST",
            dataType: 'json',
            success: function(a) {
                bindResorts(a);
            }
        });
    }

    bindResorts = function(resortarray) {
        $('#seplresort').empty();
        var arr = resortarray.result;

        for (var i = 0; i < arr.length; i++) {
            $('#seplresort').append("<option value=\"" + arr[i].ResortCode + "\">" + arr[i].ResortName + "</option>");
        }
        if ($(sprefix + 'hfRC').val() != "0") {
            $('#seplresort').val($(sprefix + 'hfRC').val());
        }
    }

    GetSnoCountriesList();

    $('#seplcountry').change(function() {
        var ddlCounties = $('#seplcountry').val();
        $(sprefix + 'hfRC').val('0');
        GetResortList(ddlCounties);
        $(sprefix + 'hfCC').val(ddlCounties);
    });
    $('#seplresort').change(function() {
        var ddlResorts = $('#seplresort').val();
        $(sprefix + 'hfRC').val(ddlResorts);
    });

});

