/*!
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 * 
 * 通用公共方法
 * @author jeeplus
 * @version 2014-4-29
 */
$(document).ready(function() {
	try{
		// 链接去掉虚框
		$("a").bind("focus",function() {
			if(this.blur) {this.blur()};
		});
		//所有下拉框使用select2
		$("select").select2();
	}catch(e){
		// blank
	}
});

// 引入js和css文件
function include(id, path, file){
	if (document.getElementById(id)==null){
        var files = typeof file == "string" ? [file] : file;
        for (var i = 0; i < files.length; i++){
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + path + name + "'";
            document.write("<" + tag + (i==0?" id="+id:"") + attr + link + "></" + tag + ">");
        }
	}
}

// 获取URL地址参数
function getQueryString(name, url) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    if (!url || url == ""){
	    url = window.location.search;
    }else{	
    	url = url.substring(url.indexOf("?"));
    }
    r = url.substr(1).match(reg)
    if (r != null) return unescape(r[2]); return null;
}

//获取字典标签
function getDictLabel(data, value, defaultValue){
	for (var i=0; i<data.length; i++){
		var row = data[i];
		if (row.value == value){
			return row.label;
		}
	}
	return defaultValue;
}

// 打开一个窗体
function windowOpen(url, name, width, height){
	var top=parseInt((window.screen.height-height)/2,10),left=parseInt((window.screen.width-width)/2,10),
		options="location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,"+
		"resizable=yes,scrollbars=yes,"+"width="+width+",height="+height+",top="+top+",left="+left;
	window.open(url ,name , options);
}

// 恢复提示框显示
function resetTip(){
	top.$.jBox.tip.mess = null;
}

// 关闭提示框
function closeTip(){
	top.$.jBox.closeTip();
}

//显示提示框
function showTip(mess, type, timeout, lazytime){
	resetTip();
	setTimeout(function(){
		top.$.jBox.tip(mess, (type == undefined || type == '' ? 'info' : type), {opacity:0, 
			timeout:  timeout == undefined ? 2000 : timeout});
	}, lazytime == undefined ? 500 : lazytime);
}

// 显示加载框
function loading(mess){
	if (mess == undefined || mess == ""){
		mess = "正在提交，请稍等...";
	}
	resetTip();
	top.$.jBox.tip(mess,'loading',{opacity:0});
}

// 警告对话框
function alertx(mess, closed){
	top.$.jBox.info(mess, '提示', {closed:function(){
		if (typeof closed == 'function') {
			closed();
		}
	}});
	top.$('.jbox-body .jbox-icon').css('top','55px');
}

// 确认对话框
function confirmx(mess, href, closed){
	
	top.layer.confirm(mess, {icon: 3, title:'系统提示'}, function(index){
	    //do something
		if (typeof href == 'function') {
			href();
		}else{
			resetTip(); //loading();
			location = href;
		}
	    top.layer.close(index);
	});
	
//	top.$.jBox.confirm(mess,'系统提示',function(v,h,f){
//		if(v=='ok'){
//			if (typeof href == 'function') {
//				href();
//			}else{
//				resetTip(); //loading();
//				location = href;
//			}
//		}
//	},{buttonsFocus:1, closed:function(){
//		if (typeof closed == 'function') {
//			closed();
//		}
//	}});
//	top.$('.jbox-body .jbox-icon').css('top','55px');
	return false;
}

// 提示输入对话框
function promptx(title,  href){

	 var index = top.layer.prompt({title: title, formType: 2}, function(text){
		 if (typeof href == 'function') {
				href();
			}else{
				resetTip(); //loading();
				location = href + encodeURIComponent(text);
			}
		 
		 top.layer.close(index);
		  });
	return false;
}


// cookie操作
function cookie(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
}

// 数值前补零
function pad(num, n) {
    var len = num.toString().length;
    while(len < n) {
        num = "0" + num;
        len++;
    }
    return num;
}

// 转换为日期
function strToDate(date){
	return new Date(date.replace(/-/g,"/"));
}

// 日期加减
function addDate(date, dadd){  
	date = date.valueOf();
	date = date + dadd * 24 * 60 * 60 * 1000;
	return new Date(date);  
}

//截取字符串，区别汉字和英文
function abbr(name, maxLength){  
 if(!maxLength){  
     maxLength = 20;  
 }  
 if(name==null||name.length<1){  
     return "";  
 }  
 var w = 0;//字符串长度，一个汉字长度为2   
 var s = 0;//汉字个数   
 var p = false;//判断字符串当前循环的前一个字符是否为汉字   
 var b = false;//判断字符串当前循环的字符是否为汉字   
 var nameSub;  
 for (var i=0; i<name.length; i++) {  
    if(i>1 && b==false){  
         p = false;  
    }  
    if(i>1 && b==true){  
         p = true;  
    }  
    var c = name.charCodeAt(i);  
    //单字节加1   
    if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {  
         w++;  
         b = false;  
    }else {  
         w+=2;  
         s++;  
         b = true;  
    }  
    if(w>maxLength && i<=name.length-1){  
         if(b==true && p==true){  
             nameSub = name.substring(0,i-2)+"...";  
         }  
         if(b==false && p==false){  
             nameSub = name.substring(0,i-3)+"...";  
         }  
         if(b==true && p==false){  
             nameSub = name.substring(0,i-2)+"...";  
         }  
         if(p==true){  
             nameSub = name.substring(0,i-2)+"...";  
         }  
         break;  
    }  
 }  
 if(w<=maxLength){  
     return name;  
 }  
 return nameSub;  
}


//打开对话框(添加修改)
function openDialog(title,url,width,height,target){
	
	if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端，就使用自适应大小弹窗
		width='auto';
		height='auto';
	}else{//如果是PC端，根据用户设置的width和height显示。
	
	}
	
	top.layer.open({
	    type: 2,  
	    area: [width, height],
	    title: title,
        maxmin: true, //开启最大化最小化按钮
	    content: url ,
        skin: 'two-btns',
	    btn: ['提交', '关闭'],
	    yes: function(index, layero){
	    	 var body = top.layer.getChildFrame('body', index);
	         var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
	         var inputForm = body.find('#inputForm');
	         var top_iframe;
	         if(target){
	        	 top_iframe = target;//如果指定了iframe，则在改frame中跳转
	         }else{
	        	 top_iframe = top.getActiveTab().attr("name");//获取当前active的tab的iframe 
	         }
	         inputForm.attr("target",top_iframe);//表单提交成功后，从服务器返回的url在当前tab中展示
	         
	        if(iframeWin.contentWindow.doSubmit() ){
	        	// top.layer.close(index);//关闭对话框。
	        	  setTimeout(function(){top.layer.close(index)}, 100);//延时0.1秒，对应360 7.1版本bug
	         }
			
		  },
		  cancel: function(index){ 
	       }
	}); 	
	
}

//打开对话框(查看)
function openDialogView(title,url,width,height){
	
	
	if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端，就使用自适应大小弹窗
		width='auto';
		height='auto';
	}else{//如果是PC端，根据用户设置的width和height显示。
	
	}
	top.layer.open({
	    type: 2,
        skin: 'one-btn',
	    area: [width, height],
	    title: title,
        maxmin: true, //开启最大化最小化按钮
	    content: url ,
	    btn: ['关闭'],
	    cancel: function(index){ 
	       }
	}); 
	
}

function search(){//查询，页码清零
	$("#pageNo").val(0);
	$("#searchForm").submit();
		return false;
}
function resetSearch(){//重置，页码清零
    $("#pageNo").val(0);
    $("#searchForm div.query input").val("");
    $("#searchForm div.query select").val("");
    $("#searchForm").submit();
    return false;
}

function reset(){//重置，页码清零
	$("#pageNo").val(0);
	$("#searchForm div.form-group input").val("");
	$("#searchForm div.form-group select").val("");
	$("#searchForm").submit();
		return false;
	 }
function sortOrRefresh(){//刷新或者排序，页码不清零
	
	$("#searchForm").submit();
		return false;
}
function page(n,s){//翻页
	$("#pageNo").val(n);
	$("#pageSize").val(s);
	$("#searchForm").submit();
	$("span.page-size").text(s);
	return false;
}
function resizeFormWindow(){
    var winH = $(window).height();
    $(".single-form").css('min-height',(winH - 8 - 1) +'px');//这里的div，选择你的那个div
}

function resizeListWindow1(){
    var winH = $(window).height();
    var updiv= $("#queryDiv").height();
    $(".contentDetails").css('min-height',(winH - updiv - 16 - 1) +'px');//这里的div，选择你的那个div
}
function resizeListWindow2(){
    var winH = $(window).height();
    var updiv1= $("#tabDiv").height();
    var updiv2= $("#queryDiv").height();
    $(".contentDetails").css('min-height',(winH - updiv1 - updiv2 - 8 - 1) +'px');//这里的div，选择你的那个div
}
function resizeListWindow3(){
    var winH = $(window).height();
    var updiv= $("#queryDiv").height();
    $(".contentDetails").css('min-height',(winH - updiv - 8 - 1) +'px');//这里的div，选择你的那个div
}
function resizeListWindow4(){
    var winH = $(window).height();
    var updiv1= $("#tabDiv").height();
    var updiv= $("#queryDiv").height();
    $(".contentDetails").css('min-height',(winH - updiv1 - updiv - 1) +'px');//这里的div，选择你的那个div
}
function getAuditContractState(id)
{
    var result ={};
    result.action = true;
	switch(id)
    {
        case "1":result.label = "tempstore";result.status="暂存";result.action = false;break;
        case "2":result.label = "auditing";result.status="审批中";break;
        case "3":result.label = "cancel";result.status="撤回";break;
        case "4":result.label = "reject";result.status="驳回";break;
        case "5":result.label = "signed";result.status="已签";break;
        case "6":result.label = "discarding";result.status="作废中";break;
        case "7":result.label = "discarded";result.status="已作废";break;
        case "8":result.label = "changing";result.status="变更中";break;
        case "9":result.label = "changing";result.status="完成中";break;
        case "10":result.label = "signed";result.status="已完成";break;
        case "11":result.label = "changing";result.status="签章中";break;
        case "12":result.label = "changing";result.status="拒签";break;
        case "14":result.label = "changing";result.status="归档中";break;

        default:
            result.label = "unknown";result.status="未知";break;
    }
    return result;
}
function getAuditState(id)
{
    var result ={};
    result.action = true;
    switch(id)
    {
        case "1":result.label = "tempstore";result.status="暂存";result.action = false;break;
        case "2":result.label = "auditing";result.status="审批中";break;
        case "3":result.label = "cancel";result.status="撤回";break;
        case "4":result.label = "reject";result.status="驳回";break;
        case "5":result.label = "signed";result.status="已签";break;
        case "6":result.label = "discarding";result.status="作废中";break;
        case "7":result.label = "discarded";result.status="已作废";break;
        case "8":result.label = "changing";result.status="变更中";break;
        default:
            result.label = "unknown";result.status="未知";break;
    }
    return result;
}
function getAuditRecordState(id)
{
    var result ={};
    result.action = true;
    switch(id)
    {
        case "1":result.label = "tempstore";result.status="暂存"; result.action = false;break;
        case "2":result.label = "auditing";result.status="审批中";break;
        case "3":result.label = "cancel";result.status="撤回";break;
        case "4":result.label = "reject";result.status="驳回";break;
        case "5":result.label = "signed";result.status="已归档";break;
        default:
            result.label = "unknown";result.status="未知";break;
    }
    return result;
}

function getAuditChangeState(id)
{
    var result ={};
    result.action = true;
    switch(id)
    {
        case "1":result.label = "tempstore";result.status="暂存"; result.action = false;break;
        case "2":result.label = "changing";result.status="变更中";break;
        case "3":result.label = "cancel";result.status="撤回";break;
        case "4":result.label = "reject";result.status="驳回";break;
        case "5":result.label = "changed";result.status="已变更";break;
        default:
            result.label = "unknown";result.status="未知";break;
    }
    return result;
}
function getAuditInvalidState(id)
{
    var result ={};
    result.action = true;
    switch(id)
    {
        case "1":result.label = "tempstore";result.status="暂存"; result.action = false;break;
        case "2":result.label = "discarding";result.status="作废中";break;
        case "3":result.label = "cancel";result.status="撤回";break;
        case "4":result.label = "reject";result.status="驳回";break;
        case "5":result.label = "discarded";result.status="已作废";break;
        default:
            result.label = "unknown";result.status="未知";break;
    }
    return result;
}

function getAuditLeaveState(id)
{
    var result ={};
    result.action = true;
    switch(id)
    {
        case "1":result.label = "tempstore";result.status="暂存";result.action = false;break;
        case "2":result.label = "auditing";result.status="审批中";break;
        case "3":result.label = "cancel";result.status="撤回";break;
        case "4":result.label = "reject";result.status="驳回";break;
        case "5":result.label = "signed";result.status="已签";break;
        case "6":result.label = "auditing";result.status="销假中";break;
        case "7":result.label = "cancel";result.status="销假撤回";break;
        case "9":result.label = "reject";result.status="销假驳回";break;
        case "10":result.label = "auditing";result.status="续假中";break;
        case "11":result.label = "cancel";result.status="续假撤回";break;
        case "12":result.label = "reject";result.status="续假驳回";break;
        case "8":result.label = "changing";result.status="完成";break;
        default:
            result.label = "unknown";result.status="未知";break;
    }
    return result;
}
/*消除list页面table出现的滚动条,默认二个按钮，130px*/
function resizeListTable(width){
    var fwidth = width || 130;
    fwidth = fwidth - 6;
    if(document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight))
    {
        var tb1 = document.getElementById("changewidth");
        tb1.innerHTML = tb1.innerHTML + "<style>.laytable-cell-1-op{ width: " + fwidth + "px;}</style>";
    }
}
function addLinkVisied(){
    $(this).addClass("a_visited");
}
function checkContentKeyUp(id,maxLength) {
    var content = document.getElementById(id);
    var length = content.value.replace(/^(\s|\u00A0)+/,'').replace(/(\s|\u00A0)+$/,'').length;
    var contentMsg = document.getElementById('contentMsg');
    contentMsg.innerHTML = "您已输入<font >" + length
        + "</font>字符，还可输入<font color='red'>" + (maxLength - length) + "</font>字符";
}