﻿function __calendar() {
    WebCalendar_1.AddPic('2009/9/20', '$1200+', 'abc', 'Y');
    WebCalendar_1.AddPic('2009/9/25', '$1300+', 'abc', 'n');
    WebCalendar_1.AddPic('2009/9/27', '$1400+', 'abc', 'n');
    WebCalendar_1.AddPic('2009/10/24', '$1500+', 'abc', 'n');
    WebCalendar_1.AddPic('2009/10/27', '$1600+', 'abc', 'n');
    WebCalendar_1.SetDivName('test1');
    WebCalendar_1.Init();
    WebCalendar_1.writeCalendar();
}

function SendCacheCalendar(divname, key, value) {
    var changeflag = false;
    if (key == "FromSeg") {
        changeflag = WebCalendar_1.SetFromSeg(value);
    }
    if (key == "ToSeg") {
        changeflag = WebCalendar_1.SetToSeg(value);
    }
    if (key == "GoDate") {
        changeflag = WebCalendar_1.SetGoDate(value);
    }
    if (changeflag && WebCalendar_1.fromseg != "" &&
        WebCalendar_1.toseg != "" && WebCalendar_1.godate != "") {
        WebCalendar_1.SetDivName(divname);
        $.ajax({
            type: "POST",
            url: window.location.protocol + "//" + window.location.host + "/GetCacheCalendar.aspx",
            data: "FromSeg=" + WebCalendar_1.fromseg + " &ToSeg=" + WebCalendar_1.toseg + " &GoDate=" + WebCalendar_1.godate,
            complete: function(http) {
                var resText = http.responseText;
                var reg = /<script>((.|\n)+)<\/script>/i;
                var match = resText.match(reg);
                var MyScript = "";
                if (match != null) //有脚本返回
                {
                    MyScript = match[1];
                    eval(MyScript); //执行脚本
                }
            },
            dataType: "html"
        });
    }
}

var Calendarid = 0;

function __WebCalendar() //初始化日历的设置
{
    this.nodelabel = "Note:";
    this.GetDateFromat = __WebCalendar_GetDateFromat;
    this.SetNodeLabel = function(label) { this.nodelabel = label; };
    this.nodeinfo = "this calendar displays the best fares found by other FlightEngine users over the past 48 hours. Due to rapidly changing airline prices & seat availability, these fares may no longer be available.";
    this.AddNodeInfo = function(info) { this.nodeinfo = info; };
    this.topinfo = new Array('Best Fares:', 'to', 'any trip length');
    this.SetTopInfo = function(idx, info) { if (idx > 2 || idx < 0) return; this.topinfo[idx] = info; };
    this.fromseg = "";
    this.SetFromSeg = function(seg) { if (this.fromseg == seg) return false; this.fromseg = seg; return true; };
    this.toseg = "";
    this.SetToSeg = function(seg) { if (this.toseg == seg) return false; this.toseg = seg; return true; };
    this.godate = "";
    this.SetGoDate = function(value) { if (this.godate == value) return false; this.godate = value; return true; };
    this.picdate = new Array();
    this.picinfo = new Array();
    this.piclink = new Array();
    this.picless = new Array();
    this.AddPic = __WebCalendar_AddPic;
    this.CleanPic = function() { this.picdate = new Array(); this.picinfo = new Array(); this.piclink = new Array(); this.picless = new Array(); };
    this.divname = "";
    this.SetDivName = function(name) { this.divname = name; };
    this.weeks = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
    this.SetWeek = function(idx, week) { if (idx > 6 || idx < 0) return; this.weeks[idx] = week; };
    this.months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    this.SetMonth = function(idx, month) { if (idx > 11 || idx < 0) return; this.months[idx] = month; };
    this.thisYear = new Date().getFullYear(); //定义年的变量的初始值
    this.thisMonth = new Date().getMonth() + 1; //定义月的变量的初始值
    this.thisDay = new Date().getDate();     //定义日的变量的初始值
    this.FirstYear = this.thisYear;
    this.FirstMonth = this.thisMonth;
    this.SetYearMonth = function(year, month) { this.FirstYear = parseInt(year,10); this.FirstMonth = parseInt(month,10); };
    this.today = this.GetDateFromat(this.thisYear ,this.thisMonth,this.thisDay);
    this.name = "__Calendar" + (Calendarid++);
    this.GetYearMonth = function(idx) { var month = this.thisMonth + idx; var year = this.thisYear; if (month > 12) { month = month - 12; year++; } return year + "" + month; };
    this.GetFirstYearMonth = function(idx) { var month = this.FirstMonth + idx; var year = this.FirstYear; if (month > 12) { month = month - 12; year++; } return year + "" + month; };
    this.GetYearMonthEx = function(idx) { var month = this.thisMonth + idx; var year = this.thisYear; if (month > 12) { month = month - 12; year++; } month = this.months[month - 1]; return month + " " + year; };
    this.daysMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    this.dayObj = new Array(42);            //定义日期展示控件数组
    this.Init = HtmlCalendarInit;   //初始化生成日历
    this.writeCalendar = HtmlCalendarWrite; //生成所有日期数据并写入到网页中
    this.hiddenCalendar = function() { this.calendar.style.display = "none"; };
    this.showCalender = function() { this.calendar.style.display = "block"; };
    this.fromnodeinfo = "this from";
    this.AddFromNode = function(info) { this.fromnodeinfo = info; };
    this.tonodeinfo = "this to";
    this.AddToNode = function(info) { this.tonodeinfo = info; };
}
function __WebCalendar_GetDateFromat(year, month, day) {
    var temp;
    temp = "0" + month;
    if (temp.length == 3) {
        temp = temp.substr(1);
    }
    var ret = year + "/";
    ret = ret + temp + "/";
    temp = "0" + day;
    if (temp.length == 3) {
        temp = temp.substr(1);
    }
    ret = ret + temp;
    return ret;
}
function __WebCalendar_AddPic(date, info, link, isless) {
    var idx;
    var flag = true;
    for (idx in this.picdate) {
        if (this.picdate[idx] == date) {
            flag = false;
        }
    }
    if (flag) {
        var len = this.picdate.length;
        this.picdate[len] = date;
        this.picinfo[len] = info;
        this.piclink[len] = link;
        this.picless[len] = isless;
    }
}

function __WebCalendar_Select(node) {
    WebCalendar_1.thisYear = parseInt(node.value.substring(0, 4),10);
    WebCalendar_1.thisMonth = parseInt(node.value.substring(4, 6),10);
    WebCalendar_1.writeCalendar();
}

function HtmlCalendarInit() {
    var strHtml;
    this.thisYear = new Date().getFullYear();
    this.thisMonth = new Date().getMonth() + 1;
    strHtml = "<TABLE id=" + this.name + " class=calendar_table_dswang cellSpacing=0 cellPadding=0><TBODY>";
    strHtml += "<TR class=calendar_toprow_dswang>";
    strHtml += "<TD class=calendar_topleft_dswang></TD>";
    strHtml += "<TD class='calendar_top_dswang'></TD>";
    strHtml += "<TD class=calendar_topright_dswang></TD></TR>";
    strHtml += "<TR>";
    strHtml += "<TD class='calendar_left_dswang'></TD>";
    strHtml += "<TD class ='calendar_body_dswang'>";
    strHtml += "<DIV class=calendar_body_title_dswang> " + this.topinfo[0] + " " + this.fromseg + " " + this.topinfo[1] + " " + this.toseg + " ";
    strHtml += "<SELECT class=calendar_body_select_dswang id=" + this.name + "_year onchange='__WebCalendar_Select(this);'>";
    for (var i = 0; i < 12; i++) {
        strHtml += "<OPTION ";
        if (this.GetFirstYearMonth(0) == this.GetYearMonth(i)) {
            strHtml += "selected";
        }
        strHtml += " value=" + this.GetYearMonth(i) + ">" + this.GetYearMonthEx(i) + "</OPTION>";
    }
    strHtml += "</SELECT> <SPAN class=calendar_body_topr_dswang>" + this.topinfo[2] + "</SPAN> </DIV>";
    strHtml += "<TABLE class=calendar_body_center_dswang cellSpacing=0> ";
    strHtml += "<TBODY> ";
    strHtml += "<TR> ";
    strHtml += "<TH class=calendar_th_dswang>" + this.weeks[0] + "</TH>";
    strHtml += "<TH class=calendar_th_dswang>" + this.weeks[1] + "</TH>";
    strHtml += "<TH class=calendar_th_dswang>" + this.weeks[2] + "</TH>";
    strHtml += "<TH class=calendar_th_dswang>" + this.weeks[3] + "</TH>";
    strHtml += "<TH class=calendar_th_dswang>" + this.weeks[4] + "</TH>";
    strHtml += "<TH class=calendar_th_dswang>" + this.weeks[5] + "</TH>";
    strHtml += "<TH class=calendar_th_dswang>" + this.weeks[6] + "</TH></TR>";
    for (x = 0; x < 6; x++) {
        strHtml += " <TR>";
        for (y = 0; y < 7; y++) {
            if (y == 0) {
                strHtml += " <TD class='calendar_day_dswang calendar_day_l_dswang' class1='calendar_day_dswang calendar_day_l_dswang' id='" + this.name + "_day_" + (x * 7 + y) + "''> </TD>";
            }
            else if (y == 6) {
            strHtml += " <TD class='calendar_day_dswang calendar_day_r_dswang' class1='calendar_day_dswang calendar_day_r_dswang' id='" + this.name + "_day_" + (x * 7 + y) + "''> </TD>";
            }
            else {
                strHtml += " <TD class='calendar_day_dswang' class1='calendar_day_dswang' id='" + this.name + "_day_" + (x * 7 + y) + "''> </TD>";
            }
        }
        strHtml += "</TR>";
    }
    strHtml += "</TBODY></TABLE>";
    strHtml += "<DIV class=calendar_body_note_dswang><B>" + this.nodelabel + "</B> " + this.nodeinfo + "</DIV></TD>";
    strHtml += "<TD class=calendar_right_dswang></TD></TR>";
    strHtml += "<TR class=calendar_bottomrow_dswang>";
    strHtml += "<TD class=calendar_bottomleft_dswang></TD>";
    strHtml += "<TD class=calendar_bottom_dswang></TD>";
    strHtml += "<TD class=calendar_bottomright_dswang></TD></TR></TBODY></TABLE>";
    var divnode = document.getElementById(this.divname);
    if (divnode != undefined) {
        divnode.innerHTML = strHtml;
    }
    for (var i = 0; i < 42; i++) {
        this.dayObj[i] = document.getElementById(this.name + "_day_" + i);
    }
    this.calendar = document.getElementById(this.name);
    this.thisYear = this.FirstYear;
    this.thisMonth = this.FirstMonth;
}

function HtmlCalendarWrite() {
    this.daysMonth[1] = (0 == this.thisYear % 4 && (this.thisYear % 100 != 0 || this.thisYear % 400 == 0)) ? 29 : 28; //闰年二月为29天
    var week = new Date(this.thisYear, this.thisMonth - 1, 1).getDay();
    var currentyear, currentmonth;
    var currentday;
    var i = 0;
    for (i = 0; i < week; i++) {
        this.dayObj[i].innerHTML = "<span class=calendar_day1_dswang></span><br><div class = 'calendar_daydiv_dswang calendar_hidden_dswang'>$+</div>";
        this.dayObj[i].className = this.dayObj[i].getAttribute("class1");
    }
    var j;
    currentyear = this.thisYear;
    currentmonth = this.thisMonth;
    for (j = 0; j < this.daysMonth[this.thisMonth - 1]; j++) {
        currentday = j + 1;
        var class_name;
        var curdate = this.GetDateFromat(currentyear,currentmonth,currentday);
        if (curdate == this.today) {
            class_name = 'calendar_day1_dswang calendar_today_dswang';
        }
        else {
            class_name = 'calendar_day1_dswang';
        }
        this.dayObj[i].innerHTML = "<span class='" + class_name + "'>" + currentday + "</span><br><div class = 'calendar_daydiv_dswang calendar_hidden_dswang'>$+</div>";
        this.dayObj[i].className = this.dayObj[i].getAttribute("class1");
        var idx;
        for (idx in this.picdate) {
            if (this.picdate[idx] == curdate) {
                if (this.picless[idx] == 'Y') {
                    this.dayObj[i].className = this.dayObj[i].getAttribute("class1") + " calendar_day_less_dswang";
                    this.dayObj[i].innerHTML = "<span class='" + class_name + "'>" + currentday + "</span><br><div class = 'calendar_daydiv_dswang calendar_less_dswang'><a href='" + this.piclink[idx] + "'>" + this.picinfo[idx] + "<a></div>";
                }
                else {
                    this.dayObj[i].innerHTML = "<span class='" + class_name + "'>" + currentday + "</span><br><div class = 'calendar_daydiv_dswang'><a href='" + this.piclink[idx] + "'>" + this.picinfo[idx] + "<a></div>";
                }
            }
        }
        i++;
    }
    if (i > 35) {
        for (; i < 42; i++) {
            this.dayObj[i].innerHTML = "<span class=calendar_day1_dswang></span><br><div class = 'calendar_daydiv_dswang calendar_hidden_dswang' >$+</div>";
            this.dayObj[i].className = this.dayObj[i].getAttribute("class1");
        }
    }
    else {
        for (; i < 35; i++) {
            this.dayObj[i].innerHTML = "<span class=calendar_day1_dswang></span><br><div class='calendar_daydiv_dswang calendar_hidden_dswang'>$+</div>";
            this.dayObj[i].className = this.dayObj[i].getAttribute("class1");
        }
        for (; i < 42; i++) {
            this.dayObj[i].innerText = "";
            this.dayObj[i].className = this.dayObj[i].getAttribute("class1");
        }
    }
}

var WebCalendar_1 = new __WebCalendar();

