﻿Function.prototype.bind = function(){
// 容错处理，防止bind的时候需要绑定NaN,undefined,null,Infinity等
	var a = _A(arguments), _this = this;
	!!a[0] || (a[0] = window);
	var b = _A(arguments);b.shift();
	return function() {
		return _this.apply(a[0], b.concat(_A(arguments)))
	}
}
function toJson(json){
	return eval('(' + json + ')');
}
function replaceAll(s,s1,s2){
	return s.replace(new RegExp(s1,"gm"),s2); 
}
function readCookie(name) {
	var cookieValue = "";
	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;}
			cookieValue = unescape(document.cookie.substring(offset, end));
		}
	}
	return cookieValue;
}
function writeCookie(name, value, hours) {
	var expire = "";
	if (!hours) {
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expire;
}
function setText(objName, newText) { //v4.01
	if ((obj = findObj(objName))!=null) {
		with (obj) {
			if (document.layers) {
				document.write(unescape(newText));
				document.close();
			} else {
				innerHTML = unescape(newText);
			}
		}
	}
}
function setValue(objName, newText) {
	if ((obj = findObj(objName))!=null) {
		obj.value=newText;
	}
}
function setFocus(objName) {
	if ((obj = findObj(objName))!=null) {
		obj.focus();
	}
}
function findObj(n, d) { //v4.01
	var p, i, x;
	if (!d) {d = document;}
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) {x = d.all[n];}
	for (i = 0; !x && d.forms && i < d.forms.length; i++) {	x = d.forms[i][n];}
	for (i = 0; !x && d.layers && i < d.layers.length; i++) {x = findObj(n, d.layers[i].document);	}
	if (!x && d.getElementById) {x = d.getElementById(n);}
	return x;
}
function $(n,d){
	return findObj(n,d);
}

function _A(iterable) {
	if (!iterable) return [];
	if (iterable.toArray) {
		return iterable.toArray();
	} else {
		var results = [];
		for (var i = 0; i < iterable.length; i++)
			results.push(iterable[i]);
		return results;
	}
}
function addEventListener (oTarget, sEventType, fnHandler) {
	if (!oTarget) {return;}
	if (oTarget.addEventListener) {
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else {
		if (oTarget.attachEvent) {
			oTarget.attachEvent("on" + sEventType, fnHandler);
		} else {
			oTarget["on" + sEventType] = fnHandler;
		}
	}
}
function getPageScroll(){
	var yScroll,xScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){// Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	if (self.pageXOffset) {
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollLeft){
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		xScroll = document.body.scrollLeft;
	}
	return {'x':xScroll,'y':yScroll};
}
function getPageSize(){	
	var xScroll, yScroll, windowWidth, windowHeight;	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}	
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	return {'pw':pageWidth,'ph':pageHeight,'ww':windowWidth,'wh':windowHeight};
}
function getElement(tagName, id, parent){
	var element = findObj(id);
	if (!element){
		element = document.createElement(tagName);
		element.setAttribute("id", id);
		element.setAttribute("name", id);
		if(typeof(parent)=='undefined'){
			document.body.appendChild(element);
		}else{
			if(parent)parent.appendChild(element);
		}
	}
	return element;
}
function getTarget(e){
	return e.srcElement ? e.srcElement : e.currentTarget;
}
function ULMaker(id){
	this.str = '<ul id="'+id+'">';
	this.addItem = function(link,txt){
		this.str +='<li><a href="';
		this.str += link;
		this.str += '" target="_blank">';
		this.str += txt;
		this.str += '</a></li>';
	}
	this.toString = function(){
		return this.str + '</ul>';
	}
}
function rnd(limit){
    return Math.floor(Math.random()*limit);
}
function sendFriend(url){
	if(window.clipboardData){
	    window.clipboardData.setData("text",url);
	    alert('地址已经放在剪贴板里了，赶快发给朋友吧！');
	}else{
	    alert('您的浏览器不支持使用剪贴板，请直接复制地址发送给朋友们吧！');
	}
}
function AddFavorite(title,url,desc){
	if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')){
		window.sidebar.addPanel(title,url,desc);
	}else{
		window.external.AddFavorite(url,title);
	}
}

function loadPanel(id, s1,template) {
	var obj = findObj(id);
	if (obj && s1) {
		if(template){
			obj.innerHTML = format(template, s1);
		}else{
			obj.innerHTML = format(unescape(obj.innerHTML), s1);
		}
		obj.style.display = "block";
	}
}
function loadRangePanel(id, s1,template,range) {
	var obj = findObj(id);
	if (obj && s1) {
		if(template){
			obj.innerHTML = format(template, s1,range);
		}else{
			obj.innerHTML = format(unescape(obj.innerHTML), s1,range);
		}
		obj.style.display = "block";
	}
}
function format(s, s1,range) {
	var re = "";
	var tmp;
	var len=s1.length;
	if(range)len=range;
	for (var i = 0; i < len && i<s1.length; i++) {
		tmp = s;
		for (var t in s1[i]) {
			tmp = repaceFormat(tmp,  t , s1[i]);
		}
		re += tmp;
	}
	return re;
}
function repaceFormat(s,s1,sr){
	var index=0;
	var re = s;
	var s2 = sr[s1];
	while(true){
		var idx = s.indexOf('{'+s1,index);
		if(idx==-1){
			break;
		}else{
			var index = s.indexOf('}',idx);
			var key = s.substring(idx,index+1);
			if(key.indexOf(':')==-1){
				re = replaceAll(re,key,s2);
			}else{
				var __sf = key.substring(key.indexOf(':')+1,key.length-1);
				var __re = ''
				if(__sf == 'date'){
					var date = new Date(s2);
					__re = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDay();
				}else if(__sf == 'datetime'){
					var date = new Date(s2);
					__re = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDay() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
				}else if(__sf.substring(0, 3) == 'num'){
					__re = s2.toFixed(__sf.length>1?__sf.substring(1):0);
				}else{
					__re = eval(__sf)(s2,s1,sr);
				}
				re = replaceAll(re,key,__re);
			}
		}
	}
	return re;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

//function clearError(){return false;}
//window.onerror=clearError;