var helper = {};

helper.http = {
	newXMLHttp: function() {
		var XMLHttp = null;
		if(window.XMLHttpRequest) {
			XMLHttp = new XMLHttpRequest();
		}
		if(XMLHttp == null && window.ActiveXObject) {
			var clsids = ["MSXML2.XMLHttp.7.0", "MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp.2.0", "MSXML2.XMLHttp.1.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];
			for(var i = 0; i < clsids.length && XMLHttp == null; i++) {
				try {
					XMLHttp = new ActiveXObject(clsids[i]);
				} catch(e) {}
			}
		}
		return XMLHttp;
	},
	get: function(URL, func) {
		try {
			var XMLHttp = this.newXMLHttp();
			if(typeof(func) != "function") {
				XMLHttp.open("GET", URL, false);
				XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
				XMLHttp.send(null);

				var result = XMLHttp.status;
				var responseText = XMLHttp.responseText;

				if(result == 200) {
					return responseText;
				} else {
					return result;
				}
			} else {
				XMLHttp.onreadystatechange = function() {
					if(XMLHttp.readyState == 4) {
						if(XMLHttp.status == 200) {
							var responseText = XMLHttp.responseText;
							func(responseText);
						} else {
							func(false);
						}
					}
				};
				XMLHttp.open("GET", URL, true);
				XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
				XMLHttp.send(null);
			}
		} catch(e) {}
	},
	post: function(URL, data, func) {
		try {
			var XMLHttp = this.newXMLHttp();
			if(typeof(func) != "function") {
				XMLHttp.open("POST", URL, false);
				XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
				XMLHttp.send(data);

				var result = XMLHttp.status;
				var responseText = XMLHttp.responseText;

				if(result == 200) {
					return responseText;
				} else {
					return false;
				}
			} else {
				XMLHttp.onreadystatechange = function() {
					if(XMLHttp.readyState == 4) {
						if(XMLHttp.status == 200) {
							var responseText = XMLHttp.responseText;
							func(responseText);
						} else {
							func(false);
						}
					}
				};

				XMLHttp.open("POST", URL, true);
				XMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
				XMLHttp.send(data);
			}
		} catch(e) {}
	}
};

helper.cookies = {
	del: function(name) {
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval = helper.cookies.get(name);
		if(cval != null)
			document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
	},
	set: function(name, value, remember) {
		if(remember == true) {
			var Days = 30;
			var exp = new Date();
			exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
			document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
		} else {
			document.cookie = name + "=" + escape(value) + ";path=/";
		}
	},
	setday: function(name, value, remember) {
		var Days = remember;
		var exp = new Date();
		exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
		document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();

	},
	setdomain: function(name, value, remember) {
		var domain = window.location.host.split(".");
		if(remember == true) {
			var Days = 30;
			var exp = new Date();
			exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
			document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=." + domain[domain.length - 2] + "." + domain[domain.length - 1];
		} else {
			document.cookie = name + "=" + escape(value) + ";path=/" + ";domain=." + domain[1] + "." + domain[2];
		}
	},
	get: function(name) {
		var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

		if(arr = document.cookie.match(reg)) {
			return unescape(arr[2]);
		} else {
			return '';
		}
	}
};

/*
获取URL中指定的参数
例如：
URL:test.com/test.aspx?test=t
var t=help.request.queryString("test");
得到t
*/
helper.request = {
	queryString: function(item) {
		locationSearch = "";
		var tmpLocationArr = location.href.split("?");
		if(tmpLocationArr.length > 1) locationSearch = "?" + tmpLocationArr[1];
		var svalue = locationSearch.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
		var Qvalue = svalue ? svalue[1] : svalue;
		if(Qvalue == null) {
			return "";
		} else {
			return Qvalue.trim();
		}
	},
	queryurl: function(url, item) {
		locationSearch = "";
		var tmpLocationArr = url.split("?");
		if(tmpLocationArr.length > 1) locationSearch = "?" + tmpLocationArr[1];
		var svalue = locationSearch.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
		var Qvalue = svalue ? svalue[1] : svalue;
		if(Qvalue == null) {
			return "";
		} else {
			return Qvalue.trim();
		}
	}
};

helper.pager = {

	//分页
	PageList1: function(strFunName) {
		/***总页数***/
		var intPageCount = parseInt((parseInt(intRecordCount) + parseInt(intPageSize) - 1) / parseInt(intPageSize));
		if(intPageCount <= 1) {
			return "";
		}

		/***页码距***/
		var intPageCurrent = 10;

		/***起始页码***/
		var intStartPage = (parseInt((parseInt(intPageIndex) + intPageCurrent - 1) / intPageCurrent) - 1) * intPageCurrent + 1;

		/***结束页码***/
		var intEndPage = intStartPage + intPageCurrent - 1;
		if(intPageIndex % intPageCurrent == 0) {
			intStartPage++;
			intEndPage++;
		}

		if(intPageIndex % intPageCurrent - 1 == 0 && intPageIndex != 1) {
			intStartPage--;
			intEndPage--;
		}

		if(intEndPage >= intPageCount) {
			intEndPage = intPageCount;
		}

		/***上一页，下一页***/
		var intUpPage = parseInt(intPageIndex) - 1;
		var intDownPage = parseInt(intPageIndex) + 1;
		var strUpPage = "<a href=\"#\" onclick=\"" + strFunName + "(" + intUpPage + ")\" >上一页</a>";
		var strDownPage = "<a href=\"#\" onclick=\"" + strFunName + "(" + intDownPage + ")\">下一页</a>";
		if(intUpPage < 1) {
			//intUpPage = 1;
			strUpPage = "";
		}
		if(intPageIndex >= intPageCount) {
			//intDownPage = intPageCount;
			strDownPage = "";
		}

		/***生成页码列表***/
		var strPageList = strUpPage;

		for(var i = intStartPage; i <= intEndPage; i++) {
			if(intPageIndex == i) {
				/***当前页码***/
				strPageList += "<a style=\"background-image:url(../images/bg/menublue22.jpg); background-position:bottom; background-repeat:repeat-x; padding:1px 6px;border-radius: 4px 4px 4px 4px; margin-right:3px; border:1px solid #aeb2b1;\" >" + i + "</a>";
			} else {
				strPageList += "<a href=\"#\" onclick=\"" + strFunName + "(" + i + ");\">" + i + "</a>";
			}
		}
		strPageList += strDownPage;

		//直接跳转
		strPageList += "总页数：" + intPageCount + "&nbsp;&nbsp;/&nbsp;&nbsp;";
		strPageList += "<input type=\"text\" onkeyup=\"this.value=this.value.replace(/\\D/g,'')\" id=\"tb_index\" />";
		strPageList += "&nbsp;&nbsp;<a style=\"cursor:pointer;\" onclick=\"javascript:helper.pager.checkPageValue()\">跳转</a>";
		return strPageList;

	},
	checkPageValue: function() {
		var intPageIndexNext = document.getElementById('tb_index').value;
		if(parseInt(intPageIndexNext) == 0) {
			document.getElementById('tb_index').value = "1";
			$.jBox.alert('输入页面不能为0', '提示');
			return;
		}
		if(intPageIndexNext == "") {
			document.getElementById('tb_index').value = "1";
			$.jBox.alert('输入页面不能为空', '提示');
			return;
		}
		if(intPageIndexNext.length != 0) {
			var intPageCount = parseInt((parseInt(intRecordCount) + parseInt(intPageSize) - 1) / parseInt(intPageSize));
			if(intPageIndexNext <= intPageCount) {
				intPageIndex = intPageIndexNext;
				loadlist();
			} else {
				document.getElementById('tb_index').value = intPageCount;
				$.jBox.alert('输入页面不能大于总页数', '提示');
			}
		} else {
			document.getElementById('tb_index').value = "1";
		}
	},
	loadJS: function(url) {
		var domScript = document.createElement('script');
		domScript.type = "text/javascript";
		domScript.src = url;
		domScript.onload = domScript.onreadystatechange = function() {
			if(!this.readyState || 'loaded' === this.readyState || 'complete' === this.readyState) {
				this.onload = this.onreadystatechange = null;
				this.parentNode.removeChild(this);
			}
		};
		document.getElementsByTagName('head')[0].appendChild(domScript);
	}
};

helper.masklayer = {
	layer: null,
	showcount: 0,
	show: function() {
		this.layer = document.getElementById("masklayer");
		if(!this.layer) {
			this.layer = document.createElement("div");
			this.layer.id = "masklayer";
			this.layer.className = "masklayer";
			/*this.layer.style.position = "absolute";
			this.layer.style.top = "0";
			this.layer.style.left = "0";
			this.layer.style.zIndex = 1000;
			var width = Math.max(
			Math.max(document.body.clientWidth, document.documentElement.clientWidth),
			Math.max(document.body.scrollWidth, document.documentElement.scrollWidth)
			);
			var height = Math.max(
			Math.max(document.body.clientHeight, document.documentElement.clientHeight),
			Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
			);
			this.layer.style.width = width + "px";
			this.layer.style.height = height + "px";*/
			this.layer.innerHTML = "<iframe src='/blank.html' style='width:100%;height:100%;background-color:#fff;" +
				"filter:alpha(opacity=0);-moz-opacity:0;-khtml-opacity: 0;opacity: 0;" +
				"position:absolute;z-index:999;top:0;left:0;' frameborder='0'></iframe>" +
				"<div style='width:100%;height:100%;position:absolute;top:0;left:0;z-index:1000;background:#000;" +
				"filter:alpha(opacity=20);-moz-opacity:0.2;-khtml-opacity: 0.2;opacity: 0.2;'></div>";
			document.body.appendChild(this.layer);
		}
		this.showcount++;
	},
	hide: function() {
		if(--this.showcount == 0) {
			if(this.layer && this.layer.parentNode) {
				this.layer.parentNode.removeChild(this.layer);
			}
		}
	},
	moveToCenter: function(e, x, y) {
		if(e.style.display == "none") e.style.display = "";
		var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
		var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
		if(x > 0) {
			x += scrollLeft;
		} else x = parseInt((document.documentElement.clientWidth - e.clientWidth) / 2) + scrollLeft;
		if(y > 0) {
			y += scrollTop;
		} else y = parseInt((document.documentElement.clientHeight - e.clientHeight) / 2) + scrollTop;
		x = x < 0 ? 0 : x;
		y = y < 50 ? 50 : y;
		e.style.left = x + "px";
		e.style.top = y + "px";
	}
};

helper.filter = {
	Transcoding: function(content) {
		content = content.replace(/∞L/g, "<");
		content = content.replace(/∞R/g, ">");
		content = content.replace(/∞∴/g, "'");
		content = content.replace(/∞≡/g, "#");
		content = content.replace(/∞※/g, "\"");
		return content;
	}
};

function str2asc(strstr) {
	return("0" + strstr.charCodeAt(0).toString(16)).slice(-2);
}
helper.url = {
	getpara: function(name) {
		var val;
		paras = window.location.search;
		paras = paras.substr(1);
		paraarray = paras.split("&");
		for(i = 0; i < paraarray.length; i++) {
			temp = paraarray[i].split("=");
			if(temp[0] == name) {
				val = temp[1];
				break;
			}
		}
		if(val == null || val == "") {
			return "";
		} else {
			return val;
		}
	}
};
helper.sys = {
	getElementsByClassName: function(pnode, className) {
		var nodes = pnode.getElementsByTagName('*');
		var elements = new Array();
		for(var i = 0; i < nodes.length; i++) {
			if(nodes[i].className == className) {
				elements[elements.length] = nodes[i];
			}
		}
		return elements;
	},
	removeElement: function(element) {
		if(element && element.parentNode) {

			element.parentNode.removeChild(element);
			return true;
		} else {
			return false;
		}
	},
	Return: function(url) {
		top.location.href = url;
	}
};

helper.DateFormat = {
	d2s: function(postdate) {
		var second = 1000;
		var minutes = second * 60;
		var hours = minutes * 60;
		var days = hours * 24;
		var months = days * 30;
		var twomonths = days * 365;
		var myDate = new Date(Date.parse(postdate));
		if(isNaN(myDate)) {
			myDate = new Date(postdate.replace(/-/g, "/"));
		}
		var nowtime = new Date();
		var longtime = nowtime.getTime() - myDate.getTime();
		var showtime = 0;
		if(longtime > months * 2) {
			return postdate;
		} else if(longtime > months) {
			return "1个月前";
		} else if(longtime > days * 7) {
			return("1周前");
		} else if(longtime > days) {
			return(Math.floor(longtime / days) + "天前");
		} else if(longtime > hours) {
			return(Math.floor(longtime / hours) + "小时前");
		} else if(longtime > minutes) {
			return(Math.floor(longtime / minutes) + "分钟前");
		} else if(longtime > second) {
			return(Math.floor(longtime / second) + "秒前");
		} else {
			return("刚刚");
		}
	},
	getPreMonth:
		/**
		 * 获取上一个月
		 *
		 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
		 */
		function(date) {
			var arr = date.split('-');
			var year = arr[0]; //获取当前日期的年份
			var month = arr[1]; //获取当前日期的月份
			var day = arr[2]; //获取当前日期的日
			var days = new Date(year, month, 0);
			days = days.getDate(); //获取当前日期中月的天数
			var year2 = year;
			var month2 = parseInt(month) - 1;
			if(month2 == 0) {
				year2 = parseInt(year2) - 1;
				month2 = 12;
			}
			var day2 = day;
			var days2 = new Date(year2, month2, 0);
			days2 = days2.getDate();
			if(day2 > days2) {
				day2 = days2;
			}
			if(month2 < 10) {
				month2 = '0' + month2;
			}
			var t2 = year2 + '-' + month2 + '-' + day2;
			return t2;
		},
	getNextMonth:
		/**
		 * 获取下一个月
		 *
		 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
		 */
		function(date) {
			var arr = date.split('-');
			var year = arr[0]; //获取当前日期的年份
			var month = arr[1]; //获取当前日期的月份
			var day = arr[2]; //获取当前日期的日
			var days = new Date(year, month, 0);
			days = days.getDate(); //获取当前日期中的月的天数
			var year2 = year;
			var month2 = parseInt(month) + 1;
			if(month2 == 13) {
				year2 = parseInt(year2) + 1;
				month2 = 1;
			}
			var day2 = day;
			var days2 = new Date(year2, month2, 0);
			days2 = days2.getDate();
			if(day2 > days2) {
				day2 = days2;
			}
			if(month2 < 10) {
				month2 = '0' + month2;
			}

			var t2 = year2 + '-' + month2 + '-' + day2;
			return t2;
		},
	getNowDate: function() {
		var NowDate;
		var myDate = new Date();
		var YY = myDate.getFullYear(); //获取完整的年份(4位,1970-????)    
		var MM = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)    
		var DD = myDate.getDate(); //获取当前日(1-31)
		MM = MM < 10 ? '0' + MM : MM;
		DD = DD < 10 ? '0' + DD : DD;

		//      myDate.getDay();         //获取当前星期X(0-6,0代表星期天)    
		//		myDate.getYear();       //获取当前年份(2位)    
		//      myDate.getTime();       //获取当前时间(从1970.1.1开始的毫秒数)    
		//      myDate.getHours();       //获取当前小时数(0-23)    
		//      myDate.getMinutes();     //获取当前分钟数(0-59)    
		//      myDate.getSeconds();     //获取当前秒数(0-59)    
		//      myDate.getMilliseconds();   //获取当前毫秒数(0-999)    
		//      myDate.toLocaleDateString();     //获取当前日期    
		//      var mytime = myDate.toLocaleTimeString();     //获取当前时间    
		//      myDate.toLocaleString();       //获取日期与时间    
		NowDate = YY + "-" + MM + "-" + DD;
		return NowDate;
	},

	getFirstDayInCurrentMonth: function() {
		var NowDate;
		var myDate = new Date();
		myDate.getYear(); //获取当前年份(2位)    
		var YY = myDate.getFullYear(); //获取完整的年份(4位,1970-????)    
		var MM = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月)    
		var DD = myDate.getDate(); //获取当前日(1-31)    
		NowDate = YY + "-" + MM + "-1";

		return NowDate;
	},
	getFormatTime: function(longTime) {
		var time = parseFloat(longTime);
		if(time != null && time != "") {
			if(time < 60) {
				var s = time;
				time = s + '秒';
			} else if(time > 60 && time < 3600) {
				var m = parseInt(time / 60);
				var s = parseInt(time % 60);
				time = m + "分钟" + s + "秒";
			} else if(time >= 3600 && time < 86400) {
				var h = parseInt(time / 3600);
				var m = parseInt(time % 3600 / 60);
				var s = parseInt(time % 3600 % 60 % 60);
				time = h + "小时" + m + "分钟" + s + "秒";
			} else if(time >= 86400) {
				var d = parseInt(time / 86400);
				var h = parseInt(time % 86400 / 3600);
				var m = parseInt(time % 86400 % 3600 / 60);
				var s = parseInt(time % 86400 % 3600 % 60 % 60);
				time = d + '天' + h + "小时" + m + "分钟" + s + "秒";
			}
		}
		return time;
	}

};

helper.stateFormat = {
	//格式化 回复状态
	formatterIsReply: function(val) {
		var stateStr = '';
		switch (val) { //是否回复（0未回复 1已回复）
			case 0:
				stateStr = '未回复';
				break;
			case 1:
				stateStr = '已回复';
				break;
		}
		return stateStr;
	}
}

helper.string = {
	stringbuilder: function() {
		this.arr = new Array();
		this.append = function(str) {
			this.arr.push(str);
		};
		this.tostring = function() {
			return this.arr.join('');
		};
	}
};
helper.init = {
	setajax: function(isload) { /// 配置AJAX
		$(document).on("ajaxStart", function() {
			if(isload) {
				//加载框显示
				//              layer.load(2);
			}
		}).on("ajaxSuccess", function(e, jqXHR, s, data) {
			try {
				if(s.type == "POST") {
					if(typeof(data) != 'object') {
						data = $.parseJSON(data);
					}
				}
				if(s.type == "GET") {
					data = data
				}
				if(data.state.toLowerCase() == "notoken") {
					mui.confirm(data.message, 'notoken', ['确定']);
					top.window.location.href = "/login.html";
					return;
				}
				if(data.state.toLowerCase() == "redirect") {
					top.window.location.href = data.data;
					return;
				}
				if(data.state.toLowerCase() == "unauthorized") {
					mui.confirm(data.message, 'unauthorized', ['确定']);
				}
				if(data.state.toLowerCase() == "error") {
					mui.confirm(data.message, 'ERROR', ['确定']);
				}
			} catch(e) {
				if(isload) {
					//加载框消失
					//                  layer.closeAll('loading');
				}
			}
		}).on("ajaxStop", function() {
			if(isload) {
				//加载框消失
				//layer.closeAll('loading');
			}
		})
	}
}

helper.init.setajax(1);