﻿
//现有对象功能扩展

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
;
String.prototype.ltrim = function() {
	return this.replace(/(^\s*)/g, "");
}
;
String.prototype.rtrim = function() {
	return this.replace(/(\s*$)/g, "");
}
;
String.prototype.cleanBlank = function() {
	return this.replace(/\s/g, "");
}
;
String.prototype.checkSpecialChar = function(flag) {
	var reg = /[%\'\"\/\\]/;
	if (this.search(reg) != -1) {
		if (flag) {
			alert("请不要输入 ＂ % \' \" \\ \/ ＂ 等特殊字符。");
		}
		return false;
	}
	return true;
}
;
String.prototype.test = function(reg) {
	if (this.search(reg) == -1) {
		return false;
	}
	return true;
}
;
String.prototype.len = function() {
	var len = 0;
	for (var i = 0;
    i < this.length;
    i++) {
		if (this.charCodeAt(i) > 255) {
			len += 2;
		}
		else {
			len++;
		}
	}
	return len;
}
;
String.prototype.html = function() {
	var html = this;
	html = html.replace(/</g, "&lt;");
	html = html.replace(/>/g, "&gt;");
	return html;
};

Date.prototype.dateAdd = function(n) {
	return new Date(this.valueOf() + n * 3600 * 24 * 1000);
}

//mootools1.2_ProajaxPro访问代理
// opt: url 访问地址 string,
//		ajaxProMethod ajaxpro对应的方法 string
//		params 请求方法使用的参数 object
//		success 成功回调方法 将返回function(backinfo,params)
//				backinfo:服务器返回的对象 object
//				params 请求方法使用的参数 object
//		failure 请求失败的回调方法 function()
function myAjax(options) {
	var opt = options || {};
	var params = opt.params || {};
	//var myXHR = new XHR({ method: 'get' }).send('http://site.com/requestHandler.php', 'name=john&lastname=dorian');

	var AjaxPro = new Request({
		url: opt.url,
		method: 'post',
		headers: { 'X-AjaxPro-Method': opt.method },
		data: JSON.encode(params),
		onSuccess: function(responseText) {
			if (responseText) {
				var tmp;
				tmp = JSON.decode(responseText);
				if (tmp.value && opt.success) {
					opt.success(tmp.value, params);
				} else if (tmp.error && tmp.error.Message && opt.failure) {
					opt.failure(tmp.error.Message, params);
				} else if (opt.failure) {
					opt.failure('返回数据错误:' + responseText, params);
				}
			} else {
				if (opt.failure) opt.failure('没有得到返回数据', params);
			}
		},
		onFailure: function() {
			if (opt.failure) opt.failure('通讯失败', params);
		}
	}).send();
}

//===========
// 通用函数
var HX = function() {
	return {
		//表单单次提交
		submitonce: function(theform) {
			if (document.all || document.getElementById) {
				for (i = 0; i < theform.length; i++) {
					var tempobj = theform.elements[i]
					if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset" || tempobj.type.toLowerCase() == "button")
						tempobj.disabled = true
				}
			}
		},
		getCookie: function(Name) {
			var search = Name + "=";
			if (document.cookie.length > 0) {
				offset = document.cookie.indexOf(search);
				if (offset != -1) {
					offset += search.length;
					end = document.cookie.indexOf(";", offset);
					if (end == -1) end = document.cookie.length;
					return unescape(document.cookie.substring(offset, end));
				}
				else return ('');
			}
			else return ('');
		},
		setCookie: function(name, value) {
			var today = new Date();
			var expires = new Date();
			expires.setTime(today.getTime() + 1000 * 60 * 60 * 24 * 365);
			document.cookie = name + "=" + escape(value) + ";path=/" + "; expires=" + expires.toGMTString();
		},
		showTxtLen: function(ElementID, showElementID) {
			document.getElementById(showElementID).innerHTML = document.getElementById(ElementID).value.len();
		},
		Esc2Tab: function(evt) {
			evt = evt ? evt : (window.event ? window.event : null);
			if (evt.keyCode == 27) evt.keyCode = 9;
		},
		inputTxt: function(obj, flg, txt) {
			if (flg == 0) {
				if (obj.value == txt) {
					obj.focus();
					obj.value = "";
					obj.style.cssText = "color:#000000";
				}
				else {
					obj.focus();
					obj.select();
					obj.style.cssText = "color:#000000";
				}
			}
			if (flg == 1 && obj.value == "") { obj.value = txt; obj.style.cssText = "color:#aaaaaa"; }
		},
		setSubmitBut: function(evt, but) {
			evt = evt ? evt : (window.event ? window.event : null);
			if (evt && evt.keyCode == 13) {
				document.getElementById(but).click();
				evt.returnValue = false;
			}
		},
		go2: function(url) {
			window.location.href = url;
		}
	}
} ();

/*
基于mootools1.2滚动窗口
var myScroll=new MyScroll({
scrollElem:"nowprint",
ItemSelect:"div[class=item]",
SCROLL_HEIGHT:85,
TIME_INTERVAL:3000
}).start();
*/
function MyScroll(options) {
	var opt = options || {};
	return {
		TIME_INTERVAL: opt.TIME_INTERVAL || 1000, // 滚动停止 时间间隔（毫秒）
		SCROLL_HEIGHT: opt.SCROLL_HEIGHT || 85,
		scrollElem: $(opt.scrollElem),
		stopscroll: false,
		scrolling: new Fx.Scroll(opt.scrollElem, { 'offset': { x: 0, y: 0} }),
		thisindex: 1,
		timer: null,
		ItemSelect: opt.ItemSelect || "div[class=item]", //滚动item选择器
		start: function() {
			this.thisindex = 1;
			this.stopscroll = false;
			this.clearTimer();
			this.scrollElem.style.overflow = "hidden";
			this.scrollElem.style.height = this.SCROLL_HEIGHT; // 设置滚动区高度
			this.scrollElem.addEvent('mouseenter', function(e) {
				this.stopscroll = true;
			} .bind(this));
			this.scrollElem.addEvent('mouseleave', function(e) {
				this.stopscroll = false;
			} .bind(this));
			this.scrollElem.appendChild(this.scrollElem.getElements(this.ItemSelect)[0].cloneNode(true));
			this.prepareTimer();
		},
		nextItem: function() {
			var objlist = this.scrollElem.getElements(this.ItemSelect);
			if (!this.stopscroll && objlist.length > 0) {
				if (this.thisindex >= objlist.length) {
					this.thisindex = 1
					this.scrollElem.scrollTop = "0px";
					this.scrollElem.scrollLeft = "0px";
				}
				this.scrolling.toElement(objlist[this.thisindex]);
				this.thisindex++;
			}

			this.prepareTimer();
		},
		prepareTimer: function() {
			this.timer = this.nextItem.delay(this.TIME_INTERVAL, this);
		},
		clearTimer: function() {
			if (this.timer) $clear(this.timer);
		}
	}
};



function Validator(inputs) {

	for (var i = 0; i < inputs.length; i++) {
		$(inputs[i][0]).addEvent('blur', (function(i) {
			var el = $(inputs[i][0]);
			if (!el.value) return;
			$(inputs[i][2]).style.display = $type(inputs[i][1]) == 'function' ? (inputs[i][1].bind(el)() ? 'none' : '') : (el.value.test(inputs[i][1]) ? 'none' : '');
		}).pass(i));
	}

	return {

		validateAll: function() {
			var valid = true, first;
			for (var i = 0; i < inputs.length; i++) {
				var el = $(inputs[i][0]);
				if (($type(inputs[i][1]) == 'function' && !inputs[i][1].bind(el)()) || ($type(inputs[i][1]) == 'string' && !el.value.test(inputs[i][1]))) {
					$(inputs[i][2]).style.display = '';
					valid = false;
					if (!first) first = $(inputs[i][0]);
				}
			}
			if (!valid) first.focus();
			return valid;
		}

	}

}