﻿//参数参数名称：roomNumber , startDate
//增加人：李志能
//增加日期：2009-07-1	
//参数说明：hoteltype-酒店类型，hid-酒店协议id ， rid-房型协议id ，id- ，
//ruleid- ， roomNumber-预订房间数量，languageType-语言类型 ， startDate-预订开始日期
//rateplanid:rateplanid,avgrage:均价
function SeoOnBooking(hoteltype, hid, rid, id, ruleid, roomNumber, languageType, startDate, querystr, apno, rateplanid, avgrage) {    
    //取消冒泡
    if (navigator.userAgent.indexOf('MSIE') >= 0) {
        event.cancelBubble = true;
    }
    else {
        SeoOnBooking.caller.arguments[0].cancelBubble = true;
    }

    //判断是否是expedia合作站点，如果是，就直接跳转到提示页面，不走预订流程(liangliang.xu修改 2008-3-21)
    if (SeoIsNotExpedia()) {
        //判断是否是expedia合作站点结束
        //增加对房量的判断(chiyubo on 2007-12-15)
        try {
            var sumFlag = SeoCheckSumRooms(hid, rid, roomNumber, languageType);
            if (sumFlag == true) //判断总房量
            {
                var remnantFlag = SeoCheckRemnantRooms(hid, rid, startDate, roomNumber, languageType);
                if (remnantFlag == false) //判断剩余房量
                {
                    return;
                }
            }
            else {
                return;
            }
        }
        catch (e)
	    { }

        //增加结束(chiyubo on 2007-12-15)

        if (hoteltype == "expedia") {
            SeoBookingExpedia(hid, rid, id, ruleid);
        }
        else {
            document.getElementById("hotelid").value = hid;
            document.getElementById("roomtypeid").value = rid;
            document.getElementById("roomid").value = id;
            document.getElementById("rateplanid").value = rateplanid;
            document.getElementById("avgrage").value = avgrage;
            //如果用户未登录则弹出登录页
            //		    if (GetCookie("member") == null || GetCookie("member") == '192928' || GetCookie("member") == '7036777182')
            //            var status = loadAjaxRequest(apno);
//            var status = null;
//            if (typeof (apno) != "undefined" && apno != null) {
//                status = SeoGetData("http://www.elong.com/hotels/AjaxRequestProxy.aspx?RequestType=getlogin&apno=" + apno);
//            } else {
//                status = SeoGetData("http://www.elong.com/hotels/AjaxRequestProxy.aspx?RequestType=getlogin");
//            }
//            //本地测试不弹出登录框 add by minjiang 09-6-5
//            //如果想测试注释掉就行
//            if (window.location.href.indexOf("localhost") != -1)
//            { status = "1"; }
//            if (status != null && status == "0") {
//                if (window.location.href.indexOf("hotel.elong.com") != -1 || window.location.href.indexOf("www.elong.com") != -1 || window.location.href.indexOf("www.elong.net") != -1 || window.location.href.indexOf("localhost") != -1) {
//                    window.top.UserLogin(querystr, languageType);
//                }
//                else if (window.location.href.toLowerCase().indexOf("big5.elong.com") != -1) {
//                    var q = querystr.replace("http://www.elong.com/smalllogin.aspx?", "http://www.elong.com/login.aspx?");
//                    window.location.href = q;
//                }
//                else {
//                    if (window.parent != null && (window.location.href.indexOf("travel.elong.com") != -1 || window.location.href.indexOf("travel.elong.net") != -1)) {
//                        window.parent.location.href = querystr;
//                    } else {
//                        window.location.href = querystr;
//                    }
//                }
//            }
//            else {
//                var pp = Math.random();
//                ThisForm().action = "http://www.elong.com/hotels/order.aspx?xx=" + pp;

//                //If this page is in a frame then open the page in top frame.
//                if (window.top != window.self) {
//                    ThisForm().target = "_top"; //"_blank"
//                }
//                ThisForm().submit();
            //            }

            //querystr = getOrderUrl(querystr);
            window.location.href = querystr;

        }
    }

}

//取订单填写页URL add by minjiang 10-1-7
function getOrderUrl(querystr) {
    querystr = unescape(querystr);
    querystr = querystr.toLowerCase();
    querystr = querystr.substring(querystr.indexOf("nexturl=") + 8);
    return querystr;
}


function SeoIsNotExpedia() {
    if (typeof (JS_CONST_HEAD) != "undefined") {
        if (JS_CONST_HEAD.CARD_NO == "4917393") {
            var url = "http://expediacorporate.elong.com/pop.htm";
            window.open(url, "newwindow", "height=140, width=400, top=300, left=400, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
            return false;
        }
        else if (JS_CONST_HEAD.CARD_NO == "4946915") {
            var url2 = "http://expediacorporate.elong.net/popen.htm";
            window.open(url2, "newwindow", "height=140, width=400, top=300, left=400, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
            return false;
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}

//增加人：chiyubo

//增加日期：2008-01-07
//功能描述：判断用户预订房量是否大于酒店房型的总房量
function SeoCheckSumRooms(hid, rid, roomNumber, languageType) {
    var roomSum = null;
    try {
        //获取总房量
        roomSum = SeoGetData("http://www.elong.com/hotels/AjaxRequestProxy.aspx?RequestType=gethotelrooms&hotel_id=" + hid + "&room_id=" + rid);
    }
    catch (e) {
        roomSum = null;
    }

    if (roomSum != null && roomSum != "") {
        if (Number(roomSum) > 0) //房量为非正数(<= 0)时不作判断
        {
            if (Number(roomSum) < Number(roomNumber)) {
                if (String(languageType) == "cn") {
                    alert("抱歉，您的订房量超出酒店房型的总量，请您选择其他房型或酒店。");
                }
                else //if(String(languageType) == "en")
                {
                    alert("Sorry, the room amount you have selected has exceeded the number of this room type.\r\nPlease choose another room type or hotel.");
                }
                return false;
            }
        }
    }
    return true;
}

//增加人：chiyubo
//增加日期：2008-01-07
//功能描述：判断用户预订房量是否大于第一天的剩余房量
function SeoCheckRemnantRooms(hid, rid, start_date, roomNumber, languageType) {
    var roomRemnantSum = null;
    try {
        //获取剩余房量
        roomRemnantSum = GetData("http://www.elong.com/Asp/GetRemnantRooms.asp?hotel_id=" + hid + "&room_type_id=" + rid + "&start_date=" + start_date + "&room_num=" + roomNumber);
    }
    catch (e) {
        roomRemnantSum = null;
    }

    if (roomRemnantSum != null && roomRemnantSum != "") {
        if (Number(roomRemnantSum) >= 0) //剩余房量为非正数(<= 0)时不作判断
        {
            if (Number(roomRemnantSum) < Number(roomNumber)) {
                if (String(languageType) == "cn") {
                    alert("抱歉，您的订房量超出酒店房型的剩余房量，请您选择其他房型或酒店。");
                }
                else //if(String(languageType) == "en")
                {
                    alert("Sorry, the room amount you have selected has exceeded the remnant number of this room type.\r\nPlease choose another room type or hotel.");
                }
                return false;
            }
        }
    }
    return true;
}

function SeoBookingExpedia(hid, rid, id, ruleid) {
    var url = "http://www.elong.com/globalhotels/confirm.aspx?hotelid=" + hid + "&roomtypeid=" + rid + "&rateplanid=" + id + "&rateruleid=" + ruleid;
    document.location.href = url;
}

function SeoGetData(url) {
    document.domain = "elong.com";
    var proxy = document.getElementById("iframeProxy").contentWindow;
    
    var proxyUrl = url;
    if (url.indexOf("http://www.elong.com") == -1) {
        proxyUrl = 'http://www.elong.com/seotemplate/AjaxProxy.aspx?url=' + escape(url);
    }

    return proxy.SyncAjaxRequest(proxyUrl);
}

function ThisForm() {
    var thisform = document.getElementById("aspnetForm");
    if (thisform != null)
        return thisform;
        
    var forms = document.getElementsByTagName("form");
    if (forms.length > 0)
        return forms[0];
    else
        return null;        
}
