function isQuitAndNotSave(){
    var content = "";
    if (window.frames["Editor"].document.getElementById("sourceEditor").style.display == "none") {
        content = window.frames["Editor"].frames["HtmlEditor"].document.body.innerHTML;
    }
    else {
        content = window.frames["Editor"].document.getElementById("sourceEditor").value;
    }
    
    if (content.length > 10) {
        return "您的博客还没有保存,确定离开吗?";
    }
    
    return;
}

function setTopAndLeft(){
    var width = document.body.clientWidth;
    var height = document.body.clientHeight;
    var widthpx = document.getElementById("chooseArea").style.width.split("px");
    document.getElementById("chooseArea").style.top = "200px";
    document.getElementById("chooseArea").style.left = ((width - widthpx[0]) / 2 + 80) + "px";
    document.getElementById("maskIframe").style.top = "200px";
    document.getElementById("maskIframe").style.left = ((width - widthpx[0]) / 2 + 80) + "px";
}

//类别操作相关
function submitCondition(){
    /*
     if(document.getElementById("year").value=="-1" && document.getElementById("month").value!="-1"){
     alert("年份未选");
     }else{
     $('selectionForm').submit();
     }
     */
    $('selectionForm').submit();
}

function addNewCategoryQucik(){
    if (document.getElementById("newCateNameForm").style.display == "none") {
        document.getElementById("newCateNameForm").innerHTML = '<input type=text name="newCateName" id="newCateName" maxlength=15 size=15 onblur="checkCatNameIsNull();">&nbsp;<span id="catNameInformation" name="catNameInformation" class="text12px redtext"></span>&nbsp;<input type="button" onclick="javascript:addNewCategoryQucikOk();" value="添加" />';
        document.getElementById("newCateNameForm").style.display = "";
    }
    else {
        document.getElementById("newCateNameForm").style.display = "none";
    }
}

function checkCatNameIsNull(){
    if (document.getElementById("newCateName").value.trim().length < 1) {
        document.getElementById("catNameInformation").innerHTML = "类别不能为空";
    }
    else {
        document.getElementById("catNameInformation").innerHTML = "";
    }
}

function addNewCategoryQucikOk(){
    if (document.getElementById("newCateName").value.trim().length < 1) {
        return false;
    }
    var url = "/manage/blog/addBlogCategory.do";
    var pars = "type=quick&newCatName=" + encodeURIComponent(document.getElementById("newCateName").value.trim());
    var div = "chooseGroup";
    new Ajax.Updater(div, url, {
        method: 'post',
        parameters: pars,
        evalScripts: true
    });
    document.getElementById("newCateNameForm").value = "";
}

function addNewCategory(){
    if (document.getElementById("newCatName").value.trim().size() == 0) {
        alert("请输入类别名");
    }
    else {
        var url = "/manage/blog/addBlogCategory.do";
        var pars = "newCatName=" + encodeURIComponent(document.getElementById("newCatName").value.trim());
        var div = "newCategoryShow";
        new Ajax.Updater(div, url, {
            method: 'post',
            parameters: pars,
            evalScripts: true
        });
    }
}

function chooseGroup(){
    if (document.getElementById("isChooseGroup").checked == true) {
        document.getElementById("chooseGroup").style.display = "block";
        var url = "/manage/blog/listMyGroup.do";
        var div = "chooseGroup";
        new Ajax.Updater(div, url, {
            method: 'post',
            evalScripts: true
        });
    }
    else {
        document.getElementById("chooseGroup").innerHTML = "";
        document.getElementById("chooseGroup").style.display = "none";
    }
}

function deleteCategory(id){
    if (confirm("删除分类将把这些类别的文章归类到未分类,你确定?")) {
        var url = "/manage/blog/deleteCategory.do";
        var pars = "id=" + id;
        var div = "category_" + id;
        new Ajax.Updater(div, url, {
            method: 'post',
            parameters: pars,
            evalScripts: true
        });
    }
}

function rename(id){
    document.getElementById("category_" + id).innerHTML = "<input type=text maxlength=15 size=15 name=renameCategory id=renameCategory value='" + document.getElementById("category_" + id).innerHTML + "'><a href='javascript:submitRename(" + id + ");'>确定</a>";
    document.getElementById("renameButton_" + id).innerHTML = "取消";
    document.getElementById("renameButton_" + id).href = "javascript:renameCancel('" + id + "');";
}

function renameCancel(id){
    document.getElementById("category_" + id).innerHTML = document.getElementById("defaultCategoryName_" + id).value;
    document.getElementById("renameButton_" + id).innerHTML = "改名";
    document.getElementById("renameButton_" + id).href = "javascript:rename( '" + id + "');";
}

function submitRename(id){
    if (document.getElementById("renameCategory").value.trim().size() == 0) {
        alert("请输入类别名");
    }
    else {
        var url = "/manage/blog/editCategory.do";
        var pars = "id=" + id + "&newCatName=" + encodeURIComponent(document.getElementById("renameCategory").value.trim());
        var div = "category_" + id;
        new Ajax.Updater(div, url, {
            method: 'post',
            parameters: pars,
            evalScripts: true
        });
        document.getElementById("renameButton_" + id).innerHTML = "改名";
        document.getElementById("renameButton_" + id).href = "javascript:rename('" + id + "')";
    }
}

//博客操作相关

function selectAll(){
    var chkIds = document.getElementsByName('blogchk');
    
    for (var i = 0; i < chkIds.length; i++) {
        chkIds[i].checked = document.getElementById("selectAll").checked;
    }
}

function selectPrivilege(){
    if (document.getElementById("privilege").value == 2) {
        document.getElementById("passwordPad").style.display = "inline";
    }
    else {
        document.getElementById("passwordPad").style.display = "none";
    }
}

function changeMonth(){
    /**
     if(document.getElementById("year").value>0){
     document.getElementById("month").style.display = "block";
     }else{
     document.getElementById("month").style.display = "none";
     }
     */
}

function changeCategory(){
    var ids = "";
    for (var i = 1; i <= 10; i++) {
        if (document.getElementById("blog_" + i) != null && document.getElementById("blog_" + i).checked) {
            ids = ids + "id=" + document.getElementById("blog_" + i).value + "&";
        }
    }
    if (ids != "") {
        var url = "/manage/blog/changeCat.do";
        var pars = "newCatId=" + document.getElementById("changeCategory").value + "&" + ids;
        new Ajax.Updater("showArea", url, {
            method: 'post',
            parameters: pars,
            evalScripts: true
        });
    }
}

function deleBlog(id){
    if (confirm("删除了就没有了哦!你确定?")) {
        var url = "/manage/blog/remove.do";
        var pars = "id=" + id;
        new Ajax.Updater("showArea", url, {
            method: 'post',
            parameters: pars,
            evalScripts: true
        });
    }
}

function deleteBlogs(){
    var chkIds = document.getElementsByName('blogchk');
    var ids = "";
    
    for (var i = 0; i < chkIds.length; i++) {
        if (chkIds[i].checked) {
            ids = ids + "id=" + chkIds[i].value + "&";
        }
    }
    
    if (ids != "") {
        if (confirm("删除了就没有了哦!你确定?")) {
            var url = "/manage/blog/remove.do";
            var pars = ids;
            new Ajax.Updater("showArea", url, {
                method: 'post',
                parameters: pars,
                evalScripts: true
            });
        }
    }
}


function changePage(id){
    document.getElementById("page").value = id;
    $('selectionForm').submit();
}

function checkTitleIsNull(){
    if (document.getElementById("blog_title").value.trim().length < 1) {
        document.getElementById("blogTitleInfo").innerHTML = "标题不能为空";
    }
    else {
        document.getElementById("blogTitleInfo").innerHTML = "";
    }
}

function checkTagIsNull(){
    if (document.getElementById("tag_").value.trim().length < 1) {
        document.getElementById("tagInfo").innerHTML = "标签不能为空";
    }
    else {
        document.getElementById("tagInfo").innerHTML = "";
    }
}

function checkFile(ele){
    if (!CheckFileFormat(ele)) {
        alert("请选择扩展名为jpg或gif的图片！")
        
        if ($('fileDiv')) {
            $('fileDiv').innerHTML = '<input type="file" name="blogPicFile"  id="photo_file" onchange="checkFile(this)" contenteditable="false" size="50">';
        }
        
        return false;
    }
    return true;
}

function CheckFileFormat(ele){
    var fileext = ele.value.substring(ele.value.length - 4, ele.value.length)
    fileext = fileext.toLowerCase()
    if (!(fileext == '.jpg' || fileext == '.gif')) {
        return false;
    }
    else {
        return true;
    }
}

function uploadphoto(){
    $("uploadphotoform").submit();
    //$("uploadphotoform").submit();
    //    if (checkFile($('photo_file'))) 
    //        $("uploadphotoform").submit();
}

function uploadblogpic(str){
    if (str == "0") {
        alert("上传失败");
        return;
    }
    if (str == "1") {
        alert("文件扩展名只能是jpg或gif");
        return;
    }
    if (str == "2") {
        alert("文件不能大于1M");
        return;
    }
    window.frames['Editor'].addhtml(str);
    closeDivDialog('insertPictureArea2');
}

function addpicId(id){
    try {
        var newInput = document.createElement("input");
        newInput.type = "hidden";
        newInput.name = "blogPicIds";
        newInput.value = id
        $("blogForm").appendChild(newInput);
    } 
    catch (e) {
    
    }
}

/**
 * add by YangYZ
 */
var myDate = new Date();

function addBlog(draft){
    $("blogTitle").value = $("blog_title").value;
    $("tag").value = $("tag_").value;
    $("crtTime").value = $("crt_time").value + " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
    $("catId").value = $("cat_id").value;
    if (checkBlogForm()) {
        var draft = (draft) ? 1 : 0;
        $("draft").value = draft;
        $("blogForm").submit();
    }
}

function checkBlogForm(){
    var form = $("blogForm");
    
    if (form.blogTitle.value.trim().size() == 0) {
        alert("请输入标题");
        $('blog_title').focus();
        return false;
    }
    
    if (form.tag.value.trim().size() == 0) {
        alert("请输入标签");
        $('tag_').focus();
        return false;
    }
    
    var content = "";
    if (window.frames["Editor"].document.getElementById("sourceEditor").style.display == "none") {
        content = window.frames["Editor"].frames["HtmlEditor"].document.body.innerHTML;
    }
    else {
        content = window.frames["Editor"].document.getElementById("sourceEditor").value;
    }
    if (content.trim().length < 10) {
        alert("文章内容太短,最少10个字符");
        return false;
    }
    if (content.trim().length > 100000) {
        alert("文章内容太长了.达到了" + 100000 + "个字");
        return false;
    }
    $("blogForm").content.value = content;
    
    return true;
}

function checkTitle(){
    if ($('blog_title').value.trim().size() < 1) {
        $('titlePrompt').style.display = 'none';
        $('titleNull').style.display = 'inline';
    }
    else {
        $('titlePrompt').style.display = 'inline';
        $('titleNull').style.display = 'none';
    }
}

function checkTag(){
    if ($('tag_').value.trim().size() < 1) {
        $('tagPrompt').style.display = 'none';
        $('tagNull').style.display = 'inline';
    }
    else {
        $('tagPrompt').style.display = 'inline';
        $('tagNull').style.display = 'none';
    }
}


function releaseBlog(){
    var chkIds = document.getElementsByName('blogchk');
    var ids = "";
    
    for (var i = 0; i < chkIds.length; i++) {
        if (chkIds[i].checked) {
            ids = ids + "id=" + chkIds[i].value + "&";
        }
    }
    
    if (ids != "") {
        if (confirm("你确定发布这些文章?")) {
            var url = "/manage/blog/submitAllBlogs.do";
            window.location.href = url + "?" + ids;
        }
    }
}

function updateBlogSet(){
    var frm = $('updateBlogFrm');
    
    if (frm.spaceTitle.value.size() == 0) {
        alert('请输入标题！')
        frm.spaceTitle.focus();
        return;
    }
    
    frm.submit();
}

function showCatEdit(id){
    document.getElementById('oldCatSpan' + id).style.display = "none";
    document.getElementById('catSpan' + id).style.display = "inline";
    document.getElementById('catSpanOper' + id).style.display = "none";
}

function cancelCatEdit(id){
    document.getElementById('oldCatSpan' + id).style.display = "inline";
    document.getElementById('catSpan' + id).style.display = "none";
    document.getElementById('catSpanOper' + id).style.display = "inline";
}

function updateCat(id){
    var newCatName = document.getElementById('catName' + id).value.trim();
    if (newCatName.size() == 0) {
        alert('请输入类别名');
    }
    else {
        var url = '/manage/blog/updatecat.do';
        var pars = 'id=' + id + '&newCatName=' + encodeURIComponent(newCatName);
        var div = 'catDiv';
        new Ajax.Updater(div, url, {
            method: 'post',
            parameters: pars,
            evalScripts: true
        });
    }
    
}

function deleteCat(id){
    if (confirm("删除分类将把这些类别的文章归类到未分类,你确定?")) {
        var url = "/manage/blog/deleteCategory.do";
        var pars = "id=" + id;
        var div = "catDiv";
        new Ajax.Updater(div, url, {
            method: 'post',
            parameters: pars,
            evalScripts: true
        });
    }
}

function addNewCat(){
    var newCatName = document.getElementById('catName').value.trim();
    
    if (newCatName.size() == 0) {
        alert("请输入类别名");
    }
    else {
        var url = "/manage/blog/addblogcat.do";
        var pars = "newCatName=" + encodeURIComponent(newCatName);
        var div = "catDiv";
        new Ajax.Updater(div, url, {
            method: 'post',
            parameters: pars,
            evalScripts: true
        });
    }
}

function deleteCommentById(id){
    var url = '/manage/blog/deletecomment.do';
    var pars = 'ids=' + id;
    var div = 'ajaxDiv';
    new Ajax.Updater(div, url, {
        method: 'post',
        parameters: pars,
        evalScripts: true
    });
}

function selectAllComment(){
    var chkIds = document.getElementsByName('ids');
    
    for (var i = 0; i < chkIds.length; i++) {
        chkIds[i].checked = document.getElementById("selectAll").checked;
    }
}

function deleteComment(){
    var isAllCheck = true;
    var hasCheck = false;
    
    var ids = document.getElementsByName('ids');
    
    for (var i = 0; ids && i < ids.length; i++) {
        if (ids[i].checked) {
            hasCheck = true;
        }
        else {
            isAllCheck = false;
        }
        
        if (hasCheck && !isAllCheck) {
            break;
        }
    }
    
    if (!hasCheck) {
        alert('请至少选择一条删除');
        return;
    }
    
    
    if (!confirm('您确认删除所选吗?')) {
        return;
    }
    
    if (isAllCheck) {
        var frm = $('commentFrm');
        var url = '/manage/blog/deletecomment.do?delAll=true';
        frm.action = url;
        frm.submit();
    }
    else {
        var url = '/manage/blog/deletecomment.do';
        var div = 'ajaxDiv';
        new Ajax.Updater(div, url, {
            method: 'post',
            parameters: Form.serialize($('commentFrm')),
            evalScripts: true
        });
    }
}

function showCommentFrm(id, blogId){
    var quoteStr = '<div class="quote">引用';
    quoteStr += $('user' + id).innerHTML;
    quoteStr += '的评论：';
    quoteStr += '<blockquote>';
    quoteStr += $('comAllDiv' + id).innerHTML;
    quoteStr += '</blockquote>';
    quoteStr += '</div>';
    $('commentTmp').innerHTML = quoteStr;
    
    openDivDialog('floatBoxWrap')
    
    $('curId').value = id;
    $('blogId').value = blogId;
    $('commentContent').value = '';
}

function addBlogComment(){
    $('btnOk').href = 'javascript:void(null);';
    
    if ($('commentContent').value.trim().size() > 0) {
        var frm = $('writeCommentFrm');
        frm.objId.value = $('blogId').value;
        frm.content.value = $('commentTmp').innerHTML + $('commentContent').value;
        
        frm.submit();
        /*
         var url = './savecomment.do';
         var pars = 'type=2&objId=' + $('blogId').value + '&content=' + encodeURIComponent($('commentTmp').innerHTML + $('commentContent').value);
         var div = 'commentDiv' + $('curId').value;
         new Ajax.Updater(div, url, { method: 'post', parameters: pars, evalScripts: true});
         closeDivDialog('floatBoxWrap');
         */
    }
    else {
        alert('未输入内容');
    }
    
    $('btnOk').href = 'javascript:addBlogComment();';
}

function showAddCatFrm(){
    openDivDialog('addCatalog');
    $('newCateName').value = '';
}

function addNewCatForQuick(){
    if ($('newCateName').value.trim().size() < 1) {
        alert('请输入一个分类的名字!')
    }
    
    var url = '/manage/blog/addblogcat.do';
    var pars = 'type=quick&newCatName=' + encodeURIComponent(document.getElementById('newCateName').value.trim());
    var div = 'addCatDiv';
    new Ajax.Updater(div, url, {
        method: 'post',
        parameters: pars,
        evalScripts: true
    });
}

function autoSave(){

    setInterval("processSave()", 1000 * 60 * 5);
}

var first = true;

function processSave(){
    var content = "";
    
    if (window.frames["Editor"].document.getElementById("sourceEditor").style.display == "none") {
        content = window.frames["Editor"].frames["HtmlEditor"].document.body.innerHTML;
    }
    else {
        content = window.frames["Editor"].document.getElementById("sourceEditor").value;
    }
    
    var t = "自动保存草稿";
    var m = "自动保存"
    
    if ($("blog_title").value.length > 0) {
        t = $("blog_title").value;
    }
    
    if ($("tag_").value.length > 0) {
        m = $("tag_").value;
    }
    
    $("blogTitle").value = t;
    $("tag").value = m;
    $('content').value = content;
    $("crtTime").value = $("crt_time").value + " " + myDate.getHours() + ":" + myDate.getMinutes() + ":" + myDate.getSeconds();
    $("catId").value = $("cat_id").value;
    $("draft").value = 1;
    
    if (first) {
        var url = '/manage/blog/autoadd.do?ddd=true';
        var div = 'ddddd';
        new Ajax.Updater(div, url, {
            method: 'post',
            parameters: Form.serialize($('blogForm')),
            evalScripts: true
        });
    }
    else {
    
        var url = '/manage/blog/autoupdate.do?ddd=true';
        var myAjax = new Ajax.Request(url, {
            method: 'post',
            parameters: Form.serialize($('blogForm')),
            onComplete: null
        });
    }
}

function showDiv2(obj){
	$("btdiv").style.display =obj.checked==true? "inline" : "none"
}











