/*******************************************************
 *	File: netrain library
 *	Author: starain
 *	Version: 1.0.0
 *	CreateTime：2006-07-13
 *	LastUpdate：2007-01-19
 *	Copyright: (C) 2005 Starain All rights reserved.
 *	Web: http://www.starain.com
 *******************************************************/

if(! Netrain) var Netrain = {};

Netrain.Util = {
	getArgsFromHref : function(sHref, sArgName, tag){
    if(!tag) tag = "?";
		var args  = sHref.split(tag);
		var retval = "";
		if(args[0] == sHref){ /*参数为空*/
			return retval; /*无需做任何处理*/
		}
		var str = args[1];
		args = str.split("&");
		for(var i = 0; i < args.length; i ++){
			str = args[i];
			var arg = str.split("=");
			if(arg.length <= 1) continue;
			if(arg[0] == sArgName) return arg[1];
		}
		return "";
	},
	getFromList : function(ops, def){
		for(var i=0; ops && i<ops.length; i++){
			if(ops[i][0]==def) return ops[i][1];
		}
		return "";
	},
  getNullStr : function(str){
    return str==null ? "" : str==undefined ? "" : str;
  },
  reimg : function(theThis){
    if(theThis.src.indexOf("?")>-1){
      theThis.src = theThis.src.split("?")[0]+"?t="+Math.random();
    }
    theThis.src = theThis.src+"?t="+Math.random();
  }
}

Netrain.Bind = {
	fromBeanTags : "|INPUT|SELECT|TEXTAREA|",
	getFormBean : function(id){
		var ret = new Object();
		try{
      var c=document.getElementById(id);
			var nds = c.getElementsByTagName("*");
			for(var i=0; i<nds.length; i++){
				if(nds[i].nodeType != 1)continue;
				var ndt = "|" + nds[i].tagName.toUpperCase() + "|";
				if(Netrain.Bind.fromBeanTags.indexOf(ndt) != -1){
					var v = Netrain.Bind.getValue(nds[i]);
					if(v){
            if(nds[i].id + "" != ""){
              ret[nds[i].id + ""] = v;
            }
            else if(nds[i].name + "" != ""){
              ret[nds[i].name + ""] = v;
            }
          }
				}
			}
			return ret;
		}
		catch (e) {
			return ret;
		}
	},
	getValue : function(ele){
		var elet = null;
		if(ele) elet = ele.tagName.toUpperCase();
		switch (elet) {
			case "INPUT" :
				var elty = (ele.type+"").toUpperCase();
				if(elty=="SUBMIT" || elty=="RESET"){
					return undefined;
				}
        else if(elty=="CHECKBOX"){
          return ele.checked ? 1 : 0;
        }
        else if(elty=="RADIO"){
          return ele.checked ? ele.value : false;
        }
				return ele.value;
			case "TEXTAREA" :
				return ele.value;
			case "SELECT" :
				return ele.value;
			default :
				return undefined;
		}
	}
}

Netrain.Fom = {
	createSelect : function(id, name, ops, def){
		var ret = '<select';
		if(id) ret += ' id="' + id +'"';
		if(name) ret += ' name="' + name +'"';
		ret += '>';

		for(var i=0; ops && i<ops.length; i++){
			var sf = ops[i][0]==def ? ' selected="true"' : "";
			ret += '<option value="'+ ops[i][0] +'"'+ sf +'>'+ ops[i][1] +'</option>';
		}

		ret += '</select>';
		return ret;
	},
	createRadioGrp : function(id, name, ops, def){
	
	}
}

Netrain.Feature = {
	ModalBox : function(){
		this.style = 0;//[0,1]
		this.overlay = null;
		this.container = null;
		this.caption = null;
		this.tBox = null;
		this.cBox = null
		this.main = null;
		this.capHeight = 25;
		this.width = 450;
		this.height = 300;
		this.title = "";
		this.close = "关闭";
		this.inHTML = "";
		this.inNode = null;
		this.loader = "images/modalloader.gif";

		this.init();
	}
}

Netrain.Feature.Taps={
	init : function(id, w, h){
		if(!w) w = 400;
		if(!h) h = 300;
		var taps = document.getElementById(id);
		var as = null;

		for(var i=0; i<taps.childNodes.length; i++){
			if(taps.childNodes[i].nodeType != 1) continue;
			if(taps.childNodes[i].tagName == "UL"){
				as = taps.childNodes[i].getElementsByTagName("A");
				for(var j=0; as && j<as.length; j++){
					as[j].setAttribute("taps", id);
					as[j].setAttribute("idx", j);
					if (window.addEventListener){
						as[j].removeEventListener("click",Netrain.Feature.Taps.achclick,true);
						as[j].addEventListener("click",Netrain.Feature.Taps.achclick,false);
					}
					else{
						as[j].onclick = Netrain.Feature.Taps.achclick;
					}
					as[j].href = "javascript:void(0);";
				}
			}
			else if(taps.childNodes[i].tagName == "DIV"){
				taps.childNodes[i].style.display = "none";
				taps.childNodes[i].style.width = w + "px";
				taps.childNodes[i].style.height = h + "px";
			}
		}
		if(as && as[0]) Netrain.Feature.Taps.achclick.call(as[0],[as[0]]);//as[0].onclick();
	},
	achclick : function(evt){
		var theSrc = this;
		var taps = document.getElementById(theSrc.getAttribute("taps"));
		var sum = 0;
		for(var i=0; i<taps.childNodes.length; i++){
			if(taps.childNodes[i].nodeType != 1) continue;
			if(taps.childNodes[i].tagName == "UL"){
				as = taps.childNodes[i].getElementsByTagName("LI");
				for(var j=0; as && j<as.length; j++){
					as[j].className = "";
					as[j].removeAttribute("className");
				}
			}
			else if(taps.childNodes[i].tagName == "DIV"){
				if(sum==theSrc.getAttribute("idx"))
					taps.childNodes[i].style.display = "block";
				else
					taps.childNodes[i].style.display = "none";
				sum++;
			}
		}
		var li = theSrc.parentNode;
		li.className = "anchorson";
		li.setAttribute("className", "anchorson");
	}
}

Netrain.Feature.ModalBox.prototype = {
	init : function(){
		this._createOverlay();
		this._createContainer();
	},
	_createOverlay : function(){
		this.overlay = document.createElement("div");
		this.overlay.className = "nr_ft_mb_overlay";
		this.overlay.style.display = "none";
		document.body.appendChild(this.overlay);
	},
	_createContainer : function(){
		this.container = document.createElement("div");
		this.container.className = "nr_ft_mb_container";
		this.container.style.left = "50%";
		this.container.style.top = "50%";
		this.container.style.display = "none";
		this.caption = document.createElement("div");
		this.caption.className = "nr_ft_mb_caption";
		this.caption.style.display = "none";
		this.main = document.createElement("div");
		this.main.className = "nr_ft_mb_main";
		this.main.style.display = "none";
		this.tBox = document.createElement("span");
		this.tBox.className = "nr_ft_mb_title";
		this.cBox = document.createElement("span");
		this.cBox.className = "nr_ft_mb_close";

		var funRef = this.show
		var theThis = this;
		this.cBox.onclick = function(){
			funRef.apply(theThis);
		}


		document.body.appendChild(this.container);
	},
	_showOverlay : function(flag){
		//if(!this.overlay) this._createOverlay();
		if(flag){
			var sW,sH;
			sW = document.body.offsetWidth;
			sH = document.body.offsetHeight;
			if (sH < screen.height){
				sH = screen.height;
			}
			this.overlay.style.width = sW + "px";
			this.overlay.style.height = sH + "px";
			document.body.style.overflow = "hidden";
			this.overlay.style.display = "inline";
		}
		else{
			this.overlay.style.width = "0px";
			this.overlay.style.height = "0px";
			document.body.style.overflow = "auto";
			this.overlay.style.display = "none";
		}
	},
	show : function(flag){
		//if(!this.container) this.createContainer
		this._showOverlay(flag);
		if(flag && this.inHTML){
			this.main.innerHTML = this.inHTML;
			this.inHTML = "";
		}
		else if(flag && this.inNode){
			this.main.appendChild(this.inNode);
			this.inNode = null;
		}
		if(this.style==0)
			this._show0(flag);
		else
			this._show1(flag);
	},
	_show0 : function(flag){
		if(flag){
			this._resize0(this.width, this.height);
			this.tBox.innerHTML = this.title;
			this.cBox.innerHTML = this.close;

			this.caption.appendChild(this.cBox);
			this.caption.appendChild(this.tBox);
			this.container.appendChild(this.caption);
			this.container.appendChild(this.main);

			this.container.style.top = document.documentElement.scrollTop +
					document.documentElement.clientHeight/2 + "px";
			this.container.style.display = "block";
		}
		else{
			this.container.innerHTML = "";
			this.container.style.display = "none";
		}
	},
	_show1 : function(flag){
		if(flag){
			this._resize1(this.width, this.height);
			this.tBox.innerHTML = this.title;
			this.cBox.innerHTML = this.close;

			this.caption.appendChild(this.cBox);
			this.caption.appendChild(this.tBox);
			this.container.appendChild(this.main);
			this.container.appendChild(this.caption);

			this.container.style.display = "block";
		}
		else{
			this.container.innerHTML = "";
			this.container.style.display = "none";
		}
	},
	resize : function(w, h){
		if(this.style==0)
			this._resize0(w, h);
		else
			this._resize1(w, h);
	},
	_resize0 : function(w, h){
		this.container.style.width = w + "px";
		this.container.style.height = (h + this.capHeight) + "px";
		this.container.style.marginLeft = "-" + w/2 + "px";
		this.container.style.marginTop = "-" + (h + this.capHeight)/2 + "px";

		this.caption.style.width = w + "px";
		this.caption.style.height = this.capHeight + "px";
		this.caption.style.display = "block";
		this.main.style.width = w + "px";
		this.main.style.height = h + "px";
		this.main.style.display = "block";
	},
	_resize1 : function(w, h){
		this.container.style.width = w + "px";
		this.container.style.height = (h + this.capHeight) + "px";
		this.container.style.marginLeft = "-" + w/2 + "px";
		this.container.style.marginTop = "-" + (h + this.capHeight)/2 + "px";

		this.caption.style.width = w + "px";
		this.caption.style.height = this.capHeight + "px";
		this.caption.style.display = "block";
		this.main.style.width = w + "px";
		this.main.style.height = h + "px";
		this.main.style.display = "block";
	},
	showLoader : function(){
		this.tBox.innerHTML = this.title + ' <img style="margin-top:4px;" src="'+ this.loader +'">';
		//this.tBox.style.marginTop = "7px";
	},
	termLoader : function(){
		this.tBox.innerHTML = this.title;
	},
	setTitle : function(str){
		this.title = str;
		this.tBox.innerHTML = this.title;
	},
	setInnerHTML : function(inStr){
		this.main.innerHTML = inStr;
	},
	trem : function(){
		//this.overlay = null
		document.body.removeChild(this.container);
		document.body.removeChild(this.overlay);
	}
}

Netrain.Loader = {
	loaderId : "nr_loader",
	loaderimg : "images/modalloader.gif",
	count : 0,
	show : function(disabled){
		var lp = document.getElementById(Netrain.Loader.loaderId);
		if (!lp) {
			var el = document.createElement('DIV');
			el.setAttribute("id", Netrain.Loader.loaderId);
			el.className = "nr_loader";
			el.innerHTML = '&nbsp;<img src="'+ Netrain.Loader.loaderimg +'" /> loading... ';
			el.onclick = function(){
				Netrain.Loader.count = 0;
				this.style.display = "none";
			}
			document.body.appendChild(el);
			lp = el;
		}
		if (disabled) {
			if(Netrain.Loader.count>0) Netrain.Loader.count--;
			if(Netrain.Loader.count<1) lp.style.display="none";
		}
		else {
			Netrain.Loader.count++;
			lp.style.display="block";
			lp.style.top = document.body.scrollTop+1;
		}
	},
	hide : function(){
		Netrain.Loader.show(true);
	}
}

/* Extent Mozilla */
if(!window.attachEvent){
  window.attachEvent = function(sEvent, oEvent){
    if(sEvent == "onload"){
      var func = window.onload;
	  window.onload = function(){
	    if(func)func();
        oEvent();
	  }
    }
  }
}

/**/
function printObject(obj){
  var out="obj:{\n";
  for (element in obj){
     out +=element+" = "+obj[element]+";"+"\n";
  }
  out +="}";
  alert(out);
}
