/**
 * ajax提交
 * @param urls
 * @return obj
 */
function ajaxgetresult(urls) {
	var result;
	$.ajax({
		type : "post",
		url : urls,
		async : false,
		success : function(msg) {
			try {
				result = eval('( ' + msg + ' )');
			} catch (e) {
				alert("Connection error");
			}
		}
	});
	return result;
}
function ajaxgetresultpost(urls,data) {
	var result;
	$.ajax({
		type : "post",
		url : urls,
		data:data,
		async : false,
		success : function(msg) {
			try {
				result = eval('( ' + msg + ' )');
			} catch (e) {
				alert("Connection error");
			}
		}
	});
	return result;
}
function ajaxgetresults(urls) {
	var result;
	$.ajax({
		type : "post",
		url : urls,
		async : false,
		success : function(msg) {
			try {
				result = msg
			} catch (e) {
				alert("Connection error");
			}
		}
	});
	return result;
}
/**
 *  ajaxform提交
 * @param formid
 * @param  urls
 * @return obj
 */
function ajaxsubmitform(formid, urls) {
	var result;
	$.ajax({
		cache : true,
		type : "POST",
		url : urls,
		data : $('#' + formid).serialize(),
		async : false,
		error : function(request) {
			alert("Connection error");
		},
		success : function(data) {
			try {
				result = eval('( ' + data + ' )');
			} catch (e) {
				// alert(e.name + ": " + e.message);
			}
		}
	});
	return result;
}
function ajaxsubmitform2(formid, urls) {
	var result;
	$.ajax({
		cache : true,
		type : "POST",
		url : urls,
		data : $('#' + formid).serialize(),
		async : false,
		error : function(request) {
			alert("操作失败");
		},
		success : function(data) {
			try {
				result = data;
			} catch (e) {
				alert("操作失败");
			}
		}
	});
	return result;
}

/**
 * 返回附件的名称供下载 
 * @param filepath:加密后的文件的地址
 */
function getfjname(filepath) {
	if (filepath == "")
		return;
	var address = upescapeparam(filepath);
	var name = address.substring(address.lastIndexOf('\\') + 1, address.length);
	var temp = '<a href="javascript:void(0)" style="font-size: 13px;" title="点击下载" onclick="downFile(\''
			+ filepath + '\')">' + name + '</a>';
	return temp;
}

/**
 * 下载模板
 */
function downMb(fn) {
	fn = escapeparam(fn);
	var urls = $("#paths").val() + "/order/downPlate.tag?fn=" + fn + "&mn="
			+ new Date().getTime();
	$.post(urls, function(result) {
		if (result == "0") {
			$.messager.alert('提示', '文件已丢失!');
		} else {
			location.href = urls;
		}
	});
}

/**
 * 参数加密
 * @param value
 * @return string
 */
function escapeparam(value) {
	return escape(escape(value));
}

/**
 * 参数解密
 * @param value
 * @return string
 */
function upescapeparam(value) {
	return unescape(unescape(value));
}

/**
 * 返回编辑器控件
 * @return array
 */
function returneditorstyle() {
	var result = [ 'fontname', 'fontsize', 'forecolor', 'hilitecolor', 'bold',
			'italic', 'underline', '|', 'justifyleft', 'justifycenter',
			'justifyright', 'insertorderedlist', 'indent', 'outdent',
			'insertunorderedlist', '|', 'superscript', 'subscript', 'link',
			'unlink', 'table', 'hr', 'lineheight', '|', 'removeformat',
			'clearhtml', 'quickformat', 'wordpaste', 'plainpaste', 'emoticons',
			'print', 'image', 'fullscreen' ]
	return result;
}
/**
 * 将多个编辑器的内容设置到原来的textarea控件里
 */
function editorsync() {
	var kes = KindEditor.instances;
	for (var i = 0; i < kes.length; i++) {
		kes[i].sync();
	}
}

/**
 * 创建编辑器 ids:编辑器的id,多个id,用","分割
 * @param  ids
 */
function createEditor(ids) {
	var path = $("#paths").val();
	removeKindEditor(ids);
	KindEditor.create(ids, {
		resizeType : 1,
		allowPreviewEmoticons : false,
		allowImageUpload : true,
		uploadJson : path + '/kindeditor/upload_json.jsp',
		fileManagerJson : path + '/kindeditor/file_manager_json.jsp',
		newlineTag : "br",
		items : returneditorstyle()
	});
}

/**
 * 移除编辑器
 * @param ids:编辑器ids,多个编辑器用","分割
 */
function removeKindEditor(ids) {
	var nids = ids.split(',');
	for (var i = 0; i < nids.length; i++) {
		KindEditor.html(nids[i], '');
		KindEditor.remove(nids[i]);
	}
}

/**
 * 判断上传的文件 
 * @param id:fileid
 * @param parma:文件格式,多个文件格式用","分割
 * @return true or false
 */
function pdupfile(id, param) {
	var temp = false;
	var value = $("#" + id).val();
	if (value != "") {
		param = param.split(",");
		var hzm = /\.[^\.]+$/.exec(value);
		for (var i = 0; i < param.length; i++) {
			if (hzm == ('.' + param[i])) {
				temp = true;
				break;
			}
		}
	}

	return temp;
}

/**
 * 显示图表
 * @param xmlname:xml文件名称
 * @param divid:图片显示的位置
 * @param xmlname:xml文件的路径
 */
function show_tb(xmlname, divid, xmlpath) {
	var path = document.getElementById("paths").value;
	AnyChart.swfFile = path + "/anychart/AnyChart.swf";
	var chart = new AnyChart();
	chart.width = "100%";
	chart.height = "100%";
	chart.wMode = "opaque";
	if (xmlpath != undefined && xmlpath != "undefined") {
		chart.setXMLFile(xmlpath);
	} else {
		chart.setXMLFile(path + "/files/" + xmlname);
	}
	chart.write(divid);
}

/**
 * 打开窗体
 * @param str:窗体id
 */
function open1(str) {
	setwindow(str);
	$('#' + str).window('open');
}
/**
 * 关闭窗体
 * @param str:窗体id
 */
function close1(strs) {
	$('#' + strs).window('close');
}

/**
 *设置窗体
 * @param str:窗体id
 */
function setwindow(str) {
	$('#' + str).window({
		modal : true,
		draggable : true,
		shadow : true
	});
}
/**
 *重置表单
 * @param id:formid
 */
function resetForm(id) {
	$('#' + id).form('reset');
}

/**
 *  添加tab页
 * @param title:标题
 * @param href:地址
 * @param iconCls:tab页的图标
 */
function addTabPage(title, href, iconCls) {
	parent.addTab(title, href, iconCls);
}

/**
 *  关闭tab页
 * @param title:标题
 */
function closeTabPage(title) {
	parent.closeTab(title);
}
//主页面关闭当前tab页
function closeTabThisPage() {
	var tab = parent.$('#tabs').tabs('getSelected');
	var tabIndex = parent.$('#tabs').tabs('getTabIndex', tab);
	closeTabPage(tabIndex);
}

/**
 *  得到comboxvalue
 * @param id:combox的id
 */
function getcomboxVal(id) {
	return $('#' + id).combobox('getValue');
}

/**
 * 改变json对象的属性名
 * @param newName
 * @param  obj
 */
function changeJsonAttr(newName, obj) {
	for ( var key in obj) {
		obj[newName + key] = obj[key]
		delete obj[key];
	}
}

/**
 * 将json对象打印到页面上去
 * @param id
 * @param obj
 */
function printJson(id, obj) {
	$("#" + id).each(function() {
		for ( var key in obj) {
			$("#" + key).html(obj[key]);
		}
	});
}

/**
 * 获得checkboxtree选中的节点ids
 * @param obj
 * @return 选中的节点ids
 */
function getTreeids(obj) {
	var nodes = obj.tree('getChecked');
	var s = '';
	for (var i = 0; i < nodes.length; i++) {
		if (s != '')
			s += ':';
		s += nodes[i].id;
	}
	return s;
}
/**
 * 获得checkboxtree选中的节点text
 * @param  obj
 * @return 选中的节点text
 */
function getTreetext(obj) {
	var nodes = obj.tree('getChecked');
	var s = '';
	for (var i = 0; i < nodes.length; i++) {
		if (s != '')
			s += ',';
		s += nodes[i].text;
	}
	return s;
}
//按数字类型排序 自定义排序
function sort_int(a, b) {
	if (a.length > b.length)
		return 1;
	else if (a.length < b.length)
		return -1;
	else if (a > b)
		return 1;
	else
		return -1;
}
//删除html代码
function removeHtml(s) {
	return (s) ? jQuery("<p>").append(s).text() : "";
}
//判断电话是否合法
function isphone(phone) {
	var myreg = /^(13[0-9]|15[012356789]|17[36780]|18[0-9]|14[57])[0-9]{8}$/;
	if (myreg.test(phone)) {
		return true;
	} else {
		return false;
	}
}
//设置面板的标题和图标
function setPanelTextIco(panelId, title, iconCls) {
	if (null == panelId || panelId == '') {
		return;
	}
	var opts = {};
	if (null != title && title != '') {
		opts['title'] = title;
	}
	if (null != iconCls && iconCls != '') {
		opts['iconCls'] = iconCls;
	}
	$('#' + panelId).panel(opts);
}
function getRootPath() {
	//获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
	var curWwwPath = window.document.location.href;
	//获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	//获取主机地址，如： http://localhost:8083
	var localhostPaht = curWwwPath.substring(0, pos);
	//获取带"/"的项目名，如：/uimcardprj
	var projectName = pathName
			.substring(0, pathName.substr(1).indexOf('/') + 1);
	return (localhostPaht + projectName);
}
//绑定下拉框
function getCombobox(selectid, url, seld) {
	var path = $("#paths").val();
	var msg = ajaxgetresult(url);//下拉框不采用异步加载， 否则读取不到默认值
	if (msg.rows.length <= 0) {
		msg.rows[0] = {
			"id" : "",
			"text" : "--请选择--"
		};
	} else {
		msg.rows.splice(0, 0, {
			"id" : "",
			"text" : "--请选择--"
		});
	}
	var arr = [];
	for (var i = 0; i < msg.rows.length; i++) {
		if (null != seld && "" != seld && msg.rows[i].id == seld) {
			msg.rows[i]["selected"] = true;
		}
	}
	if (typeof selectid == 'string') {
		$('#' + selectid).combobox({
			data : msg.rows,
			editable : false,
			valueField : 'id',
			textField : 'text'
		});
	} else {
		for (var i = 0; i < selectid.length; i++) {
			$('#' + selectid[i]).combobox({
				data : msg.rows,
				editable : false,
				valueField : 'id',
				textField : 'text'
			});
		}
	}
}

function getComboboxs(selectid, url, seld) {
	var path = $("#paths").val();

	var msg = ajaxgetresult(url);//下拉框不采用异步加载， 否则读取不到默认值
	if (msg.rows.length <= 0) {
		msg.rows[0] = {
			"id" : "",
			"text" : "--请选择--"
		};
	} else {
		msg.rows.splice(0, 0, {
			"id" : "",
			"text" : "--请选择--"
		});
	}
	var arr = [];
	for (var i = 0; i < msg.rows.length; i++) {
		if (null != seld && "" != seld && msg.rows[i].id == seld) {
			msg.rows[i]["selected"] = true;
		}
	}
	if (typeof selectid == 'string') {
		$('#' + selectid)
				.combobox(
						{
							data : msg.rows,
							editable : false,
							valueField : 'id',
							textField : 'text',
							onSelect : function(rec) {
								if (rec.id == '') {
									$("#ordercode").val("");
								} else {
									var urlscode = path
											+ '/basics/getBxcompanyCode.tag?bxcompanykey='
											+ rec.id + '&tim'
											+ new Date().getTime();
									var msgs = ajaxgetresults(urlscode);
									$("#ordercode").val(msgs);
								}
								//alert(msgs);
							}
						});
	} else {
		for (var i = 0; i < selectid.length; i++) {
			$('#' + selectid[i])
					.combobox(
							{
								data : msg.rows,
								editable : false,
								valueField : 'id',
								textField : 'text',
								onSelect : function(rec) {
									//获取简码并重新生成订单号
									if (rec.id == '') {
										$("#ordercode").val("");
									} else {
										var urlscode = path
												+ '/basics/getBxcompanyCode.tag?bxcompanykey='
												+ rec.id + '&tim'
												+ new Date().getTime();
										var msgs = ajaxgetresults(urlscode);
										$("#ordercode").val(msgs);
									}

								}
							});
		}
	}
}
//绑定单选
function getSingleRadio(selectid, seld) {
	jQuery("input[type='radio'][name='"+selectid+"'][value='"+seld+"']").attr("checked", "checked");
}
//绑定是否下拉框
function getSingleCombobox(selectid, seld) {
	var msg = [ {
		"id" : "",
		"text" : "--请选择--"
	}, {
		"id" : "0",
		"text" : "是"
	}, {
		"id" : "1",
		"text" : "否"
	} ];
	var selds = "s" + seld;
	var arr = [];
	if (selds == "s0") {
		msg[1]["selected"] = true;
	} else if (selds == "s1") {
		msg[2]["selected"] = true;
	} else {
		msg[0]["selected"] = true;
	}
	//	var flag=false;
	//	for(var i=0;i<msg.length;i++){
	//		
	//		if( null!=seld && ""!=seld && msg[i].id==seld){
	//			alert("getSingleCombobox");
	//			alert("selectid"+selectid+" seld:"+seld);
	//			msg[i]["selected"]=true;
	//			flag=true;
	//		}
	//	}
	//	if(!flag){
	//		alert("flag");
	//		msg[0]["selected"]=true;
	//	}
	if (typeof selectid == 'string') {
		$('#' + selectid).combobox({
			data : msg,
			editable : false,
			valueField : 'id',
			textField : 'text'
		});
	} else {
		for (var i = 0; i < selectid.length; i++) {
			$('#' + selectid[i]).combobox({
				data : msg,
				editable : false,
				valueField : 'id',
				textField : 'text'
			});
		}
	}

}
//绑定是否下拉框
function getSingleNoBlankCombobox(selectid, seld) {
	var msg = [{
		"id" : "0",
		"text" : "是"
	}, {
		"id" : "1",
		"text" : "否"
	} ];

	
	if (seld == "0") {
		msg[0]["selected"] = true;
	} else if (seld == "1") {
		msg[1]["selected"] = true;
	} else {
		msg[1]["selected"] = true;
	}
	//	var flag=false;
	//	for(var i=0;i<msg.length;i++){
	//		
	//		if( null!=seld && ""!=seld && msg[i].id==seld){
	//			alert("getSingleCombobox");
	//			alert("selectid"+selectid+" seld:"+seld);
	//			msg[i]["selected"]=true;
	//			flag=true;
	//		}
	//	}
	//	if(!flag){
	//		alert("flag");
	//		msg[0]["selected"]=true;
	//	}
	if (typeof selectid == 'string') {
		$('#' + selectid).combobox({
			data : msg,
			editable : false,
			valueField : 'id',
			textField : 'text'
		});
	} else {
		for (var i = 0; i < selectid.length; i++) {
			$('#' + selectid[i]).combobox({
				data : msg,
				editable : false,
				valueField : 'id',
				textField : 'text'
			});
		}
	}

}

function getOrderState(state) {
	var str = "";
	if ("0" == state) {
		str = "初始值";
	} else if ("5" == state) {
		str = "待取车预约";
	} else if ("10" == state) {
		str = "取车预约失败";
	} else if ("15" == state) {
		str = "待取车派单";
	} else if ("20" == state) {
		str = "待取车邀约";
	} else if ("25" == state) {
		str = "取车邀约成功";
	} else if ("30" == state) {
		str = "取车邀约失败";
	} else if ("31" == state) {
		str = "待取车";
	} else if ("32" == state) {
		str = "取车条件不符";
	} else if ("33" == state) {
		str = "待送修";
	} else if ("34" == state) {
		str = "取车失败";
	} else if ("343" == state) {
		str = "定损确认";
	} else if ("345" == state) {
		str = "还车预约";
	} else if ("347" == state) {
		str = "还车派单";
	}else if ("35" == state) {
		str = "待还车邀约";
	}else if ("351" == state) {
		str = "邀约失败重新预约";
	}else if ("352" == state) {
		str = "邀约失败重新派单";
	}else if ("36" == state) {
		str = "待提车";
	}else if ("361" == state) {
		str = "提车失败";
	} else if ("37" == state) {
		str = "待还车";
	}else if ("371" == state) {
		str = "还车失败";
	} else if ("40" == state) {
		str = "待理赔";
	} else if ("100" == state) {
		str = "理赔完毕";
	} else if ("101" == state) {
		str = "强制废除";
	}
	return str;
}
//拨打电话：
function outCallPhone(orderkey, customkey, phone) {
	if (!isphone(phone)) {
		$.messager.alert('提示', '电话格式错误,无法拨打!');
		return;
	}
	$.messager.confirm('提示', '确认拨打电话吗？<br/>' + phone, function(r) {
		if (r) {
			if ($("#outCall_phone").length > 0) {
				$("#outCall_phone").textbox('setValue', '');
			}
			$.post($("#paths").val()
					+ "/order/outCallOrderCustom.tag?orderkey=" + orderkey
					+ "&customerkey=" + customkey + "&calledno=" + phone,
					function(msg) {
						msg = eval('(' + msg + ')');
						if (msg.result == 0 || msg.result == '0') {
							//$.messager.alert('提示',"拔号中....");
							return;
						} else {
							$.messager.alert('提示', msg.msg);
							return;
						}
					});
		}
	});
}
