﻿var $jq = jQuery.noConflict();

function pageLoad()
{
	$jq("#buttonSend").bind("click", onButtonSendClick);

	// Preload confirmation image
	var confirmationImage = new Image();
	confirmationImage.src = "../images/layout/comments/comment-confirmation.png";
}
function onButtonSendClick()
{
	if (validateRequiredFields())
	{
		deactivateSendButton();

		showProgress();

		Egyptenportalen.Core.Web.Services.PageCommentsService.AddPageComment($jq("#hiddenPageID").val(),
																			 $jq("#inputSubject").val(),
																			 $jq("#inputName").val(),
																			 $jq("#inputEmail").val(),
																			 $jq("#textareaComment").val(),
																			 onAddPageCommentComplete,
																			 onAddPageCommentError);
	}
}
function onAddPageCommentComplete(result)
{
	hideProgress();

	if (result > 0)
	{
		clearForm();
		showConfirmation();
	}
	else
	{
		activateSendButton();
		alert("Kommentaren gick inte att spara\r\n\r\nFörsök gärna igen senare");
	}
}
function onAddPageCommentError(error)
{
	activateSendButton();
	hideProgress();

	if (error.get_timedOut())
	{
		alert("Ett fel har inträffat (timeout)\r\n\r\nMeddelande: " + error.get_message());
	}
	else
	{
		alert("Ett fel har inträffat\r\n\r\nMeddelande: " + error.get_message());
	}
}
function validateRequiredFields()
{
	// Validate subject
	if ($jq("#inputSubject").val().length == 0)
	{
		$jq("#inputSubject").addClass("invalidField");
		$jq("#infoCellSubject").addClass("invalidInfoText");
	}
	else
	{
		$jq("#inputSubject").removeClass("invalidField");
		$jq("#infoCellSubject").removeClass("invalidInfoText");
	}

	// Validate name
	if ($jq("#inputName").val().length == 0)
	{
		$jq("#inputName").addClass("invalidField");
		$jq("#infoCellName").addClass("invalidInfoText");
	}
	else
	{
		$jq("#inputName").removeClass("invalidField");
		$jq("#infoCellName").removeClass("invalidInfoText");
	}

	// Validate comment
	if ($jq("#textareaComment").val().length == 0)
	{
		$jq("#textareaComment").addClass("invalidField");
		$jq("#infoCellComment").addClass("invalidInfoText");
	}
	else
	{
		$jq("#textareaComment").removeClass("invalidField");
		$jq("#infoCellComment").removeClass("invalidInfoText");
	}

	var isFormValid = true;

	if ($jq("#inputSubject").hasClass("invalidField"))
	{
		$jq("#inputSubject").focus();
		isFormValid = false;
	}
	else if ($jq("#inputName").hasClass("invalidField"))
	{
		$jq("#inputName").focus();
		isFormValid = false;
	}
	else if ($jq("#textareaComment").hasClass("invalidField"))
	{
		$jq("#textareaComment").focus();
		isFormValid = false;
	}
	else
	{
		isFormValid = true;
	}

	return isFormValid;
}
function clearForm()
{
	$jq("#inputSubject").val("");
	$jq("#inputName").val("");
	$jq("#inputEmail").val("");
	$jq("#textareaComment").val("");
}
function showConfirmation()
{
	var tblPos = $jq("#tableCommentsForm").position();

	$jq(".confirmation").css({ 'left': tblPos.left + 50, 'top': tblPos.top + 50 });
	$jq(".confirmation").fadeIn(500, hideConfirmation);
}
function hideConfirmation()
{
	setTimeout
	(
		function()
		{
			$jq(".confirmation").fadeOut(2000, function() { activateSendButton(); });
		},
		3000
	);
}
function deactivateSendButton() { $jq("#buttonSend").attr('disabled', 'disabled'); }
function activateSendButton() { $jq("#buttonSend").removeAttr('disabled'); }
function showProgress() { $jq("#tableProgress").removeClass("hidden"); }
function hideProgress() { $jq("#tableProgress").addClass("hidden"); }
