﻿function Request()
{
    var object=this;
    
    this.Url;
    this.Method="Get";
    this.Callback;
    this.Data;
    this.Enctype="application/x-www-form-urlencoded";
    
    if(!window.XMLHttpRequest) window.XMLHttpRequest=function()
	{
		var progIDs=["Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP","Microsoft.XMLHTTP"];
		for(var i=0;i<progIDs.length;i++)
		{
			try
			{
				var xmlHttp=new ActiveXObject(progIDs[i]);
				return xmlHttp;
			}
			catch(ex)
			{			
			}
		}
		return null;
	};
    
    this.Start=function Start()
    {    
        var httpRequest=new XMLHttpRequest();  	     	
		httpRequest.onreadystatechange = function()
		{			   
			if (httpRequest.readyState==4)
			{        
			    if (object.Callback)
			    {  
				  object.Callback(httpRequest);
				}
			}
		}
		httpRequest.open(this.Method,this.Url,true);		
		if (this.Method=="Post") httpRequest.setRequestHeader("Content-Type", this.Enctype);
		httpRequest.send(this.Data);	
    }
}

function GetLeft(src){
	var left=0;
	while(src){
		left+=src.offsetLeft;
		src=src.offsetParent;
	}
	return left;
}
function GetTop(src){
	var top=0;
	while(src){
		top+=src.offsetTop;
		src=src.offsetParent;
	}
	return top;
}
function Cookie()
{	
	this.SetCookie = function(sName, sValue, nExpireDays, sDomain, sPath)
	{
		var sCookie = sName+"="+escape(sValue)+";";
		if (nExpireDays)
		{
			var oDate = new Date();
			oDate.setMilliseconds(oDate.getMilliseconds()+parseInt(nExpireDays)*24*3600*1000);
			sCookie += "expires="+oDate.toUTCString()+";";
		}
		if (sDomain)
		{
			sCookie += "domain="+sDomain+";";
		}
		if (sPath)
		{
			sCookie += "path="+sPath+";"
		}
		
		document.cookie = sCookie;
	}
	
	this.GetCookie = function(sName)
	{
		var aCookie = document.cookie.split("; ");
		for (var i=0; i < aCookie.length; i++)
		{
			var aCrumb = aCookie[i].split("=");
			if (sName == aCrumb[0])
			{
				return aCrumb.length>=2 ? unescape(aCrumb[1]) : "";
			}
		}
		return null;
	}
}
function Dialog(title,x,y,width,height)
{
	var object=this;
	var dialogMask;

    this.Title=title?title:"无标题";
    this.Width=width?width:300;
    this.Height=height?height:200;
    this.X=x?x:(document.documentElement.clientWidth-this.Width)/2;
    this.Y=y?y:document.documentElement.scrollTop+(document.documentElement.clientHeight-this.Height)/2;

    this.SetTitle=function (title)
    {
        this.Title=title;
        headerCenter.innerHTML=this.Title;  
    }   
    this.SetXY=function (x,y)
    {
       this.X=x;
       this.Y=y;
       this.Dialog.style.left=this.X+"px";
       this.Dialog.style.top=this.Y+"px";
    }    
    
   
    this.SetRange=function (width,height)
    {
        this.Width=width;
        this.Height=height;
        this.Dialog.style.width=this.Width+"px";
        this.Dialog.style.top=this.Height+"px";
    }
    
    this.Show=function ()
    {
       this.Dialog.style.display="block";
    }    
    this.Hide=function ()
    {
       if (dialogMask)
       {
          dialogMask.style.display="none";
       }
       this.Dialog.style.display="none";
    }
    
    this.Close=function ()
    {
       if (dialogMask)
       {
           dialogMask.parentNode.removeChild(dialogMask);
       }
       this.Dialog.parentNode.removeChild(this.Dialog);
       this.Dispose();
    }
    
    this.Dispose=function ()
    {       
      // delete this.Dialog;
       this.Dialog=null;
       delete dialogMask;
       dialogMask=null;
       delete object;
       object=null;
    }
    
    if (window.attachEvent) {
		window.attachEvent("onunload", this.Dispose);
	}
	else {
		window.addEventListener("unload", this.Dispose, false);
	};
    
    this.Alert=function (message)
    {
        this.showModelessDialog();
        this.Dialog.childNodes[1].innerHTML="<div class='DialogContentLeft'><img border=0 src='../images/error.gif'></div><div class='DialogContentRight'>"+message+"</div><div class='DialogContentButton'><INPUT class='Button' type='button' value='  确 定  ' onmouseover=\"javascript:this.className='ButtonOver'\" onmouseout=\"javascript:this.className='Button'\" onclick='javascript:this.parentNode.parentNode.parentNode.Dialog.Close();this.parentNode.parentNode.parentNode.Dialog.Alert.Dispose();'></div>";
        this.Dialog.lastChild.style.height=this.Dialog.offsetHeight+"px";
    }
    this.Confirm=function (message)
    {
        this.showModelessDialog();
        this.Dialog.childNodes[1].innerHTML="<div class='DialogContentLeft'><img border=0 src='../images/error.gif'></div><div class='DialogContentRight'>"+message+"</div><div class='DialogContentButton'><INPUT class='Button' type='button' value=' 确 定 ' onmouseover=\"javascript:this.className='ButtonOver'\" onmouseout=\"javascript:this.className='Button'\" onclick='javascript:this.parentNode.parentNode.parentNode.Dialog.Close();this.parentNode.parentNode.parentNode.Dialog.Confirm.OK();'>&nbsp;&nbsp;<INPUT class='Button' type='button' value=' 取 消 ' onmouseover=\"javascript:this.className='ButtonOver'\" onmouseout=\"javascript:this.className='Button'\" onclick='javascript:this.parentNode.parentNode.parentNode.Dialog.Close();this.parentNode.parentNode.parentNode.Dialog.Confirm.Cancel();'></div>";
        this.Dialog.lastChild.style.height=this.Dialog.offsetHeight+"px";
    }
    this.showModelessDialog=function ()
    {
        dialogMask=document.createElement("div");
        dialogMask.className="DialogMask";
        document.body.appendChild(dialogMask);	
        dialogMask.style.height=document.documentElement.offsetHeight+"px";
    }


    
	var dialog=this.Dialog=document.createElement("div");
	dialog.className="Dialog";
	dialog.Dialog=this;
    document.body.appendChild(dialog);	
    
           
    dialog.style.left=this.X+"px";
	dialog.style.top=this.Y+"px";
	dialog.style.width=this.Width+"px";	
	
	var header=document.createElement("div");
	header.className="DialogHeader";
	dialog.appendChild(header);
	
	var headerLeft=document.createElement("div");
	headerLeft.className="DialogHeaderLeft";
	header.appendChild(headerLeft);
	
    var expandImage=document.createElement("img");
	expandImage.className="ModuleHeaderExpandImage";
	expandImage.setAttribute("State","Expand");
	expandImage.src=baseUrl+"/images/expand.gif";
	expandImage.onmouseover=function () {this.src=baseUrl+"/images/"+this.getAttribute("State")+"_hover.gif";};
	expandImage.onmouseout=function () {this.src=baseUrl+"/images/"+this.getAttribute("State")+".gif";};
	expandImage.onmousedown=function (e) 
	{
		if (e)
		{
			window.event=e;
		}
   		window.event.cancelBubble = true; 
   		var content=this.parentNode.parentNode.parentNode.childNodes[1];
   		var footer=this.parentNode.parentNode.parentNode.childNodes[2];
		if (this.getAttribute("State")=="Expand")
		{    	   	   
       	   	this.title="展开";
      		this.setAttribute("State","Collapse");
			content.style.display="none";
			footer.style.display="none";
		}
		else
		{
			this.title="折叠";
      		this.setAttribute("State","Expand");
			content.style.display="block";
			footer.style.display="block";
		}
		content=null;
		footer=null;
  		this.src=baseUrl+"/images/"+this.getAttribute("State")+"_hover.gif"; 
	} 
	headerLeft.appendChild(expandImage);
	
	
	var headerCenter=document.createElement("div");
	headerCenter.className="DialogHeaderCenter";
	headerCenter.innerHTML=this.Title;		
	header.appendChild(headerCenter);		
			
	var headerRight=document.createElement("div");
	headerRight.className="DialogHeaderRight";
	header.appendChild(headerRight);
	
	var closeImage=document.createElement("img");
	closeImage.className="DialogHeaderCloseImage";
	closeImage.src=baseUrl+"/images/close.gif";
	closeImage.title="关闭";
	closeImage.onmouseover=function () {this.src=baseUrl+"/images/close_hover.gif";};
	closeImage.onmouseout=function () {this.src=baseUrl+"/images/close.gif";};
	closeImage.onmousedown=function (e) 
	{		 
	    window.event.returnValue = true;    
  		object.Close();
	}
	headerRight.appendChild(closeImage);	

		
	var content=document.createElement("div");
	content.className="DialogContent";
	dialog.appendChild(content);  
			
	var footer=document.createElement("div");
	footer.className="DialogFooter";
	dialog.appendChild(footer);  	

    var iframe=document.createElement("iframe");
	iframe.className="MaskDiv";
	dialog.appendChild(iframe);
	
	header.onmousedown=function (e)
	{		
		if (e)
		{
			window.event=e;
			window.event.srcElement=e.target;      
		}
		window.event.returnValue = true;
		var dialog=this.parentNode;   
		var header=this;
		this.style.cursor="move";
		var offsetX=window.event.clientX-GetLeft(dialog);
		var offsetY=window.event.clientY-GetTop(dialog);
		document.onmousemove=function (e)
		{
			if (e)
			{
				window.event=e;
				window.event.srcElement=e.target;      
			}
    		window.event.returnValue = true;   
    		object.SetXY(window.event.clientX-offsetX,window.event.clientY-offsetY);
         }
         document.onmouseup=function (e)
		 {
		    header.style.cursor="";
			document.onmousemove=null;
			document.onmouseup=null;
			dialog=null;
			header=null;
		 }
    }  
    delete expandImage;
	expandImage=null;
	delete closeImage;
	closeImage=null;
				
	delete headerLeft;
	headerLeft=null;
	delete headerCenter;
	headerCenter=null;				
	delete headerRight;
	headerRight=null;
	
	delete header;
	header=null;
	delete footer;
	footer=null;
	delete content;
	content=null;
	delete dialog;
	dialog=null; 
};
function User()
{
    var object=this;
    this.Value=new Array();
    
	var dialog=new Dialog("选择用户");
	var dialogContent=dialog.Dialog.childNodes[1];
	dialog.Dialog.style.top=document.documentElement.scrollTop+50+"px";
	
	var organize=document.createElement("div");	
	organize.className="Section";
	dialogContent.appendChild(organize); 
	
	var toolbar=document.createElement("div");
	toolbar.className="DialogToolBar";
	dialogContent.appendChild(toolbar);
	
	var toolbarLeft=document.createElement("div");
	toolbarLeft.className="DialogToolItemLeft";
	toolbar.appendChild(toolbarLeft);
	
	var okButton=document.createElement("input");
	okButton.setAttribute("type","button");
	toolbarLeft.appendChild(okButton);  	
	okButton.className="Button";
	okButton.value=" 确  定 ";
	okButton.onmouseover=function ()
	{
	    this.className="ButtonOver";
	};
	okButton.onmouseout=function ()
	{
	    this.className="Button";
	};
	okButton.onclick=function ()
	{
	   var organize=this.parentNode.parentNode.parentNode.childNodes[0];
	   var checkboxes=organize.getElementsByTagName("input");
	   for (var i=0;i<checkboxes.length;i++)
	   {
	        if (checkboxes[i].checked)
	        {
	           var user=new Object();
	           user.Id=checkboxes[i].value;
	           user.name=checkboxes[i].getAttribute("name");
	           user.Name=checkboxes[i].getAttribute("text");
	           user.Type=checkboxes[i].getAttribute("type");	           
	           object.Value.push(user);
	        }
	   }
	   object.Ok();
	   this.parentNode.parentNode.parentNode.parentNode.Dialog.Close();
	};
	
    var space=document.createTextNode("  ");
    toolbarLeft.appendChild(space);
	
	var cancelButton=document.createElement("input");
	cancelButton.setAttribute("type","button");
	toolbarLeft.appendChild(cancelButton);  	
	cancelButton.className="Button";
	cancelButton.value=" 取  消 ";
	cancelButton.onmouseover=function ()
	{
	    this.className="ButtonOver";
	}
	cancelButton.onmouseout=function ()
	{
	    this.className="Button";
	}
	cancelButton.onclick=function ()
	{
	   this.parentNode.parentNode.parentNode.parentNode.Dialog.Close();
	}
	
	var tree=new Organize();
	tree.AddNode(organize,baseUrl+"/User/images/root.gif","组织结构","组织结构","Open",true,"User_Root_Menu","100","0");
	organize.childNodes[0].Expand();
}

function Organize(container)
{  
        var object=this;
        
        this.AddNode=function (container,icon,name,text,status,isLoad,menuName,id,fid)
        {                   
			var node=document.createElement("div");     
			container.appendChild(node);
			node.className="Node";     
			node.LoadData=function (callback)
			{
				if (!isLoad) return;
				var node=this;	
    			isLoad=false;                        
				var request=new Request();
				request.Url=baseUrl+"/Service/User.asmx/GetRoleUsersByRoleId?RoleId="+id;
				request.Callback=function (httpRequest)
				{
					if (httpRequest.status==200)
					{
					    var roles=httpRequest.responseXML.getElementsByTagName("role");		
					    var users=httpRequest.responseXML.getElementsByTagName("user");		
					    if ((roles.length>0)||(users.length>0))
					    {
							var nodeChild=document.createElement("div");  
							nodeChild.className="NodeChild";
							node.appendChild(nodeChild);  
						};			
						for (i=0;i<roles.length;i++)
						{
							 var roleId=roles[i].getAttribute("id");
						     var name=roles[i].getAttribute("name");							
							object.AddNode(nodeChild,baseUrl+"/User/images/folders.gif",name,text+"/"+name,"Open",true,"User_Role_Menu",roleId,id);  		       					       
						};	
						for (i=0;i<users.length;i++)
						{
							 var userName=users[i].getAttribute("userName");
						     var name=users[i].getAttribute("name");							
							 object.AddNode(nodeChild,baseUrl+"/User/images/user.gif",name,text+"/"+name,"Leaf",true,"User_User_Menu",userName,id);  		       					       
						};
						status="Open";
						node.Expand();
						if (callback) callback();
					};					
					delete httpRequest;
					httpRequest=null;			
				}; 
				request.Start();
				   
			}; 
			node.Expand=function ()
			{				
				switch (status)
				{
					case "Open":
						status="Close";           
						this.LoadData();
						var nodeChild=this.childNodes[1];
						if (nodeChild)
						{                        
							nodeChild.style.display="block";       
							var height=nodeChild.offsetHeight;
							nodeChild.style.overflow="hidden"; 	
							nodeChild.style.height="1px";                  
							this.Show(height);
						}
						break;
					case "Close":
						status="Open";
						var nodeChild=this.childNodes[1];
						if (nodeChild)
						{  
							var height=nodeChild.offsetHeight;
							nodeChild.style.overflow="hidden"; 	
							this.Hide(height);   
						} 
						break;
				}
				var stateImage=this.childNodes[0].childNodes[0].childNodes[0];
				stateImage.src=baseUrl+"/user/images/"+status+".gif";
			}
			node.Show=function (height)
			{                
				var node=this;
				var nodeChild=this.childNodes[1];
				if (nodeChild.offsetHeight+parseInt(height/10)<height)            
				{
					nodeChild.style.height=nodeChild.offsetHeight+parseInt(height/10)+"px";
					window.setTimeout(function (){node.Show(height)},10)
				}
				else
				{  					
					nodeChild.style.overflow="";   
					nodeChild.style.height="";	  
										
				}
			};
			node.Hide=function (height)
			{
				var node=this;
				var nodeChild=this.childNodes[1];
				if (nodeChild.offsetHeight-parseInt(height/10)>0)            
				{
					nodeChild.style.height=nodeChild.offsetHeight-parseInt(height/10)+"px";
					window.setTimeout(function (){node.Hide(height);},10)
				}
				else
				{
				    nodeChild.className="NodeChild";
					nodeChild.style.overflow="";                   
					nodeChild.style.display="none";
					nodeChild.style.height="";
				}                
			};  				
			
			    
			var nodeName=document.createElement("div");  
			node.appendChild(nodeName); 
			nodeName.className="NodeName";
			nodeName.innerHTML="<div class='NodeNameIcon'><img src='"+baseUrl+"/User/images/"+status+".gif'><input type='checkbox' value='"+id+"' text='"+text+"' name='"+name+"' type='"+(status=="Leaf"?"User":"Role")+"'><img src='"+icon+"'></div><div class='NodeNameText' onmouseover=\"javascript:this.className='NodeNameTextOver'\" onmouseout=\"javascript:this.className='NodeNameText'\">"+name+"</div>";             
			nodeName.childNodes[0].childNodes[0].onclick=function ()
			{
				this.parentNode.parentNode.parentNode.Expand();				
			};    
			
			nodeName.childNodes[0].childNodes[1].onclick=function ()
			{
				var nodeChild=this.parentNode.parentNode.parentNode.childNodes[1];
				if (!nodeChild) return;
				var checkboxes=nodeChild.getElementsByTagName("input");
				for (var i=0;i<checkboxes.length;i++)
				{
				    checkboxes[i].checked=this.checked;
				}
			};      						  
	            
			delete nodeName;
			nodeName=null;
			delete node;
			node=null;      
			delete container;
			container=null;      
    }
}