
var disabler = null;
var disablerWidth = 300;
var disablerHeight = 120;

function DisableActivate(event)
{
    event.returnValue = false;
    event.cancelBubble = false;
}

function ScreenDisabler(showmsg,topmost,msg)
{
	this.visible = false;
	this.inputCollection = document.all.tags('INPUT');
	this.selectCollection = document.all.tags('SELECT');

	if (topmost == null || topmost == true)
		this.topmost = true;
	else
		this.topmost = false;
    if (showmsg == null || showmsg == true)
		this.showmsg = true;
	else
		this.showmsg = false;
    if (msg != null && msg.length > 0)
        this.msg = msg;
    else
        this.msg = null;

	this.popupDiv = this.CreatePopupDiv();
	if (this.showmsg) {
	    this.msgDiv = this.CreateMessageDiv();
//	    this.msgDiv.style.top = (this.msgDiv.offsetParent.clientHeight / 2) - (disablerHeight / 2) + 'px';  //+ this.msgDiv.offsetParent.scrollTop;
	    //	    this.msgDiv.style.left = (this.msgDiv.offsetParent.clientWidth / 2) - (disablerWidth / 2) + 'px';  //+ this.msgDiv.offsetParent.scrollLeft;
	    var isInIFrame = document.parentWindow.frameElement != null ? (document.parentWindow.frameElement.tagName.toLowerCase() == 'iframe') : false;
	    if (!isInIFrame) {
	        this.msgDiv.style.left = parseInt(document.body.clientWidth) / 2 - parseInt(disablerWidth) / 2;
	        this.msgDiv.style.top = parseInt(document.body.clientHeight) / 2 - parseInt(disablerHeight) / 2;
	    }
	    else {
	        this.msgDiv.style.left = parseInt(document.parentWindow.frameElement.offsetWidth) / 2 - parseInt(disablerWidth) / 2;
	        this.msgDiv.style.top = (parseInt(document.parentWindow.frameElement.offsetHeight) / 2 - parseInt(disablerHeight) / 2) - 15;
	    }
	}
}

ScreenDisabler.prototype.CreateMessageDiv = function() {
    var msgDiv = document.createElement("DIV");
    msgDiv.style.position = 'absolute';
    msgDiv.style.visibility = 'hidden';
    msgDiv.style.display = 'none';
    msgDiv.className = 'progress_div';

    var table = document.createElement('table');
    table.cellSpacing = 0;
    table.cellPadding = 0;
    table.className = "progress_table";
    table.style.width = disablerWidth + 'px';
    table.style.height = disablerHeight + 'px';

    var row = table.insertRow();
    var cell = row.insertCell();
    cell.className = 'progress_top_cell';
    cell.innerText = 'InsWorld Message';

    var row = table.insertRow();
    var cell = row.insertCell();
    cell.className = 'progress_message_cell';
    if (this.msg != null && this.msg.length > 0)
        cell.innerHTML = this.msg;
    else
        cell.innerText = 'Loading... Please wait.';

    var row = table.insertRow();
    var cell = row.insertCell();
    cell.className = 'progress_image_cell';
    var img = document.createElement('img');
    img.src = '../images/progress.gif';
    img.className = 'progress_image';
    img.align = 'absmiddle';
    cell.appendChild(img);

    row = table.insertRow();
    cell = row.insertCell();
    cell.className = 'progress_bottom_cell';

    msgDiv.appendChild(table);

    document.body.insertAdjacentElement('beforeEnd', msgDiv);
    msgDiv.style.zIndex = 160000;
    return msgDiv;
}

ScreenDisabler.prototype.Z = function()
{
}
ScreenDisabler.prototype.SetPopupDivOpacity = function(opacity)
{
	this.popupDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity.toString() + ");"
}
ScreenDisabler.prototype.CreatePopupDiv = function()
{
	var popupDiv = document.createElement("DIV");
	
	popupDiv.style.position = 'absolute';
	popupDiv.style.top = 0;
	popupDiv.style.left = 0;
	
	popupDiv.style.visibility = 'hidden';
	popupDiv.style.backgroundColor = '#efecdf';
	popupDiv.style.display = 'none';
	
	document.body.insertAdjacentElement('beforeEnd', popupDiv);
	if (this.topmost)
		popupDiv.style.zIndex = 150000;
	popupDiv.style.width = "100%";
	popupDiv.style.height = "100%"; 
	
	this.CreateIFrameForIE6(popupDiv);
	
	return popupDiv;
}
ScreenDisabler.prototype.CreateIFrameForIE6 = function (parentDiv)
{
	var browser_name = navigator.appName;
	var browser_version = parseFloat(navigator.appVersion);
	var needInsertIFrame = false;
	
	if (browser_name == 'Microsoft Internet Explorer')
	{
		needInsertIFrame = true;
	}
	
	if (needInsertIFrame == true)
	{
		parentDiv.style.overflow = 'hidden';

		var iframe = document.createElement("IFRAME");
		
		iframe.style.position = 'absolute';
		iframe.style.zIndex = -1;
		iframe.style.overflow = 'hidden';
		iframe.style.top = 0;
		iframe.style.left = 0;
		iframe.style.width = "5000px";
		iframe.style.height = "5000px"; 
		iframe.style.display = "block"; 
		iframe.style.filter = "mask();";
		
		parentDiv.insertAdjacentElement('beforeEnd', iframe);
	}
}
ScreenDisabler.prototype.AttachEvents = function (inputCollection)
{
	var n = inputCollection.length;
	for(var i = 0; i < inputCollection.length; i++)
	{
        inputCollection[i].attachEvent('onbeforeactivate', DisableActivate);
	}
}

ScreenDisabler.prototype.DetachEvents = function (inputCollection)
{
	var n = inputCollection.length;
	for(var i = 0; i < inputCollection.length; i++)
	{
        inputCollection[i].detachEvent('onbeforeactivate', DisableActivate);
	}
}

ScreenDisabler.prototype.Show = function(opacity)
{
	if (opacity == null)
	{
		this.SetPopupDivOpacity(50);
	}
	else
	{
		if (!(this.visible && opacity == 0))
			this.SetPopupDivOpacity(opacity);
	}

	if (!this.visible)
	{
	    this.AttachEvents(this.inputCollection);
	    this.AttachEvents(this.selectCollection);
		
		this.popupDiv.style.visibility = "visible";
		this.popupDiv.style.display = "";
		
		if (this.showmsg)
		{
			this.msgDiv.style.visibility = "visible";
			this.msgDiv.style.display = "";
		}
		
		this.visible = true;
    }
}

ScreenDisabler.prototype.Hide = function()
{
	if (this.visible)
	{
	    this.DetachEvents(this.inputCollection);
	    this.DetachEvents(this.selectCollection);
	    
	    this.popupDiv.style.visibility = "hidden";
        this.popupDiv.style.display = "none";
		
		if (this.showmsg)
		{
			this.msgDiv.style.visibility = "hidden";
			this.msgDiv.style.display = "none";
		}
        this.visible = false;
	}
}

function showDisabler(showmsg, topmost, msg, opacity) {
    if (disabler == null || opacity == null) {
        if (opacity != null) {
            showmsg = false;
        }
        if (disabler != null) {
            disabler.Hide();
        }
        disabler = new ScreenDisabler(showmsg, topmost, msg)
    }
    disabler.Show(opacity);
}
function hideDisabler()
{
    if (disabler != null)
    {
        disabler.Hide();
        disabler = null;
    }
}
function ShowTaskProgress(msg, opacity) {
    showDisabler(true,true,msg, opacity);
}
function HideTaskProgress()
{
    hideDisabler();
}
