var g_SignInDialog = null;

SignInDialog.prototype = new ModalLayerDialog();
SignInDialog.prototype.constructor = SignInDialog;

function SignInDialog(modalDialogId, titelId, serviceId, productId, userNameId, passwordId, okButtonId, errorLabelId, currentPageServiceId, currentSessionContext, urkUniqueId)
{
	// Call base constructor
	ModalLayerDialog.call(this, modalDialogId, titelId);

	// parameters	
	this.ServiceId = serviceId;
	this.ProductId = productId;
	this.UserName = document.getElementById(userNameId);
	this.Password = document.getElementById(passwordId);
	this.OkButtonId = okButtonId;
	this.ErrorLabel = document.getElementById(errorLabelId);
	this.CurrentPageServiceId = currentPageServiceId;
	this.CurrentSessionContext = currentSessionContext;
	this.UrkUniqueId = urkUniqueId;

	// call back stuff
	var self = this;
	g_SignInDialog = this;
	var callBackFunction = function(data) { self.ProccessResult(data); };
	
	// Override of the Open function
	this.Open = function()
	{
		if (this.UserName != null && this.Password != null)
		{
			if (this.ErrorLabel != null)
				this.ErrorLabel.style.display = "none";
			this.UserName.value = "";
			this.Password.value = "";
			this.CheckOk();

			// call base class function
			ModalLayerDialog.prototype.Open.call(this, false, false);
			this.UserName.focus();
		}
	}
	
	this.CheckOk = function()
	{
		if (this.OkButtonId != null)
		{
			var enabled = false;
			if (this.UserName != null && this.UserName.value.length > 0 && this.Password != null && this.Password.value.length > 0)
				enabled = true;
			
			EnableButton(this.OkButtonId, enabled);
		}
	}
	
	this.CheckKey = function(evt)
	{
		this.CheckOk();

		evt = (evt) ? evt : ((window.event) ? window.event : null);
		if (evt.keyCode == 13) // Enter key
		{
			this.SignIn();
			return false;
		}
		return true;
	}
	
	this.PreventBeep = function(evt)
	{
		evt = (evt) ? evt : ((window.event) ? window.event : null);
		if (evt.keyCode == 13) // Enter key
			return false;
		return true;
	}
	
	this.ProccessResult = function(data)
	{
		this.ErrorLabel.style.display = "none";

		if (data.Success && data.RedirectURL)
		{
			var url = data.RedirectURL;
			
			// Keep the client resolution.
			if (typeof(prevClientRes) == "string")
				url += "&VolatileResolution=" + prevClientRes;
			
			document.location = url;
		}
		else
		{
			this.Password.value = "";
			this.Password.focus();
			if (data.ErrorMessage)
			{
				this.ErrorLabel.innerHTML = data.ErrorMessage;
				this.ErrorLabel.style.display = "block";
			}
		}
	}
	
	this.SignIn = function()
	{
		if (this.UserName != null && this.UserName.value.length > 0 && this.Password != null && this.Password.value.length > 0)
		{
			var url = this.ServiceId + ".serv";
			var data = { Product: this.ProductId, Username: this.UserName.value, Password: this.Password.value };
			if (this.CurrentPageServiceId && this.CurrentSessionContext)
			{
				data.PageServiceId = this.CurrentPageServiceId;
				data.Context = this.CurrentSessionContext;
				if (this.UrkUniqueId)
					data.UrkUniqueId = this.UrkUniqueId;
			}
			// adding timestamp (normally the Cache-Control stuff in SignInUser should prevent caching, but sometimes on error results still get cached)
			var now = new Date();
			data.timestamp = now.getTime();
			$.get(url, data, callBackFunction, "json");
		}
	}
}

function GlobalSignIn()
{
	if (g_SignInDialog)
		g_SignInDialog.Open();
}

function GlobalDisableExportButtons(tooltip)
{
	$(".exportbutton").each(function(index)
	{
		EnableButton(this.id, false);
		$(this).click(GlobalSignIn);
		this.title = tooltip;
	});
}
