﻿var isMoveLoadding=false;
function ShowLoadding()
{
	var div = document.getElementById('divLoadding');
	if(div){}
	else
	{
		var body = document.getElementsByTagName('body').item(0);
        div = document.createElement('div');
        div.id = "divLoadding";
		div.innerHTML="正在执行，请稍候……";
		body.appendChild(div);
	}
	isMoveLoadding=true;
	moveLoadding();
}
function moveLoadding()
{
	var div = document.getElementById('divLoadding');
	if (isMoveLoadding){
        div.style.right = '0px';
        div.style.top = document.documentElement.scrollTop+'px';
		div.style.display = '';
        setTimeout('moveLoadding()', 300);
	}
}
function HideLoadding()
{
	isMoveLoadding=false;
	var div = document.getElementById('divLoadding');
	div.style.display = 'none';
}
function ClearErrorMsg()
{
	$("div.msg").each(function(){$(this).html('');$(this).hide();});
}
//给Checkbox提供全选功能
function SelectGroupBox(self, prefix)
{
	if(!prefix){prefix='';}
	if(self.name=='SelectAll'+prefix){
		var all = self.checked;
		$("input[name='SelectID"+prefix+"']").each(function(){$(this).attr('checked',all);});
	}else{
		var all = true;
		$("input[name='SelectID"+prefix+"']").each(function(){if(!$(this).attr('checked'))all=false;});
		$("input[name='SelectAll"+prefix+"']").attr('checked', all);
	}
}
//检查Checkbox至少要选中一条
function IsCheckedOne(frm,prefix)
{
	if(!prefix){prefix='';}
	var one = false;
	$("input[name='SelectID"+prefix+"']").each(function(){if($(this).attr('checked'))one=true;});
	return one;
}
function AjaxPost(url, frm, cnfirm, needchecked)
{
	ClearErrorMsg();
	var doit=true;
	if(needchecked==true)
	{
		if(!IsCheckedOne())
		{
			alert('请至少选择一条记录');
			doit = false;
			return;
		}
	}
	if(cnfirm==true)
	{
		if(!confirm('你确定要执行该操作？'))
		{
			doit=false;
		}
	}
	if(doit)
	{
		Ajax(url, frm, 'POST', ShowResult, true);
	}
}
function ShowResult(result)
{
	var msg = eval('(' + result + ')');
    if (msg.method != undefined) {
        switch (msg.method) {
            case "eval":
                eval(msg.data);
                break;
            case "alert":
                alert(msg.data);
                break;
            case "goto":
                window.location.href = msg.data;
                break;
            case "send_goto":
                var data = eval('(' + msg.data + ')');
                alert(data.message);
                window.location.href = data.url;
                break;
            case "reload":
                alert(msg.data);
                location.reload();
                break;
            case "error":
                var data = eval('(' + msg.data + ')');
                var bo = true;
                for (var o in data) {
                    $('#e_'+o).html(data[o]).show();
                }
                break;
        }
    }
}
function AjaxGet(url, func)
{
	Ajax(url, '', 'GET', func, true);
}
function AjaxLoad(url, div, func) {
	Ajax(url, div, 'LOAD', function(data){$('#'+div).html(data);if(func){func(data);}}, false);
}
function Ajax(url, param, type, func, showloadding)
{
	if(showloadding){ShowLoadding();}
    if (url.indexOf('?') == -1) {
        url += "?";
    }
    else
        url += "&";
	url += "timeStamp=" + new Date().getTime()

	if(type=='GET'||type=='LOAD')
	{
		$.ajax({ url: url,
			type: 'GET',
			cache: false,
			timeout: 20000,
			error: function() { if(param!=''){$('#param').html('数据加载失败');}else{alert('数据加载失败，可能是网络连接问题或者服务器错误。'); }},
			success: func,
			complete:function(){if(showloadding){HideLoadding();}}
		});
	}else if(type=='POST')
	{
		$('.submit').each(function(){$(this).attr('title',$(this).html());$(this).html('请稍候');$(this).attr('disabled','disabled');});
		$.ajax({ url: url,
			type: 'POST',
			data: $('#'+param).serialize(),
			cache: false,
			timeout: 20000,
			error: function() { alert('数据加载失败，可能是网络连接问题或者服务器错误。'); },
			success: func,
			complete:function(){if(showloadding){HideLoadding();}$('.submit').each(function(){$(this).html($(this).attr('title'));$(this).attr('disabled',false);});}
		});
	}
}

var oldDiv = new Object();
function TextOnfocus(obj) {
    if (oldDiv.id != null) {
        $(oldDiv.id).className = oldDiv.className;
    }
    var infoid = obj.id + "Info";
    var infoDiv = document.getElementById(infoid);
    oldDiv.id = infoDiv.id;
    oldDiv.className = infoDiv.className;
    infoDiv.className = "regon";
}
//弹出dialog
var dialogTitle;
function openWin(title, param,width,height) {
    if (document.getElementById('dialog')) {
        $("#dialog").dialog("destroy").remove();
    }
	if(height==undefined)
	{
		height='auto';
	}
    AjaxGet(param, function(t){
		var body = document.getElementsByTagName('body').item(0);
		var div = document.createElement('div');
		div.id = "dialog";
		div.innerHTML = t;
		body.appendChild(div);
		$("#dialog").dialog({ title: title, width: width == undefined ? 'auto' : width, height: height == undefined ? 'auto' : height,  resizable: false,bgiframe: false, modal: true });
	});
}

//创建loading层
function createLoading() {
    var a = createLoading.arguments;
    if (document.getElementById('loading')) {
        var div = document.getElementById('loading');
        div.style.display = 'block';
    } else {
        var head = document.getElementsByTagName('body').item(0);
        var div = document.createElement('div');
        div.id = "loading";
        div.style.top = '';
        div.style.left = '';
        var msg = '<img src="/util/images/loading.gif" />';
        if (a[1] != undefined) {
            msg = a[1];
        }
        div.innerHTML = '<div style="text-align:center;">'+ msg +'</div>';
        head.appendChild(div);
    }
}

//创建隐藏层

function ShowHideDiv(obj, url, title,id, classname) {
    if (!id)
        id = "HideDiv";
    if (!classname)
        classname = "hideDiv";
    var t = obj.offsetTop;
    var l = obj.offsetLeft;
    while (obj = obj.offsetParent) {
        l += obj.offsetLeft;
        t += obj.offsetTop;
    }
    l=l-50;
    t = t + 20;
    if (document.getElementById(id)) {
        var div = document.getElementById(id);
        div.style.display = 'block';
        div.style.top = t + "px";
        div.style.left = l + "px";
        AjaxLoad(url, 'HideDiv' + id);
    }
    else {
        var head = document.getElementsByTagName('body').item(0);
        var div = document.createElement('div');
        head.appendChild(div);
        div.id = id;
        div.className = 'flora';
        div.style.position = "absolute";
        div.style.top = t + "px";
        div.style.left = l + "px";
        div.style.display = '';
        div.style.background = "#FFF";
        //div.setAttribute("onmouseout", "CloseHideDiv('hidden', '" + id + "' )");
        var s = "<table><tr><td align='right'>" + title + "&nbsp;&nbsp;<a href='javascript:;' onclick=\"CloseHideDiv('hidden','" + id + "')\">关闭</a>";
        s += "</td></tr>";
        s += "<tr><td id='HideDiv" + id + "'>";
        s += "</td></tr>";
        s += "</table>";
        div.innerHTML = s;
        AjaxLoad(url, 'HideDiv'+ id);
    }

}
function CloseHideDiv(status,id) {
    var o = document.getElementById(id);
    if (status == "show") {
        o.style.display = '';
    }
    else {
        o.style.display = 'none';
    }
}

//点击添加文章关键字
function AddTagToTxt(tag)
{
	var spliter=' ';
	var tid='Tags';
	var txt = $('#'+tid).val();
	if(txt.length==0){
		$('#'+tid).val(tag);
	}else{
		txt = spliter+txt+spliter;
		if(txt.indexOf(spliter+tag+spliter)==-1)
		{
			$('#'+tid).val($('#'+tid).val()+spliter+tag);
		}else{
			txt = txt.replace(spliter+tag, '');
			txt += tag;
			txt = txt.replace(spliter, '');
			$('#'+tid).val(txt);
		}
	}
}
function ShowTags(id)
{
    if(id>0){
        var ts = tags[id].split(',');
        var tl = '';
        for(i=0;i<ts.length;i++)
            tl+='<a href="javascript:;" onclick="AddTagToTxt(\''+ts[i]+'\');">'+ts[i]+'</a>';
        $('#keywords').html(tl);
    }
}

//上传附件后将返回的文件名加入队列
function AddAttachment(id, name)
{
	var link='<li id="attLi'+id+'"><a class="closeIt" title="删除附件" href="javascript:;" onclick="RemoveAttachment('+id+')">&nbsp;</a><span class="fuj">'+name+'</span></li>';
	$('#attUl').prepend(link);
	$('#Attachments').append('<input type="hidden" id="AttachID'+id+'" name="AttachID" value="'+id+'" />');
}
function RemoveAttachment(id)
{
	$.get('/ajax/DeleteAttachment.ashx?id='+id+'&timeStamp=' + new Date().getTime(), null, function(){$('#attLi'+id).remove();$('#AttachID'+id).remove();});
}

var successShow=false;
function ShowEditSuccess()
{
if(!successShow){$('#header').after('<div id="msg-succ" class="msg-succ" style="display:none">修改成功</div>');successShow=true;}
$('#msg-succ').hide();$('#msg-succ').fadeIn('3000');
}

//===============================ctrl+enter
function isKeyTrigger(e, keyCode) {
    var argv = isKeyTrigger.arguments;
    var argc = isKeyTrigger.arguments.length;
    var bCtrl = false;
    if (argc > 2) {
        bCtrl = argv[2];
    }
    var bAlt = false;
    if (argc > 3) {
        bAlt = argv[3];
    }

    var nav4 = window.Event ? true : false;

    if (typeof e == 'undefined') {
        e = event;
    }

    if (bCtrl &&
        !((typeof e.ctrlKey != 'undefined') ?
            e.ctrlKey : e.modifiers & Event.CONTROL_MASK > 0)) {
        return false;
    }
    if (bAlt &&
        !((typeof e.altKey != 'undefined') ?
            e.altKey : e.modifiers & Event.ALT_MASK > 0)) {
        return false;
    }
    var whichCode = 0;
    if (nav4) whichCode = e.which;
    else if (e.type == "keypress" || e.type == "keydown")
        whichCode = e.keyCode;
    else whichCode = e.button;

    return (whichCode == keyCode);
}

function ctrlEnter(e) {
    var returnvalue = false;
    var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
    if (ie) {
        if (event.ctrlKey && window.event.keyCode == 13) {
            returnvalue = true;
        }
    } else {
        if (isKeyTrigger(e, 13, true)) {
            returnvalue = true;
        }
    }
    return returnvalue;
}

