﻿var w3c = (document.getElementById)? true : false;
var agt = navigator.userAgent.toLowerCase();
var ie  = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1) && (agt.indexOf("omniweb") == -1));
var ie5 = (w3c && ie)? true : false;
var ns6 = (w3c && (navigator.appName=="Netscape")) ? true : false;
var op8 = (navigator.userAgent.toLowerCase().indexOf("opera") == -1) ? false : true;

function getOffset(obj){
	var offsetLeft = 0;
	var offsetTop = 0;
	if (ie5 || ns6 || op8) {
		var offsetParent = obj;
		while (offsetParent != null && offsetParent != document.body) {
			offsetLeft += offsetParent.offsetLeft;
			offsetTop += offsetParent.offsetTop;
			if(!ns6) {
				var blw = parseInt(offsetParent.currentStyle.borderLeftWidth);
				var btw = parseInt(offsetParent.currentStyle.borderTopWidth);
				if (blw > 0) offsetLeft += blw;
				if (btw > 0) offsetTop += btw;
			}
			offsetParent = offsetParent.offsetParent;
		}
	} else {
		offsetLeft = obj.offsetLeft;
		offsetTop = obj.offsetTop;
	}
	obj.offsetLeft2 = offsetLeft;
	obj.offsetTop2 = offsetTop;
	return obj;
}

function extractNodes(obj) {
	if(obj.nodeType == 3) return null;
	var node, nodes = [];
	for(var i = 0; node = obj.childNodes[i]; i++){
		if(node.nodeType == 1) nodes.push(node);
	}
	return nodes;
}

function gc() {
	if (CollectGarbage) setTimeout(CollectGarbage, 1);
}


//客户端输出
function o(s) { document.write(s); }
window.$ = function(n) { return document.getElementById(n); }
window.$$ = function(n) { return document.getElementsByName(n); }

//烂图替代图加载
new Image().src = 'http://img.xxx.com/global/trans.gif';

var setTimeout222 = window.setTimeout;
window.setTimeout = function(fRef, mDelay) {
	if(typeof fRef == 'function') {
		var argu = Array.prototype.slice.call(arguments, 2);
		var f = (function(){ fRef.apply(null, argu); });
		return setTimeout222(f, mDelay);
	}
	return setTimeout222(fRef, mDelay); 
} 

//模拟asp的request.querystring
function request(name) {
	var url = document.location.href; if(url.indexOf('?') != -1) { url = url.split('?'); url = url[url.length - 1].split('&');
	for(var i = 0; i < url.length; i ++) { var v = url[i].split('='); if(v[0] == name) return v[1]; } }
}


Date.prototype.toString = function(f) {
	function tempfunc(opo, pos) { var val = ''; opo = String(opo);
		for(var a = 1; a < arguments.length; a ++) { 
		var chr = opo.charAt(arguments[a] - 1); val += chr; } return val;
	}

	if(!f) f = 'yyyy-MM-dd HH:mm:ss';
	var h12 = this.getHours() > 12 ? (this.getHours() - 12) : this.getHours()
	var tmp = {
		'yyyy' : this.getYear(),
		'yy'   : tempfunc(this.getYear(), 3, 4),
		'MM'   : (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1),
		'M'	: this.getMonth() + 1,
		'dd'   : (this.getDate() < 10 ? '0' : '') + this.getDate(),
		'd'	: this.getDate(),
		'hh'   : (h12 < 10 ? '0' : '') + h12,
		'h'	: h12,
		'HH'   : (this.getHours() < 10 ? '0' : '') + this.getHours(),
		'H'	: this.getHours(),
		'mm'   : (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(),
		'm'	: this.getMinutes(),
		'ss'   : (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(),
		's'	: this.getSeconds()
	};

	for (var p in tmp) f = f.replace(new RegExp('\\b' + p + '\\b', 'g'), tmp[p]);
	return f;
}

Date.prototype.getWeekey = function() {
	return def.week[this.getDay()];
}

Number.prototype.round = function(r) {
	r = typeof(r) == 'undefined' ? 1 : r;
	var rv = String(this);
	var io = rv.indexOf('.');
	var ri = io == -1 ? '' : rv.substr(io + 1, r);
	var le = io == -1 ? (rv + '.') : rv.substr(0, io + 1 + r);
	for(var a = ri.length ; a < r ; a ++) le += '0';
	return le;
}

String.Empty = '';
String.prototype.trim = function() {
	return this.ltrim().rtrim();
}
String.prototype.ltrim = function() {
	return this.replace(/^ +([^ ]*)/g, '$1');
}
String.prototype.rtrim = function() {
	return this.replace(/([^ ]*) +$/g, '$1');
}

String.prototype.format = function() {
	var val = this.toString();
	for(var a = 0 ; a < arguments.length ; a ++) val = val.replace(new RegExp("\\{" + a + "\\}", "g"), arguments[a]);
	return val;
}

String.prototype.getLength = function() {
	return this.replace(/([\u0391-\uFFE5])/ig,'11').length;
}

String.prototype.parseDate = function() {
	/^(\d{1,4})\-(\d{1,2})\-(\d{1,2})( (\d{1,2})(:(\d{1,2})(:(\d{1,2}))?)?)?$/gi.exec(this);
	return new Date(
	RegExp.$1 ? parseInt(RegExp.$1, 10) : 0,
	RegExp.$2 ? parseInt(RegExp.$2, 10) - 1 : 0,
	RegExp.$3 ? parseInt(RegExp.$3, 10) : 0,
	RegExp.$5 ? parseInt(RegExp.$5, 10) : 0,
	RegExp.$7 ? parseInt(RegExp.$7, 10) : 0,
	RegExp.$9 ? parseInt(RegExp.$9, 10) : 0);
}

String.prototype.toFloat = function(Round) {
	return /^[+\-]?\d+(\.\d+)?$/g.test(this) == false ? 0 : parseFloat(this);
}

String.prototype.toInteger = function(Default) {
	var temp = this.replace(/[^\d]/g, '');
	return temp == '' ? (Default ? Default : 0) : parseInt(temp);
}

String.prototype.toJavaString = function() {
	return this.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\n/g, '\\n').replace(/\r/g, '');
}

String.prototype.toTitleString = function() {
	return this.replace(/"/g, '&quote;').replace(/\n/g, '&#13;');
}

String.prototype.toHtmlString = function() {
	return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br>').replace(/\t/g, '&nbsp;&nbsp;').replace(/ /g, '&nbsp;').replace(/"/g, '&quot;');
}

function setCookie(name, value)
{
	var expires = new Date(new Date().getTime() + (100 + 30) * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires=" + expires.toGMTString();
}

function getCookie(name)
{
	var a_start, an_end, text;
	if (document.cookie)
	{
		text = document.cookie.toString();
		a_start = document.cookie.indexOf(name + "=");
		if (a_start < 0) return '';
		text = text.substr(a_start + name.length + 1, text.length);
		if (text.indexOf(';') != -1) text = text.substr(0, text.indexOf(';'));
		return unescape(text);
	}
	else return '';
}