﻿// JavaScript Document

///*$函数用来查找节点，判断是否有节点
///_$("id")获取对应id的节点
///_$("id", "nodeName")获取对应id节点下名为nodeNamed的节点的集合
///_$(obj, "nodeName")获取obj下名为nodeNamed的节点的集合
function $(idObj, tName, num){
	var obj;
	if(typeof(idObj) == 'object'){
		var xmlText = idObj.responseText;
		if(xmlText != undefined){
			return xmlText;
		}
		else{
			obj = idObj;
		}
	}
	else if(typeof(idObj) == 'string'){
		if(idObj.charAt(0) == '.' && $["."] != undefined){
			for(var i = 0; i < $["."].length; i++){
				if("." + i.toString() == idObj){
					obj = $["."][i];
					break;
				}
			}
		}
		else{
			obj = document.getElementById(idObj);
		}
	}
	else if(idObj == undefined){
		obj = document.body;
	}
	if(obj == undefined){
		return null;	
	}
	obj.data = function(){
		return data(this);
	}
	//如果对象是select,设定values为select的选定值
	if(tName == undefined){
		if(typeof(idObj) == 'string' || idObj == undefined){
			return ((obj == undefined) ? null : obj);
		}
		else{
			if(tName == undefined){
				if(obj.id == undefined || obj.id == ""){
					if($["."] == undefined){
						$["."] = new Array();	
					}
					for(var i = 0; i < $["."].length; i++){
						if($["."][i] == obj){
							return "." + i.toString();
						}
					}
					$["."][$["."].length] = obj;
					return "." + ($["."].length - 1).toString();
				}
				else{
					return obj.id;
				}
			}
		}
	}
	else if(typeof(tName) == 'boolean'){
		if(tName){
			obj.style.display = "block";	
		}
		else{
			obj.style.display = "none";	
		}
		return;
	}
	else if(typeof(tName) == 'string'){
		if(tName == "_v"){
			if(obj.nodeName.toLowerCase() == "select"){
				var value = "";
				for(var i = 0; i < obj.options.length; i++){
						if(obj.options[i].selected){
							if(value != ""){
								value += ",";	
							}
							value += obj.options[i].value;
						}
					}
				return value;
			}
			//如果对象是checkbox或者radio,设定values 为选中的值
			else if(obj.nodeName.toLowerCase() == "input" && (obj.type == "checkbox" || obj.type == "radio")){
				var value = "";
				var allInputs = $($(), "input");
				for(var j = 0; j < allInputs.length; j++){
					if((allInputs[j].type == "checkbox" || allInputs[j].type == "radio") && allInputs[j].name == obj.name){
						if(allInputs[j].checked){
							if(value != ""){
								value +=","
							}
							value += allInputs[j].value;
							if(allInputs[j].type == "radio"){
								break;
							}
						}
					}
				}
				return value;
			}
			else if(obj.nodeName.toLowerCase() == "input" || obj.nodeName.toLowerCase() == "textarea"){
				return obj.value;	
			}
			else{
				return obj.innerHTML;	
			}
		}
		else if(tName == "_a"){
			if(typeof(num) == 'number'){
				if(num < 0){
					num = 0;	
				}
				else if(num > 100){
					num = 100;
				}
				if(num == 100){
					obj.style.opacity = "";
					obj.style.filter = "";
					return;
				}
				else{
					obj.style.opacity = (num >= 10) ? Number("0." + num.toString()) : Number("0.0" + num.toString());
				}
				obj.style.filter = "Alpha(Opacity=" + num.toString() + ")";
				return;
			}
			else{
				if(obj.style.opacity != undefined){
					if(obj.style.opacity.toString() != ""){
						return Math.floor(obj.style.opacity*100);
					}
					else{
						return 100;	
					}
				}
				else{
					if(obj.style.filter.match(/alpha\(opacity\=\w+(?=\))/) != undefined){
						return Math.floor(Number(obj.style.filter.match(/alpha\(opacity\=\w+(?=\))/)[0].replace(/alpha\(opacity\=/, "")));
					}
					else{
						return 100;
					}
				}
			}
		}
		else if(tName == "_X"){
			if(typeof(num) == 'number'){
				var X = 0;
				var temp = obj;
				while(temp = temp.offsetParent){
					X += temp.scrollLeft;	
				}
				obj.style.left = (X + num).toString() + "px";
				return;
			}
			else{
				var X = obj.offsetLeft;
				while(obj = obj.offsetParent)
				{
					X += obj.offsetLeft - obj.scrollLeft;
				}
				return X;
			}
		}
		else if(tName == "_Y"){
			if(typeof(num) == 'number'){
				var Y = 0;
				var temp = obj;
				while(temp = temp.offsetParent){
					Y += temp.scrollTop;	
				}
				obj.style.top = (Y + num).toString() + "px";
				return;
			}
			else{
				var Y = obj.offsetTop;
				while(obj = obj.offsetParent)
				{
					Y += obj.offsetTop - obj.scrollTop;
				}
				return Y;
			}
			
		}
		else if(tName == "_x"){
			if(typeof(num) == 'number'){
				obj.style.marginLeft = num.toString() + "px";
				return;
			}
			else{
				return obj.style.marginLeft.replace(/px/g, "") == "" ? 0 : parseInt(obj.style.marginLeft.replace(/px/g, ""));
			}
		}
		else if(tName == "_y"){
			if(typeof(num) == 'number'){
				obj.style.marginTop = num.toString() + "px";
				return;
			}
			else{
				return obj.style.marginTop.replace(/px/g, "") == "" ? 0 : parseInt(obj.style.marginTop.replace(/px/g, ""));
			}
		}
		else if(tName == "_w"){
			if(typeof(num) == 'number'){
				var pl = obj.style.paddingLeft.replace(/px/g, "") == "" ? 0 : parseInt(obj.style.paddingLeft.replace(/px/g, ""));
				var pr = obj.style.paddingRight.replace(/px/g, "") == "" ? 0 : parseInt(obj.style.paddingRight.replace(/px/g, ""));
				obj.style.width = (num - (pl + pr)).toString() + "px";
				return;
			}
			else{
				return obj.clientWidth;
			}
		}
		else if(tName == "_h"){
			if(typeof(num) == 'number'){
				var pt = obj.style.paddingTop.replace(/px/g, "") == "" ? 0 : parseInt(obj.style.paddingTop.replace(/px/g, ""));
				var pb = obj.style.paddingBottom.replace(/px/g, "") == "" ? 0 : parseInt(obj.style.paddingBottom.replace(/px/g, ""));
				obj.style.height = (num - (pt + pb)).toString() + "px";
				return;
			}
			else{
				return obj.clientHeight;
			}
		}
		else if(typeof(tName) == 'string' && tName.charAt(0) != '_'){
			var objs = obj.getElementsByTagName(tName);
			if(num == undefined){
				return objs;
			}
			else if(typeof(num) == 'boolean'){
				for(var i = 0; i < objs.length; i++){
					$(objs[i], num);
				}
				return;
			}
		}
	}
}

//*
///* 获取网页的url传值
function G(name){ 
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r!=null){
		return unescape(r[2]);
	}
	else{
		return null;
	}
}
//*/

//*/
///*设置或获取cookie值
///_C("name")获取name的cookie值
///_C("name", "value")添加或设置name的值为value
///_C("name", "value", Date)添加或设置name的值为value,过期时间为Date
///_C("name", Date)设置name的过期时间为Date
function C(name, value, expires){
	if(value == undefined){
		var strCookie = unescape(document.cookie);
		var arrCookie=strCookie.split("; ");
		for(var i=0;i<arrCookie.length;i++){
			var arr=arrCookie[i].split("=");
			if(arr[0]==name){
				if(arr.length>1){
					return	arr[1];
				}
				else{
					return null;
				}
			}
		}
		return null;
	}
	else if(typeof(value) == 'object'){
		document.cookie = name + "=" + escape(_C(name)) + "; path=/; " + "expires=" + value.toGMTString();
	}
	else if(typeof(value) == 'string'){
		document.cookie = name + "=" + escape(value) + "; path=/; " + ((expires == undefined) ? "" : "expires=" + expires.toGMTString());
	}
}

//*/
///*设置cookie过期时间
///_T("addSecond")设置时间为当前时间+addSecond
///_T()设置时间为当前时间
function T(addSecond){
	var tempTime = new Date();
	tempTime.setTime(tempTime.getTime() + ((addSecond == undefined) ? 0 : 1000 * addSecond));
	return tempTime;
}
//*/
//获取一个随机整数,指定最大整数和最小整数,不指定表示1-100之间的数
function R(a, b){
	return Math.ceil((Math.random()*100));
}

///*执行XMLrespone操作,输入data为字符,表示post提交,尾部输入函数表示提交错误后执行的函数
///执行前请先使用_["name"]=function(){}来设定请求完成后需要执行的函数
///_("name", "url", other)使用_("name")的xmlHttp发送GET的"url"请求,若请求成功并返回结束后执行handler函数,失败则执行other函数
///_("name", "url", "data", other)使用_("name")的xmlHttp发送POST("data")的"url"请求,若请求成功后执行_["name"](xmlHttp)函数,失败则执行_["name"](false)函数
function _(name, arg1, arg2, arg3){
	if(name === null){
		if(_["."] != undefined){
			_(_["."], null);	
		}
		return;
	}
	else if(typeof(name) != 'string' || name == ""){
		return _["."];//表示当前执行函数的名称
	}
	if(arg1 === null){
		_(name, false);
		_[name + ".argument"] = null;
		_[name + ".type"] = null;
		_[name + ".able"] = null;
		_[name + ".load"] = null;
		_[name + ".interval"] = null;
		return;
	}
	var args = new Array(arg1, arg2, arg3);
	var url;//ajax请求地址
	var data;//ajax post数据,
	var able;//异步函数执行许可
	var time;//异步调用时间间隔
	for(var i = 0; i < args.length; i++){
		if(typeof(args[i]) == 'string'){
			if(url == undefined && time == undefined){
				url = args[i];	
			}
			else if(time == undefined){
				data = args[i];	
			}
			else{
				return;	
			}
		}
		else if(typeof(args[i]) == 'number'){
			if(url == undefined){
				time = args[i];
			}
			else{
				return;	
			}
		}
		else if(typeof(args[i]) == 'boolean'){
			able = args[i];	
		}
	}
	
	//强制调用函数
	if(url == undefined && time == undefined && able == undefined){
		if(typeof(_[name]) == 'function'){
			_["." + name] = _["."];
			_["."] = name;
			try{
				_[name](_[name + ".argument"]);
			}
			catch(e){
				_[name + ".error"] = e;
			}
			_["."] = _["." + name];
		}
		return;
	}
	
	//设定函数为可以运行状态
	_[name + ".able"] = true;	
	//设定函数为定时函数
	if(time != undefined){
		_[name + ".time"] = time;
	}
	if(able != undefined){
		_[name + ".able"] = able;
		if(url == undefined && time == undefined  && _[name + ".able"] && _[name + ".type"] == "ajax" && _[name + ".load"]){
			_(name);
			return;
		}
	}
	
	//设定函数异步调用时间
	if(url == undefined && ((time != undefined) || (_[name + ".type"] == "interval"))){
		_[name + ".type"] = "interval";
		if(typeof(_[name + ".argument"] != 'number')){
			_[name + ".argument"] = 0;
		}
		if(_[name + ".able"]){
			clearInterval(_[name + ".interval"]);
			try{
			_[name + ".interval"] = setInterval(function(){_[name + ".argument"] += _[name + ".time"];_(name);}, _[name + ".time"]);
			}
			catch(e){
			}
		}
		else{
			clearInterval(_[name + ".interval"]);
		}
		return;
	}

	if(url == undefined){
		return;	
	}
	_[name + ".type"] = "ajax";
	_[name + ".load"] = false;
	if(able == undefined){
		_[name + ".able"] = true;
	}
	
	try{
		_[name + ".argument"] = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e){
		try{
			_[name + ".argument"] = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E){
			_[name + ".argument"] = null;
		}
	}
	if(!_[name + ".argument"] && typeof(XMLHttpRequest) != undefined){
		_[name + ".argument"] = new XMLHttpRequest();
	}
	if(_[name + ".argument"] == undefined){
		_[name + ".error"] = "xmlHttp is null";
		return;
	}
	_[name + ".argument"].onreadystatechange = function(){
		if(_[name + ".argument"].readyState == 4){
			_[name + ".load"] = true;
			if(_[name + ".argument"].status < 400){	
				if(_[name + ".able"]){
					_(name);
				}
			}
			else{
				_[name + ".argument"] = false;
				if(_[name + ".able"]){
					_(name);
				}
			}
		}
	}
	var now = new Date();
	now.setTime(now.getTime() + 0);
	var arrUrl = url.split("?");
	url = arrUrl[0] + "?" + now.toString().replace(/\W/g, "_");
	if(arrUrl.length > 1){
		url += "&" + arrUrl[1];
	}
	try{
		if(typeof(data) == 'string'){
			_[name + ".argument"].open("POST", url, true);
			_[name + ".argument"].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			_[name + ".argument"].send(data);
		}
		else{
			_[name + ".argument"].open("GET", url, true);
			_[name + ".argument"].send(null);
		}
	}
	catch(e){
		_[name + ".error"] = e;
	}
}
//*/
//*/
//用来取值的函数,将值转换为get, post(data)的形式,并escape包装
function data(obj_or_id){
	var obj;
	if(obj_or_id == undefined){
		obj = $();
	}
	else if(typeof(obj_or_id) == 'string'){
		obj = $(obj_or_id);
	}
	else if(typeof(obj_or_id) == 'object'){
		obj = obj_or_id;
	}
	else{
		return "";
	}
	var tag =  obj.nodeName.toLowerCase();
	var names = new Array();
	var objs = new Array();
	if(tag != "input" && tag != "select" && tag != "textarea"){
		var inputs = $(obj, "input");
		var selects = $(obj, "select");
		var textareas = $(obj, "textarea");
		for(var i = 0; i < inputs.length; i++){
			objs[objs.length] = inputs[i]	
		}
		for(var i = 0; i < textareas.length; i++){
			objs[objs.length] = textareas[i]	
		}
		for(var i = 0; i < selects.length; i++){
			objs[objs.length] = selects[i]	
		}
	}
	else{
		objs[0] = obj;
	}
	var data = "";
	for(var i = 0; i < objs.length; i++){
		var able = true;
		for(var j = 0; j < names.length; j++){
			if(names[j] == objs[i].name){
				able = false;
				break;
			}
		}
		if(!able || objs[i].name == undefined){
			continue;	
		}
		if(objs[i].type == "text" || objs[i].type == "password" || objs[i].type == "hidden" || objs[i].type == "textarea"){
			if(data != ""){
				data += "&";
			}
			names[names.length] = objs[i].name;
			data += escape(objs[i].name) + "=" + escape(objs[i].value);
		}
		else if(objs[i].type == "checkbox" || objs[i].type == "radio" || objs[i].nodeName.toLowerCase() == "select"){
			if(data != ""){
				data += "&";
			}
			names[names.length] = objs[i].name;
			data += escape(objs[i].name) + "=" + escape($(objs[i], "_v"));
		}
	}
	return data;
}

function $$(obj_or_id, name, num, time, speed){
	var obj;
	var id;
	if(typeof(obj_or_id) == 'string'){
		obj = $(obj_or_id);
		id = obj_or_id;
	}
	else if(typeof(obj_or_id) == 'object'){
		obj = obj_or_id;
		id = $(obj_or_id);
	}
	else{
		return;	
	}
	var	time = 30;
	var	speed = 3;
	_[id + ".$$." + name] = function(){
		var old = $(obj, name);
		if(old == num){
			if(typeof($[id]) == 'function'){
				$[id](id);
			}
			_(null);
		}
		else if(num > old){
			$(obj, name, old + Math.ceil(((num - old)/speed)));	
		}
		else if(num < old){
			$(obj, name, old + Math.floor(((num - old)/speed)));
		}
	}
	_(id + ".$$." + name, time);
}

//给对象增加拖动功能
function D(obj, obj2){
	obj.onmousedown = function(ev){
		D["."] = this;
		
		var temp = document.createElement(this.nodeName);
		temp.innerHTML = this.innerHTML;
		this.parentNode.insertBefore(temp, this);
		D[".temp"] = temp;
		$(temp, "_a", 50);
		D["._X"] = $(temp, "_X");
		D["._Y"] = $(temp, "_Y");
		temp.style.position = "absolute";
		
		ev = ev || window.event;
		D[".mx"] = document.body.scrollLeft + ev.clientX;
		D[".my"] = document.body.scrollTop + ev.clientY;
		if(typeof(D[$(D["."])]) == 'function'){
			D[$(D["."])]($(D[".temp"], "_X"), $(D[".temp"], "_Y"));
		}
		document.onmouseup = function(){
			document.onmouseup = null;
			document.onmousemove = null;
			if(typeof(D[$(D["."])]) == 'function'){
				D[$(D["."])]($(D[".temp"], "_X"), $(D[".temp"], "_Y"));
			}
			D[".temp"].parentNode.removeChild(D[".temp"]);
			D[".temp"] == null;
		}
		document.onmousemove = function(ev){
			ev = ev || window.event;
			var x = document.body.scrollLeft + ev.clientX;
			var y = document.body.scrollTop + ev.clientY;
			var X = D["._X"] + (x - D[".mx"]);
			var Y = D["._Y"] + (y - D[".my"]);
			$(D[".temp"], "_X", X);
			$(D[".temp"], "_Y", Y);
			
			if(typeof(D[$(D["."])]) == 'function'){
				D[$(D["."])](X, Y);
			}
		}
	}
}