﻿function checksearch() {
    var key = $.trim($("#keyword").val());
    var sel = $("input[name=selType]:checked");
    if (sel.length == 0) {
        sel = $.trim($("#selType").val());
    }
    if (key == "") {
        alert("请输入关键字！");
        return false;
    } else if (sel.length == 0) {
        alert("请选择搜索类别！");
        return false;
    }
    return true;
}
function saveBlog(id){
    if (checkBlog()){        
        $("#btnSave").attr("disabled", "disabled");
        var txtTitle=$("#txtTitle").val();
        var selClass=$.trim($("#selClass").val());
        var txtKeyword=$("#txtKeyword").val();
        var areaContent=CKEDITOR.instances.areaContent.getData();
        $.ajax({
            url:"/ajax/blogs/saveBlog.ashx",
            type:"POST",
            dataType:"xml",
            data:{id:id,title:txtTitle,blogclass:selClass,keyword:txtKeyword,content:areaContent,date:new Date()},
            success:function(xml){
                $(xml).find("result").each(function(){
                    alert($(this).text());
                    if ($(this).attr("flag")=="1"){
                        location.href="blog-"+$(this).attr("id")+".aspx";                        
                    }
                });
                $("#btnSave").attr("disabled","");        
            },
            error:function(){
                alert("文件载入错误，请稍后重试！");
                $("#btnSave").attr("disabled","");
            }
        });
    }    
}
function checkBlog(){
    var txtTitle=$.trim($("#txtTitle").val());
    var selClass=$.trim($("#selClass").val());
    var txtKeyword=$.trim($("#txtKeyword").val());
    var areaContent=$.trim(CKEDITOR.instances.areaContent.getData());
    var reg=new regular();
    if (reg.isEmpty(txtTitle)){
        alert("请输入标题！");
        return false;
    }else if (reg.isEmpty(selClass)){
        alert("请选择类别！");
        return false;
    }else if (reg.isEmpty(txtKeyword)){
        alert("请输入关键字！");
        return false;
    }else if (reg.isEmpty(areaContent)){
        alert("请输入博文内容！");
        return false;
    }    
    return true;
}
function saveBlogReply(id){
    if (checkBlogReply()){
        maskReply(true);    
        $("#btnReply").attr("disabled","disabled");
        var anonymous=$.trim($("#Anonymous:checked").val());
        var content=$.trim($("#replyContent").val());
        $.ajax({
            url:"/ajax/blogs/saveBlogReply.ashx",
            type:"POST",
            dataType:"xml",
            data:{blogid:id,anonymous:anonymous,content:content,date:new Date()},
            success:function(xml){
                $(xml).find("result").each(function(){
                    alert($(this).text());
                    if ($(this).attr("flag")=="1"){
                        location.href=location.href;
                    }
                });
                maskReply(false);
                $("#btnReply").attr("disabled","");        
            },
            error:function(){
                alert("文件载入错误，请稍后重试！");
                maskReply(false);
                $("#btnReply").attr("disabled","");
            }
        });
    }    
}
function checkBlogReply(){
    var anonymous=$.trim($("#Anonymous:checked").val());
    var content=$.trim($("#replyContent").val());
    var reg=new regular();
    if (reg.isEmpty(content)){
        alert("请输入评论内容！");
        return false;
    }
    return true;
}
//message box

String.prototype.replaceAll  = function(s1,s2){ 
    return this.replace(new RegExp(s1,"g"),s2);   
  } 
//up photo method
function showAlbum(){
    $(".albumContainer ul").removeClass("none");
}
function hideAlbum(){
    $("#albName").val("");
    $("#albDesc").val("");
    $(".albumContainer ul").addClass("none");
}
function checkAlbum(){
    var albName=$.trim($("#albName").val());
    var reg = new regular();
    if (reg.isEmpty(albName)) {
        alert("请输入相册名称");
        return false;
    }
    return true;    
}
function saveAlbum(){
    var albName=$("#albName").val();    
    if (checkAlbum()){
        $("#btnSave").attr("disabled", "disabled");
        $.ajax({
            url: "/ajax/blogs/saveAlbum.ashx",
            type: "POST",
            dataType: "xml",
            data: { title: albName, date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                    if ($(this).attr("flag") == "1") {
                        hideAlbum();
                        getAlbum();
                    }
                });
                $("#btnSave").attr("disabled", "");
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");
                $("#btnSave").attr("disabled", "");
            }
        });
    }
}
function getAlbum(){
    $.ajax({
        url: "/ajax/blogs/getAlbum.ashx",
        type: "POST",
        dataType: "xml",
        data: {date: new Date() },
        success: function(xml) {
            $(xml).find("result").each(function() {
                if ($(this).attr("flag")=="1"){
                    $("#albumName").empty();
                    $(this).children().each(function(){
                        $("#albumName").append("<option value=\""+$(this).attr("id")+"\">"+$(this).attr("name")+"</option>");
                    });  
                } else {
                    alert($(this).text());
                }                            
            });
        },
        error: function() {
            alert("文件载入错误，请刷新页面！");
            location.href=location.href;            
        }
    });
}
function deleteAlbum(id,count){
    if(count>0){
        alert("请先删除相册内的照片再删除相册");
    }else{
        $.ajax({
            url: "/ajax/photo/deleteAlbum.ashx",
            type: "POST",
            dataType: "xml",
            data: {albumid:id, date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                    if ($(this).attr("flag")=="1"){
                        location.href=location.href;
                    }  
                });
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");
            }
        });
    }
}
function editAName(flag,id){
    if (flag){
        $("#txtTitle_"+id).val("");  
        $("#aTitleD_"+id).hide();
        $("#aOptionD_"+id).hide();
        $("#aTitleE_"+id).show();
        $("#aOptionE_"+id).show();
    }else{
        $("#aTitleE_"+id).hide();
        $("#aOptionE_"+id).hide();
        $("#aTitleD_"+id).show();
        $("#aOptionD_"+id).show();   
    }
}
function saveAlbumName(id){
    var aname=$.trim($("#txtTitle_"+id).val());
    if (aname.length>0){
        $("#btnEdit").attr("disabled","disabled");
        $.ajax({
            url: "/ajax/photo/editAlbum.ashx",
            type: "POST",
            dataType: "xml",
            data: {id:id,aname:aname , date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                    if ($(this).attr("flag")=="1"){
                        $("#aLink_"+id).text(aname);
                        editAName(false,id);
                    }
                    $("#btnEdit").attr("disabled","");
                });
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");
                $("#btnEdit").attr("disabled","");
            }
        });
    } else {
        alert("请输入相册标题");
    }
}
//about photo
function addFiles(id) {    
    var filename=$("#file_"+id).val();
    var files=$("input[type=file]");
    if (filename.toLowerCase().indexOf(".gif")<0&&filename.toLowerCase().indexOf(".jpg")<0){
        alert("您只能上传.gif和.jpg格式的图片文件！");
    }else{
        var arr=filename.split("\\");
        if (arr.length>1){
            filename=arr[arr.length-1];
        }
        $("#file_"+id).hide();
        $("#delete_"+id).removeClass("none");
        if (id>1){
            $("#delete_"+(id-1)).addClass("none");
        }
        $("#name_"+id).val(filename).show();
        if (filename.length>20){
            $("#title_"+id).val(filename.substr(0,20));
        }else{
            $("#title_"+id).val(filename);
        }        
        if (files.length<=5){
            $("#fileList").append($("#fileBlank").html().replaceAll("_id_", (id + 1)));
        }
    }
}
function deleteFiles(id){
    if (id<5){
        //删除li元素
        $("#fileList li").eq(id).remove();
    }
    $("#delete_"+id).addClass("none");
    $("#delete_"+(id-1)).removeClass("none");
    $("#name_"+id).val("").hide();
    $("#title_"+id).val("");        
    $("#file_"+id).val("").show();
}
function checkName(id){
    var title=$("#title_"+id).val();
    var reg = new regular();
    if (reg.isEmpty(title)) {
        $("#title_"+id).val($("#name_"+id).val());
    }
}
function uploadPhoto(){
    var count=$("#fileList li").length;
    if (count==1){
        alert("请至少选择上传一组相片！");
        return false;
    }
    return true;
}
function editPNamehs(flag){
    if (flag){
        $("#defaultTitle3").hide();
        $("#editTitle3").show();
    }else{
        $("#editTitle3").hide();
        $("#defaultTitle3").show();
    }
}
function ediths(id){
    var pname=$.trim($("#hsName").val());
    if (pname.length>0){
        $("#btnEdit").attr("disabled","disabled");
        $.ajax({
            url: "/ajax/blogs/ediths.ashx",
            type: "POST",
            dataType: "xml",
            data: {photoid:id,ptoName:pname, date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                    if ($(this).attr("flag")=="1"){
                        $("#h1DefaultName").text(pname);
                        $("#topicPhotoName").text(pname);
                        document.title=pname;
                        editPNamehs(false);
                    } 
                    $("#btnEdit").attr("disabled",""); 
                });
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");
                $("#btnEdit").attr("disabled","");
            }
        });
    }else{
        alert("请输入标题");
    }
}

function deletePhoto(id, albumid) {
    if (confirm("您确定要删除该相片吗？这是不可恢复的操作！")) {
        $.ajax({
            url: "/ajax/blogs/deletePhoto.ashx",
            type: "POST",
            dataType: "xml",
            data: { id: id, date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                    if ($(this).attr("flag") == "1") {
                        location.href = "photoList-" + albumid + ".aspx";
                    }
                });
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");
            }
        });
    }
}
function changPhoto(id,url){
    //$("#photoDetail").attr("src",url.replace("thum_","").replaceAll("'",""));
    //resize();
    location.href = "photo-" + id + ".aspx";
}
function resize(){    
    var img=$("#photoDetail");
    var width=0,height=0;    
    if (img.width()>500){
        width=500;
        height=(500*img.height())/img.width();
    }else{
        width=img.width();
        height=img.height();
    }
    $("#photoDetail").width(width).height(height);
}
//about favorites
function saveFavorites(blogid) {
    $.ajax({
        url: "/ajax/uc/ajax_addFavorites.ashx",
        type: "POST",
        dataType: "xml",
        data: { id: blogid, date: new Date() },
        success: function(xml) {
            $(xml).find("result").each(function() {
                alert($(this).text());
            });
        },
        error: function() {

            alert("文件载入错误，请稍后重试！");
        }
    });
}
function deleFavo(favid) {
    $.ajax({
        url: "/ajax/uc/ajax_deleFavorites.ashx",
        type: "POST",
        dataType: "xml",
        data: { id: favid, date: new Date() },
        success: function(xml) {
            $(xml).find("result").each(function() {
                alert($(this).text());
                if ($(this).attr("flag") == "1") {
                    location.href = location.href;
                }
            });
        },
        error: function() {
            alert("文件载入错误，请稍后重试！");
        }
    });
}
//about friend
function addFriend(friName) {
    if (friName.length > 0) {
        $("#btnFriend").attr("disabled", "disabled");
        $.ajax({
            url: "/ajax/uc/ajax_addFriend.ashx",
            type: "POST",
            dataType: "xml",
            data: { friName: friName, date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                });
                $("#btnFriend").attr("disabled", "");
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");
                $("#btnFriend").attr("disabled", "");
            }
        });
    } else {
        alert("请输入要添加的好友");
    }
}
function deleteFriend(friID) {
    if (confirm("您确定要删除该好友吗？这是不可恢复的操作！")) {
        $.ajax({
            url: "/ajax/uc/deleteFriend.ashx",
            type: "POST",
            dataType: "xml",
            data: { id: friID, date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                    if ($(this).attr("flag") == "1") {
                        location.href = location.href;
                    }
                });
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");
            }
        });
    }
}
//audit friend
function friAudit(friID) {
    //authorFriend.ashx
    $.ajax({
    url: "/ajax/uc/authorFriend.ashx",
        type: "POST",
        dataType: "xml",
        data: { id: friID, date: new Date() },
        success: function(xml) {
            $(xml).find("result").each(function() {
                alert($(this).text());
                if ($(this).attr("flag") == "1") {
                    location.href = location.href;
                }
            });
        },
        error: function() {
            alert("文件载入错误，请稍后重试！");
        }
    });
}
//about the reply
function checkReply() {
    var content = $.trim($("#replyContent").val());
    var reg = new regular();
    if (reg.isEmpty(content)) {
        alert("请输入评论内容！");
        return false;
    } else if (content.length > 100) {
        alert("评论内容请勿超过100字！");
        return false; 
    }
    return true;
}
function saveBlogReply(id) {
    if (checkReply()) {
        $("#btnReply").attr("disabled", "disabled");
        var content = $("#replyContent").val();
        $.ajax({
            url: "/ajax/blogs/saveBlogReply.ashx",
            type: "POST",
            dataType: "xml",
            data: { blogid: id, content: content, date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                    if ($(this).attr("flag") == "1") {
                        location.href = location.href;
                    }
                });
                $("#btnReply").attr("disabled", "");
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");
                $("#btnReply").attr("disabled", "");
            }
        });
    }
}
function deleteBlogReply(id) {
    if (confirm("您确定要删除该回复吗？")) {
        $.ajax({
            url: "/ajax/blogs/deleteBlogReply.ashx",
            type: "POST",
            dataType: "xml",
            data: { id: id, date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                    if ($(this).attr("flag") == "1") {
                        location.href = location.href;
                    }
                });
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");
            }
        });
    }
}
function savePhotoReply(id) {
    if (checkReply()) {
        $("#btnReply").attr("disabled", "disabled");
        var content = $("#replyContent").val();
        $.ajax({
            url: "/ajax/photo/savePhotoReply.ashx",
            type: "POST",
            dataType: "xml",
            data: { photoid: id, content: content, date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                    if ($(this).attr("flag") == "1") {
                        location.href = location.href;
                    }
                });
                $("#btnReply").attr("disabled", "");
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");
                $("#btnReply").attr("disabled", "");
            }
        });
    }
}
function deletePhotoReply(id) {
    if (confirm("您确定要删除该回复吗？")) {
        $.ajax({
            url: "/ajax/photo/deletePhotoReply.ashx",
            type: "POST",
            dataType: "xml",
            data: { id: id, date: new Date() },
            success: function(xml) {
                $(xml).find("result").each(function() {
                    alert($(this).text());
                    if ($(this).attr("flag") == "1") {
                        location.href = location.href;
                    }
                });                
            },
            error: function() {
                alert("文件载入错误，请稍后重试！");                
            }
        });
    }
}
