﻿/** The Message object constructor. */
Message = function()
{
	// member variables
	this.Message = "";
	this.Title = "";
	this.SeverityLevel = Message.Severity.Low;
	this.AutoExpireDuration = 0;
	this.ButtonEnableDuration = 0;
	this.Buttons = [];
	this.Height = Message._minHeight;
	this.Width = Message._minWidth;
};

/**
 *  This class represents a Message Button
 */
MessageButton = function(sText, sTitle, oReturnValue)
{
	this.Text = sText;
	this.Title = sTitle;
	this.ReturnValue = oReturnValue;
	this.EnableDuration = 0;
};

// ** constants

/// "static", needed for message severity.
Message.Severity = {};
Message.Severity["Low"] = "Low";
Message.Severity["Medium"] = "Medium";
Message.Severity["High"] = "High";
Message.Severity["Confirm"] = "Confirm";

/// "static", needed for displaying image for each severity.
Message.Image = {};
Message.Image["Low"] = "message_info.gif";
Message.Image["Medium"] = "message_warning.gif";
Message.Image["High"] = "message_warning2.gif";
Message.Image["Confirm"] = "message_confirm.gif";

/// "static", needed for initial width and height of the modal window
Message._minHeight = 1;
Message._minWidth = 1;

// BEGIN: MESSAGE OBJECT FUNCTIONS

/**
 *  Adds a Menu button to the message. 
 *  Buttons are displayed as entered order from left to right.
 */
Message.prototype.addButton = function(button)
{
	if (arguments.length > 0)
		this.Buttons[this.Buttons.length] = button;
}

/**
 *  This function creates the message and returns the user's choice as boolean
 *  true for clicking OK Button and false for Cancel button (or window close icon which is found top-right corner)
 *  Some properties need to be set before calling this function.
 */
Message.prototype.display = function()
{
	if (this.Buttons.length == 0)
	{
		//alert("No button")
		var oOKButton = new MessageButton();
		oOKButton.Text = "OK";
		oOKButton.Title = this.AutoExpireDuration == 0 ? "OK_Title" : "OK_Title_Close";
		oOKButton.ReturnValue = true;
		oOKButton.EnableDuration = this.ButtonEnableDuration*1000;
		this.addButton(oOKButton)
		//Add Cancel Button
		if (this.AutoExpireDuration == 0)
		{
			var oCancelButton = new MessageButton();
			oCancelButton.Text = "Cancel";
			oCancelButton.Title = "Cancel_Title";
			oCancelButton.ReturnValue = false;
			this.addButton(oCancelButton)
		}
	}else
	{
		//alert("user's buttons")
		if (this.Buttons.length > 1)
		{
			for (var i=0; i<(this.Buttons.length-1); i++)
			{
				if (this.Buttons[i].EnableDuration == 0)
					this.Buttons[i].EnableDuration = this.ButtonEnableDuration*1000;
				else
					this.Buttons[i].EnableDuration = this.Buttons[i].EnableDuration*1000;
			}
		}
		
	}

	var oMessageWindow = null;
	var oArguments = {
		Title : this.Title,
		Message : replaceNewLine(this.Message),
		SeverityLevel : this.SeverityLevel,
		AutoExpireDuration : this.AutoExpireDuration*1000,
		ButtonEnableDuration : this.ButtonEnableDuration*1000,
		Buttons : this.Buttons,
		RootPath : getRootPath()
	};
	//var oMessageWindow = window.showModalDialog(getRootPath() + "/js/ConfirmationMessage.html", oArguments,"dialogHeight:" + this.Height + "px;dialogWidth:" + this.Width + "px;status:no;help:no;resizable:no;scroll:no;");
	var oMessageWindow = window.showModalDialog(getRootPath() + "/scripts/message/ConfirmationMessage.html", oArguments,"dialogHeight:" + this.Height + "px;dialogWidth:" + this.Width + "px;status:no;help:no;resizable:no;scroll:no;");
	if(oMessageWindow)
	{
		return oMessageWindow.confirmed;
	}
	return false;
}

// END: MESSAGE OBJECT FUNCTIONS

// BEGIN: UTILITY FUNCTIONS

// replace <CR> "\n" with <BR> tag
function replaceNewLine(text)
{
	rExp = /\n/gi;
	return text.replace(rExp, "<br>");
}

/** 
 *  gets current URL's protocol + hostname and first folder
 *  and returns it
 */
function getRootPath()
{   
    //return window.location.protocol + "//" + window.location.hostname + "/" + window.location.pathname.split("/")[1];
	var rcode;
	
	rcode = window.location.protocol + "//" + window.location.host + "/";
	if (rcode.indexOf("localhost")!=-1) 
	    rcode += window.location.pathname.split("/")[1];
	
	//alert("rcode : " + rcode);
	return rcode;
	
	// Visual Studio 2005: localhost (localhost + port) number included - 06/07/2006 TA
	//return window.location.protocol + "//" + window.location.host + "/" + window.location.pathname.split("/")[1];
	
	
	
	
}

// END: UTILITY FUNCTIONS


//CONSTANT
Message._Buttons = {};
Message._Buttons["OK"] = "Tamam";
Message._Buttons["OK_Title"] = "Tamam";
Message._Buttons["OK_Title_Close"] = "Tamam";
Message._Buttons["Cancel"] = "İptal";
Message._Buttons["Cancel_Title"] = "İptal";

Message._ImagesTitle = {};
Message._ImagesTitle["Low"] = "Title Low";
Message._ImagesTitle["Medium"] = "Title Medium";
Message._ImagesTitle["High"] = "Title High";
Message._ImagesTitle["Confirm"] = "Title Confirm";


//MESSAGE FUNCTION
function InformationMessage(sMessage, sTitle, nAutoExpireDuration)
{
	if (typeof(Message) != "undefined")
	{
		var message = new Message();
		message.Message = sMessage;
		message.Title = sTitle;
		message.AutoExpireDuration = nAutoExpireDuration;
		return message.display();
	}
}

function WarningMessage(sMessage, sTitle, oSeverityLevel, nButtonEnableDuration)
{
	if (typeof(Message) != "undefined")
	{
		var message = new Message();
		message.Message = sMessage; 
		message.Title = sTitle;
		message.SeverityLevel = oSeverityLevel;
		message.ButtonEnableDuration = nButtonEnableDuration;
		return message.display();
	}
}

function ConfirmMessage(sMessage, sTitle, nButtonEnableDuration)
{
	
	if (typeof(Message) != "undefined")
	{
		var message = new Message();
		message.Message = sMessage;
		message.Title = sTitle;
		message.ButtonEnableDuration = nButtonEnableDuration;
		message.SeverityLevel = Message.Severity.Confirm;
		
		
		var oMessageButton = new MessageButton("Evet", "Evet", true);
		message.addButton(oMessageButton);
		
		oMessageButton = new MessageButton("Hayır", "Hayır", false);
		message.addButton(oMessageButton);
		
		return message.display();
	}
}
