﻿function Trim(s)
{
	s = s.replace(/^[\s　]+/g, "");　//從前面去除
	s = s.replace(/[\s　]+$/g, ""); //從後面去除 
	return s;
}

function Search(root, qid, tid, cid, defaultText)
{
    var q = Trim($("#" + qid).val());
    var t = encodeURI($("#" + tid).val());
    var c = encodeURI($("#" + cid).val());
    if (q == defaultText) {
        q = "";
    }
    else {
        q = encodeURI(q);
    }
    var qs = "";
    if (q != "") {
        qs += "&q=" + q;
    }
    if (t != "") {
        qs += "&rtype=" + t;
    }
    if (c != "") {
        qs += "&county=" + c;
    }
    var url = root + "Search.aspx";
    if (qs != "") {
        qs = qs.substr(1, qs.length - 1);
        url += "?" + qs;
    }
    location.href = url;
    return false;
}

function Search2(root, qid, defaultText) {
    var q = Trim($("#" + qid).val());
    if (q == defaultText) {
        q = "";
    }
    else {
        q = encodeURI(q);
    }
    var qs = "";
    if (q != "") {
        qs += "&q=" + q;
    }
    var url = root + "Search.aspx?lnk=recipe";
    if (qs != "") {
        url += qs;
    }
    location.href = url;
    return false;
}

function ClearIfDefaultText(defaultText, e)
{
    //var input = $("searchfield");
    var input = GetElementTriggerEvent(e);
    if (input.value == defaultText)
    {
        input.value = "";
    }
}

function GetElementTriggerEvent(e)
{
    var targ;
    if (!e)
    {
        e = window.event;
    }
    if (e.target)
    {
        targ = e.target;
    }
    else if (e.srcElement)
    {
        targ = e.srcElement;
    }
    if (targ.nodeType==3) // defeat Safari bug
    {
        targ = targ.parentNode;
    }
    return targ;
}
function InitToEmailPopup(winSize, showHandler) {
    var width = document.documentElement.clientWidth + document.documentElement.scrollLeft;
    var height = document.documentElement.clientHeight + document.documentElement.scrollTop;
    var ctrl;

    ctrl = $('#dmail')[0];
    ctrl.style.position = 'absolute';
    ctrl.style.top = ((height + document.documentElement.scrollTop) / 2) - (winSize.height / 2) + 'px';
    ctrl.style.left = (width / 2) - (winSize.width / 2) + 'px';
    ctrl.style.height = winSize.height + 'px';
    ctrl.style.width = winSize.width + 'px';
    $(ctrl).jqm({ ajax: '@href', trigger: 'a.lnkmail', overlay: 50, onShow: showHandler });
}
var openToEmailPopup = function ResetToEmailPopupPos(hash) {
    SetPopupPos($('#dmail')[0]);
    hash.w.show();
};
function InitToRecommendPopup(rid, winSize, showHandler) {
    var width = document.documentElement.clientWidth + document.documentElement.scrollLeft;
    var height = document.documentElement.clientHeight + document.documentElement.scrollTop;
    var ctrl;
    var href = "editFrame/ToRecommend.aspx?rid=" + rid.toString();

    ctrl = $('#drecommend')[0];
    ctrl.style.position = 'absolute';
    ctrl.style.top = ((height + document.documentElement.scrollTop) / 2) - (winSize.height / 2) + 'px';
    ctrl.style.left = (width / 2) - (winSize.width / 2) + 'px';
    ctrl.style.height = winSize.height + 'px';
    ctrl.style.width = winSize.width + 'px';
    $(ctrl).jqm({ ajax: href, overlay: 50, onShow: showHandler });
}
var openToRecommendPopup = function ResetToRecommendPopupPos(hash) {
    SetPopupPos($('#drecommend')[0]);
    hash.w.show();
};
function SetPopupPos(ctrl) {
    var width = document.documentElement.clientWidth + document.documentElement.scrollLeft;
    var height = document.documentElement.clientHeight + document.documentElement.scrollTop;

    ctrl.style.position = 'absolute';
    ctrl.style.top = ((height + document.documentElement.scrollTop) / 2) - (parseInt(ctrl.style.height.toString().replace('px', ''), 10) / 2) + 'px';
    ctrl.style.left = (width / 2) - (parseInt(ctrl.style.width.toString().replace('px', ''), 10) / 2) + 'px';
}
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
function htmlEncode(value) {
    return $('<div/>').text(value).html();
}

function htmlDecode(value) {
    return $('<div/>').html(value).text();
}

function GetTagDataAP(tag) {
    var pd = "tag=" + tag;
    $.ajax({
        type: "POST",
        url: "Default.aspx",
        cache: false,
        data: pd,
        dataType: 'html',
        global: false,
        complete: function(request, textStatus) {
            var tv;
            if (textStatus == 'success') {
                var results = $(request.responseText).find('#results')[0];
                if (results != null) {
                    var html1 = $(results).children("#result1").html();
                    var html2 = $(results).children("#result2").html();
                    $("#tagshowd").html(html1 + html2);

                    $("#tagcon").children("div.tag").children("a.c").removeClass("c").addClass("b");
                    $("#tagcon").children("div.tag").removeClass("tag").addClass("tag2");
                    $("#tagcon div.tag2").children("#alnk_" + tag).removeClass("b").addClass("c");
                    $("#tagcon div.tag2").children("#alnk_" + tag).parent().removeClass("tag2").addClass("tag");
                }
                else {
                    //alert(html);
                }
            }
            else {
                //alert(request.responseText);
                alert('發生錯誤!');
            }
        }
    });
}

