//jquery 获取表单json ,为表单赋值扩展 
$.extend({
    setForm :function(frm,jsonValue) {   
    var obj=$(frm);  
    $.each(jsonValue, function (name, ival) {  
        var $oinput = obj.find("input[name=" + name + "]");  
		if ($oinput.attr("type")== "radio" || $oinput.attr("type")== "checkbox"){  
             $oinput.each(function(){  
                 if(Object.prototype.toString.apply(ival) == '[object Array]'){// 是复选框，并且是数组
                      for(var i=0;i<ival.length;i++){  
                          if($(this).val()==ival[i])  
                             $(this).attr("checked", "checked");  
                      }  
                 }else{  
                     if($(this).val()==ival)  
                        $(this).attr("checked", "checked");  
                 }  
             });  
        }else{
             obj.find("[name="+name+"]").val(ival);   
        } 
    });
},
getFormJson:function(frm) {
        var o = {};
        var a = $(frm).serializeArray();
        $.each(a, function() {
            if (this.name == "password") {
                //this.value = $.md5(this.value) //md5操作 
　　　　　　　　　　this.value=this.value;
            }
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [ o[this.name] ];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
}
});

com={};
com.initMirror=function(height){
	editor_1 = CodeMirror.fromTextArea($("#s_luascript")[0], {
		theme:"solarized light",
		lineNumbers: true,
		indentUnit:4,
		smartIndent:true,
		indentWithTabs:true,
		extraKeys:{
		    "F11": function(cm) {
		        cm.setOption("fullScreen", !cm.getOption("fullScreen"));
		    },
			"F9":function(cm) {
				var str_tab = "\t".repeat(cm.getCursor().ch);
				var _keyWord = "something";
				cm.replaceSelection(`if true then\n${str_tab}\treturn ${_keyWord}\n${str_tab}end`);
			},
		    "Esc": function(cm) {
		        if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
		    }
		}
	});
	if(height){
		editor_1.setSize("auto",height);
	}else{
		editor_1.setSize("auto","520");
	}
}
com.serializeArray=function(id){
	var serializeArray={};
	$("#"+id+" [name]").each(function(){
		var name=$(this).prop("name");
		var value=$(this).prop("value");
		if($(this).get(0).tagName=="TEXTAREA" && $(this).attr("id")){
			if($(this).attr("id").indexOf("s_luascript")>-1){
				value=editor_1.getValue()
			}
		}else{
			value=$(this).prop("value");
			value=value?value:"";
		}
		serializeArray[name]=value.trim();
	});
	return serializeArray
}
com.setExpert=function(editor,text){
	text=text.substr(text.indexOf("func"));
	eval(editor+".setValue("+JSON.stringify(text, null, "\t")+")")
}

com.pushArry=function(arr1,arr2){
	for(i=0,len=arr2.length;i<len;i++){
	  if($.inArray(arr2[i],arr1)<0){
	    arr1.push(arr2[i]);
	  }
	}
	return arr1
}

com.maskShow=function(title){
	var change=function(){
		if(title){
			$("#maskTitle").text(title);
		}else{
			$("#maskTitle").text("加载中");
		}
		var width=$(window).width();
		var height=$("body").height();
		$(".mask>.box").css("left",(width-width*0.3)/2);
		$(".mask").removeClass("hide").css("height",height);
	}
	$(window).resize(function(){
		change();
	});
	change();
}
com.maskHide=function(){
	$(".mask").addClass("hide");
	$(window).unbind("resize");
}
com.trimStr = function (str) {
	str = str.replace(/^(\s|\u00A0)+/,'');
	str = str.replace(/\s/g,'');
	for(var i=str.length-1; i>=0; i--){
		if(/\S/.test(str.charAt(i))){
			str = str.substring(0, i+1);
			break;
		}
	}
	return str;
}
com.setCookie = function(name,value){
	var expdate = new Date();   //初始化时间
	expdate.setTime(expdate.getTime() + 30 * 60 * 1000);   //时间
	document.cookie = name+"="+value+";expires="+expdate.toGMTString()+";path=/";
}
com.getCookie = function (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 deleteArrayVal(arrTobeDel, indexItem) {
	//索引集合里面必须全部为‘number’
	for (var i = 0; i < indexItem.length; ++i) {
		if (typeof indexItem[i] != 'number') throw new TypeError(`index is not a number`);
	}

	//避免后面的操作改变传入数组的值
	var newArrItem = JSON.parse(JSON.stringify(arrTobeDel)),
		newIndexItem = JSON.parse(JSON.stringify(indexItem));
	//将传入的数组按照倒序排列
	newIndexItem.sort((a, b) => b - a);

	for (var j = 0; j < newIndexItem.length; j++) {
		newArrItem.splice(newIndexItem[j], 1);
	}

	return newArrItem;
}

Date.prototype.format=function(format){var o={"M+":this.getMonth()+1,"d+":this.getDate(),"h+":this.getHours(),"m+":this.getMinutes(),"s+":this.getSeconds(),"q+":Math.floor((this.getMonth()+3)/3),"S":this.getMilliseconds()};if(/(y+)/.test(format)){format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length))}for(var k in o){if(new RegExp("("+k+")").test(format)){format=format.replace(RegExp.$1,RegExp.$1.length==1?o[k]:("00"+o[k]).substr((""+o[k]).length))}}return format};function getDate(l){var date=new Date(l);pattern="yyyy-MM-dd hh:mm:ss";return date.format(pattern)};