﻿// Handles messages related to bookings
// This class could be further abstracted
var MessageType=
{
	SERVER: 1, //Indicates a message originating after an ajax call
	LOCAL: 2 //Indicates a local message (usually pre-post validation)
}

var MessageController=Class.create(
{
	initialize:function(ServerNotifyContainer,LocalMsgs)
	{
		this.ServerNotifyContainer=ServerNotifyContainer;
		this.LocalMsgs=LocalMsgs; //This should be a map
		this.ServerMsgs=new Hash();
		this.HasLocalMessages=false;
		this.HasServerMessages=false;
		this.LastMessageKey=null;
	},
	AddMessage: function(item)
	{
		if (item.MessageType==MessageType['LOCAL'])
			this.LocalMsgs.set(item.MessageNum,item);
		else
		{
			this.ServerMsgs.set(item.MessageNum,item);
			this.HasServerMessages=true;
		}
	},
	GetStatus: function()
	{
		return !(this.HasServerMessages || this.HasLocalMessages)
	},
	ClearServerMessages: function()
	{
		this.ServerMsgs=new Hash();
		this.HasServerMessages=false;
	},
	ShowLocalMsg: function(MsgNum,Container)
	{
		var item=this.LocalMsgs.get(MsgNum);

		if (item!=undefined)
		{
			/*
				Two checks are made:
					1. If a Container was passed set this to be the item's Container
					2. If still null, set the Server Container as the item's Container
			*/

			if (Container!=undefined || Container!=null)
				item.Container=Container;

			if (item.Container==undefined || item.Container==null)
				item.Container=	this.ServerNotifyContainer;

			if (!Element.visible(item.Container))
				Effect.BlindDown($(item.Container));

			$(item.Container).innerHTML="<ul><li class='warning'><span>" + item.MessageText + "</span></li></ul>";
			this.HasLocalMessages=true;
			this.LastMessageKey=MsgNum;
		}
	},
	ShowLocalMsgs: function(Messages)
	{
		var msgList="";
		this.ClearServerMessageContainer();
		if (!Element.visible(this.ServerNotifyContainer))
			Effect.BlindDown($(this.ServerNotifyContainer));
		//this.ServerNotifyContainer.innerHTML="<ul>";
		Messages.each(function(MsgNum)
		{
			var item=this.LocalMsgs.get(MsgNum);
			if (item!=undefined)
			{
				if (item.Container==undefined || item.Container==null)
					item.Container=	this.ServerNotifyContainer;

				msgList +="<li class='warning'><span>" + item.MessageText + "</span></li>";
				this.HasLocalMessages=true;
				this.LastMessageKey=MsgNum;
			}
		},this);
		this.ServerNotifyContainer.innerHTML="<ul>" + msgList + "</ul>";
	},
	ClearLastMessage: function()
	{
		if (this.LastMessageKey!=null)
		{
			var item=this.LocalMsgs.get(this.LastMessageKey);
			if (item!=undefined)
				$(item.Container).innerHTML="";
		}
		if (this.HasServerMessages)
			this.ClearServerMessageContainer();

		this.HasLocalMessages=false;
		this.LastMessageKey=null;
	},
	ClearServerMessageContainer: function()
	{
		this.ServerNotifyContainer.innerHTML="";
		this.HasServerMessages=false;
	},
	ShowServerMessages: function()
	{
		if (this.HasServerMessages)
		{
			var msgList="";
			if (!Element.visible(this.ServerNotifyContainer))
            	Effect.BlindDown($(this.ServerNotifyContainer));
			//this.ServerNotifyContainer.innerHTML="<ul>";

			this.ServerMsgs.values().each(function(item)
			{
				msgList+=this.ShowServerMsg(item);
			},this);
			this.ServerNotifyContainer.innerHTML="<ul>" + msgList  +"</ul>";
		}
	},
	ShowServerMsg: function(item)
	{
       	return "<li class='warning'><span>" + item.MessageText + "</span></li>";
	}
}); //End MessageController

//This is an abstract class
var MessageItem=Class.create(
{
	initialize:function(MessageNum,MessageText,MessageType)
	{
		this.MessageNum=MessageNum;
		this.MessageText=MessageText;
		this.MessageType=MessageType;
	}
}); //End MessageItem

var LocalMessage=Class.create(MessageItem,
{
	initialize: function($super,MessageNum,MessageText,Container) //pass null for Container
	{
		$super(MessageNum,MessageText,MessageType['LOCAL']);
		this.Container=Container;
		//alert(this.Container);
	}
}); //End LocalMessage

var ServerMessage=Class.create(MessageItem,
{
	initialize: function($super,MessageNum,MessageText)
	{
		$super(MessageNum,MessageText,MessageType['SERVER']);
	}
}); //End ServerMessage