﻿String.format = function()
{
	for (var i = 1; i < arguments.length; i++)
	{
		var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
		arguments[0] = arguments[0].replace(re, arguments[i]);
	}
	return arguments[0];
}
String.prototype.trim = function()
{
	return this.replace(/^\s+/, '').replace(/\s+$/, '');
}
String.prototype.replaceAll = function(from, to)
{
	var i = this.indexOf(from);
	var c = this;
	while (i > -1)
	{
		c = c.replace(from, to);
		i = c.indexOf(from);
	}
	return c;
}

if (!Array.indexOf)
{
	Array.prototype.indexOf = function(obj)
	{
		for (var i = 0; i < this.length; i++)
		{
			if (this[i] == obj)
				return i;
		}
		return -1;
	}
}

var events =
{
	keyCode: function(e)
	{
		return e.charCode || e.keyCode || e.which || 0;
	},
	cancel: function(e)
	{
		e.preventDefault();
		e.stopImmediatePropagation();
	}
}

function getIframe(id)
{
	for (var i = 0; i < window.frames.length; i++)
	{
		try
		{
			if (window.frames[i].frameElement.id == id)
				return window.frames[i];
		}
		catch (e)
		{
			continue;
		}
	}
	return null;
}

var cookies =
{
	get: function(name)
	{
		if (document.cookie && document.cookie != '')
		{
			var list = document.cookie.split(';');
			for (var i = 0; i < list.length; i++)
			{
				var cookie = $.trim(list[i]);
				if (cookie.substring(0, name.length + 1) == (name + '='))
					return decodeURIComponent(cookie.substring(name.length + 1));
			}
		}
		return null;
	}
}

var modal =
{
	open: function(id, width, onClose)
	{
		$('#' + id).dialog(
		{
       		autoOpen: false,
			resizable: false,
       		draggable: false,
       		modal: false,
       		closeOnEscape: false,
       		width: width != undefined && width != null ? width : 'auto',
       		close: onClose,
       		show: 'drop',
       		hide: 'drop'
		});
		$('#' + id).dialog('open');
		return false;
	},
	close: function(id)
	{
		$('#' + id).dialog('close');
		return false;
	},
	autoclose: function(obj)
	{
		var parent = obj;
		while (parent.getAttribute('modalpopup') != '1')
			parent = parent.parentNode;
		if (parent != null)
			modal.close(parent.getAttribute('id'));
		return false;
	}
}

function accessdenied()
{
	modal.open(resources.get('Cafe_PopupDenied'), 480);
	return false;
}

function preventEnter(id)
{
	$('#' + id).keydown(function(event)
	{
		if (events.keyCode(event) == 13)
			return false;
		return true;
	});
}
