﻿/// <reference path="../Util.js" />
/// <reference path="../ModalPopup.js" />

/**
 * MessageBoxBTD
 * 
 * @author     banhthidiem <banhthidiem@gmail.com>
 * @copyright  2011 Bach Khoa Computer Inc.
 * @version    $Id: MessageBoxBTD.js, v2.0 2011/04/15
 */

var MessageBoxIconBTD =
{
	None: "noneBTD",
	Error: "errorBTD",
	Hand: "handBTD",
	Stop: "stopBTD",
	Question: "questionBTD",
	Warning: "warningBTD",
	Information: "informationBTD"
};

var MessageBoxButtonsBTD =
{
	/// <summary>The message box contains OK button.</summary>
	OK: 0,
	/// <summary>The message box contains OK and Cancel buttons.</summary>
	OKCancel: 1,
	/// <summary>The message box contains Abort, Retry, and Ignore buttons.</summary>
	AbortRetryIgnore: 2,
	/// <summary>The message box contains Yes, No, and Cancel buttons.</summary>
	YesNoCancel: 3,
	/// <summary>The message box contains Yes and No buttons.</summary>
	YesNo: 4,
	/// <summary>The message box contains Retry and Cancel buttons.</summary>
	RetryCancel: 5
};

var DialogResultBTD = 
{
	/// <summary>Nothing is returned from the dialog box. This means that the modal dialog continues running.</summary>
	None: 0,
	/// <summary>The dialog box return value is OK (usually sent from a button labeled OK).</summary>
	OK: 1,
	/// <summary>The dialog box return value is Cancel (usually sent from a button labeled Cancel).</summary>
	Cancel: 2,
	/// <summary>The dialog box return value is Abort (usually sent from a button labeled Abort).</summary>
	Abort: 3,
	/// <summary>The dialog box return value is Retry (usually sent from a button labeled Retry).</summary>
	Retry: 4,
	/// <summary>The dialog box return value is Ignore (usually sent from a button labeled Ignore).</summary>
	Ignore: 5,
	/// <summary>The dialog box return value is Yes (usually sent from a button labeled Yes).</summary>
	Yes: 6,
	/// <summary>The dialog box return value is No (usually sent from a button labeled No).</summary>  
	No: 7
};


var MessageBoxBTD =
{
	oPopup: new ModalPopup(),
	callback: function (data) { return true; },
	elOK: null,
	elCancel: null,
	elAbort: null,
	elRetry: null,
	elIgnore: null,
	elYes: null,
	elNo: null,
	elContainer: null,
	elText: null,
	isInit: false,
	onClick: function (dialogResult) {
		if (this.callback != null) {
			var re = this.callback(dialogResult);
		}
		if (re == null || re) {
			this.oPopup.hide();
		}
	},

	init: function () {
		var me = this;		
		this.elContainer = document.createElement("DIV");
		this.elContainer.className = "messageBoxBTD";
		this.elOK = document.createElement("INPUT");
		utilObj.addEvent(this.elOK, "click", function () { me.onClick(DialogResultBTD.OK); });
		this.elCancel = document.createElement("INPUT");
		utilObj.addEvent(this.elCancel, "click", function () { me.onClick(DialogResultBTD.Cancel); });
		this.elAbort = document.createElement("INPUT");
		utilObj.addEvent(this.elAbort, "click", function () { me.onClick(DialogResultBTD.Abort); });
		this.elRetry = document.createElement("INPUT");
		utilObj.addEvent(this.elRetry, "click", function () { me.onClick(DialogResultBTD.Retry); });
		this.elIgnore = document.createElement("INPUT");
		utilObj.addEvent(this.elIgnore, "click", function () { me.onClick(DialogResultBTD.Ignore); });
		this.elYes = document.createElement("INPUT");
		utilObj.addEvent(this.elYes, "click", function () { me.onClick(DialogResultBTD.Yes); });
		this.elNo = document.createElement("INPUT");
		utilObj.addEvent(this.elNo, "click", function () { me.onClick(DialogResultBTD.No); });
		this.elOK.type = this.elCancel.type = this.elAbort.type = this.elRetry.type = this.elIgnore.type = this.elYes.type = this.elNo.type = "button";
		this.elText = document.createElement("DIV");
		this.elText.className = "cText";
		utilObj.addChildToBody(this.elContainer);
		this.elContainer.appendChild(this.elText);
		var elContainerBtn = document.createElement("DIV");
		elContainerBtn.className = "cBtn";
		elContainerBtn.appendChild(this.elOK);
		elContainerBtn.appendChild(this.elCancel);
		elContainerBtn.appendChild(this.elAbort);
		elContainerBtn.appendChild(this.elRetry);
		elContainerBtn.appendChild(this.elIgnore);
		elContainerBtn.appendChild(this.elYes);
		elContainerBtn.appendChild(this.elNo);
		this.elContainer.appendChild(elContainerBtn);
		this.oPopup = new ModalPopup(this.elContainer, "Thông báo", true);
		this.oPopup.setZIndex(99999);
		this.isInit = true;
	},

	show: function (text, caption, messageBoxButton, messageBoxIcon, callback) {
		/// <summary>Displays a message box with specified text, caption, buttons, and icon.</summary>
		/// <param name="text">The text to display in the message box.</param>
		/// <param name="caption">The text to display in the title bar of the message box.</param>
		/// <param name="messageBoxButton" type="MessageBoxButtonsBTD">One of the MessageBoxButtonsBTD values that specifies which  buttons to display in the message box.</param>
		/// <param name="messageBoxIcon" type="MessageBoxIconBTD">One of the MessageBoxIconBTD values that specifies which  Icon to display in the message box.</param>	

		this.callback = callback != null ? callback : null;
		this.elOK.style.display = this.elCancel.style.display = this.elAbort.style.display = this.elRetry.style.display = this.elIgnore.style.display = this.elYes.style.display = this.elNo.style.display = "none";

		this.elText.innerHTML = text;
		if (caption != null) {
			this.oPopup.setTitle(caption);
		}
		this.oPopup.display();
		this.elContainer.className = "messageBoxBTD" + (messageBoxIcon != null ? " " + messageBoxIcon : "");
		switch (messageBoxButton) {
			case MessageBoxButtonsBTD.OK:
				this.elOK.style.display = "";
				this.elOK.focus();
				break;
			case MessageBoxButtonsBTD.OKCancel:
				this.elOK.style.display = "";
				this.elCancel.style.display = "";
				this.elOK.focus();
				break;
			case MessageBoxButtonsBTD.RetryCancel:
				this.elRetry.style.display = "";
				this.elCancel.style.display = "";
				this.elRetry.focus();
				break;
			case MessageBoxButtonsBTD.AbortRetryIgnore:
				this.elRetry.style.display = "";
				this.elAbort.style.display = "";
				this.elIgnore.style.display = "";
				this.elRetry.focus();
				break;
			case MessageBoxButtonsBTD.YesNo:
				this.elYes.style.display = "";
				this.elNo.style.display = "";
				this.elYes.focus();
				break;
			case MessageBoxButtonsBTD.YesNoCancel:
				this.elYes.style.display = "";
				this.elNo.style.display = "";
				this.elCancel.style.display = "";
				this.elYes.focus();
				break;
		}

		this.elOK.value = " Ok ";
		this.elCancel.value = " Hủy bỏ ";
		this.elAbort.value = " Ngưng ";
		this.elRetry.value = " Thử lại ";
		this.elIgnore.value = " Bỏ qua ";
		this.elYes.value = " Có ";
		this.elNo.value = " Không ";
	}
};

function alert(text, callback) {
	if (!MessageBoxBTD.isInit) {
		MessageBoxBTD.init();
	}
	MessageBoxBTD.show(text, "Thông báo", MessageBoxButtonsBTD.OK, MessageBoxIconBTD.Warning, callback);
};

$(document).ready(function () {
	MessageBoxBTD.init();
});
