﻿ForwardTo = null;
ModalWindow = null;

IsModalEnabled = function () {
	return window.showModalDialog;
}

ShowCenteredWindow = function(name, url, width, height) {
  	var windowOptions = "height=" + height + "px,width=" + width + "px,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,modal=yes,menubar=no";

	var x = GetXCentered(width);
	var y = GetYCentered(height);

	if ( navigator.appName.indexOf("scape") > 1 ) {
		windowOptions += ",screenX=" + x;
		windowOptions += ",screenY=" + y;
	} else {
		windowOptions += ",left=" + x;
		windowOptions += ",top=" + y;
	}

  	return window.open(url, name, windowOptions);
}

ShowModal = function (name, url, width, height, eventHandler) {
	if (IsModalEnabled()) {
		var value = window.showModalDialog(url, name, "resizable:1;center:1;dialogleft: " + GetXCentered(width) + 
		    "px;dialogtop:" + GetYCentered(height) + "px;dialogWidth:" + width + "px;dialogHeight:" + height + "px");
		CallEventHandler(eventHandler, value);
	} else {
		if (ModalWindow == null) {
			ModalWindow = new Object;
			ModalWindow.eventHandler = eventHandler;
			ModalWindow.window = ShowCenteredWindow(name, url, width, height);
			ModalWindow.intervalId = setInterval("KeepFocus()", 5);
		}
	}
}

CallEventHandler = function (eventHandler, value) {
    if (eventHandler.toString().length > 0) {
	    eval(eventHandler + "('" + value + "')");
	}
}

KeepFocus = function () {
	try {
		if (ModalWindow != null) {
			if (ModalWindow.window && ModalWindow.window.closed) {
				clearInterval(ModalWindow.intervalId);
				CallEventHandler(ModalWindow.eventHandler, ModalWindow.window.returnValue);
                ModalWindow = null;
				return ;
			}
			ModalWindow.window.focus();
		}
	} catch (everything) {
	}
}

CloseModal = function (value) {
    if (IsModalEnabled()) {
        window.returnValue = value;
    } else {
	    var wnd = window.opener;

	    if (wnd && wnd.closed == false) {
	        wnd.returnValue = value;
	    }
	}
	window.close();
}

Forward = function (url, value) {
    this.url = url;
    this.value = value;
}

ForwardIfEqualValue = function (value) {
    if (ForwardTo.value == value) {
	    document.location.href = ForwardTo.url;
    }
}

ShowModalAndForward = function (name, url, width, height, forwardUrl, value) {
    ForwardTo = new Forward(forwardUrl, value);
    ShowModal(name, url, width, height, "ForwardIfEqualValue");
}

ShowDisclaimer = function (topic, width, height) {
    var url = "/en/_disclaimers/Display.aspx?Id=" + topic;
    ShowModal(topic, url, width, height, "");
}

ShowSpanishDisclaimer = function (topic, width, height) {
    var url = "/sp/_disclaimers/Display.aspx?Id=" + topic;
    ShowModal(topic, url, width, height, "");
}

ShowConfirmation = function (topic, width, height, forwardUrl) {
    var url = "/en/_disclaimers/Confirm.aspx?Id=" + topic;
    ShowModalAndForward(topic, url, width, height, forwardUrl, 'true');
}

ShowSpanishConfirmation = function (topic, width, height, forwardUrl) {
    var url = "/sp/_disclaimers/Confirm.aspx?Id=" + topic;
    ShowModalAndForward(topic, url, width, height, forwardUrl, 'true');
}

CenterWindow = function () {
	var x = GetXCentered(window.outerWidth);
	var y = GetYCentered(window.outerHeight);
    window.moveTo(x,y);
}

GetXCentered = function (width) {
	var x = screen.width - width;
	if (x < 0) x = 0;
	return Math.round(x/2);
}

GetYCentered = function (height) {
	var y = screen.height - height;
	if (y < 0) y = 0;
	return Math.round(y/2);
}