
var gNumPages = 0;
var gValidateType = '';
var gFormName = '';
var gCurrentPage = 1;

function hideScrollablePages()
{
	for (i = 1; i < (gNumPages+1); i++)
	{
		$('#Page' + i).hide();
	}
	$('#LinkPage1').removeClass('bold').addClass('contactUs');
}

function showNextPagePrompt(pPage, pSuccess)
{
	if (pSuccess)
	{
		if (!$('#Completed' + pPage).length)
		{
			$('#Page' + pPage).append('<div id="Completed' + pPage + '"><img src="./images/icons/icon_tick.png" alt="" /> <a href="#" id="LinkQuickPage' + (pPage + 1) + '">click here</a> to proceed or use the top navigation bar &gt;&gt;</div>');
			$('#LinkQuickPage' + (pPage + 1)).attr('title', (pPage + 1));
			$('#LinkQuickPage' + (pPage + 1)).click(function(){
				$('#LinkPage' + $(this).attr('title')).click();
			});
		}
	}
	else
	{
		$('#Completed' + pPage).remove();
	}
}

function scrollableFormValidate()
{
	$.ajax({
		url: 'index.php?a=Ajax&b=FormValidate&c=' + gValidateType,
		type: 'POST',
		async: false,
		dataType: 'json',
		data: $('#' + gFormName).serialize(),
		success: function(data){
			message = ''; redirect = 1;
			$.each(data, function(i,item){
				//excessArray[i] = item;
				if (i == 0){message = item;}
				if (i == 1){redirect = item;}
			});
			if (message == 'refer')
			{
				$('#ReferralReasons').load('index.php?a=Ajax&b=ReferralReasons&QuoteID=' + $('#QuoteID').val());

				$('#DivSuccess').hide();
				$('#DivReferral').show();
				$('#DivFailure').hide();
			}
			else if (message == '')
			{
				$('#DivSuccess').show();
				$('#DivReferral').hide();
				$('#DivFailure').hide();

				// set up links on comparison products
				if ($('#ProductID').val() == 12 || $('#ProductID').val() == 13)
				{
					var quotes = redirect.split('~');
					var content = '<p>You have obtained the following quotes:</p>';
					for (i = 0; i < quotes.length; i++)
					{
						if (quotes[i] != '')
						{
							var parts = quotes[i].split('|');
							content += '<div class="subHeader">Quotation ' + (i+1) + ' ' + parts[1] + ' &pound;' + parts[2] + '</div>';
						}
					}
					if (gValidateType == 'FullQuoteUnderwriter')
						content += '<p><ul><li><a href="index.php?a=Internal&amp;b=Underwriting&amp;c=ViewDocs&amp;QuoteID=' + parts[0] + '">view the documentation for these quotes...</a></li></ul></p>';
					else
						content += '<p><ul><li><a href="index.php?a=Broker&amp;b=Quotes&amp;c=ViewDocs&amp;QuoteID=' + parts[0] + '">view the documentation for these quotes...</a></li></ul></p>';

					$('#DivSuccess').html(content);

					//alert($('#DivSuccess').html());
				}
			}
			else
			{
				if (redirect > 0)
				{
					$('#ErrorMessage').html(message);
					$('#Redirect').html('<a href="#" class="LinkQuickPage' + redirect + '">return to page ' + redirect + '...</a>');
					$('.LinkQuickPage' + redirect).attr('title', redirect);
					$('.LinkQuickPage' + redirect).click(function(){
						$('#LinkPage' + $(this).attr('title')).click();
					});
				}
				else
				{
					$('#ErrorMessage').html(message);
					$('#Redirect').html('');
				}

				$('#DivSuccess').hide();
				$('#DivReferral').hide();
				$('#DivFailure').show();
			}
		}
	});

}

function saveCurrentPage(pFunction)
{
	$.ajax({
		url: 'index.php?a=Ajax&b=FormPartSave&c=' + gValidateType + '&Page=' + gCurrentPage,
		type: 'POST',
		async: false,
		data: $('#' + gFormName).serialize()
	});

	if (typeof(pFunction) == 'function')
	{
		pFunction(gCurrentPage);
	}	
	if (typeof(updateSubjectivities) == 'function')
	{
		updateSubjectivities();
	}
}

function setScrollable(pNumPages, pValidateType, pFormName, pFunction)
{
	gNumPages = pNumPages;
	gValidateType = pValidateType;
	gFormName = pFormName;

	$('div.scrollable').scrollable({
		size: 3, 
		clickable: true, 
		loop: false
	});

	hideScrollablePages();
	$('#LinkPage1').addClass('bold');
	$('#Page1').show();


	for (i = 1; i < (gNumPages+1); i++)
	{
		if (typeof(pFunction) == 'function')
		{
			bindLink(i, pFunction);
		}
		else
		{
			bindLink(i, '');
		}
	}

	$('#LinkFormSubmit').click(function(){
		$('#' + gFormName).submit();
	});

	$('#DivSuccess').hide();
	$('#DivReferral').hide();
	scrollableFormValidate();
}

function bindLink(pNum, pFunction)
{

	$('#LinkPage' + pNum).attr('title', pNum);

	$('#LinkPage' + pNum).click(function(){

		var stopChange = false;
		// medical screening check
		if ($('#ProductID').length && ($('#ProductID').val() == 12 || $('#ProductID').val() == 13))
		{

			if ($('#Page3').css('display') == 'block' && pNum != 3)
			{
				if ($('#SelectorMedDec12').hasClass('selButtonSelected') || $('#SelectorMedDec22').hasClass('selButtonSelected') || $('#SelectorMedDec32').hasClass('selButtonSelected') || $('#SelectorMedDec42').hasClass('selButtonSelected'))
				{
					var declared = false;
					for (i = 0; i < 20; i++)
					{
						if ($('#ScreeningConditionsList' + i).length && $('#ScreeningConditionsList' + i).html() != '' && $('#ScreeningConditionsList' + i).html() != 'No conditions declared')
						{
							declared = true;
						}
					}
					
					if (!declared)
					{
						stopChange = true;
					}
				}
			}
		}
		var stopMed = false;
		// med questions check
		if ($('#ProductID').length && ($('#ProductID').val() == 12 || $('#ProductID').val() == 13))
		{
			if (pNum == 5)
			{
				if (!$('#SelectorMedDec12').hasClass('selButtonSelected') && !$('#SelectorMedDec11').hasClass('selButtonSelected') &&
					!$('#SelectorMedDec22').hasClass('selButtonSelected') && !$('#SelectorMedDec21').hasClass('selButtonSelected') &&
					!$('#SelectorMedDec32').hasClass('selButtonSelected') && !$('#SelectorMedDec31').hasClass('selButtonSelected') &&
					!$('#SelectorMedDec42').hasClass('selButtonSelected') && !$('#SelectorMedDec41').hasClass('selButtonSelected'))
				{
					stopMed = true;
				}
			}
		}

		hideScrollablePages();
		$('#Page' + $(this).attr('title')).show();

		if (typeof(pFunction) == 'function')
		{
			saveCurrentPage(pFunction);
		}
		else
		{
			saveCurrentPage('');
		}

		if ($(this).attr('title') == gNumPages)
		{
			// final page

			scrollableFormValidate();
			if ($('#FinalPremiumLine').length)
			{
				if (typeof(recalculate) == 'function')
				{
					recalculate(gCurrentPage);
				}
			}
			scrollableFormValidate();
		}
		else
		{
			if (typeof(recalculate) == 'function')
			{
				recalculate(gCurrentPage);
			}
			if (typeof(updateEndorsements) == 'function')
			{
				updateEndorsements();
			}
		}

		gCurrentPage = $(this).attr('title');


		if ($('#ProductID').length && ($('#ProductID').val() == 12 || $('#ProductID').val() == 13))
		{
			if ($('#Page5').css('display') == 'block')
				$('.contentRightPremiumBox2').show();
			else
				$('.contentRightPremiumBox2').hide();
		}

		if (stopChange)
		{
			$('#DialogNoConditions').dialog('open');

			//$('#LinkPage3').click();
		}
		if (stopMed)
		{
			$('#DialogNoMedical').dialog('open');
		}
	});

	$('.LinkQuickPage' + pNum).attr('title', pNum);

	$('.LinkQuickPage' + pNum).click(function(){
		$('#LinkPage' + $(this).attr('title')).click();
	});
}

$(function()
{
	if ($('#LinkSaveQuote').length)
	{
		$('#LinkSaveQuote').click(function(){
			saveCurrentPage('');
			$('#OngoingPremium').after('<div id="DialogSaved" title="quote saved"><br /><p style="text-align:center">The quotation has been saved with the reference ' + $('#QuoteID').val() + '.</p></div>');

			$('#DialogSaved').dialog({
				bgiframe: true,
				autoOpen: true,
				modal: true,
				width: 400,
				resizable: false,
				buttons: {
					'continue': function() {
						$(this).dialog('close');
					},
					'quit': function() {
						window.location = 'index.php?a=Broker';
					}
				}
			});

		});
	}
});
