/* 
* (c) Microsoft Corporation, 2005
*
* Purpose:
*		This script is used by MessageBox.aspx,
*		and pages that use MessageBox.aspx
*/

/*
*	Caller portion
*/

var mbMessageBoxPagePath = 'MessageBox.aspx';

// Only first three parameters are mandatory
function openMessageBox(sCaption, sHeaderText, sBodyText, sDialogType, sButtons, sResults)
{
	// See if mandatory parameters are present
	if(sCaption == null || sHeaderText == null || sBodyText == null)
	{
		throw "One or more of mandatory parameters are null or missing";
	}
	
	// Form dialog parameters and open the dialog
	var o = new Object();	
	o.DialogType = sDialogType == null ? "warning" : sDialogType;
	o.Buttons = sButtons == null ? "OK" : sButtons;
	o.Results = sResults == null ? "OK" : sResults;
	o.HeaderText = sHeaderText;
	o.BodyText = sBodyText;
	o.Caption = sCaption;
	
	return window.showModalDialog(mbMessageBoxPagePath, o, '');
}

//////////////////////////////////////////////////////////
// Dialog portion

// Where to load the icons and images from
var mbImagesDir = "/_layouts/wh/images/";
var mbIconsDir = mbImagesDir + "icons/";

function setDialogIcon(sDialogType)
{
	di = document.getElementById("dialogIcon");
	switch(sDialogType.toLowerCase())
	{
		// BUGBUG: No icons are defined yet, when there are final icons
		// replace alert.gif with corresponding ones
		case "error": di.src = mbIconsDir + "alert.gif"; break;
		case "warning": di.src = mbIconsDir + "alert.gif"; break;
		case "delay": di.src = mbIconsDir + "alert.gif"; break;
		default: throw "Unsupported dialog type: " + sDialogType;
	}
}

function createButtons(sButtons, sButtonResults)
{
	var bar = document.getElementById("buttonBar");
	var bns = sButtons.split(";");
	var brs = sButtonResults.split(";");
	
	if(bns.length != brs.length)
	{
		throw "Button count must match button result count";
	}
	
	for(var i=0; i < bns.length; i++)
	{
		var input = document.createElement("input");
		input.type = "button"
		input.value = bns[i];
		input.name = brs[i];
		input.attachEvent('onclick', buttonClick);
		
		buttonBar.appendChild(input);
	}
}

function buttonClick()
{
	var o = new Object();
	var btn = event.srcElement;

	o.dialogResult = btn.name;			
	returnValue = o;
	window.close();
}

function initDialog()
{
	var da = dialogArguments;

	var h = document.getElementById("headerText");
	var b = document.getElementById("bodyText");
	
	h.innerHTML = da.HeaderText;
	b.innerHTML = da.BodyText;
	document.title = da.Caption;
	
	setDialogIcon(da.DialogType);
	createButtons(da.Buttons, da.Results);
}
