﻿//获得Cookie解码后的值 
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

//获得Cookie的原始值
function getCookie(name) {
    //         var arg = name + "="; 
    //         var alen = arg.length; 
    //         var clen = document.cookie.length; 
    //         var i = 0; 
    //         while (i < clen) { 
    //             var j = i + alen; 
    //             if (document.cookie.substring(i, j) == arg) return getCookieVal (j); 
    //             i = document.cookie.indexOf(" ", i) + 1; 
    //             if (i == 0) break; 
    //         }
    //	     return null;
    var cookieValue = '';
    var search = name + '=';
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(';', offset);
            if (end == -1) end = document.cookie.length;
            cookieValue = unescape(document.cookie.substring(offset, end))
        }
    }
    return cookieValue;
}
//设定Cookie值
function setCookie(name, value) {
    var expire = "";
    path = "/";
    expire = new Date((new Date()).getTime() + 3600000);
    expire = "; expires=" + expire.toGMTString();
    var s = name + "=" + value + expire + ((path == null) ? "" : ("; path=" + path));
    document.cookie = s;

}

function ckshowlog(sname, url) {
    old_info = getCookie("history_info");
    if (sname.length > 13) {
        var name = sname.substring(0, 12) + "..."
    }
    else {
        var name = sname;
    }
    if (old_info != "") {
        var listCount = 10; //记录和显示最近10条
        var myCookie = old_info.split("_");
        var myCookieTit = new Array();
        var myCookieUrl = new Array();
        for (var i = 0; i < myCookie.length -1; i++) {
            var myCookieOne = myCookie[i].split("+");
            myCookieTit[i] = myCookieOne[0];
            myCookieUrl[i] = myCookieOne[1];
        }
        var newCookie = "";
        if (myCookie.length <= listCount  ) {
            for (var i = myCookie.length - 2; i >=0; i--) {
                if (myCookieTit[i] == name) {
                    continue; //去除重复
                }
                else {
                    newCookie = escape(myCookieTit[i]) + "+" + escape(myCookieUrl[i]) + "_" + newCookie;
                }
            }
        } else {
        for (var i = listCount - 2; i >= 0; i--) {
                if (myCookieTit[i] == name) {
                    continue;
                }
                else {
                    newCookie = escape(myCookieTit[i]) + "+" + escape(myCookieUrl[i]) + "_" + newCookie;
                }
            }
        }
        var n = escape(name) + "+" + escape(url) + "_";
        setCookie("history_info", n + newCookie);
    }
    else {
        var n = escape(name) + "+" + escape(url) + "_";
        setCookie("history_info", n);
    }
    history_show();
}
//document.onclick=glog;

function history_show() {
    var history_info = getCookie("history_info");
    var content = "";
    if (history_info != "") {                              
        history_arg = history_info.split("_");
        var i;
        if (history_arg.length > 10)
        {
            for (i = 0; i <= 10; i++) {
                if (history_arg[i] != "") {
                    var wlink = history_arg[i].split("+");
                    //                content += ("<font color='#ff000'>↑</font>" + "<a href='" + wlink[1] + "' target='_blank'>" + wlink[0] + "</a><br>");
                    content += ("<li><a href='" + wlink[1] + "'>" + wlink[0] + "</a></li>");
                }
            }
        }
        else
        {
            for (i = 0; i < history_arg.length; i++) {
                if (history_arg[i] != "") {
                    var wlink = history_arg[i].split("+");
                    //                content += ("<font color='#ff000'>↑</font>" + "<a href='" + wlink[1] + "' target='_blank'>" + wlink[0] + "</a><br>");
                    content += ("<li><a href='" + wlink[1] + "'>" + wlink[0] + "</a></li>");
                }
            }
        }
        document.getElementById("history").innerHTML = content;
    }
    else
    { document.getElementById("history").innerHTML = "对不起，您没有任何浏览纪录"; }
}

$(document).ready(function() {
    if (_g("history"))
    { history_show(); }

    if (_g("hdHotelName")) {
        var url = location.href;
        var name = _g("hdHotelName").value;
        ckshowlog(name, url);
    }
    
});

function search_show() {
    var search_info = getCookie("search_info");
    var content = "";
    if (search_info != "") {
        search_arg = search_info.split("_");
        _g("hd_p").value = search_arg[0];
        _g("hd_c").value = search_arg[1];
//        _g("sh_cn").value = _g("hd_c").text;
    }


}