$.postJSON=function(url,args,fn){$.ajax({type:"POST",dataType:"json",'url':url,data:args,success:fn});};
$.mergeJSON=function(a,b){var j={};for(var k in a){if(typeof(eval("a['"+k+"']"))=='object'){eval("j['"+k+"']=$.mergeJSON(j['"+k+"'],a['"+k+"']);");}else{eval("j['"+k+"']=a['"+k+"'];");}}jQuery.each(a,function(i,f){if(typeof(f.value)=='object'){eval("j['"+f.name+"']=$.mergeJSON(j['"+f.name+"'],f.value);");}else{eval("j['"+f.name+"']=f.value;");}});jQuery.each(b,function(i,f){if(typeof(f.value)=='object'){eval("j['"+f.name+"']=$.mergeJSON(j['"+f.name+"'],f.value);");}else{eval("j['"+f.name+"']=f.value;");}});return j;};
function callback(data) {
	var result = fromCallback(data, "result");
	var js = fromCallback(data, "js");
	var info = fromCallback(data, "info");
	if (result == null) {
		alert("程序出现未知错误，请重新登录后再试。\n如果此问题连续出现，请联系管理员解决。");
		return false;
	}
	if (js != null) {
		eval(js);
	}
	if (info != null) {
		alert(info);
	}
	if (result == "success") {
		return true;
	}
	return false;
}
function fromCallback(data, name) {
	var pos_s = data.indexOf("[" + name + "]");
	var pos_e = data.indexOf("[/" + name + "]");
	if (pos_s > -1 && pos_e > -1 && pos_e > pos_s) {
		return data.substring(pos_s + name.length + 2, pos_e);
	} else {
		return null;
	}
}
/* 导出文件 */
function exportFile(query) {
	var export_window = window.open("export_action.php?" + query);
	if (export_window == null) {
		alert("无法打开弹出窗口，\n您的浏览器可能将弹出窗口屏蔽了。");
	}
}
/* 选择短信发送通道 */
function choosePassage(uid) {
	var passage1 = $("select#passage1").val();
	var passage2 = $("select#passage2").val();
	var passage3 = $("select#passage3").val();
	var passage4 = $("select#passage4").val();
	$.post("message_action.php", {"action":"choose_passage", "id":uid, "passage1":passage1, "passage2":passage2, "passage3":passage3, "passage4":passage4, "rand":Math.random()}, function(data) {callback(data);});
}
/* 选择彩信发送通道 */
function chooseMMSPassage(uid) {
	var passage1 = $("select#passage1_mms").val();
	var passage2 = $("select#passage2_mms").val();
	var passage3 = $("select#passage3_mms").val();
	var passage4 = $("select#passage4_mms").val();
	$.post("message_action.php", {"action":"choose_mms_passage", "id":uid, "passage1":passage1, "passage2":passage2, "passage3":passage3, "passage4":passage4, "rand":Math.random()}, function(data) {callback(data);});
}
/* 设置优先级 */
function setPriority(uid, priority) {
	$.post('message_action.php', {"action":"set_priority", "id":uid, "priority":priority, "rand":Math.random()}, function (data) {callback(data);});
}
/* 检测待审核短信 */
function checkValidMsg() {
	window.clearInterval(alertValidMsgInterval);
	window.setTimeout('checkValidMsg()', 30000);
	$.post("system_action.php", {"action":"check_valid_message", "rand":Math.random()}, function(data) {
		var result = callback(data);
		if (result) {
			var need = fromCallback(data, 'need');
			if (need == 'true') {
				alertValidMsgInterval = window.setInterval('alertValidMsg()', 500);
			} else {
				document.title = document.title.replace(/^.审核短信. /, '');
			}
		}
	});
}
/* 提醒待审核短信 */
var alertValidMsgInterval;
function alertValidMsg() {
	var title = document.title;
	if (title.indexOf('★') > -1) {
		title = title.replace(/★/g, '☆');
	} else if (title.indexOf('☆') > -1) {
		title = title.replace(/☆/g, '★');
	} else {
		title = '☆审核短信☆ ' + title;
	}
	document.title = title;
}
/* 按比例缩放图片 */
function scaleImg(img, width, height) {
	var image = new Image();
	image.src = img.src;
	if (image.width > 0 && image.height > 0) {
		if (image.width / image.height >= width / height) {
			if (image.width > width) {
				img.width = width;
				img.height = (image.height * width) / image.width;
			} else {
				img.width = image.width;
				img.height = image.height;
			}
			img.alt = image.width + "×" + image.height;
		} else {
			if (image.height > height) {
				img.height = height;
				img.width = (image.width * height) / image.height;
			} else {
				img.width = image.width;
				img.height = image.height;
			}
			img.alt = image.width + "×" + image.height;
		}
		img.style.cursor = "pointer";
		img.onclick = function() {window.open(this.src);}
		if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) {
			// img.title = "请使用鼠标滚轮缩放图片，点击图片可在新窗口打开";
			img.onmousewheel = function img_zoom() {
				var zoom = parseInt(this.style.zoom, 10) || 100;
				zoom += event.wheelDelta / 12;
				if (zoom > 0) this.style.zoom = zoom + "%";
				return false;
			}
		} else {
			// img.title = "点击图片可在新窗口打开";
		}
	}
}
/* 向鼠标位置插入字符 */
function insertAtCursor(obj, txt) {
	obj.focus();
	if (document.selection) { // IE support
		sel = document.selection.createRange();
		sel.text = txt;
	} else { // MOZILLA/NETSCAPE support
		var startPos = obj.selectionStart;
		var endPos = obj.selectionEnd;
		obj.value = obj.value.substring(0, startPos) + txt + obj.value.substring(endPos, obj.value.length);
		startPos += txt.length;
		obj.setSelectionRange(startPos, startPos);
	}
}
/* 不间断滚动
<script type="text/javascript">
var up = new scrolls("id","方向");
up.addNodes();
up.scrollPro();
</script> */
function scrolls(id,dir){
	this.id = id; // id的对象的id
	this.dir = dir; //方向  "up","down","left","right"
	//this.dec; //方向决定 代表的scrollLeft,scrollTop
	this.h; //子层高度
	this.w; //子层宽度
	var pro;
}
scrolls.prototype = {
	addNodes : function (){ //在主层下添加复制元素
	
		//var p = id;
		var _this = this;
		var c = document.createElement("div");//子层
		var c1 = document.createElement("div");//双子层 1
		var c2 = document.createElement("div");//双子层 2
		var dir = _this.getDirection();
			
		if(this.dir == "err")
		{
			alert("方向参数错误!");
			return false;
		}
		
		//定死外框高度宽度
		_this._$(_this.id).style.height = _this._$(_this.id).offsetHeight + "px";
		_this._$(_this.id).style.width = _this._$(_this.id).offsetWidth + "px";
		
		
		c1.innerHTML = _this._$(_this.id).innerHTML;//复制层内的节点
		c2 = c1.cloneNode(true);//双子层2
		
		_this._$(_this.id).appendChild(c1);	
		_this._$(_this.id).appendChild(c2);
	
		if(dir == "ud")//设置样式
		{
			_this.h = c1.offsetHeight > _this._$(_this.id).offsetHeight ? c1.offsetHeight :  _this._$(_this.id).offsetHeight ;
			_this.w = _this._$(_this.id).offsetWidth;
			c1.style.cssText = "float:left;width:" + _this.w +"px;height:" + _this.h + "px;"
			c2.style.cssText = c1.style.cssText;
			c.style.cssText = "width:" + c1.offsetWidth + "px;height:"+ c1.offsetHeight*2 +"px;overflow:hidden;margin:0px;padding:0px;border-width:0px;"
		}
		else
		{
			_this.w = c1.offsetWidth > _this._$(_this.id).offsetWidth ? c1.offsetWidth :  _this._$(_this.id).offsetWidth;
			_this.h = _this._$(_this.id).offsetHeight;
			c1.style.cssText = "float:left;width:" + _this.w +"px;height:" + _this.h + "px;"
			c2.style.cssText = c1.style.cssText;
			c.style.cssText = "width:" + (c1.offsetWidth*2) + "px;height:"+ c1.offsetWidth +"px;overflow:auto;margin:0px;padding:0px;border-width:0px;"			
			
		}
		c.appendChild(c1);
		c.appendChild(c2);
		var pc = _this._$(_this.id).childNodes;
		for(var i = 0 ; i < pc.length ; i++)
		{
			pc[i].removeNode;
			_this._$(_this.id).removeChild(pc[i]);
		}
		_this._$(_this.id).innerHTML = "";
		_this._$(_this.id).appendChild(c);

		_this.maxSize = _this.getDirection() == "lr" ? _this._$(_this.id).getElementsByTagName("div")[0].offsetWidth/2 : _this._$(_this.id).getElementsByTagName("div")[0].offsetHeight/2;
		//alert(maxSize);
		_this.dec = _this.getDirection()=="lr" ? _this._$(_this.id).scrollLeft : _this._$(_this.id).scrollTop; 
		
		//alert(_this._$(id).scrollWidth);
		//_this._$(id).scrollTop =20;
		
	},

	scrollPro:function(){
		var _this = this;
		// alert(_this.dir)
		switch(_this.dir){
			case "up" : _this.pro = setInterval(function(){_this.scrollUp()},100);break;
			case "down" : _this.pro = setInterval(function(){_this.scrollDown()},100);break;
			case "left" : _this.pro = setInterval(function(){_this.scrollLeft()},100);break;
			case "right" : _this.pro =setInterval(function(){_this.scrollRight()},100);break;
		}
		_this._$(_this.id).onmouseover = function(){clearTimeout(_this.pro)};
		_this._$(_this.id).onmouseout = function(){
			switch(_this.dir){
				case "up" : _this.pro = setInterval(function(){_this.scrollUp()},100);break;
				case "down" : _this.pro = setInterval(function(){_this.scrollDown()},100);break;
				case "left" : _this.pro = setInterval(function(){_this.scrollLeft()},100);break;
				case "right" : _this.pro =setInterval(function(){_this.scrollRight()},100);break;
			}
		}
	},

	scrollUp:function(){
		var _this = this;
		if(_this._$(this.id).scrollTop < _this.maxSize)
		{
			_this._$(this.id).scrollTop+=3;
		}
		else
		{
			_this._$(this.id).scrollTop = 0;
		}
		
	},

	scrollDown:function(){
		
		var _this = this;
		if(_this._$(this.id).scrollTop <= 0)
		{
			_this._$(this.id).scrollTop = _this.maxSize;
		}
		else
		{
			_this._$(this.id).scrollTop-=3;
		}
		
	},

	scrollRight:function(){
		
		var _this = this;
		if(_this._$(this.id).scrollLeft <= 0)
		{
			_this._$(this.id).scrollLeft = _this.maxSize;
		}
		else
		{
			_this._$(this.id).scrollLeft-=3;
		}
		
	},
	scrollLeft:function(){
		
		var _this = this;
		if(_this._$(this.id).scrollLeft < _this.maxSize)
		{
			_this._$(this.id).scrollLeft+=3;
		}
		else
		{
			_this._$(this.id).scrollLeft = 0;
		}
		
	},

	_$:function (o){ //获取对象
		if(typeof(o) == "string")
		{
			if(document.getElementById(o))
			{
				return document.getElementById(o);
			}
			else
			{
				alert("err" + "\"o" + "\"");
				return false;
			}
		}
		else
		{
			return o;
		}
	},


	getDirection : function(){ //获取方向
		switch(this.dir){
			case "up" : return "ud" ; break;
			case "down" : return "ud" ; break;
			case "left" : return "lr" ; break;
			case "right" : return "lr" ; break;
			default : return "err";
		}
	}
}
/* 提交问题 */
function askQuestion() {
	var question = $("textarea[name=question]").val();
	var linkman = $("input[name=linkman]").val();
	var linktel = $("input[name=linktel]").val();
	if (question == '') {
		$.messager.alert("警告", '必须填写问题描述！', 'warning');
		return false;
	}
	if (linkman == '') {
		$.messager.alert("警告", '必须填写联系人！', 'warning');
		return false;
	}
	if (linktel == '') {
		$.messager.alert("警告", '必须填写联系电话！', 'warning');
		return false;
	}
	$.ajaxFileUpload(
		{
			url:'/index_action.php',
			postdata:{
				'action':'ask_question',
				'question':question,
				'linkman':linkman,
				'linktel':linktel,
				rand:Math.random()
			},
			secureuri:false,
			fileElementId:'pic',
			dataType: 'text',
			success: function (data, status)
			{
				var result = callback(data);
				if (result) {
					$.messager.alert('提示', '您的问题已经提交。');
				}
			},
			error: function (data, status, e)
			{
				alert(data);
				alert(status);
				alert(e);
			}
		}
	);
}
