﻿function expireOffer(key) {
    if (confirm("你确定要截止该招聘信息吗？") == false)
        return;
    $.ajax({
        url: '/offer/OfferManager.asmx/CutOffOffer',
        data: '{key: ' + key + '}',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json; charset=utf8',
        cache: false,
        success: function(data) {
            alert(data.d);
        },
        error: function(xhr) {
            alert(xhr.responseText);
        }
    });
}
function UpdateOfferLastUpdated(key) {
    if (confirm("你确定要更新该职位的发布时间吗？每天只能更新一次哦 :)")) {
        $.ajax({
            url: '/offer/OfferManager.asmx/UpdateLastUpdated',
            data: '{key: ' + key + '}',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json; charset=utf8',
            cache: false,
            success: function(data) {
                alert(data.d);
            },
            error: function(xhr) {
                alert(xhr.responseText);
            }
        });
    }
}
function hideOffer(key) {
    if (confirm("你确定要隐藏该招聘信息吗？") == false)
        return;
    $.ajax({
        url: '/offer/OfferManager.asmx/HiddenOffer',
        data: '{key: ' + key + '}',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json; charset=utf8',
        cache: false,
        success: function(data) {
            alert(data.d);
        },
        error: function(xhr) {
            alert(xhr.responseText);
        }
    });

}
function deleteOffer(key) {
    var isConfirm = false;
    isConfirm = confirm("确定删除此职位？");
    //        ShowConfirm($dialog, isConfirm, "确定删除此职位？");
    if (isConfirm)
        $.getJSON('/Enterprise/DeleteJob/' + key, function (data) {
            if (data && data.isSuccess)
            { alert("删除成功！"); window.history.go(-1); }
            else {
                alert(data.msg);
            }
        });
}
function DelComment(key) {
    if (confirm("你确定要删除该评论吗？") == false)
        return;
    $.ajax({
        url: '/offer/OfferComment.asmx/DelComment',
        data: '{key:' + key + '}',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json;charset=utf8',
        cache: false,
        success: function(data) {
            $('#li_' + key).html('已删除成功!');
        },
        error: function(xhr) {
            alert(xhr.responseText);
            window.location.reload();
        }
    });
}
function Edit(key) {
    $('#txtContent').focus();
    $('#txtContent').removeClass('txtContent_bg')
    var str = $('#li_' + key + ' .comment_main').html();
    $("#Comment_Edit_ID").val(key);
    $('#txtContent').val(str);
}
function ctlent2(eventobject) {
    $('#lab_error').addClass('hidden');
    if (eventobject.ctrlKey && eventobject.keyCode == 13) {

        doComment();
    }
}
function showComment(data) {
    var entity = eval("(" + data.d + ")");
    id = entity.ID;
    userInfo = entity.UserInfo;
    content = entity.Content;
    floor = $('#user_comment_content').children().length + 1;
    liStr = "<li id='li_" + id + "' class='user_comment'><div class='commenter_info'><span class='floor'>第" + floor + "楼</span> " + userInfo + " <span class='time'>发表于 " + entity.PostDate + "</span>";
    liStr += "<div class='comment_main'>" + content + "</div></li>";
    $('#user_comment_content').append(liStr);
    $('#txtContent').val('');
}
function UpdateComment(data) {
    var entity = eval("(" + data.d + ")");
    id = entity.ID;
    content = entity.Content.replace(/<BR>/ig, '\n');
    $('#li_' + id + ' .comment_main').html(content);
    $('#txtContent').val('');
}
function getDate() {
    var date = new Date();
    var str = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDay();
    return str;
}
function doComment() {
    var userName = $('#txtUsername').val();
    var email = $('#txtUserEmail').val();
    var content = $('#txtContent').val().replace(/\x22/g, '”');
    var key = $('#Comment_Edit_ID').val();
    if (content.length == 0) {
        $('#lab_error').removeClass('hidden');
    }
    else if (key == '' || key == null) {
        var offerkey = $("#offer_key").html();
        $.ajax({
            url: '/offer/OfferComment.asmx/doComment',
            data: '{userName:"' + userName + '",email:"' + email + '",content:"' + content + '",offerKey:' + offerkey + '}',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json;charset=utf8',
            cahce: false,
            success: function(data) {
                showComment(data);
            },
            error: function(xhr) {
                alert(xhr.responseText);
            }
        });
    }
    else {
        $.ajax({
            url: '/offer/OfferComment.asmx/UpdateComment',
            data: '{key:' + key + ',content:"' + content + '"}',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json;charset=utf8',
            cahce: false,
            success: function(data) {
                UpdateComment(data);
            },
            error: function(xhr) {
                alert(xhr.responseText);
            }
        });
    }
}
function SetAuthor(key) {
    $('#txtContent').removeClass('txtContent_bg');
    author = $('#li_' + key + ' #span_' + key).text();
    document.getElementById("txtContent").focus();
    document.getElementById("txtContent").value = "@" + author + "\n" + document.getElementById("txtContent").value;
    return false;
}
function Reference(key) {
    $('#txtContent').removeClass('txtContent_bg')
    var str = $('#li_' + key + ' .comment_main').html().replace(/<br>/ig, '\n');
    str = '--引用----------------------------------\n' + str + '\n--------------------------------------\n';
    document.getElementById("txtContent").focus();
    document.getElementById("txtContent").value = str + document.getElementById("txtContent").value;
    return false;
}
function SendResume(to,enterpriseId,offerId) {    
    if (to == "") {
        alert("此职位没有邮箱，无法投递简历");
        return false;
    }
    $("#send_state").text("正在投递中...");
    var sendResume = {};
    sendResume.to = to;
    sendResume.subject = $("#post_resume_subject").val();
    sendResume.body = $("#post_resume_content").val();
    sendResume.enterpriseId = enterpriseId,
    sendResume.offerId = offerId;
    $.ajax({
        url: '/offer/ResumeManager.asmx/PostResume',
        data: $.toJSON(sendResume),
        type: 'post',
        dataType: "json",
        contentType: 'application/json;charset=utf8',
        cache: false,
        success: function(data) {
            if (data.d == "success") {
                $("#btn_post_resume").unbind("click");
                $("#btn_post_resume").unbind("onclick");
                $("#btn_post_resume").click(function() { alert("您今天已经投过此职位了 :-)"); });
                $("#post_resume_block").html("<tr><td><span class='red_text'>已投递成功，静等佳音:-)</span></td></tr>");
                $("#post_resume_block").after("<div class='gray_text' style='padding-left:193px;'>查看<a href='http://home.cnblogs.com/job/myapplylog/' target='_blank'>我投递的职位</a></div>");
                $("#post_resume_block").fadeOut(3000, function() { $("#post_resume_block").hide(); });
            }
            else {
                alert("邮件发送失败，请联系管理员或直接发邮件至招聘企业");
                $("#send_state").html("投递失败，请<a href='mailto:contact@cnblogs.com'>联系管理员</a>或<a href='http://space.cnblogs.com/forum/public' target='_blank'>反馈此信息</a>");
                //alert(data.d);
            }
        },
        Error: function(xhr) {
            alert(xhr.responseText);
        }
    });
}
function SendResumeTip(days) {
    var numberStr = "今天";
    if (days > 0) {
        numberStr = "在" + days + "天前";
    }    
    if ($("#post_resume_block").css("display") != "none") {
        $('#post_resume_block').toggle();
    }
    else if (confirm("您" + numberStr + "曾向该职位投递过简历，确定还要投递吗？")) {
        toggleBlock();
    }
}
function toggleBlock() {
    if ($("#count_tip").html() == "") {
        //添加还能发送简历的次数
        $.ajax({
            url: '/offer/ResumeManager.asmx/GetAvailableCount',
            data: '{}',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json;chartset=utf8',
            cache: false,
            success: function(data) {
                var number = data.d;
                if (number > 10)
                    number = 10;
                if (number < 0)
                    number = 0;
                if (number == 0) {
                    $('#post_resume_block').empty();
                    alert("您今天已经发送了10份简历了，明天再投吧 :)");
                }
                $("#count_tip").html("<span class='gray_text'>您今天还有<span class='red_text'>" + number + "</span>次发送简历的机会，将机会送给更适合您的职位 :)</span>");
            },
            Error: function(xhr) {
                alert(xhr.responseText);
            }
        });

        //添加用户简历邮箱
        $.ajax({
            url: '/offer/ResumeManager.asmx/GetUserResumeEmail',
            data: '{}',
            type: 'post',
            dataType: 'json',
            contentType: 'application/json;chartset=utf8',
            cache: false,
            success: function(data) {
                var result = data.d;
                if (result != "")
                    $("#post_email_block").html("将通过您简历中的" + result + "邮箱，将您的简历发送给企业");
            },
            Error: function(xhr) {
                alert(xhr.responseText);
            }
        });
    }    
    $('#post_resume_block').toggle();
}

function BindSendBtnClick(userId, offerId) {
    $.ajax({
        url: "/offer/ResumeManager.asmx/GetSendSpan",
        data: "{userId:'" + userId + "',offerKey:" + offerId + "}",
        dataType: "json",
        type: "post",
        contentType: 'application/json;chartset=utf8',
        cache: false,
        success: function(data) {
            //今天已经投递过此职位了
            //您曾在N天前投递过此职位。
            if (data.d >= 0) {
                $('#btn_post_resume').click(function() { SendResumeTip(data.d); });
            }           
            else {
                $('#btn_post_resume').click(function() { toggleBlock() });
            }
        },
        error: function(xhr) {
        }
    });

}

function IsRecommendOffer(offerId) {
    $.ajax({
    url: "/offer/OfferManager.asmx/IsRecommendOffer",
        data: "{offerId:" + offerId + "}",
        dataType: "json",
        type: "post",
        contentType: 'application/json;chartset=utf8',
        cache: false,
        success: function(data) {
            //是否是推荐职位
            if (data.d) {
                $("#position_flag").html("<img src='/image/ico_recommend.gif' alt='推荐' title='博客园推荐职位' />");
            }
        },
        error: function(xhr) {
        }
    });
}

function ShowPostResumeBlock() {
    $("#post_resume_block").show();
}


function PutInWz() {
    var width = 460;
    var height = 353;
    var leftVal = (screen.width - width) / 2;
    var topVal = (screen.height - height) / 2;
    var d = document;
    var t = d.selection ? (d.selection.type != 'None' ? d.selection.createRange().text : '') : (d.getSelection ? d.getSelection() : '');
    window.open('http://wz.cnblogs.com/create?t=' + encodeURIComponent(d.title) + '&u=' + d.location.href + '&c=' +
	     escape(t) + '&i=0', '_blank', 'width=' + width + ',height=' + height + ',toolbars=0,resizable=1,left=' + leftVal + ',top=' + topVal);
}
