// JavaScript Document

//function
function ge(str){return document.getElementById(str);}
function gn(str){return document.getElementsByName(str);}
function ce(str){return document.createElement(str);}
function ac(z){var a=0,b=0;while(z){a+=z.offsetLeft;b+=z.offsetTop;z=z.offsetParent} return [a,b]}
function gE(o,s,n){
	if(o!=null) o=document;
	if(n!=null) n=0;
	return o.getElementsByTagName(s)[n];
}
function getEvent(){     //同时兼容ie和ff的写法　　取得操作对像
         if(document.all)    return window.event;        
         func=getEvent.caller;            
         while(func!=null){    
             var arg0=func.arguments[0];
             if(arg0){
                 if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
                     || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){    
                     return arg0;
                 }
             }
             func=func.caller;
         }
         return null;
 }
 //删除文本节点，DEMO
function cleanWhitespace(node){
	if(node!=null){
	for (var x=0; x<node.childNodes.length; x++) {
		var child = node.childNodes[x];
		//if it's a whitespace text node  && (!notspace.test(child.nodeValue)
		if (child.nodeType == 3){
			node.removeChild(node.childNodes[x]);
			//node.childNodes[x].removeNode();
			x--;
		}
		//elements can have text child nodes of their own
		//if(child.nodeType == 1) {
		//	cleanWhitespace(child);
		//}
	}
	}
}
//主菜单标签

function onmainTag(page){
    var _evt=getEvent();
    var _e=_evt.srcElement || _evt.target;
    
    var mn=ge("mainNav");
    if(mn.current==null) mn.current=mn.getElementsByTagName("li")[0];
    mn.current.className="";    
    mn.current=_e.parentNode.parentNode;    
    mn.current.className="active";
    _e.parentNode.blur();
    
    if(page!=null) window.frames["subPage"].location=page;
}
//左导航菜单控制

function onoffMenu(ostr,cn){
	var _evt=getEvent();
    var _e=_evt.srcElement || _evt.target; 
	var _lt=ge(ostr);
	if(_lt.onText==null){_lt.onText=_e.firstChild.nodeValue;}
	if(_lt.offText==null){_lt.offText=_e.lastChild.innerHTML;}
	if(_e.state==null){_lt.w=_lt.getAttribute("width");}
	if(_e.state==null || _e.state=="on"){
		_lt.getElementsByTagName("div")[0].style.display="none";
		_lt.style.width="0%";
		_e.innerHTML=_lt.offText;
		_e.state="off";
		if(cn!=null) _e.className+=" "+cn;
	}else{
		_lt.style.width=_lt.w;
		_lt.getElementsByTagName("div")[0].style.display="block";
		_e.innerHTML=_lt.onText;
		_e.state="on";
		if(cn!=null) _e.className=_e.className.split(" ")[0];
	}
	_e.blur();
}
//URL重定位

function iframeRefresh(){
	var iframe=window.frames["subPage"];
	if(iframe!=null){
		if(arguments[0]!=null){         //返回首页
			iframe.location=arguments[0];
		}else{                          //刷新
			iframe.location.reload();
		}
	}
	
	//var iframe=gn("subPage")[0];
	//iframe.contentWindow.location.reload();
}
//
function objDisplay(){
	var evt=getEvent();
    var element=evt.srcElement || evt.target;
	 var target=ge(arguments[0]);
	 if(target){
		 if(target.style.display=="block"){
			 target.style.display="none";
			 element.setAttribute("title","开启信息框");
		 }else{
			 target.style.display="block";
			 element.setAttribute("title","半闭信息框");
		 }
	 }
}
//多级菜单
function onControlMenu(page){
	var _evt=getEvent();
    var _e=_evt.srcElement || _evt.target;
	var root=ge("treeNav");
	if(root.deep!=0) root.deep=0;
	if(root.sonAffirm==null || root.sonAffirm==false){
		root.sonAffirm=treeDeep(root,root.deep);
		root.current=new Array();
	}

	var li=_e.parentNode;
	var acsonUL=li.getElementsByTagName("ul")[0];
	
	if(root.current[li.position]!=null){
		if(root.current[li.position]!=li){
			root.current[li.position].className="";
			var cusonUL=root.current[li.position].getElementsByTagName("ul")[0];
			cusonUL!=null?cusonUL.style.display="none":"";
			if(acsonUL!=null){
				acsonUL.style.display="block";
				cleanWhitespace(acsonUL);
				var _s=acsonUL.childNodes;
				for(i=0;i<_s.length;i++){
					_s[i].className="";
					_s[i].getElementsByTagName("ul")[0]!=null?_s[i].getElementsByTagName("ul")[0].style.display="none":"";
				}
				root.current[li.position+1]=null;
			}
			li.className="active";		
			root.current[li.position]=li;		
		}else{
			if(acsonUL!=null){
			   if(acsonUL.style.display=="none"){
				   acsonUL.style.display="block";
			   }else{
				   acsonUL.style.display="none";
			   }
			}
		}		
	}else{	
		if(acsonUL!=null){
			acsonUL.style.display="block";
			cleanWhitespace(acsonUL);
			var _s=acsonUL.childNodes;
			for(i=0;i<_s.length;i++) _s[i].className="";
			root.current[li.position+1]=null;
		}
		li.className="active";		
		root.current[li.position]=li;	
	}
	
	ge("atLocation").innerHTML=writeLocation(_e);
	if(page!=null || page!="") window.frames["subPage"].location=page;
	_e.blur();
}
//遍历树节点

function treeDeep(obj,dep){  
	var curDeep=++dep;
	var ul=obj.getElementsByTagName("ul")[0];
	if(ul!=null){
		cleanWhitespace(ul);
		var children = ul.childNodes;
		for(var i = 0; i < children.length; i++){
			//alert(children[i].innerHTML+"\n"+curDeep);
			children[i].position=curDeep;  //设置深度
			treeDeep(children[i],curDeep); // 递归操作
		}
	}
	//alert(curDeep);
	return true;
} 
function writeLocation(e){    //更改位置
	var str="",b=1;
	var li=e.parentNode;
	var deep=li.position;
	try{
	
	while(deep>0){
		if(b==1){
			str=" > <b>"+li.getElementsByTagName("a")[0].innerHTML+"</b>"+str;
			b=0;
		}else{
			str=" > "+li.getElementsByTagName("a")[0].innerHTML+str;
		}
		li=li.parentNode.parentNode;
		deep=li.position==null?0:li.position;
	}
	}catch(err){
	}
	return " &raquo; 当前位置：OA系统"+str;
}

//按比例缩放

function DrawImage(ImgD,iwidth,iheight){
	var image=new Image();
	if(iwidth==null) iwidth = 580; //定义允许图片宽度
	if(iheight==null) iheight = 435; //定义允许图片高度
	image.src=ImgD.src;
	if(image.width>0 && image.height>0){
		if(image.width/image.height>= iwidth/iheight){
			if(image.width>iwidth){ 
				ImgD.width=iwidth;
				ImgD.height=(image.height*iwidth)/image.width;
			}else{
				ImgD.width=image.width; 
				ImgD.height=image.height;
			}
		}else{
			if(image.height>iheight){ 
				ImgD.height=iheight;
				ImgD.width=(image.width*iheight)/image.height; 
			}else{
				ImgD.width=image.width; 
				ImgD.height=image.height;
			}
		}
	}
} 

//数据操作
function addNewData(){		//添加新表单

	var _evt=getEvent();
    var _e=_evt.srcElement || _evt.target;
	var tbody=_e.parentNode.parentNode.parentNode;
	//get <tbody> node
	tbody=tbody.previousSibling.nodeType==3?tbody.previousSibling.previousSibling:tbody.previousSibling;
	if(tbody.state!="add"){
		cleanWhitespace(tbody);
		if(tbody.tdNum==null) tbody.tdNum=tbody.firstChild.getElementsByTagName("td").length;
		
		var tr=ce("tr");
		var td=ce("td");
		td.setAttribute("colSpan",tbody.tdNum);
		td.innerHTML=ge("formContent").innerHTML;
		td.style.backgroundColor="#fafdfe";
		tr.appendChild(td);
		tbody.appendChild(tr);
		
		if(tbody.current!=null) cancelAddNew(tbody);	
		tbody.current=tr;
		tbody.state="add";
	}
	_e.blur();
}
function cancelAddNew(p){　　//取消添加或修改

	if(p==null){
		var _evt=getEvent();
		var _e=_evt.srcElement || _evt.target;
		var tbody=_e.parentNode.parentNode.parentNode.parentNode;
	}else{
		tbody=p;
	}
	//alert(tbody.addNew);
	if(tbody.current!=null){
		if(tbody.state=="add"){
			tbody.removeChild(tbody.current);
			tbody.current=null;
			tbody.state="";
		}else if(tbody.state=="edit"){
			var tr=tbody.current;
			while(tr.childNodes.length>0){
				tr.removeChild(tr.firstChild);
			}
			for(i=0;i<tbody.tdData.length;i++){
				td=ce("td");
				if(i==0) td.setAttribute("align","center");
				td.innerHTML=tbody.tdData[i];
				tr.appendChild(td);
			}
			tbody.current=null;
			tbody.state="";
			tr.onclick=editData;
		}
	}
	
}
function editData(){
	var _evt=getEvent();
    var _e=_evt.srcElement || _evt.target;
	_e=_e.parentNode;
	cleanWhitespace(_e);
	var tbody=_e.parentNode;
	if(tbody.tdNum==null) tbody.tdNum=_e.childNodes.length;
	var tdData=new Array();
	while(_e.childNodes.length>1){
		tdData.push(_e.firstChild.innerHTML);
		_e.removeChild(_e.firstChild);
	}
	var td=_e.firstChild;
	tdData.push(td.innerHTML);	
	td.setAttribute("colSpan",tbody.tdNum);	
	td.innerHTML=ge("formContent").innerHTML;
	td.getElementsByTagName("a")[1].style.display="inline";
	td.style.backgroundColor="#fafdfe";
	
	if(tbody.current!=null) cancelAddNew(tbody);		
	tbody.current=_e;
	tbody.state="edit";
	tbody.tdData=tdData;	
	
	//添加表单内容
	var li=td.getElementsByTagName("ul")[0].getElementsByTagName("li");
	for(i=0;i<li.length;i++){	
		var f=li[i];
		cleanWhitespace(f);
		f.firstChild.value=tdData[i];
	}
	
	_e.onclick=function(){};
}

function delChildNode(node){
	alert(node.childNodes.length);
	for (var x=0; x<node.childNodes.length; x++) {
		node.removeChild(node.childNodes[x]);
		//node.childNodes[x].removeNode();
	}
}
//on-off   Table have: Thead,Tbody,Tfoot;
function onOffTbody(){
	var _evt=getEvent();
    var _e=_evt.srcElement || _evt.target;
	var tab=_e.parentNode.parentNode.parentNode;
	try{
		var tbody=tab.getElementsByTagName("tbody")[0];
		var tfoot=tab.getElementsByTagName("tfoot")[0];
		if(tbody.style.display=="none" || tbody.style.display==null){
			if(tfoot!=null) tfoot.style.display="";
			tbody.style.display="";
		}else{
			tbody.style.display="none";
			if(tfoot!=null) tfoot.style.display="none";
		}
	}catch(err){
	}
}


//selectStyle
function selectStyle(osel){
	this.source=ge(osel);
	this.ids=osel+"_Style";	
	this.create=function(){
		var div=ce("div");
		div.s=this.source;
		div.id=this.ids;
		div.className="dropSelect";
		div.input=ce("input");
		div.input.setAttribute("readOnly","readonly");
		div.ul=ce("ul");				
		div.appendChild(div.input);					
		div.appendChild(div.ul);
		//div.span.onclick=function(){this.parentNode.ul.style.display="block";}
		div.input.onfocus=this.getFocus;	
		div.onmouseover=function(){
		    this.input.focus();
			this.input.className="shover";
			this.ul.style.display="block";
		}
		div.onmouseout=function(){
			//this.input.className="";
			this.ul.style.display="none";
		}		
		try{
			this.source.parentNode.insertBefore(div,this.source);			
			this.getData();	
		}catch(err){
		}
	}
	this.getData=function(){
		var ssdiv=ge(this.ids);
		ssdiv.input.value=this.source.options[this.source.selectedIndex].text;
		ssdiv.input.selectedIndex=this.source.selectedIndex;
		ssdiv.ul.innrHTML="";
		for(i=0;i<this.source.options.length;i++){
			var li=ce("li");
			li.innerHTML=this.source.options[i].text;
			li.index=i;
			li.onmouseover=function(){this.className="lhover";}
			li.onmouseout=function(){this.className="";}
			li.onclick=this.changeValue;
			ssdiv.ul.appendChild(li);
		}
		//动态设置宽度

		//alert(ssdiv.ul.clientWidth);
		ssdiv.input.style.width=ssdiv.ul.clientWidth+14+"px";
		ssdiv.ul.style.width=ssdiv.ul.clientWidth+14+"px";
		ssdiv.ul.style.display="none";
	}
	this.updata=function(){
	    var ssdiv=ge(this.ids);
		ssdiv.input.value=this.source.options[this.source.selectedIndex].text;
		ssdiv.input.selectedIndex=this.source.selectedIndex;
	}
	this.changeValue=function(){
		var ssdiv=this.parentNode.parentNode;
		ssdiv.s.selectedIndex=this.index;
		ssdiv.input.value=this.innerHTML;
		ssdiv.input.selectedIndex=this.index;
		ssdiv.ul.style.display="none";
		ssdiv.input.className="";
		ssdiv.input.focus();
	}
	this.getFocus=function(){
		this.className="shover";
		this.onblur=function(){this.className="";}
		var ssdiv=this.parentNode;		
		this.onkeydown=function(e){		
			if(document.all) e=window.event;
			if(e.keyCode==38){
				//向上键

				if(ssdiv.input.selectedIndex!=0){
					ssdiv.input.selectedIndex--;
					ssdiv.input.value=ssdiv.ul.getElementsByTagName("li")[ssdiv.input.selectedIndex].innerHTML;
					ssdiv.s.selectedIndex=ssdiv.input.selectedIndex;
				}
			}
			if(e.keyCode==40){
				//向下键

				if(ssdiv.input.selectedIndex!=ssdiv.s.options.length-1){
					ssdiv.input.selectedIndex++;
					ssdiv.input.value=ssdiv.ul.getElementsByTagName("li")[ssdiv.input.selectedIndex].innerHTML;
					ssdiv.s.selectedIndex=ssdiv.input.selectedIndex;
				}
			}
		}
	}
}
//创建对象实例
//var selectArray = null;
function swpaSelect(target){
    //var t=ge(target);
	//var sarray=t.getElementsByTagName("select");
	//selectArray = new Array(sarray.length);
	//for(n=0;n<sarray.length;n++){
		//var name=sarray[n].getAttribute("name") || sarray[n].getAttribute("id");
		//sarray[n].setAttribute("id",name);
		//sarray[n].style.display="none";
		//selectArray[n] = new selectStyle(name);
		//selectArray[n].create();

	//}
}
//更新当前显示数据
function updataSelected(){
    //for(i=0;i<selectArray.length;i++){
        //selectArray[i].updata();
    //}
}
//更新下拉列表值

function updataListData(){
    
}