﻿var alternateWin=null;
var showWindow = false; //if or not show the msg win

function alternateWindow()
{
    this.width = 600;//the width of win 
    this.height = 400;//the height of win
    this.bordercolor="#0065cd";//the color of win border
    this.titlecolor="#fff";//the color of title
    this.titlestr = "";
		this.show = true;
		
    this.MsgSourceObj = null; //the message source control
		this.BackGroundObj = null; //background object
		this.WinDiv = null; //win
		this.TitleDiv = null; //title
		this.CloseDiv = null; //close
		this.ContentDiv = null; //content
		this.PageFormObj = null;

		this.moveable=false;
		this.MouseStartX;
		this.MouseStartY;
		this.DivStartX;
		this.DivStartY;

}

CloseAlert = function()
{
		if(alternateWin!=null && showWindow)
			alternateWin.close();
}

/*模仿的alert窗口*/
function ShowAlert(){
	showWindow = true;
	
	var _PageFormObj = $(ShowAlert.arguments[0]); //the formid of page
	var _contentObj = $(ShowAlert.arguments[1]); //the msg source control id
  var _width = ShowAlert.arguments[2]; //the win's width
  var _bordercolor = ShowAlert.arguments[3]; 
  var _titlecolor = ShowAlert.arguments[4]; 
  var _titlestr = ShowAlert.arguments[5]; 
	if(alternateWin != null)
	{
		alternateWin.close();
	}	
	alternateWin=new alternateWindow();
  
	if (_contentObj != "" && typeof _contentObj != "undefined")
	{
		alternateWin.MsgSourceObj =_contentObj;
	}
	else
	{
		return;
	}

	if (_PageFormObj != "" && typeof _PageFormObj != "undefined")
	{
		alternateWin.PageFormObj = _PageFormObj;
	}
		
	if (_width != "" && typeof _width != "undefined")
		alternateWin.width = _width;
	if (_bordercolor != "" && typeof _bordercolor != "undefined")
		alternateWin.bordercolor = _bordercolor;
	if (_titlecolor != "" && typeof _titlecolor != "undefined")
		alternateWin.titlecolor = _titlecolor;
	if (_titlestr != "" && typeof _titlestr != "undefined")
		alternateWin.titlestr = _titlestr;
	
	alternateWin.init();
	return;
}

alternateWindow.prototype.init=function()
{ 
    if(typeof this.MsgSourceObj == "undefined" || this.MsgSourceObj == null)
    	return;
    
		if(this.PageFormObj == null)
			this.PageFormObj = document.body;
			
		this.BackGroundObj = $('win_BackGroundIFrame');
		if(typeof this.BackGroundObj == "undefined" || this.BackGroundObj==null){
			this.BackGroundObj=document.createElement("iframe"); 
	    this.BackGroundObj.setAttribute('id','win_BackGroundIFrame');
	    this.BackGroundObj.setAttribute('frameBorder','no'); 
	    this.BackGroundObj.setAttribute('scrolling','no'); 
    }
    with(this.BackGroundObj.style)
    {
      	position="absolute"; 
        top="0"; 
        left="0"; 
        background="#ccc"; 
        filter="progid:DXImageTransform.Microsoft.Alpha(opacity=60)"; 
        opacity="0.6"; 
        zIndex = "10000"; 
    }
    this.PageFormObj.appendChild(this.BackGroundObj); 
     
		this.WinDiv = $('win_msgWinDiv');
		if(typeof this.WinDiv == "undefined" || this.WinDiv==null){
			this.WinDiv=document.createElement("div"); 
	    this.WinDiv.setAttribute("id","win_msgWinDiv"); 
    }
    
    with(this.WinDiv.style)
    {
        background="#fff"; 
        border="2px solid " + this.bordercolor; 
        position = "absolute"; 
        font="12px/1.6em Verdana, Arial, sans-serif"; 
        width = this.width + "px"; 
        textAlign = "center"; 
        lineHeight = "25px"; 
        overflow = "hidden";
        zIndex = "10001"; 
   }
   this.PageFormObj.appendChild(this.WinDiv); 
	 
	 this.resize();
	 
   this.TitleDiv= $('win_msgTitleDiv');
	 if(typeof this.TitleDiv == "undefined" || this.TitleDiv==null)
   {
	   this.TitleDiv = document.createElement("div"); 
	   this.TitleDiv.setAttribute("id","win_msgTitleDiv"); 
	   this.TitleDiv.setAttribute("align","left"); 
  }
   with(this.TitleDiv.style)
   {
	   margin="0";
	   padding="0";
	   background=this.bordercolor; 
	   border="1px solid " + this.bordercolor; 
	   height="22px"; 
	   font="14px Verdana, Arial, sans-serif"; 
	   fontWeight = "bold";
	   color=this.titlecolor; 
	   cursor="move"; 
	   filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=100,finishOpacity=70)"; 
	   opacity="0.75"; 
	 }
	 this.TitleDiv.onmousedown=function(ev){alternateWin.StartDrag(ev);};
	 this.TitleDiv.onmouseup=function(ev){alternateWin.StopDrag(ev);};
	 this.TitleDiv.onmousemove=function(ev){alternateWin.Drag(ev);};
	 this.TitleDiv.innerHTML = this.titlestr;
	 
	 this.WinDiv.appendChild(this.TitleDiv);
	 
   this.CloseDiv= $('win_CloseDiv');
	 if(typeof this.CloseDiv == "undefined" || this.CloseDiv==null)
   {
	   this.CloseDiv = document.createElement("div"); 
	   this.CloseDiv.setAttribute("id","win_CloseDiv"); 
	   this.CloseDiv.setAttribute("valign","top"); 
   }
   with(this.CloseDiv.style)
   {
	   position = "absolute";
	   overflow = "hidden";
	   right = "1";
	   top = "1";
	   width = "18px";
	   textAlign = "center";
	   border = "2px outset #fff";
	   color = "#000";
	   fontSize = "12px";
	   cursor="pointer"; 
	 }
   this.CloseDiv.innerHTML="<B>X</B>";
   this.CloseDiv.onclick=function(){alternateWin.close();};
	 this.CloseDiv.onmousedown=function(){this.style.borderStyle = "inset";};
	 this.CloseDiv.onmouseup=function(){this.style.borderStyle = "outset";};
	 this.TitleDiv.appendChild(this.CloseDiv);
	    
   this.ContentDiv= $('win_msgContentDiv');
	 if(typeof this.ContentDiv == "undefined" || this.ContentDiv==null)
   {
	   this.ContentDiv = document.createElement("div"); 
	   this.ContentDiv.setAttribute('id','win_msgContentDiv'); 
	 }
   with(this.ContentDiv.style)
   {
			textAlign = "left";
			padding = "0 0 15px 0";
   		width = "96%";
      margin = "0 auto";
   }
   this.WinDiv.appendChild(this.ContentDiv);
   
   this.MsgSourceObj.style.display = 'block';
   this.ContentDiv.appendChild(this.MsgSourceObj);
} 

alternateWindow.prototype.close=function()
{
    if(alternateWin!=null)
    {
			this.MsgSourceObj.style.display = 'none';
			this.PageFormObj.appendChild(this.MsgSourceObj);
			
	    this.PageFormObj.removeChild(this.BackGroundObj); 
	    this.TitleDiv.removeChild(this.CloseDiv);
	    this.WinDiv.removeChild(this.TitleDiv); 
	    this.WinDiv.removeChild(this.ContentDiv); 
	
	    this.PageFormObj.removeChild(this.WinDiv); 
	    showWindow = false;
	    alternateWin = null;
	    this.moveable = false;
  	}
  return;
}

alternateWindow.prototype.StartDrag = function(ev)
{
	var xx,yy;
	var _event = ev?ev:window.event;
	xx= _event.clientX; 
	yy= _event.clientY;

	if(_event.button==1 || _event.button==0)
	{
		//lock the win
		if(window.event)
			this.TitleDiv.setCapture();
		else
		{
			  window.addEventListener('mousemove',alternateWin.Drag, true);
			  window.addEventListener('mouseup',alternateWin.StopDrag, true);
		}
		//record the start position
		this.MouseStartX = xx;
		this.MouseStartY = yy;
		this.DivStartX = parseInt(this.WinDiv.style.left);
		this.DivStartY = parseInt(this.WinDiv.style.top);
		this.moveable = true;
	}
}
//drop
alternateWindow.prototype.Drag = function(ev)
{
	var xx,yy;
	var _event = ev?ev:window.event;
	xx= _event.clientX; 
	yy= _event.clientY;
	if(this.moveable)
	{
		this.WinDiv.style.left = this.DivStartX + xx - this.MouseStartX;
		this.WinDiv.style.top = this.DivStartY + yy - this.MouseStartY;
	}
}
//stop drag
alternateWindow.prototype.StopDrag = function(ev)
{
	if(this.moveable)
	{
		if(window.event)
				this.TitleDiv.releaseCapture();
		else
		{
			window.removeEventListener('mousemove',alternateWin.Drag, false);
			window.removeEventListener('mouseup',alternateWin.StopDrag, false);
		}
			
		this.moveable = false;
	}
}

alternateWindow.prototype.resize=function()
{
	  if(alternateWin!=null && showWindow)
	  {
		  var sWidth=(document.body.scrollWidth>document.body.clientWidth)?document.body.scrollWidth:document.body.clientWidth; 
	    var sHeight=(document.body.scrollHeight>document.body.clientHeight)?document.body.scrollHeight:document.body.clientHeight;  
	    with(this.BackGroundObj.style)
	    {
	        width=sWidth + "px"; 
	        height=sHeight + "px"; 
	    }
	    var _left = (document.body.clientWidth - this.width)/2;
	    var _top = (document.body.clientHeight - this.height)/2;  
	    _left = (_left > 20)?_left:20;
	    _top = (_top > 20 )? _top: 20;
	    with(this.WinDiv.style)
	    {
	        left = _left + document.body.scrollLeft +"px" ; 
	        top =  _top + document.body.scrollTop+"px"; 
	    }
    }
}

alternateWin_Resize = function()
{
		if(alternateWin!=null && showWindow)
			alternateWin.resize();
}

function $(obj)
{
	if(typeof obj == 'string')
	    return document.getElementById(obj);
	else 
	    return obj;
}

var Ka=navigator.userAgent.toLowerCase(); 
var rt=Ka.indexOf("opera")!=-1; 
var r=Ka.indexOf("msie")!=-1&&(document.all&&!rt); 
if(r){ window.attachEvent("onresize",alternateWin_Resize);}
else{window.addEventListener("resize",alternateWin_Resize,false);}

