﻿//登录的用户
var User = null;

//旅游线路
var Tour = null;

//出发日期
var DateRates = [];

//当前日期, 开始日期, 结束日期...
var now = new Date();
var datebegin = new Date(now.getYear(), now.getMonth(), 1);
var dateend = new Date(now.getYear(), now.getMonth() + 2, 0);
var datebeginstring = datebegin.getYear() + '-' + (datebegin.getMonth() + 1) + '-' + datebegin.getDate();
var dateendstring = dateend.getYear() + '-' + (dateend.getMonth() + 1) + '-' + dateend.getDate();

function CheckSubmit() {
    var ddlDate = document.getElementById('date');
    if (ddlDate.selectedIndex < 0) {
        alert('请选择出行日期。');
        return false;
    }

    SumRate(true);

    var mancount = 0;
    var txtManCounts = document.getElementsByName('mancount');
    for (var i = 0; i < txtManCounts.length; i++) {
        mancount += parseInt(txtManCounts[i].value);
    }
    if (mancount < 1) {
        alert('请填写预订人数。');
        return false;
    }

    var date;
    for (var i = 0; i < DateRates.length; i++) {
        if (DateRates[i].date.getYear() + '-' + (DateRates[i].date.getMonth() + 1) + '-' + DateRates[i].date.getDate() == ddlDate.options[ddlDate.selectedIndex].value) {
            date = DateRates[i];
            break;
        }
    }
    if (date && !date.isUnlimitedSeat && mancount > date.emptySeatCount) {
        alert('本线路可以预订人数为' + date.emptySeatCount + '人，您填写的人数为' + mancount + '人，超出' + (mancount - date.emptySeatCount) + '人。');
        return false;
    }

    return true;
}

function SumRate(check) {
    var ddlStation = document.getElementById('station');
    var stationRate = 0;
    if (ddlStation.selectedIndex >= 0) {
        for (var i = 0; i < Tour.selectionGroups.length; i++) {
            if (Tour.selectionGroups[i].groupName == '上车地点') {
                for (var j = 0; j < Tour.selectionGroups[i].selections.length; j++) {
                    var selection = Tour.selectionGroups[i].selections[j];
                    if (selection.id == ddlStation.options[ddlStation.selectedIndex].value) {
                        stationRate = selection.rate;
                        break;
                    }
                }
                break;
            }
        } 
    }

    var ddlDate = document.getElementById('date');
    var hidRateids = document.getElementsByName('rateid');
    var ratesum = 0;
    for (var i = 0; i < hidRateids.length; i++) {
        var date;
        for (var j = 0; j < DateRates.length; j++) {
            if (DateRates[j].date.getYear() + '-' + (DateRates[j].date.getMonth() + 1) + '-' + DateRates[j].date.getDate() == ddlDate.options[ddlDate.selectedIndex].value) {
                date = DateRates[j];
                break;
            }
        }
    
        var rate;
        for (var j = 0; j < date.rates.length; j++) {
            var rate1 = date.rates[j];
            if (rate1.id == hidRateids[i].value) {
                rate = rate1;
                break;
            }
        }

        var txtManCount = document.getElementById('mancount_' + hidRateids[i].value);
        var lblRateSum = document.getElementById('ratesum_' + hidRateids[i].value);
        var mancount = parseInt(txtManCount.value);
        if (isNaN(mancount)) {
            txtManCount.value = '0';
            lblRateSum.innerHTML = '0';
        } else if (check && rate.buygetfree > 0 && mancount > 0 && mancount % rate.buygetfree != 0) {
            txtManCount.value = '0';
            lblRateSum.innerHTML = '0';
            alert(rate.name + '为' + rate.ratestring + '，\n预订人数需填写' + rate.buygetfree + '或' + rate.buygetfree + '的倍数。');
        } else {
            txtManCount.value = mancount;
            lblRateSum.innerHTML = mancount * (rate.rate + rate.airticketTax + stationRate);
            ratesum += mancount * (rate.rate + rate.airticketTax + stationRate);
        }
    }

    var selectionId = document.getElementsByName('selectionId');
    for (var i = 0; i < selectionId.length; i++) {
        var id = selectionId[i].value;
        var selection;
        for (var j = 0; j < Tour.selectionGroups.length; j++) {
            var find = false;
            for (var k = 0; k < Tour.selectionGroups[j].selections.length; k++) {
                if (Tour.selectionGroups[j].selections[k].id == id) {
                    selection = Tour.selectionGroups[j].selections[k];
                    find = true;
                    break;
                }
            }
            if (find) break;
        }
        var selected = document.getElementById('selectionSelected_' + id).value == "1";
        if (selected)
            ratesum += parseInt(document.getElementById("selectionCount_" + id).value) * selection.rate;
    }
    
    document.getElementById('allratesum').innerHTML = ratesum;
}

function SelectionInput_Click(sender, args) {
    sender.select();
    if (window.event)
        args.cancelBubble=true; 
    else
        args.stopPropagation();
}

function SelectionInput_Blur(sender, args, id) {
    var selectionId = document.getElementById('selectionId_' + id).value;
    var selectionCount = document.getElementById('selectionCount_' + id);
    var oldSelectionCount = selectionCount.value;
    var selectionSelected = document.getElementById('selectionSelected_' + id);
    var wasSelectionSelected = selectionSelected.value == '1';
    var selectionGroup;
    for (var i = 0; i < Tour.selectionGroups.length; i++) {
        var find = false;
        for (var j = 0; j < Tour.selectionGroups[i].selections.length; j++) {
            var selection = Tour.selectionGroups[i].selections[j];
            if (selection.id == selectionId) {
                selectionGroup = Tour.selectionGroups[i];
                find = true;
                break;
            }
        }
        if (find) break;
    }

    var count = parseInt(oldSelectionCount);
    if (isNaN(count) || count == 0) {
        if (selectionGroup.notNull) {
            var selected = false;
            for (var i = 0; i < selectionGroup.selections.length; i++) {
                var selection = selectionGroup.selections[i];
                selected = document.getElementById('selectionSelected_' + selection.id).value == '1';
                if (selected) break;
            }
            if (!selected) {
                sender.parentNode.className = 'selected';
                selectionSelected.value = '1';
                selectionCount.value = count;
            } else {
                if (selectionGroup.isSingel) {
                    for (var i = 0; i < selectionGroup.selections.length; i++) {
                        var selection = selectionGroup.selections[i];
                        if (selected.id != id) {
                            document.getElementById('selectionCount_' + selection.id).value = '0';
                            document.getElementById('selectionSelected_' + selection.id).value = '0';
                            document.getElementById('selectionSelected_' + selection.id).parentNode.className = '';
                        }
                    }
                }
                sender.parentNode.className = 'selected';
                selectionSelected.value = '1';
                selectionCount.value = count;
            }
        }
        else {
            sender.parentNode.className = '';
            selectionSelected.value = '0';
            selectionCount.value = '0';
        }
    } else {
        if (selectionGroup.isSingel) {
            for (var i = 0; i < selectionGroup.selections.length; i++) {
                var selection = selectionGroup.selections[i];
                if (selection.id != id) {
                    document.getElementById('selectionCount_' + selection.id).value = '0';
                    document.getElementById('selectionSelected_' + selection.id).value = '0';
                    document.getElementById('selectionSelected_' + selection.id).parentNode.className = '';
                }
            }
        }
        sender.parentNode.className = 'selected';
        selectionSelected.value = '1';
        selectionCount.value = count;
    }

    SumRate(true);
}

function Selection_Click(sender, args, id) {
    var selectionId = document.getElementById('selectionId_' + id).value;
    var selectionCount = document.getElementById('selectionCount_' + id);
    var oldSelectionCount = selectionCount.value;
    var selectionSelected = document.getElementById('selectionSelected_' + id);
    var wasSelectionSelected = selectionSelected.value == '1';
    var selectionGroup;
    for (var i = 0; i < Tour.selectionGroups.length; i++) {
        var find = false;
        for (var j = 0; j < Tour.selectionGroups[i].selections.length; j++) {
            var selection = Tour.selectionGroups[i].selections[j];
            if (selection.id == selectionId) {
                selectionGroup = Tour.selectionGroups[i];
                find = true;
                break;
            }
        }
        if (find) break;
    }

    if (selectionGroup.isSingel) {
        for (var i = 0; i < selectionGroup.selections.length; i++) {
            var selection = selectionGroup.selections[i];
            document.getElementById('selectionCount_' + selection.id).value = '0';
            document.getElementById('selectionSelected_' + selection.id).value = '0';
            document.getElementById('selectionSelected_' + selection.id).parentNode.className = '';
        }
    }
    
    if (wasSelectionSelected) {
        sender.parentNode.className = '';
        selectionSelected.value = '0';
        selectionCount.value = '0';
    } else {
        sender.parentNode.className = 'selected';
        selectionSelected.value = '1';
        var count = parseInt(oldSelectionCount);
        if (count == 0)
            selectionCount.value = '1';
        else
            selectionCount.value = oldSelectionCount;
    }

    if (selectionGroup.notNull) {
        var selected = false;
        for (var i = 0; i < selectionGroup.selections.length; i++) {
            var selection = selectionGroup.selections[i];
            selected = document.getElementById('selectionSelected_' + selection.id).value == '1';
            if (selected) break;
        }
        if (!selected) {
            sender.parentNode.className = 'selected';
            selectionSelected.value = '1';
        }
    }

    sender.blur();
    SumRate(true);
}

function SelectDdlDate(datestring) {
    var ddlDate = document.getElementById('date');
    for (var i = 0; i < ddlDate.options.length; i++) {
        if (ddlDate.options[i].value == datestring) {
            ddlDate.selectedIndex = i;
            BuildRate();
            break;
        }
    }
    SumRate(true);
}

function BuildSelection() {
    var html = '<table cellpadding="0" cellspacing="0" border="0" width="100%;"><colgroup><col width="100" /></colgroup>';
    for (var i = 0; i < Tour.selectionGroups.length; i++) {
        if (Tour.selectionGroups[i].groupName != '上车地点') {
            html += '<tr><td align="right">' + Tour.selectionGroups[i].groupName + '：</td><td><ul class="tourSelectionList">';
            for (var j = 0; j < Tour.selectionGroups[i].selections.length; j++) {
                var selection = Tour.selectionGroups[i].selections[j];
                html += '<li' + (selection.selected ? ' class="selected"' : '') + (selection.lineFeed ? ' style="clear:both;"' : '') + '>' + 
                    '<input type="hidden" id="selectionId_' + selection.id + '" name="selectionId" value="' + selection.id + '" />' +
                    '<input type="hidden" id="selectionSelected_' + selection.id + '" name="selectionSelected" value="' + (selection.selected ? "1" : "0") + '" />' +
                    '<a href="javascript:void(0);" onclick="Selection_Click(this, event, \'' + selection.id + '\')" title="' + selection.rate + '元/份">' +
                    selection.name + '<input type="text" value="0" autocomplete="off" onclick="SelectionInput_Click (this, event);" onblur="SelectionInput_Blur(this.parentNode, event, \'' + selection.id + '\')" onkeyup="SelectionInput_Blur(this.parentNode, event, \'' + selection.id + '\')" maxlength="2" id="selectionCount_' + selection.id + '" name="selectionCount" />份</a>' +
                    '<i>已选中</i></li>';
            }
            html += '</ul></td></tr>';
        }
    }
    html += '</table>';
    document.getElementById('tdSelection').innerHTML = html;
}

function BuildStation() {
    var ddlStation = document.getElementById('station');
    for (var i = 0; i < Tour.selectionGroups.length; i++) {
        if (Tour.selectionGroups[i].groupName == '上车地点') {
            for (var j = 0; j < Tour.selectionGroups[i].selections.length; j++) {
                var selection = Tour.selectionGroups[i].selections[j];
                ddlStation.options.add(document.createElement("OPTION"))
                ddlStation.options[ddlStation.options.length - 1].text = (selection.name.substr(0, 5) != '00:00' ? selection.name : selection.name.substr(5)) + " - " + selection.rate + "元/人";
                ddlStation.options[ddlStation.options.length - 1].value = selection.id;
                if (selection.selected)
                    ddlStation.options[ddlStation.options.length - 1].selected = true;
            }
            break;
        }
    }
    document.getElementById('trStation').style.display = ddlStation.options.length == 0 ? 'none' : '';
}

function BuildDdlDate() {
    var ddlDate = document.getElementById('date');
    var weekdays = ['日', '一', '二', '三', '四', '五', '六'];
    for (var i = 0; i < DateRates.length; i++) {
        if (DateRates[i].date > new Date()) {
            var datestring1 = DateRates[i].date.getYear() + '-' + (DateRates[i].date.getMonth() + 1) + '-' + DateRates[i].date.getDate() + '[周' + weekdays[DateRates[i].date.getDay()] + ']';
            var datestring2 = DateRates[i].date.getYear() + '-' + (DateRates[i].date.getMonth() + 1) + '-' + DateRates[i].date.getDate();
            ddlDate.options.add(document.createElement("OPTION"))
            ddlDate.options[ddlDate.options.length - 1].text = datestring1;
            ddlDate.options[ddlDate.options.length - 1].value = datestring2;
        }
    }
}

function BuildRate() {
    var ddlDate = document.getElementById('date');
    var html = '<table width=" " border="0" cellspacing="0"  class="other_price_show">' +
		'<tr><th width=" " align="left">价格名称</th><th width="80">价格</th><th width="80">人数</th><th>合计</th></tr>';

    var date;
    for (var i = 0; i < DateRates.length; i++) {
        if (DateRates[i].date.getYear() + '-' + (DateRates[i].date.getMonth() + 1) + '-' + DateRates[i].date.getDate() == ddlDate.options[ddlDate.selectedIndex].value) {
            date = DateRates[i];
            break;
        }
    }

    if (date) {
        for (var i = 0; i < date.rates.length; i++) {
            var rate = date.rates[i];
            var valid = !(rate.membergroup != '' && (User == null || User.membergroup != rate.membergroup));
            html += '<tr><td align="left">' + (rate.membergroup ? '<b>&lt;' + rate.membergroup + '&gt;</b>' : '') + rate.name + '</td>' +
            '<td><span class="other_price_span">' + rate.finalrate + (rate.airticketTax > 0 ? '+' + rate.airticketTax : '') + '元</span></td>' +
            '<td><input type="text" autocomplete="off" name="mancount" id="mancount_' + rate.id + '" style="width:40px;" maxlength="2" value="0" onfocus="this.select()" onblur="SumRate(true)" onkeyup="SumRate()" ' + (valid ? '' : 'disabled="disabled" ') + '/></td>' +
            '<td><span id="ratesum_' + rate.id + '">0</span>元' + (valid ? '<input type="hidden" name="rateid" value="' + rate.id + '" />' : '') + '</td>' +
            '</tr>';
        } 
    }
    html += '<tr><td align="left"><b>总计：</b></td><td></td><td></td><td><b><span id="allratesum">0</span>元</b></td></tr></table>'

    document.getElementById('divRates').innerHTML = html;
}

function ShowOtherpriceleft(sender, args, datestring) {
    var content =
		    '<table id="other_price_show" class="pub_price_info" cellpadding="0" cellspacing="0" width=" " style="display:block ;" >' +
		    '	<tbody>' +
		    '		<tr><td class="top_l"></td><td class="top_m"></td><td class="top_r"></td></tr>' +
		    '		<tr><td class="middle_l"></td><td class="middle_m">' +
		    '			<table width=" " border="0" cellspacing="0"  class="other_price_show">' +
		    '				<tr><th width=" " align="left">价格名称</th><th width=" " align="left">市场价</th><th width="80">优惠价<span style="text-align:right;position:relative; top:-10px;left:25px;"><img src="templates/5itrips/skins/images/cross.gif" alt="" style="cursor:pointer;" onclick="hideOtherprice(this, event);"/></span></th></tr>';
    var date;
    for (var i = 0; i < DateRates.length; i++) {
        if (DateRates[i].date.toString() == datestring) {
            date = DateRates[i].date;
            for (var j = 0; j < DateRates[i].rates.length; j++) {
                content += '				<tr><td align="left">' + (DateRates[i].rates[j].membergroup ? '<b>&lt;' + DateRates[i].rates[j].membergroup + '&gt;</b>' : '') + DateRates[i].rates[j].name + '</td><td><span class="">' + DateRates[i].rates[j].finalmarketrate + (DateRates[i].rates[j].airticketTax > 0 ? '+' + DateRates[i].rates[j].airticketTax : '') + '元</span></td><td><span class="other_price_span">' + DateRates[i].rates[j].finalrate + (DateRates[i].rates[j].airticketTax > 0 ? '+' + DateRates[i].rates[j].airticketTax : '') + '元</span></td></tr>';
            }
            break;
        }
    }
    content += '			</table>' +
		    '			<div style="margin-top:10px;width:100%; text-align:center;"><a href="' + location.href.split('#')[0] + '#tourOrderThis"><img src="templates/5itrips/skins/images/yuding.gif" alt="马上预订" style="cursor:pointer;" onclick="SelectDdlDate(\'' + (date.getYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()) + '\'); hideOtherprice(this, event);" /></a></div>' +
		    '		</td><td class="middle_r"></td></tr>' +
		    '		<tr><td class="bottom_l"></td><td class="bottom_m"></td><td class="bottom_r"></td></tr>' +
		    '	</tbody>' +
		    '</table>';
    document.getElementById("frame_show").innerHTML = content;
    var posX = findPosX(sender) - 0;
    var posY = findPosY(sender) - 100;
    document.getElementById('other_price_show').style.left = posX + 'px';
    document.getElementById('other_price_show').style.top = posY + 'px';
    document.getElementById('other_price_show').style.position = 'absolute';
    document.getElementById('other_price_show').style.display = 'block';
}

function hideOtherprice(sender, args) {
    document.getElementById('other_price_show').style.display = 'none';
}

function BuildTourCalendar(render, date, daterates, showprevious, shownext) {
    var now = new Date();

    var html = '<table class="tCal" cellspacing="0" cellpadding="2" border="1"><thead><tr><td colspan="7">' +
            '<div style="position: relative; width:100%;">' + (showprevious ? '<span class="tCalFlipBw tCalFlip_disabled">&lt; 上月</span>' : '') +
            '<span class="tCalMonth">' + date.getYear() + '年' + (date.getMonth() + 1) + '月</span>' +
        (shownext ? '<span class="tCalFlipFw tCalFlip_disabled">下月 &gt;</span>' : '') + '</div>' +
            '</td></tr><tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr></thead><tr>';

    var nunmonthstart = new Date(date.getYear(), date.getMonth(), 1).getDay();
    var nunmonthend = new Date(date.getYear(), date.getMonth() + 1, 0).getDate();
    var maxi = (nunmonthstart + nunmonthend < 36) ? 36 : 43;

    for (var i = 1; i < maxi; i++) {
        var iv = i - nunmonthstart;

        if (i > nunmonthstart && i <= nunmonthend + nunmonthstart) {
            var planadultrate = "&nbsp;";
            for (var j = 0; j < daterates.length; j++) {
                if (date.getYear() == daterates[j].date.getYear() && date.getMonth() == daterates[j].date.getMonth() && iv == daterates[j].date.getDate() && daterates[j].rates.length > 0) {
                    planadultrate = "<span class=\"price_span\">" + (daterates[j].rates[0].finalrate + daterates[j].rates[0].airticketTax) + "<span class=\"tCalYuan\">元</span></span>";
                    break;
                }
            }
            var hasplan = planadultrate != "&nbsp;";

            if (new Date(date.getYear(), date.getMonth(), iv).toString() == new Date(now.getYear(), now.getMonth(), now.getDate()).toString()) {
                html += "<td class=\"tCalToday tCalDay\"><span class=\"date_span\">" + iv + "</span><br/>今天</td>";
            } else if (new Date(date.getYear(), date.getMonth(), iv) < new Date(now.getYear(), now.getMonth(), now.getDate())) {
                html += "<td class=\"tCalPassed tCalDay\"><span class=\"date_span\">" + iv + "</span><br/>" + planadultrate + "</td>";
            } else if (hasplan) {
                html += "<td class=\"tCalPrice tCalDay\" onclick=\"javascript:ShowOtherpriceleft(this, event, '" + new Date(date.getYear(), date.getMonth(), iv).toString() + "');\"><a href=\"javascript:void(0)\" title=\"点击日期查看详情\" class=\"calendar_bg\"><span class=\"date_span\">" + iv + "</span><span class=\"site_span cyellow\">" + (!daterates[j].isUnlimitedSeat ? daterates[j].emptySeatCount : "") + "</span><br/>" + planadultrate + "</a></td>";
            } else {
                html += "<td class=\"tCalDay\"><span class=\"date_span\">" + iv + "</span><br/>" + planadultrate + "</a></td>";
            }
        } else {
            html += "<td>&nbsp;</td>";
        }

        if (i % 7 == 0) {
            html += '</tr><tr>';
        }
    }

    html += '</tr></table>';

    render.innerHTML = html;
}


function after_tour_get_dates(ajax) {
    eval('Tour = ' + ajax.responseText);
    DateRates = Tour.dates;
    User = Tour.user;

    if (User != null) {
        document.getElementById('trCommentLogin').style.display = 'none';
    }
    
    BuildTourCalendar(document.getElementById('show_cal1'), datebegin, DateRates, 1, 0);
    BuildTourCalendar(document.getElementById('show_cal2'), dateend, DateRates, 0, 1);

    if (DateRates.length > 0) {
        BuildDdlDate();
        BuildRate();
        BuildStation();
        BuildSelection();
        SumRate(true);
        document.getElementById('divOrderLoader').style.display = 'none';
        document.getElementById('divOrder').style.display = '';
    } else {
        document.getElementById('divOrderLoader').innerHTML = '此线路出发日期未定，如需预订请来电咨询。';
    }
}

function CheckCommentSubmit() {
    if (document.getElementById('commentname').value == '') {
        alert('请填写咨询主题。');
        return false;
    }

    if (document.getElementById('commentcontent').value == '') {
        alert('请填写咨询内容。');
        return false;
    }

    return true;
}
