﻿function PageRequestManagerEndRequest(sender, eventArgs)
{
	ensureElementRemoved("errorDialog");

	var response = eventArgs.get_response();
	var error = eventArgs.get_error();

	// firefox will have bad network connections, aborted requests, and real errors all come through here.
	// only way to check for real error is to check if response is empty or not. unfortunately we can't differentiate
	// between a bad network connnection and an aborted request, and by no means we want to put up an error message
    // on an aborted request
	if (error != null && !isResponseEmpty(response))
	{
		// ASP.NET prepends the name, which is very unfriendly, to the description, with a colon and space after
		var message = error.message.substring(error.name.length + 2);
		showModalBox("errorDialog", message, 300, 150, "Error", true);
	}

	if (error != null)
	    eventArgs.set_errorHandled(true);
}

function isResponseEmpty(response)
{
    try
    {
        var responseData = response.get_responseData();
        return responseData == null || responseData == "";
    }
    catch (ex) // IE throws sometimes when checking response data
    {
        return true;
    }
}

function attachToTabPanel(tabContainer)
{
	var childElements = getChildElements(tabContainer);
	var tabs = getChildElements(childElements[0]);
	
	for (var i = 0; i < tabs.length; i++)
	{
		attachToTab(tabContainer, tabs[i], i);
	}
	
	selectTab(tabContainer, 0);
}

function attachToTab(tabContainer, tab, index)
{
	tab.onclick = function() { selectTab(tabContainer, index); };
}

function selectTab(tabContainer, index)
{
	var childElements = getChildElements(tabContainer);
	var tabs = getChildElements(childElements[0]);
	var panels = getChildElements(childElements[1]);

	for (var i = 0; i < tabs.length; i++)
	{
		tabs[i].className = (i == index ? "Selected" : "");
	}

	for (var i = 0; i < panels.length; i++)
	{
		panels[i].style.display = (i == index ? "block" : "none");
	}
}

function getChildElements(element)
{
	var index = 0;
	var elementArray = new Array(element.childNodes.length);
	
	for (var i = 0; i < element.childNodes.length; i++)
	{
		if (element.childNodes[i].nodeType == 1)
		{
			elementArray[index++] = element.childNodes[i];
		}
	}
	
	elementArray.length = index;
	
	return elementArray;
}

function ensureElementRemoved(id)
{
	var element = document.getElementById(id);
	
	if (element != null)
	{
		element.parentNode.removeChild(element);
	}
}

function addWindowLoadHandler(handler)
{
	if (window.attachEvent)
	{
		window.attachEvent("onload", handler);
	}
	else
	{
		window.addEventListener("load", handler, false);
	}
}

function showModalActivityBox(id, activityImageUrl, message, title)
{
	var element = document.createElement("DIV");
	element.style.textAlign = "center";
	element.style.fontStyle = "italic";
	element.style.fontSize = "14pt";
	element.style.fontWeight = "bold";
	element.style.paddingTop = "25px";

	var imageElement = document.createElement("IMG");
	imageElement.style.verticalAlign = "middle";
	imageElement.src = activityImageUrl;
	element.appendChild(imageElement);
	
	var textNode = document.createTextNode(" " + message);
	element.appendChild(textNode);

	showModalDialog(id, element, 300, 150, title, false);
}

function showModalBox(id, message, width, height, title, canClose)
{
	var element = document.createElement("P");
	element.style.padding = "8px";
	element.style.margin = "0px";
	element.style.overflow = "auto";
	var textNode = document.createTextNode(message);
	element.appendChild(textNode);
	showModalDialog(id, element, width, height, title, canClose);
}

function showModalPage(id, url, width, height, title, canClose)
{
	var frame = document.createElement("IFRAME");
	frame.src = url;
	frame.style.borderWidth = "0px";
	frame.style.height = "100%";
	showModalDialog(id, frame, width, height, title, canClose);
}

function showModalDialog(id, element, width, height, title, canClose)
{
	var modalDialog = document.createElement("DIV");
	modalDialog.id = id;
	modalDialog.style.position = "fixed";
	modalDialog.style.zIndex = 1000;
	modalDialog.style.top = modalDialog.style.left = modalDialog.style.right = modalDialog.style.bottom = "0px";
	document.body.appendChild(modalDialog);

	var backgroundElement = document.createElement("DIV");
	backgroundElement.style.position = "absolute";
	backgroundElement.style.top = backgroundElement.style.left = backgroundElement.style.right = backgroundElement.style.bottom = "0px";
	backgroundElement.style.opacity = "0.65";
	backgroundElement.style.filter = "alpha(opacity=65)";
	backgroundElement.style.backgroundColor = "black";
	modalDialog.appendChild(backgroundElement);
	
	var panelElement = document.createElement("DIV");
	panelElement.style.position = "absolute";
	panelElement.style.top = "50%";
	panelElement.style.left = "50%";
	panelElement.style.marginTop = -(height / 2) + "px";
	panelElement.style.marginLeft = -(width / 2) + "px";
	panelElement.style.width = width + "px";
	panelElement.style.height = height + "px";
	panelElement.style.opacity = "1.0";
	panelElement.style.backgroundImage = "url(RoundedRectangle.axd?Width=" + width + "&Height=" + height + "&Radius=10&FillColor=333333)";
	panelElement.style.backgroundRepeat = "no-repeat";
	modalDialog.appendChild(panelElement);
	
	var titleElement = document.createElement("DIV");
	titleElement.style.position = "absolute";
	titleElement.style.top = "8px";
	titleElement.style.left = "16px";
	titleElement.style.color = "#FFF";
	titleElement.style.fontWeight = "bold";
	panelElement.appendChild(titleElement);
	
	var titleTextElement = document.createTextNode(title);
	titleElement.appendChild(titleTextElement);

	if (canClose)
	{
		var closeElement = document.createElement("A");
		closeElement.href = "#";
		closeElement.style.position = "absolute";
		closeElement.style.top = "8px";
		closeElement.style.right = "16px";
		closeElement.style.color = "#FFF";
		closeElement.style.textDecoration = "none"
		closeElement.style.fontWeight = "bold";
		panelElement.appendChild(closeElement);
		
		var closeTextElement = document.createTextNode("close [X]");
		closeElement.appendChild(closeTextElement);

		backgroundElement.onclick = function() { document.body.removeChild(modalDialog); };
		closeElement.onclick = function() { document.body.removeChild(modalDialog); return false; };
	}
	
	var contentElement = document.createElement("DIV");
	contentElement.style.position = "absolute";
	contentElement.style.top = "32px";
	contentElement.style.left = "16px";
	contentElement.style.width = (width - 32) + "px"; // yes, tried 'top' and 'right' properties, but don't work for iframe in firefox
	contentElement.style.height = (height - 48) + "px";
	contentElement.style.margin = "0px";
	contentElement.style.padding = "0px";
	contentElement.style.backgroundColor = "#FFF";
	contentElement.style.color = "#000";
	panelElement.appendChild(contentElement);
	
	contentElement.appendChild(element);
}

