﻿//取得某对象，若提供ID下的对象不存在则自动创建
function c$(fctId, fctClassName) {
    var varTempDivObj = document.getElementById(fctId);
    if (!varTempDivObj) {
        GetPyzyIframe("ifm" + fctId);
        varTempDivObj = document.createElement("div");
        varTempDivObj.id = fctId;
        if (fctClassName && fctClassName != "") varTempDivObj.className = fctClassName;
        document.body.appendChild(varTempDivObj);
    }
    return varTempDivObj;
}

//取得某ID的iframe对象，若不存在该ID的对象则自动创建
function GetPyzyIframe(fctIfmId, fctVisibility, fctTop, fctLeft, fctWidth, fctHeight) {
    var varTempIfmObj = document.getElementById(fctIfmId);
    if (!varTempIfmObj) {
        varTempIfmObj = document.createElement("iframe");
        varTempIfmObj.id = fctIfmId;
        varTempIfmObj.style.position = "absolute";
        varTempIfmObj.style.zIndex = "1";
        varTempIfmObj.style.visibility = "hidden";
        document.body.appendChild(varTempIfmObj);
    }
    if (fctTop) varTempIfmObj.style.top = fctTop + "px";
    if (fctLeft) varTempIfmObj.style.left = fctLeft + "px";
    if (fctWidth) varTempIfmObj.style.width = fctWidth + "px";
    if (fctHeight) varTempIfmObj.style.height = fctHeight + "px";
    if (fctVisibility) varTempIfmObj.style.visibility = (document.all ? fctVisibility: "hidden");
    return varTempIfmObj;
}

//取得某对象的坐标位置、宽、高
function getPosition(obj) {
    var top = 0;
    var left = 0;
    var width = obj.offsetWidth;
    var height = obj.offsetHeight;
    while (obj.offsetParent) {
        top += obj.offsetTop;
        left += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return {
        "top": top + 10,
        "left": left + 8,
        "width": width,
        "height": height
    };
}

//取得编码存储框对象
function GetValueToInputObj(fctThisObj) {
    if (!fctThisObj) return null;
    var varThisObjAutoInput = (fctThisObj.getAttributeNode("value_to_input") ? fctThisObj.getAttributeNode("value_to_input").value: "");
    if (varThisObjAutoInput == "") return null;
    return document.getElementById(varThisObjAutoInput);
}

//自动触发下一个对象的Act事件
function AutoNextInputAct(fctThisObj, fctAct) {
    var varNextInput = fctThisObj.getAttributeNode("nextinput");
    if (varNextInput && varNextInput != "") {
        if (document.all) {
            eval("document.getElementById('" + varNextInput.value + "')." + fctAct + "()");
        } else {
            var evt = document.createEvent("MouseEvents");
            evt.initEvent(fctAct, true, true);
            document.getElementById(varNextInput.value).dispatchEvent(evt);
        }
        document.getElementById(varNextInput.value).focus();
    }
}

//给某对象的某事件增加处理函数AddFunToObj(document,"onclick","alert('1');")
function AddFunToObj(fctObj, fctAct, fctFunction) {
    if (fctObj.addEventListener) { //!IE
        fctObj.addEventListener(fctAct.replace("on", ""),
        function(e) {
            e.cancelBubble = !eval(fctFunction);
        },
        false);
    } else if (fctObj.attachEvent) { //IE
        fctObj.attachEvent(fctAct,
        function() {
            return eval(fctFunction);
        });
    }
}
document.write('\
<style type="text/css">\
 body{FONT-FAMILY:"宋体";font-size:12px; line-height:20px;}\
.DateListBox{float:left;border:solid #FC7A7D 1px;width:147px !important;width:141px;height:168px !important;height:180px;font-size:12px;text-align:center;}\
.DateListBox h1{width:100%;background-color:#FFF4F4;color:#B42929;font-size:12px;height:20px;font-weight:bold;line-height:20px;vertical-align:middle;margin:0px;}\
.DateListBox div{float:left;border:solid #EB696C 1px;background-color:#EB696C;color:#FFFFFF;width:19px !important;width:18px;height:20px;font-size:12px;font-weight:bold;line-height:20px;vertical-align:middle;}\
.DateListBox a{float:left;color:#990000;border:solid #ffffff 1px;background-color:#ffffff;width:19px!important;_width:18px;height:19px !important;height:19px;font-size:12px;line-height:20px;vertical-align:middle;}\
.DateListBox a:hover{border:solid #F2C2BD 1px;background-color:#FBEDEC;}\
.DateListBox .aSelect{cursor:pointer;border:solid #DEB4B4 1px;background-color:#FAE0CF;color:#FF0000;}\
.PyzyDateBox{position:absolute;z-index:99999;visibility:hidden;background-color:#FFFFFF;border:solid #EBcccC 1px;height:170px;width:298px !important;width:289px;}\
</style>\
');

//取得日历列表，该函数输出的内容需要根据需求放置到特定的容器中，如浮动层、固定DIV…
function GetMonthHTML(fctStaDate, fctDate) {
    if (!fctDate) fctDate = new Date(); //默认为当前日期
    var varYear = fctDate.getFullYear();
    var varMonth = fctDate.getMonth();
    var varNextMonth = new Date(varYear, varMonth + 1, 1);
    var varThisMonthButDay = new Date(varNextMonth - 86400000); //本月最后一天

    var varThisDate, varThisWeekDay, varThisMonthHTML = "";
    varThisMonthHTML += "<h1>" + varYear + "年" + (varMonth + 1) + "月</h1>";
	varThisMonthHTML += "<div>日</div>\
	<div>一</div><div>二</div><div>三</div><div>四</div><div>五</div><div>六</div>"
    for (var DayI = 1; DayI <= varThisMonthButDay.getDate(); DayI++) {
        varThisDate = new Date(varYear, varMonth, DayI);
        varThisWeekDay = varThisDate.getDay();
        if (DayI == 1) {
            for (var spcI = 0; spcI < varThisWeekDay; spcI++) {
                varThisMonthHTML += "<a></a>"
            }
        }
        varThisMonthHTML += "<a " + ((fctStaDate && fctStaDate != "") ? (varThisDate < fctStaDate ? "old": "") : "") + " href=javascript:; onclick='SelectDate(this)' title='" + varYear + "-" + (varMonth + 1) + "-" + DayI + "'>" + DayI + "</a>";
    }
    return '<div class="DateListBox">' + varThisMonthHTML + '</div>';
}
//选择日期
function SelectDate(fctAObj) {
    if (fctAObj.href || fctAObj.className == "aSelect") {
        var varValueObj = document.getElementById("divPyzyDateBox").Obj;
        var varValueToObj = GetValueToInputObj(varValueObj);
        if (varValueToObj) varValueObj = varValueToObj;
        if (varValueObj.value == fctAObj.title) {
            varValueObj.value = "";
        } else {
            varValueObj.value = fctAObj.title;
            //_statPrice();
        }
        document.getElementById("divPyzyDateBox").style.visibility = "hidden";
        document.getElementById("divPyzyDateBox").bodyclick = "";
        GetPyzyIframe("ifmdivPyzyDateBox", "hidden");
        AutoNextInputAct(document.getElementById("divPyzyDateBox").Obj, "click");
    }
}
//隐藏日历
function HiddenDateBox() {
    if (document.getElementById("divPyzyDateBox")) {
        if (document.getElementById("divPyzyDateBox").style.visibility != "hidden" && document.getElementById("divPyzyDateBox").bodyclick == "1") {
            document.getElementById("divPyzyDateBox").style.visibility = "hidden";
            document.getElementById("divPyzyDateBox").bodyclick = "";
            GetPyzyIframe("ifmdivPyzyDateBox", "hidden");
        } else {
            document.getElementById("divPyzyDateBox").bodyclick = "1";
        }
    }
}
//显示日历
function ShowTwoMonthList(fctThisObj, fctJNum, fctStaDate) {
    try {
        jQuery.ajax({
            type: "GET",
            url: "/Webmanage/Common/GetServerTime.aspx?tmp=" + Math.random(),
            error: function() {
                _showTime();
            },
            success: function(msg) {
                fctStaDate = msg;
                _showTime();
            }
        });
    } catch(e) {
        _showTime();
    }
    function _showTime() {
        if (fctStaDate == "") fctStaDate = jQuery("#txtToday").val();
        fctThisObj = document.getElementById(fctThisObj);
        if (!fctJNum) fctJNum = 0;
        if (!fctThisObj) fctThisObj = "";
        var varStaDate = null,
        varTheDate = new Date();
        if (fctStaDate || fctStaDate == "") {
            varStaDate = new Date(new Date() - 86400000); //如果没有定义具体可使用的开始时间则自定义为今天
            var varStaDateSplit = fctStaDate.split("-");
            if (varStaDateSplit.length == 3) {
                varStaDate = new Date(varStaDateSplit[0], parseInt(varStaDateSplit[1], 10) - 1, varStaDateSplit[2]); //fctStaDate=new Date();
            }

            varTheDate = varStaDate;
            if (fctStaDate == "") fctStaDate = varStaDate.getFullYear() + "-" + (varStaDate.getMonth() + 1) + "-" + varStaDate.getDate();
        }
        var varShowTwoMonthHTML = "";
        for (var i = 0 + fctJNum; i < 2 + fctJNum; i++) {
            varShowTwoMonthHTML += GetMonthHTML((varStaDate ? varStaDate: ""), new Date(varTheDate.getFullYear(), varTheDate.getMonth() + i, 1));
        }
        if (varStaDate) varShowTwoMonthHTML = varShowTwoMonthHTML.replace(/old href/g, "style=color:#999 old");
        if (fctThisObj.value != "") { //对已选日期样式进行改变
            if (/^((\d{4})|(\d{2}))-(\d{1,2})-(\d{1,2})$/g.test(fctThisObj.value)) varShowTwoMonthHTML = varShowTwoMonthHTML.replace(fctThisObj.value, fctThisObj.value + "' class='aSelect");
        }
        var varDateBoxObj = c$("divPyzyDateBox", "PyzyDateBox");
        varDateBoxObj.bodyclick = "";
        if (fctThisObj != "") { //调整坐标到合适位置
            var varThisObjPosition = getPosition(fctThisObj);
            varDateBoxObj.style.top = (varThisObjPosition.top + varThisObjPosition.height) + "px";
            varDateBoxObj.style.left = varThisObjPosition.left + "px";
            varDateBoxObj.style.visibility = "visible"; //hidden
            varDateBoxObj.Obj = fctThisObj;
            GetPyzyIframe("ifmdivPyzyDateBox", "visible", (varThisObjPosition.top + varThisObjPosition.height), varThisObjPosition.left, varDateBoxObj.offsetWidth, varDateBoxObj.offsetHeight); //取Iframe
        }
        varDateBoxObj.innerHTML = varShowTwoMonthHTML + '<div style="margin-top:0px;z-index: 2;left:5px;position:absolute;color:#B42929;font-size:12px;font-weight:bold;line-height:24px;vertical-align:bottom;">&nbsp;<span style="padding-right:' + (document.all ? '220': '232') + 'px;cursor:pointer;" onclick="ShowTwoMonthList(null,' + (fctJNum - 1) + (fctStaDate ? ',\'' + fctStaDate + '\'': '') + ')" title="上月"><--</span><span style="cursor:pointer;" onclick="ShowTwoMonthList(null,' + (fctJNum + 1) + (fctStaDate ? ',\'' + fctStaDate + '\'': '') + ')" title="下月">--></span></div>';
        // alert(varDateBoxObj.innerHTML)

    }

}
/**********日历选择功能 End huzj 2008-4-14*************/

//选择某城市
function WriteCity(fctI) {

    var varMenuObj = c$("divAddressMenu");
    var varThisObj = varMenuObj.obj;
    var varMenuValue = document.getElementById("menuA" + fctI).title;
    varMenuValue = varMenuValue.split("|");
    varThisObj.value = varMenuValue[1];
    var varThisObjAutoInput = GetValueToInputObj(varThisObj); //取得城市编码值存储对象
    if (!varThisObjAutoInput) {
        varThisObjAutoInput = varThisObj;
        varThisObjAutoInput.value = varMenuValue[1];
    } else varThisObjAutoInput.value = varMenuValue[2];
    if (typeof(fctI) == "number") AutoNextInputAct(varThisObj, "click");
}

//隐藏城市列表
function _Hidden(e) {
    e = e ? e: event;
    if (document.getElementById("divAddressMenu") != null) {
        var varMenuObj = c$("divAddressMenu");
        var varThisObj = varMenuObj.obj;
        if (varMenuObj.style.visibility != "hidden") {
            if (e) {
                var EventOBJ = (e.srcElement ? e.srcElement: e.target);
                if (varThisObj == EventOBJ || EventOBJ.id.indexOf("menuPage") == 0 || EventOBJ.id.indexOf("divAddressMenu") == 0) return false;
            }
            var varThisObjAutoInput = GetValueToInputObj(varThisObj); //取得目的地类别对象
            if (document.getElementById("menuA1")) { //如果下拉菜单中存在第一个城市
                if (!varThisObjAutoInput) varThisObjAutoInput = varThisObj;
                if (varThisObjAutoInput.value == "" || varThisObjAutoInput == varThisObj) {
                    WriteCity("1");
                }
            } else if (EventOBJ.id.indexOf("menuA") < 0) { //如果下拉菜单中不存在满足条件的城市
                if (varThisObj) varThisObj.value = "";
            }
            varMenuObj.style.visibility = "hidden";
            GetPyzyIframe("ifm" + varMenuObj.id, "hidden");
        }
    }
}
//为window.onload增加一个为document增加onclick处理函数的函数(^_^)huzj 2008-4-14
//AddFunToObj(window,"onload","AddFunToObj(document,'onclick','HiddenDateBox();');");
AddFunToObj(window, "onload", "AddFunToObj(document,'onclick','_Hidden(" + (document.all ? "": "e") + ");HiddenDateBox();');");
function onloadTime() {
    var now = new Date();
    var today = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
    var tomorrow = DateAdd("d", 2);
    tomorrow = tomorrow.getFullYear() + "-" + (tomorrow.getMonth() + 1) + "-" + tomorrow.getDate();
    //jQuery("#txtComeTime").val(today);
    //jQuery("#txtLeaveTime").val(tomorrow);
}
function DateAdd(strInterval, Num) {
    var dtTmp = new Date();
    switch (strInterval) {
    case 's':
        return new Date(Date.parse(dtTmp) + (1000 * Num));
    case 'n':
        return new Date(Date.parse(dtTmp) + (60000 * Num));
    case 'h':
        return new Date(Date.parse(dtTmp) + (3600000 * Num));
    case 'd':
        return new Date(Date.parse(dtTmp) + (86400000 * Num));
    case 'w':
        return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Num));
    case 'q':
        return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Num * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    case 'm':
        return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Num, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    case 'y':
        return new Date((dtTmp.getFullYear() + Num), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
    }
}

$(document).ready(function() {
    onloadTime()
});