﻿;
var xmlHttp = null;
var json = null;
var now = new Date();
var staticDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var displayDate = staticDate;
var correctDate = new Date(staticDate.getFullYear() + 10, staticDate.getMonth(), 1);
var m_days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var id_div = null;
var id_start = null;
var id_price = null;
var id_child = null;
function isAt(text) {
    var patrn = /^[@]\d{1,2}$/;
    if (patrn.exec(text)) {
        return true;
    } else {
        return false;
    };
};
function isPlus(text) {
    var patrn = /^[+]\d{1,2}$/;
    if (patrn.exec(text)) {
        return true;
    } else {
        return false;
    };
};
function isMinus(text) {
    var patrn = /^[-]\d{1}$/;
    if (patrn.exec(text)) {
        return true;
    } else {
        return false;
    };
};
function isNumber(text) {
    var patrn = /^\d{1,2}$/;
    if (patrn.exec(text)) {
        return true;
    } else {
        return false;
    };
};
function is_leap(year) {
    return (year % 100 == 0 ? res = (year % 400 == 0 ? 1 : 0) : res = (year % 4 == 0 ? 1 : 0));
};
var jsonArray = null;
function getRightDate() {
    if (json) {
        var jsonLength = 0;
        while (json.getdata[jsonLength]) {
            jsonArray = json.getdata[jsonLength].dates.split(",");
            if (jsonArray.length > 1) {
                for (count = 0; count < jsonArray.length; count++) {
                    changeCorrectDate(count);
                };
            } else if (jsonArray.length < 2) {
                changeCorrectDate(jsonLength);
            };
            jsonLength++;
        };
        displayDate = correctDate;
    };
};
function changeCorrectDate(l) {
    var strdate = null;
    if (jsonArray.length > 1) {
        strdate = jsonArray[l].split("/");
    } else {
        strdate = json.getdata[l].dates.split("/");
    };
    m_days[1] = 28 + is_leap(strdate[0]);
    if (!isNumber(strdate[1])) {
        if (strdate[0] > staticDate.getFullYear()) {
            temp = new Date(strdate[0], 0, 1);
            if (temp > staticDate && temp <= correctDate) {
                correctDate = temp;
            };
        } else if (strdate[0] == staticDate.getFullYear()) {
            if (isAt(strdate[1])) {
                correctDate = staticDate;
            } else if (isPlus(strdate[1])) {
                temp = new Date(staticDate.getFullYear(), staticDate.getMonth(), strdate[1].substring(1));
                if (temp <= correctDate) {
                    correctDate = temp;
                };
            } else {
                temp = new Date(staticDate.getFullYear(), staticDate.getMonth(), 1);
                thefirstweekday = strdate[1].substring(1);
                if (thefirstweekday > 6) {
                    thefirstweekday = 0;
                };
                firstDay = temp.getDay();
                date_str = 31;
                for (i = 0; i < 6; i++) {
                    for (k = 0; k < 7; k++) {
                        idx = i * 7 + k;
                        if (k == thefirstweekday && (idx - firstDay + 1) > 0 && (idx - firstDay + 1) <= m_days[staticDate.getMonth()] && (idx - firstDay + 1) <= date_str && (idx - firstDay + 1) > staticDate.getDate()) {
                            date_str = idx - firstDay + 1;
                            break;
                        };
                    };
                };
                temp = new Date(staticDate.getFullYear(), staticDate.getMonth(), date_str);
                if (temp > staticDate && temp <= correctDate) {
                    correctDate = temp;
                };
            };
        };
    } else if (isNumber(strdate[1])) {
        if (strdate[0] >= staticDate.getFullYear()) {
            if (isNumber(strdate[2])) {
                temp = new Date(strdate[0], strdate[1] - 1, strdate[2]);
                if (temp > staticDate && temp <= correctDate) {
                    correctDate = temp;
                };
            } else if (isAt(strdate[2])) {
                if (strdate[1] == staticDate.getMonth() + 1) {
                    temp = new Date(staticDate.getFullYear(), staticDate.getMonth(), staticDate.getDate() + 1);
                } else {
                    temp = new Date(strdate[0], strdate[1] - 1, 1);
                };
                if (temp > staticDate && temp <= correctDate) {
                    correctDate = temp;
                };
            } else {
                temp = new Date(strdate[0], strdate[1] - 1, 1);
                thefirstweekday = strdate[2].substring(1);
                if (thefirstweekday > 6) {
                    thefirstweekday = 0;
                };
                firstDay = temp.getDay();
                date_str = 31;
                for (i = 0; i < 6; i++) {
                    for (k = 0; k < 7; k++) {
                        idx = i * 7 + k;
                        if (k == thefirstweekday && (idx - firstDay + 1) > 0 && (idx - firstDay + 1) <= m_days[strdate[1] - 1] && (idx - firstDay + 1) <= date_str) {
                            if (strdate[1] > staticDate.getMonth() + 1) {
                                date_str = idx - firstDay + 1;
                                break;
                            } else { if ((idx - firstDay + 1) > staticDate.getDate()) {
                                    date_str = idx - firstDay + 1;
                                    break;
                                };
                            };
                        };
                    };
                };
                temp = new Date(strdate[0], strdate[1] - 1, date_str);
                if (temp > staticDate && temp <= correctDate) {
                    correctDate = temp;
                };
            };
        };
    };
};
