var MW_STEP_X = 30; // it is used to specify the x-axis increment for child-window
var MW_STEP_Y = 30;// it is used to specify the y-axis increment for child-window
var MW_LAST_OPENED = null; // holds the pointer to the last opened window
var MW_LAST_ZINDEX = 4500;
function ModalWindow(id)
{
	// Properties
	this.id = id;
	this.object = document.getElementById(id);
	this.Layer = null;
	this.Width = 0;
	this.Height = 0;
	this.Left = mwGetLeftDefault();
	this.Top = mwGetTopDefault();
	this.firstTxt = GetFirstTextBox(this.object);
	// Methods
	this.Show = mwShow;					// Shows the window
	this.Close = mwClose;				// Hides window
	//this.DrawLayer = mwDrawLayer;		// Creates a div element to use for modal windows
	this.WindowSize = mwWindowSize;		// Gets the Width and Height properties
	this.SetPosX = mwSetPosX;			// Sets the left position of the window
	this.SetPosY = mwSetPosY;			// Sets the top position of the window
	this.Exists = mwExists;				// Returns true if pointer to this window is already specified
	this.GetTop = mwGetTop;
	this.GetLeft = mwGetLeft;
}

function GetFirstTextBox(obj)
{
	if (!obj) return null;
	var objs1 = (document.all) ? obj.all.tags("INPUT") : obj.getElementsByTagName("INPUT");
	var objs2 = (document.all) ? obj.all.tags("TEXTAREA") : obj.getElementsByTagName("TEXTAREA");
	if (!objs1 && !objs2) return null;
	if (objs1 && objs2)	for (var i= 0; i < objs2.length; i++) objs1[objs1.length] = objs2[i];
	var objs = (objs1) ? objs1 : objs2;
	if (objs.length > 0)
	{
		for (var i=0; i < objs.length; i++)
			if (objs[i].type.toLowerCase() == "text" || objs[i].type.toLowerCase() == "password" || objs[i].type.toLowerCase() == "file") return objs[i]; 
	}
	else null;
}

function mwShow(isModal)
{
	this.WindowSize();
	if(isModal)
	{
		//this.DrawLayer(true);		
	}
	if (!this.Exists()){
		this.Parent = MW_LAST_OPENED;
		MW_LAST_OPENED = this ;
	}
	var k = makeWinOnTop(0);
	this.object.style.zIndex = k;
	this.object.style.display = "";
 
	if (this.Parent == null)
	{
		this.object.style.top = this.GetTop();
		this.object.style.left = this.GetLeft();
	}
	else
	{
		//alert(this.Parent.object.style.left);
		this.object.style.left = this.GetTop() + MW_STEP_X;
		this.object.style.top = this.GetLeft() + MW_STEP_Y;
	}
	 
 
	
	this.object.style.zIndex = k;
	
	if (this.firstTxt && this.firstTxt.focus && this.firstTxt.style.display != 'none') 
	{
		this.firstTxt.focus();
	}
}

function mwWindowSize()
{
	var isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
	var width = isMac ? document.body.offsetWidth : document.body.scrollWidth;
	var height = isMac ? document.body.offsetHeight : document.body.scrollHeight;
	if(isMac)
	{
		if(parseInt(width) > 1000)
		{
			width = 1020;
			height=600;
		}
	}
	
	this.Width = width;
	this.Height = height;
	
}

function mwDrawLayer(show)
{
/*alert('111');
	if(show)
	{	
		if(this.Layer == null)
		{

			this.Layer = document.createElement("DIV");
			document.body.appendChild(this.Layer);
			this.Layer.style.backgroundImage = "url(../Resources/img/spacer.gif)";
			this.Layer.style.position = "absolute";
			this.Layer.style.left = "0px";
			this.Layer.style.top = "0px";
			//this.Layer.style.backgroundColor = "red";
			this.Layer.style.width = this.Width;
			this.Layer.style.height = this.Height;		
		}
		else
		{
			this.Layer.style.display = "";
		}
		this.Layer.style.zIndex = makeWinOnTop(0);
		
	}
	else 
	{	
		if(this.Layer) 
		{
			this.Layer.style.display = "none";
		}
	}
	*/		
}

var SYS_THEBIGGESTZINDEX = 0;
function makeWinOnTop(index) {
/*	if (SYS_THEBIGGESTZINDEX == 0)
	{
		var daiz;
		var max = 0;
		alert(document.layers.length);
		return 500;
		var da = document.all;
		for (var i=0; i<da.length; i++) {
			daiz = da[i].style.zIndex;
			
			if (daiz != "" && daiz > max)
				max = daiz;
				
		}
		if (max > index)
		{
			SYS_THEBIGGESTZINDEX = max + 1;
			return max;
		}
		else
		{
			SYS_THEBIGGESTZINDEX = index + 1;
			return index;
		}
	} 
	return SYS_THEBIGGESTZINDEX ++;*/
	return MW_LAST_ZINDEX++;
}

function mwClose()
{
	//this.DrawLayer(false);
	var obj = MW_LAST_OPENED;
	if (this == MW_LAST_OPENED)
	{
		MW_LAST_OPENED = this.Parent;
	}
	else{
		while(obj != null)
		{
			if (obj.Parent == this)
			{
				obj.Parent = this.Parent;
			}
			obj = obj.Parent;
		}
	}
	this.object.style.display = "none";	

}
function mwExists()
{
	var obj = MW_LAST_OPENED;
	while(obj != null)
	{
		if (obj.Parent == this)
		{
			return true;
		}
		obj = obj.Parent;
	}
	return false;
}
function mwGetLeft()
{
	return this.Left;
}
function mwGetTop()
{
	return this.Top;
}

function mwGetLeftDefault()
{
	if (window.screen.width == 800)
	{
		return 120;
	}
	else
	{
		return 250;
	}
}
function mwGetTopDefault()
{
	if (window.screen.height == 600)
	{
		return 120;
	}
	else
	{
		return 180;
	}
}
function mwSetPosX(left)
{
	this.Left = left;
}
function mwSetPosY(top)
{
	 this.Top = top;
}
