﻿var popUp;

function OpenCalendar(idname, postBack) {
    popUp = window.open('Calendar.aspx?formname=' + document.forms[0].name +
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack,
		'popupcal',
		'width=500,height=300,left=200,top=250');
    popUp.focus();
}

function OpenCalendar(idname, iLeft, iTop, postBack) {
    popUp = window.open('Calendar.aspx?formname=' + document.forms[0].name +
		'&id=' + idname + '&selected=' + document.forms[0].elements[idname].value + '&postBack=' + postBack,
		'popupcal',
		'width=500,height=300,top=' + (iTop + 20) + '&,left=' + (iLeft + 80));
    popUp.focus();
}

function SetDate(formName, id, newDate, postBack) {
    eval('var theform = document.' + formName + ';');
    popUp.close();
    theform.elements[id].value = newDate;
    if (postBack)
        __doPostBack(id, '');
}

function LoadCalendar() {
    document.write('<iframe id=CalFrame name=CalFrame scrolling=no frameborder=0 src=calendar.htm style=display:none;position:absolute;z-index:100></iframe>');
    document.onclick = hideCalendar;
}

function isDateString(sDate) {
    var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    var iaDate = new Array(3);
    var year, month, day;

    if (arguments.length != 1) return false;
    iaDate = sDate.toString().split("-");
    if (iaDate.length != 3) return false;
    if (iaDate[1].length > 2 || iaDate[2].length > 2) return false;
    if (isNaN(iaDate[0]) || isNaN(iaDate[1]) || isNaN(iaDate[2])) return false;

    year = parseFloat(iaDate[0]);
    month = parseFloat(iaDate[1]);
    day = parseFloat(iaDate[2]);

    if (year < 1900 || year > 2100) return false;
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29;
    if (month < 1 || month > 12) return false;
    if (day < 1 || day > iaMonthDays[month - 1]) return false;
    return true;
}

function stringToDate(sDate, bIgnore) {
    var bValidDate, year, month, day;
    var iaDate = new Array(3);

    if (bIgnore) bValidDate = true;
    else bValidDate = isDateString(sDate);

    if (bValidDate) {
        iaDate = sDate.toString().split("-");
        year = parseFloat(iaDate[0]);
        month = parseFloat(iaDate[1]) - 1;
        day = parseFloat(iaDate[2]);
        return (new Date(year, month, day));
    }
    else return (new Date(1900, 1, 1));
}

//sImg ,控制对象
//bOpenBound,是否不可以选择变灰的日期
//sFld1,输入控件
function showCalendar(sImg, bOpenBound, sFld1, sFld2, sCallback) {	// alert(sImg);
    var fld1, fld2;
    var cf = document.getElementById("CalFrame");
    var wcf = window.frames.CalFrame;
    var oImg = document.getElementById(sImg);
    if (!oImg) { alert("控制对象不存在！"); return; }
    if (!sFld1) { alert("输入控件未指定！"); return; }
    fld1 = document.getElementById(sFld1);
    if (!fld1) { alert("输入控件不存在！"); return; }

    if (document.getElementById("rdState")) {	//航班状态查询不用日期
        if (fld1.id == "txWord" && document.getElementById("rdState").checked)
            return;
    }

    if (fld1.tagName != "INPUT" || fld1.type != "text") { alert("输入控件类型错误！"); return; }
    if (sFld2) {
        fld2 = document.getElementById(sFld2);
        if (!fld2) { alert("参考控件不存在！"); return; }
        if (fld2.tagName != "INPUT" || fld2.type != "text") { alert("参考控件类型错误！"); return; }
    }

    if (!wcf || !wcf.bCalLoaded)
    { alert("日历未成功装载！请刷新页面！"); return; }
    if (cf.style.display == "block") { cf.style.display = "none"; return; }
    //debugger;
    
//    var eT = 0, eL = 0, p = oImg;
//    var sT = document.body.scrollTop, sL = document.body.scrollLeft; //图片的位置
//    var eH = oImg.height, eW = oImg.width;
//    while (p && p.tagName != "BODY") { eT += p.offsetTop; eL += p.offsetLeft; p = p.offsetParent; }
//    cf.style.top = (document.body.clientHeight - (eT - sT) - eH >= cf.height) ? eT + eH + 22 : eT - cf.height + 25; //原高度加25
//    //cf.style.top = oImg.style.top + oImg.style.height;
//    cf.style.left = (document.body.clientWidth - (eL - sL) >= cf.width) ? eL + 80 : eL + eW - cf.width + 80;      //原宽度加80
//    if (location.href.toLowerCase().indexOf("ordersubmit.aspx") > 0) {
//        cf.style.top = (document.body.clientHeight - (eT - sT) - eH >= cf.height) ? eT + eH - 20 : eT - cf.height + 25; //原高度加25
//        cf.style.left = (document.body.clientWidth - (eL - sL) >= cf.width) ? eL + 22 : eL + eW - cf.width + 80;      //原宽度加80
//    }

    // 丁文钊 修改
    // 2011/05/06
    //-----VVV----------
    var top = GetY(oImg);
    var left = GetX(oImg);
    cf.style.top = top + oImg.clientHeight + "px";
    cf.style.left = left + "px";
    //-----AAA----------
    
    cf.style.display = "block";
    wcf.openbound = bOpenBound;
    wcf.fld1 = fld1;
    wcf.fld2 = fld2;
    wcf.callback = sCallback;
    wcf.initCalendar();
    //	alert(sImg);
}

function hideCalendar() {
    var cf = document.getElementById("CalFrame");
    if (cf != null)
        cf.style.display = "none";
}


// 丁文钊
// 2011/05/06
//-----VVV----------
function GetX(obj) {
    return obj.getBoundingClientRect().left + document.documentElement.scrollLeft;
    if (obj.offsetLeft && typeof (obj.offsetLeft) != "undefined") {
        var x = obj.offsetLeft ||0;
        while (obj = obj.offsetParent) {
            x += obj.offsetLeft;
        }
        return x;
    } else {
        return 0;
    }
} //-----AAA----------

// 丁文钊
// 2011/05/06
//-----VVV----------
function GetY(obj) {
    return obj.getBoundingClientRect().top+document.documentElement.scrollTop;
    if (obj.offsetLeft && typeof (obj.offsetLeft) != "undefined") {
        var y = obj.offsetTop || 0;
        while (obj = obj.offsetParent) {
            y += obj.offsetTop;
        }
        return y;
    } else {
        return 0;
    }
} //-----AAA----------
