/////////////////////////////////////////////////////////////
// Verify the includes I need
/////////////////////////////////////////////////////////////
var i;
var MESSAGE_PATH_RELATIVE;

// Search linked stylesheets to found my styles
for (i=0; i < document.scripts.length; i++)
{
	var iPos = document.scripts(i).src.indexOf ("MessageBox.js");
	if (iPos >= 0)
	{
		MESSAGE_PATH_RELATIVE = document.scripts(i).src.substring(0,iPos);
	}
}

/// <summary>
/// Mensaje a mostrar para el si
/// </summary>
//var _MESSAGE_YES = "Si";
/// <summary>
/// Mensaje a mostrar para el no
/// </summary>
//var _MESSAGE_NO = "No";
/// <summary>
/// Mansaje a mostrar para el aceptar
/// </summary>
//var _MESSAGE_OK = "Aceptar";
/// <summary>
/// Mansaje a mostrar para el cancelar
/// </summary>
//var _MESSAGE_CANCEL = "Cancelar";

/////////////////////////////////////////////////////////////
// Public constants
/////////////////////////////////////////////////////////////
// Images src's
IMAGE_EXCLAMATION = "images/Exclamation.gif";
IMAGE_INFORMATION = "images/Information.gif";
IMAGE_QUESTION = "images/Question.gif";
IMAGE_CRITICAL = "images/critical.gif";

// Command id's
COMMAND_NOTHING = -1;
COMMAND_OK = 1;
COMMAND_CANCEL = 2;
COMMAND_YES = 3;
COMMAND_NO = 4;
COMMAND_RETRY = 5;



/////////////////////////////////////////////////////////////
// Public methods
/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
//	Muestra un mensaje de error
/////////////////////////////////////////////////////////////
function Msg_showError(text)
{
	var objMsg = Msg_CreateMessage ("objMsg", text, IMAGE_CRITICAL);
	return objMsg.show();
}

/////////////////////////////////////////////////////////////
//	Muestra un mensaje de alerta
/////////////////////////////////////////////////////////////
function Msg_showWarning(text)
{
	var objMsg = Msg_CreateMessage ("objMsg", text, IMAGE_EXCLAMATION);
	return objMsg.show();
}

/////////////////////////////////////////////////////////////
//	Muestra un mensaje de pregunta con dos opciones si, no
/////////////////////////////////////////////////////////////
function Msg_showQuestionYN(text)
{
	var objMsg = Msg_CreateMessage ("objMsg", text, IMAGE_QUESTION);
	objMsg.command = COMMAND_NOTHING;		//Default return
	// Configure Yes
	objMsg.button1.command = COMMAND_YES;
	objMsg.button1.visible = true;
	objMsg.button1.caption = _MESSAGE_YES;
	// Configure No
	objMsg.button2.command = COMMAND_NO;
	objMsg.button2.visible = true;
	objMsg.button2.isDefault = true;
	objMsg.button2.caption = _MESSAGE_NO;
	return objMsg.show();
}

/////////////////////////////////////////////////////////////
//	Muestra un mensaje de pregunta con tres opciones si, no, cancel
/////////////////////////////////////////////////////////////
function Msg_showQuestionYNC(text)
{
	var objMsg = Msg_CreateMessage ("objMsg", text, IMAGE_QUESTION);
	objMsg.command = COMMAND_CANCEL;		//Default return
	// Configure Yes
	objMsg.button1.command = COMMAND_YES;
	objMsg.button1.visible = true;
	objMsg.button1.caption = _MESSAGE_YES;
	// Configure No
	objMsg.button2.command = COMMAND_NO;
	objMsg.button2.visible = true;
	objMsg.button2.caption = _MESSAGE_NO;
	// Configure Cancel
	objMsg.button3.command = COMMAND_CANCEL;
	objMsg.button3.visible = true;
	objMsg.button3.caption = _MESSAGE_CANCEL;
	return objMsg.show();
}

/////////////////////////////////////////////////////////////
//	Muestra un mensaje normal
/////////////////////////////////////////////////////////////
function Msg_showMessage(text)
{
	var objMsg = Msg_CreateMessage ("objMsg", text, IMAGE_INFORMATION);
	return objMsg.show();
}

/////////////////////////////////////////////////////////////
// Crea un mensaje
//
// Para usar esta función se debe indicar obligatoriamente un nombre 
//	y la cabecera del mensaje
/////////////////////////////////////////////////////////////
function Msg_CreateMessage(name, caption, image)
{
	// Comprobamos que el nombre sea correcto
	if (typeof(name) != 'string' || name == '')
	{
		alert ('Err 428: Agumento no válido [name]. Debe indicar un nombre válido.');
		return null;
	}

	// Comprobamos que haya mensaje
	if (typeof(caption) != 'string' || caption == '')
	{
		alert ('Err 428: Agumento no válido [caption]. Debe indicar un mensaje válido.');
		return null;
	}
	
	// Asignamos la imagen inicial
	if (typeof(image) != 'string' || image == '')
		image = "";

	var objMessage = new _Message(name, caption, image, MESSAGE_PATH_RELATIVE + "MessageDlg.htm");
	//eval(name + ' = objMessage');
	return objMessage;
}



/////////////////////////////////////////////////////////////
// Prototipo
/////////////////////////////////////////////////////////////
function _Message(name, caption, image, dialog)
{
	// Initialize the prototype
	//if (typeof(_bMsgPrototypeCalled) == 'undefined') 
		_Msg__Prototype();

	// public properties
	this.id = name;
	this.caption = caption;
	this.image = (image == "" ? IMAGE_EXCLAMATION : image);
	this.command = COMMAND_OK;
	
	// The buttons
	this.button1 = new Msg_Button(COMMAND_OK, _MESSAGE_OK, true, true);
	this.button2 = new Msg_Button(COMMAND_CANCEL, _MESSAGE_CANCEL, false, false);
	this.button3 = new Msg_Button(COMMAND_NOTHING, "", false, false);
	
	// private propeties
	this._dialog = dialog;
}

function _Msg__Prototype()
{
	//Constants
	DEFAULT_WIDTH = "22";
	DEFAULT_HEIGHT = "8";
	
	//public methods
	_Message.prototype.show = _Msg_show;
	
	_bMsgPrototypeCalled = 1;


	//Methods implementation's
	
	//////////////////////////////////////////////////////////////////////////////
	// Muestra el mensaje
	//////////////////////////////////////////////////////////////////////////////
	function _Msg_show ()
	{
		// Show the message
		window.showModalDialog(this._dialog, this, "scroll:no;status:no;help:no;resizable:no;dialogwidth:" + DEFAULT_WIDTH + ";dialogheight:" + DEFAULT_HEIGHT + ";");
		return this.command;
	}	
}


//---------------------------------------------------------------------------------------
//	Class: Msg_Button
//---------------------------------------------------------------------------------------
function Msg_Button(Command, Caption, Visible, Default)
{
	// Initialize the prototype
	if (typeof(_bMsgButtonPrototypeCalled) == 'undefined') 
		_MsgButton__Prototype();
		
	// Public properties
	this.command = Command;
	this.caption = Caption;
	this.visible = Visible;
	this.isDefault = Default;
}

function _MsgButton__Prototype()
{
	_bMsgButtonPrototypeCalled = 1;
}



