$(function()
{
	//view schedule benefits
	if ($('.benefitsLink').length)
	{
		$('.benefitsLink').click(function(e){
			schemeID = $(this).attr('rel');
			e.preventDefault();
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=ScheduleBenefits&c='+schemeID,
				success: function (html){
					$('#ScheduleOfBenefitsDialog').html(html);
					$('#ScheduleOfBenefitsDialog').dialog({
						bgiframe: true,	autoOpen: false, modal: false, width: 800, height: 500,	resizable: true,
						buttons: {
							'Close': function(){
								$(this).dialog('close');
							}
						}
					});

					$('#ScheduleOfBenefitsDialog').dialog('open');
				}
			});
		});
	}

	$('input[type="submit"], input[type="image"]').dblclick(function(){
		$('#leftWrapper').after('<div id="DialogClick" title="Warning">Please do not double click the submit buttons.</div>');

		$('#DialogClick').dialog({
			bgiframe: true, autoOpen: false, modal: true, width: 500, resizable: true,
			buttons: {
				'Close': function(){
					$(this).dialog('close');
				}
			}
		});
		
		$('#DialogClick').dialog('open');
		return false;
	});
	
	//generate sales pdf
	$('#SalesPDF').click(function() {
		$.ajax({
			url: SITEPATH + 'index.php?a=Ajax&b=CreateSalesPDF',
			success: function(data){
				$('#SalesDialog').dialog({
					bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500, resizable: true,
					buttons: {
						'Close': function(){
							$(this).dialog('close');
						}
					}
				});
								
				$('#SalesDialog').html('<embed src="' + SITEPATH + 'temp/'+ data + '" href="' + SITEPATH + 'temp/'+ data + '" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
				$('#SalesDialog').dialog('open');
			}
		});
		return false;
	});


	//sortable list feedback internal
	$('#sortable').sortable({
		axis: 'y',
		cursor: 'pointer',
		distance: 5,
		opacity: 0.6,
		tolerance: 'pointer',
		update: function(event, ui){
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=Feedback',
				type: 'POST',
				data: $('#sortable').sortable('serialize')
			});
		}
	});

	$('#addanswer').click(function(e){
		records = $('#records').val();
		$('<li class="ui-state-default" style="list-style:none;" id="Feedback_'+records+'" ><input type="hidden" id="AnswerID'+records+'" name="AnswerID'+records+'" value="0" /><table width="100%"><tr><td style="width:250px;"><input type="text" name="Answer'+records+'" style="padding:0;margin:0;width:200px;"/></td><td>Live? <input type="checkbox" name="is_active'+records+'" style="width:20px;"</td></tr></table></li>').appendTo('#sortable2');
		$('#records').val(parseInt(records)+1);
		return false;
	});

	if ($('#Type').val() == '1')
	{
		$('#showhide').show();
		$('#link').show();
	}

	else if ($('#Type').val() == '2' || $('#Type').val() == '3')
	{
		$('#showhide').hide();
		$('#link').hide();
	}

	$('#answer_type').change(function(e){
		 if ($("option:selected", $(this)).attr("value")==1) 
		{
			$('#showhide').show();
			$('#link').show();
		}
		else 
		{
			$('#showhide').hide();
			$('#link').hide();
		}
	});
	$(".slider-range-max").each(function(){
		$(this).slider(
		{
			range: "max",
			min: 0,
			max: 10,
			value: $("#" + $(this).attr('rel')).val(),
			slide: function(event, ui) {
				$("#" + $(this).attr('rel')).val(ui.value);
			}
		});
	});

	// Help text for the Cookies usage policies
	
	$('#DialogCookies').html('<p>A cookie is a small file of letters and numbers that is stored on your computer to help us provide you with a better experience.</p><p>These cookies help us to do the following things:</p><p><strong>Enable our website to function correctly</strong><br />We put a session cookie on your computer for the duration of your visit to our site. It stores data about the browser you are using and information you have entered in order for our website to function correctly and for us to provide you with a quotation or policy. This cookie is deleted when you leave our website.</p><p><strong>Help us improve the customer experience on our website</strong></p><p>We use cookies to collect trend data which we analyse to help us improve how our site works. This trend data includes how visitors find our site and how they move around our site. They do not collect or store any personal data.</p><p><strong>Improve the quality of our email messages</strong><br />We use cookies to identify whether you have visited our site in response to one of our email newsletters.   This enables us to improve our email messages and send you more relevant information. To opt out of email marketing click on the unsubscribe link which appears at the bottom of all of our emails, or email<br/><a href="mailto:optout@sunselction.co.uk" "opt out">optout@sunselction.co.uk</a></p><p><strong>Related links</strong></p><p>If you wish to delete any of these cookies simply follow these instructions:<br /><a href="' + SITEPATH + 'privacy#delete" target="_blank" style="text-decoration:underline">Instructions on how to delete cookies from your computer</a></p><p>You can read our full privacy statement here:<br /><a href="' + SITEPATH + 'privacy" target="_blank"  style="text-decoration:underline">Full privacy statement</a></p>');
				
				$('#DialogCookies').dialog({
				bgiframe: true,	autoOpen: false, modal: true, width: 400, resizable: true,
				buttons: {
					'Close': function() {
						$(this).dialog('close');
					}
				}
			});
	
	$('#Cookiestrigger').click(function(e){
	
				e.preventDefault();
				$('#DialogCookies').dialog('open');
		
		});

	
	// internal magenta page
	if ($('.linkCanxDetails').length)
	{
		$('.linkCanxDetails').click(function(){
			$('#DialogDetails').html('<table><tr><td class="captionShort">Affiliate:</td><td>' + $('#AffiliateName' + $(this).attr('rel')).val() + '</td></tr><tr><td class="captionShort">Premium:</td><td>' + $('#AffiliatePremium' + $(this).attr('rel')).val() + '</td></tr><tr><td class="captionShort">Cancellation Date:</td><td>' + $('#AffiliateCanxDate' + $(this).attr('rel')).val() + '</td></tr><tr><td class="captionShort">Notes:</td><td>' + $('#AffiliateNotes' + $(this).attr('rel')).val() + '</td></tr></table>');
			$('#DialogDetails').dialog('open');
		});

		$('.linkEndorsementDetails').click(function(){
			$('#DialogDetails').html('<table><tr><td class="captionShort">Reference:</td><td>' + $('#EndorsementName' + $(this).attr('rel')).val() + '</td></tr><td class="captionShort">Affiliate:</td><td>' + $('#EndorsementAffiliate' + $(this).attr('rel')).val() + '</td></tr><tr><td class="captionShort">User:</td><td>' + $('#EndorsementUser' + $(this).attr('rel')).val() + '</td></tr><tr><td class="captionShort">Premium:</td><td>' + $('#EndorsementPremium' + $(this).attr('rel')).val() + '</td></tr><tr><td class="captionShort">Added Date:</td><td>' + $('#EndorsementDate' + $(this).attr('rel')).val() + '</td></tr><tr><td class="captionShort">Content:</td><td>' + $('#EndorsementNotes' + $(this).attr('rel')).val() + '</td></tr></table>');
			$('#DialogDetails').dialog('open');
		});


		$('#DialogDetails').dialog({
			bgiframe: true, autoOpen: false, modal: true, width: 500, resizable: false,
			buttons: {
				'Close': function(){
					$(this).dialog('close');
				}
			}
		});

		$('.linkSignOff').click(function(){
			$('#SelectedQuote').val($(this).attr('rel'));
			$('#DialogSignOffConfirmation').dialog('open');
		});
		$('.linkSignOffEndorsement').click(function(){
			$('#SelectedEndorsement').val($(this).attr('rel'));
			$('#DialogSignOffConfirmationEndorsement').dialog('open');
		});

		$('#DialogSignOffConfirmation').dialog({
			bgiframe: true, autoOpen: false, modal: true, width: 500, resizable: false,
			buttons: {
				'Cancel': function(){
					$(this).dialog('close');
				},
				'Confirm': function(){
					window.location = SITEPATH + 'index.php?a=Internal&b=Magenta&c=SignOff&QuoteID=' + $('#SelectedQuote').val();
				}
			}
		});
		$('#DialogSignOffConfirmationEndorsement').dialog({
			bgiframe: true, autoOpen: false, modal: true, width: 500, resizable: false,
			buttons: {
				'Cancel': function(){
					$(this).dialog('close');
				},
				'Confirm': function(){
					window.location = SITEPATH + 'index.php?a=Internal&b=Magenta&c=SignOff&EndorsementID=' + $('#SelectedEndorsement').val();
				}
			}
		});
	}

	// dialogs for affiliate home page
	if ($('#DialogInfo').length)
	{
		$('#DialogInfo').dialog({
			bgiframe: true, autoOpen: false, modal: true, width: 700, height: 500, resizable: true,
			buttons: {
				'Close': function(){
					$(this).dialog('close');
				}
			}
		});

		$('#LinkWording').click(function(){
			
			$('#DialogInfo').html('<embed src="' + SITEPATH + 'templates/affiliates/' + $('#AffiliateID').val() + '/wording' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/affiliates/' + $('#AffiliateID').val() + '/wording' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
			$('#DialogInfo').dialog('open');
			return false;
			
		});
		$('#LinkKeyfacts').click(function(){
			$('#DialogInfo').html('<embed src="' + SITEPATH + 'templates/affiliates/' + $('#AffiliateID').val() + '/keyfacts' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/affiliates/' + $('#AffiliateID').val() + '/keyfacts' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
			$('#DialogInfo').dialog('open');
			return false;
		});
		$('#LinkMedical').click(function(){
			$.ajax({
				url: SITEPATHU + 'index.php?a=Ajax&b=AffiliateMedicalText',
				success: function(data){
					$('#DialogInfo').html(data);
					$('#DialogInfo').dialog('open');
				}
			});
			return false;
		});
		$('#LinkClaims').click(function(){
			$.ajax({
				url: SITEPATHU + 'index.php?a=Ajax&b=AffiliateClaimsText',
				success: function(data){
					$('#DialogInfo').html(data);
					$('a[rel="external"][href]').each(function(){
						$(this).attr('target', '_blank');
					});
					$('#DialogInfo').dialog('open');
				}
			});
		});
		return false;
	}

	//affiliate reports
	
	if ($('.affiliate_banner').length)
	{
		$('.affiliate_banner').hide();

		$('#Affiliates').change(function(){
			var affiliate_id = $(this).val();
			$('.affiliate_banner').hide();
			$('#affiliate_'+affiliate_id).show();
		});

		if ($('#Affiliates').val())
		{
			$('#affiliate_'+$('#Affiliates').val()).show();
		}

		
	}

	// add text link for affiliate
	if ($('#addtextlink').length)
	{
		$('#addtextlink').click(function(e){
			var textLink = $('#TextLink').val();
			var affiliateID = $('#ArticleID').val();
			var affiliateName = $('#Name').val();
		
			var newValue = '<a href="http://www.goodtogoinsurance.com/index.php?xyzid='+affiliateID+'&utm_source=affiliate&utm_medium=TextLink&utm_campaign='+affiliateName+'" rel="canonical" target="_blank">'+textLink+'</a>';
			
			if (textLink.indexOf('<a href') == -1)
			{
				$('#TextLink').val(newValue);
			}
			else
			{
				$('#TextLink').val(textLink);
			}
			return false;

		});
	}

	// set banner link on affiliate form
	$('.bannerLink').click(function(){
		var i = $(this).attr('rel');
		var numBan = $('#numBanners').val();
		$.ajax({
			url: SITEPATH + 'index.php?a=Ajax&b=AffiliateAddBanner&c='+i,
			data: $('#affiliate_form').serialize(),
			success: function(data){
				$(data).appendTo('#addbanner');

			}
		});
		$('#numBanners').val(parseInt(numBan)+1);
		$('#DialogBanners').dialog('close');
	});

	if ($('#DialogBanners').length)
	{
		$('#DialogBanners').dialog({
			bgiframe: true, autoOpen: false, modal: true, width: 500, resizable: false,
			buttons: {
				'Close': function(){
					$(this).dialog('close');
				}
			}
		});
		$('#LinkBanners').click(function(){
			$('#DialogBanners').dialog('open');
		});
	}

	// date fields on policy list
	if ($('#ListStartDate').length || $('#ListEndDate').length)
	{
		$('#ListStartDate').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			minDate: '-1y',
			maxDate: '+1d',
			hideIfNoPrevNext: true
		});
		$('#ListEndDate').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			minDate: '-1y',
			maxDate: '+1d',
			hideIfNoPrevNext: true
		});
	}

	// hide javascript warning
	$('#JavascriptWarning').hide();

	// details on ABTA and ATII
	$('#DialogABTA').dialog({
		bgiframe: true,	autoOpen: false, modal: true, width: 500, resizable: false,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('#LinkABTA').click(function(e){
		//e.preventDefault();
		$('#DialogABTA').html('<p>Goodtogoinsurance.com is an ABTA Travel Industry Partner. This helps us keep up to date with the latest in developments in the travel industry and the needs of UK holiday makers.</p>');
		$('#DialogABTA').dialog('open');
	});
	$('#DialogATII').dialog({
		bgiframe: true,	autoOpen: false, modal: true, width: 500, resizable: false,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('#LinkATII').click(function(e){
		//e.preventDefault();
		$('#DialogATII').html('<p>Goodtogoinsurance.com is a member of the Association of Travel Insurance Intermediaries, an industry body which encourages best practice in the selling of travel insurance to promote trust and consumer satisfaction within the industry.</p>');
		$('#DialogATII').dialog('open');
	});


	// news archive links
	$('.monthLink').hide();
	$('a.yearLink').click(function(){
		var year = $(this).attr('rel');
		$('.monthLink').each(function(){
			if ($(this).attr('rel') == year)
				$(this).show();
			else
				$(this).hide();
		});
	});

	// cancel policy
	if ($('#DialogCancel').length)
	{
		$('#DialogCancel').dialog({
			bgiframe: true, autoOpen: false, modal: true, width: 440, resizable: false,
			buttons: {
				'OK': function(){
					$('#FormCancel').submit();
				},
				'Back': function(){
					$(this).dialog('close');
				}
			}
		});
		$('#CancelLink').click(function(){
			$('#DialogCancel').dialog('open');
		});
	}

	//[[SitePath]]index.php?a=Internal&amp;b=Policies&amp;QuoteID=[[Var:Name=QuoteID:HTML]]&amp;SendCert=1

	$('#SendDocsLink').click(function(){
		$('#DialogSendDocs').html('<form id="FormSend" action="' + SITEPATH + 'index.php?a=Internal&amp;b=Policies&amp;QuoteID=' + $('#QuoteID').val() + '&amp;SendCert=1" method="POST"><table><tr><td style="width:100px">Email:</td><td><input type="text" id="Email" name="Email" value="' + $('#Email').val() + '" /></td></tr></table></form>');
		$('#DialogSendDocs').dialog({
			bgiframe: true,	autoOpen: true,	modal: false, width: 400, resizable: false,
			buttons: {
				'Send': function(){
					$(this).dialog('close');
					$('#FormSend').submit();
				}
			}
		});
	});

	// create pdf from all unsent policy packs
	$('#MegaPDFLink').click(function(){
		$.ajax({
			url: SITEPATH + 'index.php?a=Ajax&b=MegaPDF',
			success: function(){
				$('#DialogMegaPDF').html('<embed src="' + SITEPATH + '/temp/unsent.pdf" href="' + SITEPATH + '/temp/unsent.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
				$('#DialogMegaPDF').dialog({
					bgiframe: true, autoOpen: true,	modal: false, width: 700, height: 500, resizable: true,
					buttons: {
						'Close': function(){
							$(this).dialog('close');
						}
					}
				});
			}
		});
	});
	$('#MegaPDFLink2').click(function(){
		$.ajax({
			url: SITEPATH + 'index.php?a=Ajax&b=MegaPDF2',
			success: function(){
				$('#DialogMegaPDF2').html('<embed src="' + SITEPATH + '/temp/unsent2.pdf" href="' + SITEPATH + '/temp/unsent2.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
				$('#DialogMegaPDF2').dialog({
					bgiframe: true, autoOpen: true,	modal: false, width: 700, height: 500, resizable: true,
					buttons: {
						'Close': function(){
							$(this).dialog('close');
						}
					}
				});
			}
		});
	});

	// quote end options
	$('#LinkReference').click(function(e){
		e.preventDefault();
		if ($('#NewReference').val())
		{
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=SaveNewReference', //&QuoteID=' + $('#QuoteID').val(),
				data: $('#FormSave').serialize(),
				success: function (html){
					$('#DialogReference').html('<p>Your new reference has been saved and emailed to you if you stated an email address. You can still use the original reference.</p>');
					$('#DialogReference').dialog({
						bgiframe: true,	autoOpen: true,	modal: true, width: 500, resizable: false,
						buttons: {
							'Close': function() {
								$(this).dialog('close');
							}
						}
					});
				}
			});
		}
	});
	$('#LinkSMS').click(function(e){
		e.preventDefault();
		if ($('#SMS').val())
		{
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=SendQuoteSMS', //&QuoteID=' + $('#QuoteID').val(),
				data: $('#FormSave').serialize(),
				success: function (html){
					$('#DialogSMS').html('<p>An SMS message has been sent.</p>');
					$('#DialogSMS').dialog({
						bgiframe: true, autoOpen: true,	modal: true, width: 500, resizable: false,
						buttons: {
							'Close': function() {
								$(this).dialog('close');
							}
						}
					});
				}
			});
		}
	});
	$('#LinkEmail').click(function(e){
		e.preventDefault();
		if ($('#Email').val())
		{
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=SendQuoteEmail', //&QuoteID=' + $('#QuoteID').val(),
				data: $('#FormSave').serialize(),
				success: function (html){
					$('#DialogEmail').html('<p>The quotation email has been sent.</p>');
					$('#DialogEmail').dialog({
						bgiframe: true,	autoOpen: true,	modal: true, width: 500, resizable: false,
						buttons: {
							'Close': function() {
								$(this).dialog('close');
							}
						}
					});
				}
			});
		}
	});
	
	

	for (i = 3363; i < 3367; i++)
	{
		for (j = 0; j < $('#NumTravellers').val(); j++)
		{
			$('#Option' + i + '_' + j).click(function(){
				if ($(this).attr('checked') == true)
				{
					parts = $(this).attr('rel').split('_');
					k = parts[1];
					$('#Option3363_' + k).attr('checked', '');
					$('#Option3364_' + k).attr('checked', '');
					$('#Option3365_' + k).attr('checked', '');
					$('#Option3366_' + k).attr('checked', '');
					$(this).attr('checked', 'checked');
				}
			});
		}
	}
	for (i = 3472; i < 3477; i++)
	{
		for (j = 0; j < $('#NumTravellers').val(); j++)
		{
			$('#Option' + i + '_' + j).click(function(){
				if ($(this).attr('checked') == true)
				{
					parts = $(this).attr('rel').split('_');
					k = parts[1];
					$('#Option3472_' + k).attr('checked', '');
					$('#Option3473_' + k).attr('checked', '');
					$('#Option3474_' + k).attr('checked', '');
					$('#Option3476_' + k).attr('checked', '');
					$(this).attr('checked', 'checked');
				}
			});
		}
	}
	for (i = 3478; i < 3482; i++)
	{
		for (j = 0; j < $('#NumTravellers').val(); j++)
		{
			$('#Option' + i + '_' + j).click(function(){
				if ($(this).attr('checked') == true)
				{
					parts = $(this).attr('rel').split('_');
					k = parts[1];
					$('#Option3478_' + k).attr('checked', '');
					$('#Option3479_' + k).attr('checked', '');
					$('#Option3480_' + k).attr('checked', '');
					$('#Option3481_' + k).attr('checked', '');
					$(this).attr('checked', 'checked');
				}
			});
		}
	}
	for (i = 7119; i < 7123; i++)
	{
		for (j = 0; j < $('#NumTravellers').val(); j++)
		{
			$('#Option' + i + '_' + j).click(function(){
				if ($(this).attr('checked') == true)
				{
					parts = $(this).attr('rel').split('_');
					k = parts[1];
					$('#Option7119_' + k).attr('checked', '');
					$('#Option7120_' + k).attr('checked', '');
					$('#Option7121_' + k).attr('checked', '');
					$('#Option7122_' + k).attr('checked', '');
					$(this).attr('checked', 'checked');
				}
			});
		}
	}
	for (i = 7102; i < 7106; i++)
	{
		for (j = 0; j < $('#NumTravellers').val(); j++)
		{
			$('#Option' + i + '_' + j).click(function(){
				if ($(this).attr('checked') == true)
				{
					parts = $(this).attr('rel').split('_');
					k = parts[1];
					$('#Option7102_' + k).attr('checked', '');
					$('#Option7103_' + k).attr('checked', '');
					$('#Option7104_' + k).attr('checked', '');
					$('#Option7105_' + k).attr('checked', '');
					$(this).attr('checked', 'checked');
				}
			});
		}
	}
	for (i = 7110; i < 7114; i++)
	{
		for (j = 0; j < $('#NumTravellers').val(); j++)
		{
			$('#Option' + i + '_' + j).click(function(){
				if ($(this).attr('checked') == true)
				{
					parts = $(this).attr('rel').split('_');
					k = parts[1];
					$('#Option7110_' + k).attr('checked', '');
					$('#Option7111_' + k).attr('checked', '');
					$('#Option7112_' + k).attr('checked', '');
					$('#Option7113_' + k).attr('checked', '');
					$(this).attr('checked', 'checked');
				}
			});
		}
	}
	for (i = 7127; i < 7131; i++)
	{
		for (j = 0; j < $('#NumTravellers').val(); j++)
		{
			$('#Option' + i + '_' + j).click(function(){
				if ($(this).attr('checked') == true)
				{
					parts = $(this).attr('rel').split('_');
					k = parts[1];
					$('#Option7127_' + k).attr('checked', '');
					$('#Option7128_' + k).attr('checked', '');
					$('#Option7129_' + k).attr('checked', '');
					$('#Option7130_' + k).attr('checked', '');
					$(this).attr('checked', 'checked');
				}
			});
		}
	}
	for (i = 7142; i < 7146; i++)
	{
		for (j = 0; j < $('#NumTravellers').val(); j++)
		{
			$('#Option' + i + '_' + j).click(function(){
				if ($(this).attr('checked') == true)
				{
					parts = $(this).attr('rel').split('_');
					k = parts[1];
					$('#Option7142_' + k).attr('checked', '');
					$('#Option7143_' + k).attr('checked', '');
					$('#Option7144_' + k).attr('checked', '');
					$('#Option7145_' + k).attr('checked', '');
					$(this).attr('checked', 'checked');
				}
			});
		}
	}
	for (i = 7151; i < 7155; i++)
	{
		for (j = 0; j < $('#NumTravellers').val(); j++)
		{
			$('#Option' + i + '_' + j).click(function(){
				if ($(this).attr('checked') == true)
				{
					parts = $(this).attr('rel').split('_');
					k = parts[1];
					$('#Option7151_' + k).attr('checked', '');
					$('#Option7152_' + k).attr('checked', '');
					$('#Option7153_' + k).attr('checked', '');
					$('#Option7154_' + k).attr('checked', '');
					$(this).attr('checked', 'checked');
				}
			});
		}
	}


	// traveller options dialogs
	if ($('#NumTravellers').length && $('#NumTravellers').val() > 1)
	{
		var optionArray = $('#OptionIDs').val().split(" ");
		for (i = 0; i < optionArray.length; i++)
		{
			if (optionArray[i] != ' ' && optionArray[i] != '')
			{
				for (j = 0; j < $('#NumTravellers').val(); j++)
				{
					$('#Option' + optionArray[i] + '_' + j).remove();
				}

				var optionText = '<p>Select which travellers require this option:</p><table>';
				for (j = 0; j < $('#NumTravellers').val(); j++)
				{
					optionText += '<tr><td><input type="checkbox" class="checkbox optionChange" name="Option' + optionArray[i] + '_' + j + '" rel="' + optionArray[i] + '_' + j + '" id="Option' + optionArray[i] + '_' + j + '" value="1"';
					if (optionArray[i] == 10000 || optionArray[i] == 7110 || optionArray[i] == 7102 || optionArray[i] == 7142 || optionArray[i] == 7127 || optionArray[i] == 7151 || optionArray[i] == 7119 || optionArray[i] == 3363 || optionArray[i] == 3472 || optionArray[i] == 3478)
					{
						optionText += ' checked="checked"';
					}
					if ($('#CatExtra').val() == 1 && (optionArray[i] == 3477 || optionArray[i] == 7131 || optionArray[i] == 7155 || optionArray[i] == 7114 || optionArray[i] == 3482 || optionArray[i] == 7106 || optionArray[i] == 7123 || optionArray[i] == 7146))
					{
						optionText += ' checked="checked"';
					}
					
					optionText += ' /></td><td style="width:150px">Traveller ' + (j + 1) + ' (age ' + $('#Age' + j).val() + ')</td><td>' + premiumFormatHTML($('#Gross' + optionArray[i]).val()) + '</td></tr>';
					//alert('added Option' + optionArray[i] + '_' + j);
					
				}
				optionText += '</table>';
				$('#DialogOptions' + optionArray[i]).html(optionText);
				$('#DialogOptions' + optionArray[i]).attr('rel', optionArray[i]);

				for (j = 0; j < $('#NumTravellers').val(); j++)
				{
					$('#Option' + optionArray[i] + '_' + j).click(function(){
						parts = $(this).attr('rel').split('_');
						k = parts[1];
						if ($(this).attr('checked') == true)
						{
							if (parts[0] == 3363 || parts[0] == 3364 || parts[0] == 3365 || parts[0] == 3366 ||
								parts[0] == 3472 || parts[0] == 3473 || parts[0] == 3474 || parts[0] == 3476 ||
								parts[0] == 3478 || parts[0] == 3479 || parts[0] == 3480 || parts[0] == 3481 ||
								parts[0] == 7102 || parts[0] == 7103 || parts[0] == 7104 || parts[0] == 7105 ||
								parts[0] == 7119 || parts[0] == 7120 || parts[0] == 7121 || parts[0] == 7122 ||
								parts[0] == 7142 || parts[0] == 7143 || parts[0] == 7144 || parts[0] == 7145 ||
								parts[0] == 7127 || parts[0] == 7128 || parts[0] == 7129 || parts[0] == 7130 ||
								parts[0] == 7151 || parts[0] == 7152 || parts[0] == 7153 || parts[0] == 7154||
								parts[0] == 7110 || parts[0] == 7111 || parts[0] == 7112 || parts[0] == 7113)
							{
								$('#Option3363_' + k).attr('checked', '');
								$('#Option3364_' + k).attr('checked', '');
								$('#Option3365_' + k).attr('checked', '');
								$('#Option3366_' + k).attr('checked', '');
								$('#HiddenOption3363_' + k).val(0);
								$('#HiddenOption3364_' + k).val(0);
								$('#HiddenOption3365_' + k).val(0);
								$('#HiddenOption3366_' + k).val(0);
								$('#Option3472_' + k).attr('checked', '');
								$('#Option3473_' + k).attr('checked', '');
								$('#Option3474_' + k).attr('checked', '');
								$('#Option3476_' + k).attr('checked', '');
								$('#HiddenOption3472_' + k).val(0);
								$('#HiddenOption3473_' + k).val(0);
								$('#HiddenOption3474_' + k).val(0);
								$('#HiddenOption3476_' + k).val(0);
								$('#Option3478_' + k).attr('checked', '');
								$('#Option3479_' + k).attr('checked', '');
								$('#Option3480_' + k).attr('checked', '');
								$('#Option3481_' + k).attr('checked', '');
								$('#HiddenOption3478_' + k).val(0);
								$('#HiddenOption3479_' + k).val(0);
								$('#HiddenOption3480_' + k).val(0);
								$('#HiddenOption3481_' + k).val(0);
								$('#Option7102_' + k).attr('checked', '');
								$('#Option7103_' + k).attr('checked', '');
								$('#Option7104_' + k).attr('checked', '');
								$('#Option7105_' + k).attr('checked', '');
								$('#HiddenOption7102_' + k).val(0);
								$('#HiddenOption7103_' + k).val(0);
								$('#HiddenOption7104_' + k).val(0);
								$('#HiddenOption7105' + k).val(0);
								$('#Option7110_' + k).attr('checked', '');
								$('#Option7111_' + k).attr('checked', '');
								$('#Option7112_' + k).attr('checked', '');
								$('#Option7113_' + k).attr('checked', '');
								$('#HiddenOption7110_' + k).val(0);
								$('#HiddenOption7111_' + k).val(0);
								$('#HiddenOption7112_' + k).val(0);
								$('#HiddenOption7113_' + k).val(0);
								$('#Option7119_' + k).attr('checked', '');
								$('#Option7120_' + k).attr('checked', '');
								$('#Option7121_' + k).attr('checked', '');
								$('#Option7122_' + k).attr('checked', '');
								$('#HiddenOption7119_' + k).val(0);
								$('#HiddenOption7120_' + k).val(0);
								$('#HiddenOption7121_' + k).val(0);
								$('#HiddenOption7122_' + k).val(0);
								$('#Option7127_' + k).attr('checked', '');
								$('#Option7128_' + k).attr('checked', '');
								$('#Option7129_' + k).attr('checked', '');
								$('#Option7130_' + k).attr('checked', '');
								$('#HiddenOption7127_' + k).val(0);
								$('#HiddenOption7128_' + k).val(0);
								$('#HiddenOption7129_' + k).val(0);
								$('#HiddenOption7130_' + k).val(0);
								$('#Option7151_' + k).attr('checked', '');
								$('#Option7152_' + k).attr('checked', '');
								$('#Option7153_' + k).attr('checked', '');
								$('#Option7154_' + k).attr('checked', '');
								$('#HiddenOption7151_' + k).val(0);
								$('#HiddenOption7152_' + k).val(0);
								$('#HiddenOption7153_' + k).val(0);
								$('#HiddenOption7154_' + k).val(0);
								$('#Option7142_' + k).attr('checked', '');
								$('#Option7143_' + k).attr('checked', '');
								$('#Option7144_' + k).attr('checked', '');
								$('#Option7145_' + k).attr('checked', '');
								$('#HiddenOption7142_' + k).val(0);
								$('#HiddenOption7143_' + k).val(0);
								$('#HiddenOption7144_' + k).val(0);
								$('#HiddenOption7145_' + k).val(0);
								$('#HiddenOption' + parts[0] + '_' + parts[1]).val(1);
								$(this).attr('checked', 'checked');
								// untick other categories if all travellers gone
								for (m = 3363; m < 3367; m++)
								{
									found = false;
									for (k = 0; k < $('#NumTravellers').val(); k++)
									{
										if ($('#Option' + m + '_' + k).attr('checked') == true)
										{
											found = true;
										}
									}
									if (!found)
									{
										$('#LinkOption' + m).attr('checked', '');
									}
								}
								for (m = 3363; m < 3367; m++)
								{
									found = false;
									for (k = 0; k < $('#NumTravellers').val(); k++)
									{
										if ($('#Option' + m + '_' + k).attr('checked') == true)
										{
											found = true;
										}
									}
									if (!found)
									{
										$('#LinkOption' + m).attr('checked', '');
									}
								}
								for (m = 3472; m < 3477; m++)
								{
									found = false;
									for (k = 0; k < $('#NumTravellers').val(); k++)
									{
										if ($('#Option' + m + '_' + k).attr('checked') == true)
										{
											found = true;
										}
									}
									if (!found)
									{
										$('#LinkOption' + m).attr('checked', '');
									}
								}
								for (m = 3478; m < 3482; m++)
								{
									found = false;
									for (k = 0; k < $('#NumTravellers').val(); k++)
									{
										if ($('#Option' + m + '_' + k).attr('checked') == true)
										{
											found = true;
										}
									}
									if (!found)
									{
										$('#LinkOption' + m).attr('checked', '');
									}
								}
								for (m = 7102; m < 7106; m++)
								{
									found = false;
									for (k = 0; k < $('#NumTravellers').val(); k++)
									{
										if ($('#Option' + m + '_' + k).attr('checked') == true)
										{
											found = true;
										}
									}
									if (!found)
									{
										$('#LinkOption' + m).attr('checked', '');
									}
								}
								for (m = 7110; m < 7114; m++)
								{
									found = false;
									for (k = 0; k < $('#NumTravellers').val(); k++)
									{
										if ($('#Option' + m + '_' + k).attr('checked') == true)
										{
											found = true;
										}
									}
									if (!found)
									{
										$('#LinkOption' + m).attr('checked', '');
									}
								}
								for (m = 7127; m < 7131; m++)
								{
									found = false;
									for (k = 0; k < $('#NumTravellers').val(); k++)
									{
										if ($('#Option' + m + '_' + k).attr('checked') == true)
										{
											found = true;
										}
									}
									if (!found)
									{
										$('#LinkOption' + m).attr('checked', '');
									}
								}
								for (m = 7151; m < 7155; m++)
								{
									found = false;
									for (k = 0; k < $('#NumTravellers').val(); k++)
									{
										if ($('#Option' + m + '_' + k).attr('checked') == true)
										{
											found = true;
										}
									}
									if (!found)
									{
										$('#LinkOption' + m).attr('checked', '');
									}
								}
								for (m = 7119; m < 7123; m++)
								{
									found = false;
									for (k = 0; k < $('#NumTravellers').val(); k++)
									{
										if ($('#Option' + m + '_' + k).attr('checked') == true)
										{
											found = true;
										}
									}
									if (!found)
									{
										$('#LinkOption' + m).attr('checked', '');
									}
								}
								for (m = 7142; m < 7146; m++)
								{
									found = false;
									for (k = 0; k < $('#NumTravellers').val(); k++)
									{
										if ($('#Option' + m + '_' + k).attr('checked') == true)
										{
											found = true;
										}
									}
									if (!found)
									{
										$('#LinkOption' + m).attr('checked', '');
									}
								}
							}
							$(this).attr('checked', 'checked');
						}
					});
				}

				$('#DialogOptions' + optionArray[i]).dialog({
					bgiframe: true,	autoOpen: false, modal: false, width: 500, resizable: false,
					buttons: {
						'OK': function(){
							// reset parent checkbox if none selected
							var num = 0;
							parts = $(this).attr('rel').split('_');
							var option = parts[0];
							for (j = 0; j < $('#NumTravellers').val(); j++)
							{
								if ($('#Option' + option + '_' + j).attr('checked') == true)
								{
									$('#HiddenOption' + option + '_' + j).val(1);
									num++;
								}
								else
								{
									$('#HiddenOption' + option + '_' + j).val(0);
								}
							}
							if (num == 0)
							{
								$('#LinkOption' + option).attr('checked', '');
							}

							$(this).dialog('close');
						}
					}
				});

				if (optionArray[i] == 10000 || optionArray[i] == 7110 || optionArray[i] == 7127 || optionArray[i] == 7151 || optionArray[i] == 7142 || optionArray[i] == 7102 || optionArray[i] == 7119 || optionArray[i] == 3363 || optionArray[i] == 3472 || optionArray[i] == 3478)
				{
					hiddenVars += ' checked="checked"';
				}
				if ($('#CatExtra').val() == 1 && (optionArray[i] == 3477 || optionArray[i] == 7131 || optionArray[i] == 7155 || optionArray[i] == 7114 || optionArray[i] == 3482 || optionArray[i] == 7106 || optionArray[i] == 7123 || optionArray[i] == 7146))
				{
					hiddenVars += ' checked="checked"';
				}
				hiddenVars += ' />';
				for (j = 0; j < $('#NumTravellers').val(); j++)
				{
					hiddenVars += '<input type="hidden" id="HiddenOption' + optionArray[i] + '_' + j + '" name="HiddenOption' + optionArray[i] + '_' + j + '"';
					if (optionArray[i] == 7102 || optionArray[i] == 7127 || optionArray[i] == 7151 || optionArray[i] == 7110 || optionArray[i] == 7142 || optionArray[i] == 7119 || optionArray[i] == 3363 || optionArray[i] == 3472 || optionArray[i] == 3478)
						hiddenVars += ' value="1"';
					hiddenVars += ' />';
				}
				$('#OptionCell' + optionArray[i]).html(hiddenVars);
				$('#LinkOption' + optionArray[i]).click(function(){
					parts = $(this).attr('rel').split('_');
					
					//not open dialog for policy options
					if (parts[0] != 10000 && parts[0] != 7256 && parts[0] != 7258 && parts[0] != 7148 && parts[0] != 7149 && parts[0] != 7107 && parts[0] != 7108 && parts[0] != 7124 && parts[0] != 7125)
					{
						$(this).attr('checked', 'checked');
						$('#DialogOptions' + parts[0]).dialog('open');
					}
				});

			}
		}

		//if traveller selected addional sports
		if ($('#CatExtra').val() != 1)
		{
			for (i = 0; i < optionArray.length; i++)
			{
				if (optionArray[i] != ' ' && optionArray[i] != '')
				{
					for (j = 0; j < $('#NumTravellers').val(); j++)
					{
						$('#Option' + optionArray[i] + '_' + j).each(function(){
							parts = $(this).attr('rel').split('_');
							k = parts[1];
							if (parts[0] == 3363 || parts[0] == 3364 || parts[0] == 3365 || parts[0] == 3366 ||
								parts[0] == 3472 || parts[0] == 3473 || parts[0] == 3474 || parts[0] == 3476 ||
								parts[0] == 3478 || parts[0] == 3479 || parts[0] == 3480 || parts[0] == 3481 ||
								parts[0] == 7102 || parts[0] == 7103 || parts[0] == 7104 || parts[0] == 7105 ||
								parts[0] == 7119 || parts[0] == 7120 || parts[0] == 7121 || parts[0] == 7122 ||
								parts[0] == 7142 || parts[0] == 7143 || parts[0] == 7144 || parts[0] == 7145 ||
								parts[0] == 7127 || parts[0] == 7128 || parts[0] == 7129 || parts[0] == 7130 ||
								parts[0] == 7151 || parts[0] == 7152 || parts[0] == 7153 || parts[0] == 7154||
								parts[0] == 7110 || parts[0] == 7111 || parts[0] == 7112 || parts[0] == 7113)
							{
								$('#Option3363_' + k).attr('checked', '');
								$('#Option3364_' + k).attr('checked', '');
								$('#Option3365_' + k).attr('checked', '');
								$('#Option3366_' + k).attr('checked', '');
								$('#HiddenOption3363_' + k).val(0);
								$('#HiddenOption3364_' + k).val(0);
								$('#HiddenOption3365_' + k).val(0);
								$('#HiddenOption3366_' + k).val(0);
								$('#Option3472_' + k).attr('checked', '');
								$('#Option3473_' + k).attr('checked', '');
								$('#Option3474_' + k).attr('checked', '');
								$('#Option3476_' + k).attr('checked', '');
								$('#HiddenOption3472_' + k).val(0);
								$('#HiddenOption3473_' + k).val(0);
								$('#HiddenOption3474_' + k).val(0);
								$('#HiddenOption3476_' + k).val(0);
								$('#Option3478_' + k).attr('checked', '');
								$('#Option3479_' + k).attr('checked', '');
								$('#Option3480_' + k).attr('checked', '');
								$('#Option3481_' + k).attr('checked', '');
								$('#HiddenOption3478_' + k).val(0);
								$('#HiddenOption3479_' + k).val(0);
								$('#HiddenOption3480_' + k).val(0);
								$('#HiddenOption3481_' + k).val(0);
								$('#Option7102_' + k).attr('checked', '');
								$('#Option7103_' + k).attr('checked', '');
								$('#Option7104_' + k).attr('checked', '');
								$('#Option7105_' + k).attr('checked', '');
								$('#HiddenOption7102_' + k).val(0);
								$('#HiddenOption7103_' + k).val(0);
								$('#HiddenOption7104_' + k).val(0);
								$('#HiddenOption7105' + k).val(0);
								$('#Option7110_' + k).attr('checked', '');
								$('#Option7111_' + k).attr('checked', '');
								$('#Option7112_' + k).attr('checked', '');
								$('#Option7113_' + k).attr('checked', '');
								$('#HiddenOption7110_' + k).val(0);
								$('#HiddenOption7111_' + k).val(0);
								$('#HiddenOption7112_' + k).val(0);
								$('#HiddenOption7113_' + k).val(0);
								$('#Option7119_' + k).attr('checked', '');
								$('#Option7120_' + k).attr('checked', '');
								$('#Option7121_' + k).attr('checked', '');
								$('#Option7122_' + k).attr('checked', '');
								$('#HiddenOption7119_' + k).val(0);
								$('#HiddenOption7120_' + k).val(0);
								$('#HiddenOption7121_' + k).val(0);
								$('#HiddenOption7122_' + k).val(0);
								$('#Option7127_' + k).attr('checked', '');
								$('#Option7128_' + k).attr('checked', '');
								$('#Option7129_' + k).attr('checked', '');
								$('#Option7130_' + k).attr('checked', '');
								$('#HiddenOption7127_' + k).val(0);
								$('#HiddenOption7128_' + k).val(0);
								$('#HiddenOption7129_' + k).val(0);
								$('#HiddenOption7130_' + k).val(0);
								$('#Option7151_' + k).attr('checked', '');
								$('#Option7152_' + k).attr('checked', '');
								$('#Option7153_' + k).attr('checked', '');
								$('#Option7154_' + k).attr('checked', '');
								$('#HiddenOption7151_' + k).val(0);
								$('#HiddenOption7152_' + k).val(0);
								$('#HiddenOption7153_' + k).val(0);
								$('#HiddenOption7154_' + k).val(0);
								$('#Option7142_' + k).attr('checked', '');
								$('#Option7143_' + k).attr('checked', '');
								$('#Option7144_' + k).attr('checked', '');
								$('#Option7145_' + k).attr('checked', '');
								$('#HiddenOption7142_' + k).val(0);
								$('#HiddenOption7143_' + k).val(0);
								$('#HiddenOption7144_' + k).val(0);
								$('#HiddenOption7145_' + k).val(0);

								if ($('#CatExtra').val() == 2)
								{
									$('#Option3364_' + k).attr('checked', 'checked');
									$('#HiddenOption3364_' + k).val(1);
									$('#Option3473_' + k).attr('checked', 'checked');
									$('#HiddenOption3473_' + k).val(1);
									$('#Option3479_' + k).attr('checked', 'checked');
									$('#HiddenOption3479_' + k).val(1);
									$('#Option7103_' + k).attr('checked', 'checked');
									$('#HiddenOption7103_' + k).val(1);
									$('#Option7120_' + k).attr('checked', 'checked');
									$('#HiddenOption7120_' + k).val(1);
									$('#Option7143_' + k).attr('checked', 'checked');
									$('#HiddenOption7143_' + k).val(1);
									$('#Option7152_' + k).attr('checked', 'checked');
									$('#HiddenOption7152_' + k).val(1);
									$('#Option7128_' + k).attr('checked', 'checked');
									$('#HiddenOption7128_' + k).val(1);
									$('#Option7111_' + k).attr('checked', 'checked');
									$('#HiddenOption7111_' + k).val(1);
								}
								else if ($('#CatExtra').val() == 3)
								{
									$('#Option3365_' + k).attr('checked', 'checked');
									$('#HiddenOption3365_' + k).val(1);
									$('#Option3474_' + k).attr('checked', 'checked');
									$('#HiddenOption3474_' + k).val(1);
									$('#Option3480_' + k).attr('checked', 'checked');
									$('#HiddenOption3480_' + k).val(1);
									$('#Option7104_' + k).attr('checked', 'checked');
									$('#HiddenOption7104_' + k).val(1);
									$('#Option7121_' + k).attr('checked', 'checked');
									$('#HiddenOption7121_' + k).val(1);
									$('#Option7144_' + k).attr('checked', 'checked');
									$('#HiddenOption7144_' + k).val(1);
									$('#Option7153_' + k).attr('checked', 'checked');
									$('#HiddenOption7153_' + k).val(1);
									$('#Option7129_' + k).attr('checked', 'checked');
									$('#HiddenOption7129_' + k).val(1);
									$('#Option7112_' + k).attr('checked', 'checked');
									$('#HiddenOption7112_' + k).val(1);
								}
								else if ($('#CatExtra').val() == 4)
								{
									$('#Option3366_' + k).attr('checked', 'checked');
									$('#HiddenOption3366_' + k).val(1);
									$('#Option3476_' + k).attr('checked', 'checked');
									$('#HiddenOption3476_' + k).val(1);
									$('#Option3481_' + k).attr('checked', 'checked');
									$('#HiddenOption3481_' + k).val(1);
									$('#Option7105_' + k).attr('checked', 'checked');
									$('#HiddenOption7105_' + k).val(1);
									$('#Option7122_' + k).attr('checked', 'checked');
									$('#HiddenOption7122_' + k).val(1);
									$('#Option7145_' + k).attr('checked', 'checked');
									$('#HiddenOption7145_' + k).val(1);
									$('#Option7154_' + k).attr('checked', 'checked');
									$('#HiddenOption7154_' + k).val(1);
									$('#Option7130_' + k).attr('checked', 'checked');
									$('#HiddenOption7130_' + k).val(1);
									$('#Option7113_' + k).attr('checked', 'checked');
									$('#HiddenOption7113_' + k).val(1);
								}

							}


						});

					}

					$('#LinkOption' + optionArray[i]).attr('checked', '');

				}
			}

			if ($('#CatExtra').val() == 2)
			{
				$('#LinkOption3364').attr('checked', 'checked');
				$('#LinkOption3473').attr('checked', 'checked');
				$('#LinkOption3479').attr('checked', 'checked');
				$('#LinkOption7103').attr('checked', 'checked');
				$('#LinkOption7120').attr('checked', 'checked');
				$('#LinkOption7152').attr('checked', 'checked');
				$('#LinkOption7128').attr('checked', 'checked');
				$('#LinkOption7111').attr('checked', 'checked');
				
			}
			else if ($('#CatExtra').val() == 3)
			{
				$('#LinkOption3365').attr('checked', 'checked');
				$('#LinkOption3474').attr('checked', 'checked');
				$('#LinkOption3480').attr('checked', 'checked');
				$('#LinkOption7104').attr('checked', 'checked');
				$('#LinkOption7121').attr('checked', 'checked');
				$('#LinkOption7144').attr('checked', 'checked');
				$('#LinkOption7153').attr('checked', 'checked');
				$('#LinkOption7129').attr('checked', 'checked');
				$('#LinkOption7112').attr('checked', 'checked');
			}
			else if ($('#CatExtra').val() == 4)
			{
				$('#LinkOption3366').attr('checked', 'checked');
				$('#LinkOption3476').attr('checked', 'checked');
				$('#LinkOption3481').attr('checked', 'checked');
				$('#LinkOption7105').attr('checked', 'checked');
				$('#LinkOption7122').attr('checked', 'checked');
				$('#LinkOption7145').attr('checked', 'checked');
				$('#LinkOption7154').attr('checked', 'checked');
				$('#LinkOption7130').attr('checked', 'checked');
				$('#LinkOption7113').attr('checked', 'checked');

				
			}

		}
	}

	//condition confirm exlusion dialog

	if ($('#TravellerIDs').length)
	{
		var travelArray = $('#TravellerIDs').val().split(",");
		for (i = 0; i < travelArray.length; i++)
		{
			if (travelArray[i] != '' && travelArray[i] != ',')
			{
				$('#Traveller'+ travelArray[i]).live("click",function() {

					var index = $(this).attr('rel');

					if ($('#TravPrem'+index).length && $('#TravPrem'+index).val() > 0.00 && $(this).attr("checked") == false)
					{
						$('#DialogMedical').html('<p>Are you sure you want to remove cover for this medical condition?</p><p>By unticking this box you are choosing to remove the insurance cover for this medical condition. If you do go ahead and remove the cover, any medical treatment resulting from or associated with the condition will be excluded from your policy.</p>');
						$('#DialogMedical').dialog('open');

						$('#DialogMedical').dialog({
							bgiframe: true,	autoOpen: true,	modal: false, width: 400, height: 200, resizable: true,
							buttons: {
								'No': function(){
									$(this).dialog('close');
									$('#Traveller'+index).attr("checked", 'checked');
								},
								'Yes': function(){
									$(this).dialog('close');
								}
								
							}
						});
					}
				});
			}
		}
	}

	//home internal page

	$('#InternalGoogle').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 400, height: 300, resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});

	$('.openGoogle').click(function(e){
		e.preventDefault();
		$('#InternalGoogle').dialog('open');
	});

	$('#InternalQuotations').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 400, height: 300, resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});

	$('.openQuotes').click(function(e){
		e.preventDefault();
		$('#InternalQuotations').dialog('open');
	});

	$('#InternalPolicies').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 400, height: 300, resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});

	$('.openPolicies').click(function(e){
		e.preventDefault();
		$('#InternalPolicies').dialog('open');
	});


	// put the claims forms into dialog

	$('#DialogPersonalBelongingsDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500, resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkPersonalBelongingsDoc').click(function(e){
		e.preventDefault();
		$('#DialogPersonalBelongingsDoc').html('<embed src="' + SITEPATH + '/pdf/personalbelongings' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/pdf/personalbelongings' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogPersonalBelongingsDoc').dialog('open');
	});

	$('#DialogCancellationDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkCancellationDoc').click(function(e){
		e.preventDefault();
		$('#DialogCancellationDoc').html('<embed src="' + SITEPATH + '/pdf/cancellation' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/pdf/cancellation' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogCancellationDoc').dialog('open');
	});

	$('#DialogCurtailmentDoc').dialog({
		bgiframe: true, autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkCurtailmentDoc').click(function(e){
		e.preventDefault();
		$('#DialogCurtailmentDoc').html('<embed src="' + SITEPATH + '/pdf/curtailment' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/pdf/curtailment' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogCurtailmentDoc').dialog('open');
	});

	$('#DialogLuggageDelayDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkLuggageDelayDoc').click(function(e){
		e.preventDefault();
		$('#DialogLuggageDelayDoc').html('<embed src="' + SITEPATH + '/pdf/luggagedelay' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/pdf/luggagedelay' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogLuggageDelayDoc').dialog('open');
	});

	$('#DialogTravelDelayDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkTravelDelayDoc').click(function(e){
		e.preventDefault();
		$('#DialogTravelDelayDoc').html('<embed src="' + SITEPATH + '/pdf/traveldelay' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/pdf/traveldelay' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogTravelDelayDoc').dialog('open');
	});

	$('#DialogMedicalExpensesDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500, resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkMedicalExpensesDoc').click(function(e){
		e.preventDefault();
		$('#DialogMedicalExpensesDoc').html('<embed src="' + SITEPATH + '/pdf/medicalexpenses' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/pdf/medicalexpenses' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogMedicalExpensesDoc').dialog('open');
	});





	// put policy wording and key facts into dialogs
	$('#DialogWordingDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500, resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkWordingDoc').click(function(e){
		e.preventDefault();
		var affiliate = '';
		if ($('#Affiliate').length && !$('#Skip').val())
			affiliate = 'affiliates/' + $('#Affiliate').val() + '/';
		$('#DialogWordingDoc').html('<embed src="' + SITEPATH + '/pdf/' + affiliate + 'wording' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/pdf/' + affiliate + 'wording' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogWordingDoc').dialog('open');
	});

	$('#DialogKeyFactsDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkKeyFactsDoc').click(function(e){
		e.preventDefault();
		$('#DialogKeyFactsDoc').html('<embed src="' + SITEPATH + '/pdf/keyfacts' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/pdf/keyfacts' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogKeyFactsDoc').dialog('open');
	});

	$('#DialogCertificateDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkCertificateDoc').click(function(e){
		e.preventDefault();
		$('#DialogCertificateDoc').html('<embed src="' + SITEPATH + '/temp/cert' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/cert' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogCertificateDoc').dialog('open');
	});
	$('#DialogCertificateLetterheadDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500, resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkCertificateLetterheadDoc').click(function(e){
		e.preventDefault();
		$('#DialogCertificateLetterheadDoc').html('<embed src="' + SITEPATH + '/temp/certletterhead' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/certletterhead' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogCertificateLetterheadDoc').dialog('open');
	});

	$('#DialogLetterDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkLetterDoc').click(function(e){
		e.preventDefault();
		$('#DialogLetterDoc').html('<embed src="' + SITEPATH + '/temp/purchaseletter' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/purchaseletter' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogLetterDoc').dialog('open');
	});
	$('#DialogQuotationDoc').dialog({
		bgiframe: true, autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkQuotationDoc').click(function(e){
		e.preventDefault();
		$('#DialogQuotationDoc').html('<embed src="' + SITEPATH + '/temp/quote' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/quote' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogQuotationDoc').dialog('open');
	});
	$('#DialogQuotationLetterheadDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkQuotationLetterheadDoc').click(function(e){
		e.preventDefault();
		$('#DialogQuotationLetterheadDoc').html('<embed src="' + SITEPATH + '/temp/quoteletterhead' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/quoteletterhead' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogQuotationLetterheadDoc').dialog('open');
	});


	$('#DialogQuotePackDoc').dialog({
		bgiframe: true, autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkQuotePackDoc').click(function(e){
		e.preventDefault();
		$('#DialogQuotePackDoc').html('<embed src="' + SITEPATH + '/temp/quotepack' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/quotepack' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogQuotePackDoc').dialog('open');
	});
	$('#DialogQuotePackLetterheadDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkQuotePackLetterheadDoc').click(function(e){
		e.preventDefault();
		$('#DialogQuotePackLetterheadDoc').html('<embed src="' + SITEPATH + '/temp/quotepackletterhead' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/quotepackletterhead' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogQuotePackLetterheadDoc').dialog('open');
	});
	$('#DialogPolicyPackDoc').dialog({
		bgiframe: true, autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkPolicyPackDoc').click(function(e){
		e.preventDefault();
		$('#DialogPolicyPackDoc').html('<embed src="' + SITEPATH + '/temp/policypack' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/policypack' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogPolicyPackDoc').dialog('open');
	});
	$('#DialogPolicyPackLetterheadDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkPolicyPackLetterheadDoc').click(function(e){
		e.preventDefault();
		$('#DialogPolicyPackLetterheadDoc').html('<embed src="' + SITEPATH + '/temp/policypackletterhead' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/policypackletterhead' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogPolicyPackLetterheadDoc').dialog('open');
	});
	
	$('#DialogDeclarationDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkDeclarationDoc').click(function(e){
		e.preventDefault();
		$('#DialogDeclarationDoc').html('<embed src="' + SITEPATH + '/temp/declaration' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/declaration' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogDeclarationDoc').dialog('open');
	});
	$('#DialogDeclarationLetterheadDoc').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('.linkDeclarationLetterheadDoc').click(function(e){
		e.preventDefault();
		$('#DialogDeclarationLetterheadDoc').html('<embed src="' + SITEPATH + '/temp/declarationletterhead' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + '/temp/declarationletterhead' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogDeclarationLetterheadDoc').dialog('open');
	});

	$('.moreDetails').click(function(e){
		var divid = $(this).attr('rel');
		e.preventDefault();
		$('#InternalQuote'+divid).dialog('open');
	});
	$('.InternalDialogue').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 500, resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});




	// set links with a rel value of external to target a new frame
	$('a[rel="external"][href]').each(function(){
		$(this).attr('target', '_blank');
	});

	// hide faq answers until needed
	//$('.faqGroup').hide(); 
	$('.faqAnswer').hide();
	$('.faqSection').addClass('clickableHelp'); $('.faqQuestion').addClass('clickableHelp');
	$('.faqSection').click(function(){
		//$('.faqGroup').hide();
		$(this).next('.faqGroup').toggle();
	});
	$('.faqQuestion').click(function(){
		//$('.faqAnswer').hide();
		$(this).next('.faqAnswer').toggle();
	});


	//hide submit button when click
	$('input[type="submit"], input[type="image"]').click(function(){
		
		
		if ($('#PaymentPage').val() == 1)
		{
			if (!$('#JustTravelPayment').length || $('#JustTravelPayment').attr('checked') == false)
			{
				$(this).hide();
				$('<div class="ajaxloader" />').appendTo('#PaymentSubmit');
			
				$('#PaymentForm').submit();
			}
			else
			{
				$('#DialogPaymentCheck').html('Please make sure payment details are taken manually.');
				$('#DialogPaymentCheck').dialog({
					bgiframe: true, autoOpen: true, modal: true, width: 400, resizable: false,
					buttons: {
						'OK':function(){
							$('#PaymentForm').submit();
						},
						'Cancel':function(){
							$(this).dialog('close');
						}
					}
				});
				return false;
			}
		}
		else
		{
			if ($('.Throbber').length)
			{
				if (!$(this).hasClass('noThrob'))
				{
					$(this).fadeOut(200).hide();
					//$('<div class="ajaxloader" />').appendTo('#PaymentSubmit');
					if ($('.Throbber').length)
					{
						if ($('.Throbber').html() != '<div class="ajaxloader2" />')
						{
							$('<div class="ajaxloader2" />').appendTo('.Throbber');
						}
						
					}
				}
			}
			

		}
		

	});

	// postcode search
	$('#PostcodeLookup').each(function(){
		if ($('#Affiliate').val() > 0)
			$(this).append('<img id="PostcodeLookupTrigger" src="' + SITEPATH + 'images/affiliates/' + $('#Affiliate').val() + '/button_findaddress.png" alt="Find Address" style="float:right;position:relative;right:52px;" />');
		else
			$(this).append('<img id="PostcodeLookupTrigger" src="' + SITEPATH + 'images/button_findaddress.png" alt="Find Address" style="float:right;position:relative;right:52px;" />');
		$('.mainWrapper').after('<div id="DialogPostcodeLookup" title="Postcode Search"></div>');
		$('#RowAddress').hide();

		$('#DialogPostcodeLookup').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 400, height: 600, resizable: false,
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});
		$('#PostcodeLookupTrigger').click(function(){
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=PostcodeSearch&c=CheckPostcode',
				data: $('#FormCoverDetails').serialize(),
				success: function (html){
					$('#DialogPostcodeLookup').append(html);
					$('#DialogPostcodeLookup').dialog('open');

					$('.pickAddress').click(function(){
						$.ajax({
							url: SITEPATH + 'index.php?a=Ajax&b=PostcodeSearch&c=CheckAddress&id=' + $(this).attr('rel'),
							dataType: 'json',
							success: function (data){
								$.each(data, function(i,item){
									$('#' + i).val(item);
								});
								$('#RowAddress').show();
								$('#DialogPostcodeLookup').dialog('close');
							}
						});
					});
					$('#RowAddress').show();
				}
			});
		});
	});

	// save or buy button
	$('#SubmitBuyNow, #SubmitBuyNowScheme').each(function(){
		if ($(this).attr('id') == 'SubmitBuyNowScheme')
			$(this).after('<br /><input type="image" id="SubmitSave" src="' + SITEPATH + 'images/button_savequotation.png" style="height:39px; width:169px; background-color:transparent" class="submit" alt="Save Quotation" />');
		else
			$(this).after('<br /><br /><input type="image" id="SubmitSave" src="' + SITEPATH + 'images/button_savequotation.png" style="height:39px; width:169px; background-color:transparent" class="submit" alt="Save Quotation" />');

		$('.mainWrapper').after('<div id="DialogEmail" title="Email Address"><p id="text">To continue with your purchase, please enter your email address. This will enable us to save your quotation, so that if you abandon the process for any reason, you can come back later and retrieve all the details.</p><table><tr><td class="caption"><label for="Email">Email address</label></td><td><input type="text" id="Email" name="Email" maxlength="80" class="short" /></td></tr></table><br /><p style="margin-top:10px;">We will use your email address in accordance with our <a href="'+SITEPATH+'/privacy#marketing" title="privacy policy" target="_blank">privacy policy</a></p></div>');
		
		$('#DialogEmail').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 500, resizable: false,
			buttons: {
				'I do not have an email address': function(){
					if ($('#Email').val() == '' || $('#Email').val() != '')
					{
						$('#DialogEmail').dialog('close');
						$('#DialogContactName').html('<p>In order to retrieve a quote in the future, please enter your contact name and use this when prompted:</p><table><tr><td class="caption"><label for="ContactName">Contact Name</label></td><td><input type="text" id="ContactName" name="ContactName" maxlength="80" class="short" /></td></tr></table>');
						$('#DialogContactName').dialog('open');
						$('#HiddenEmail').val('');
						//$('#FormSchemes').submit();
					}
				},
				'Continue': function(){

					var emailval = $('#Email').val();
					var emailindex = emailval.indexOf("@");
					var emailindex2 = emailval.indexOf(".");
					var emailindex3 = emailval.indexOf(".couk");

					if (emailval != '' && emailindex != -1 && emailindex2 != -1 && emailindex3 == -1)
					{
						$('#HiddenEmail').val($('#Email').val());
						$('#HiddenContactName').val('');
						$('#FormSchemes').submit();
					}
					else 
					{
						$('#DialogEmail #text').html('<span style="color:red;font-weight:bold;">You have entered an invalid email address:</span>');
					}
				}
				
			}
			
		});

		$('#DialogContactName').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 400, resizable: false,
			buttons: {
				'Continue': function(){
					if ($('#ContactName').val() != '')
					{
						$('#HiddenContactName').val($('#ContactName').val());
						$('#FormSchemes').submit();
					}
				}
			}
		});
	});

	$('#SubmitBuyNowScheme').click(function(event){
		$('#QuitNow').val(0);
	});
	$('#SubmitBuyNow').click(function(event){
		$('#QuitNow').val(0);
		$('#DialogEmail').html('<p id="text">To continue with your purchase, please enter your email address. This will enable us to save your quotation, so that if you abandon the process for any reason, you can come back later and retrieve all the details.</p><table><tr><td class="caption"><label for="Email">Email address</label></td><td><input type="text" id="Email" name="Email" maxlength="80" class="short" /></td></tr></table><p style="margin-top:10px;">We will use your email address in accordance with our<br /><a href="'+SITEPATH+'privacy#marketing" title="privacy policy" target="_blank">privacy policy</a></p>');
		$('#DialogEmail').dialog('open');
		event.preventDefault();
	});
	$('#SubmitBuyNow2').click(function(event){
		$('#QuitNow').val(0);
		$('#DialogEmail').html('<p id="text">To continue with your purchase, please enter your email address. This will enable us to save your quotation, so that if you abandon the process for any reason, you can come back later and retrieve all the details.</p><table><tr><td class="caption"><label for="Email">Email address</label></td><td><input type="text" id="Email" name="Email" maxlength="80" class="short" /></td></tr></table><p style="margin-top:10px;">We will use your email address in accordance with our<br /><a href="'+SITEPATH+'privacy#marketing" title="privacy policy" target="_blank">privacy policy</a></p>');
		$('#DialogEmail').dialog('open');
		event.preventDefault();
	});
	$('#SubmitSave, #SubmitSave6').click(function(event){
		$('#QuitNow').val(1);
		$('#DialogEmail').html('<p id="text">Please enter your email address to save your quotation.</p><table><tr><td class="caption"><label for="Email">Email address</label></td><td><input type="text" id="Email" name="Email" maxlength="80" class="short" /></td></tr></table><p style="margin-top:10px;">We will use your email address in accordance with our<br /><a href="'+SITEPATH+'privacy#marketing" title="privacy policy" target="_blank">privacy policy</a></p>');
		$('#DialogEmail').dialog('open');
		event.preventDefault();
	});

	$('#SubmitSave2').click(function(event){
		$('#QuitNow').val(1);
		$('#FormSchemes').submit();
		event.preventDefault();
	});
	
	//internal resend quote
	$('.ResendQuote').click(function(event){
		var QuoteID = $(this).attr('name');
		var ResendEmail = $('#ResendEmail_'+QuoteID).val();
		$('#DialogEmailQuote').html('<p id="text">Enter the email address to send this quote to:</p><form method="POST" id="ResendQuote" action="' + SITEPATH + 'Internal/Quotes"><input type="hidden" name="ResendQuote" value="1" /><input type="hidden" name="QuoteID" id="QuoteID" value="'+QuoteID+'" /><table><tr><td class="caption"><label for="Email">Email address</label></td><td><input type="text" id="Email" name="Email" maxlength="80" class="short" value="'+ResendEmail+'" /></td></tr></table></form>');
		$('#DialogEmailQuote').dialog('open');
		event.preventDefault();
	});

	$('#DialogEmailQuote').dialog({
		bgiframe: true,	autoOpen: false, modal: true, width: 400, resizable: false,
		buttons: {
			'Continue': function(){

				var emailval = $('#Email').val();
				var emailindex = emailval.indexOf("@");
				var emailindex2 = emailval.indexOf(".");
				var emailindex3 = emailval.indexOf(".couk");

				if (emailval != '' && emailindex != -1 && emailindex2 != -1 && emailindex3 == -1)
				{
					$('#ResendQuote').submit();
				}
				else 
				{
					$('#DialogEmailQuote #text').html('<span style="color:red;font-weight:bold;">You have entered an invalid email address:</span>');
				}
			}
		}
	});

	$('#SubmitBuyNow').click(function(event){
		$('#QuitNow').val(0);
		$('#DialogEmail').html('<p id="text">To continue with your purchase, please enter your email address. This will enable us to save your quotation, so that if you abandon the process for any reason, you can come back later and retrieve all the details.</p><table><tr><td class="caption"><label for="Email">Email address</label></td><td><input type="text" id="Email" name="Email" maxlength="80" class="short" /></td></tr></table><p style="margin-top:10px;">We will use your email address in accordance with our<br /><a href="'+SITEPATH+'privacy" title="privacy policy" target="_blank">privacy policy</a></p>');
		$('#DialogEmail').dialog('open');
		event.preventDefault();
	});
	$('#SubmitBuyNow2').click(function(event){
		$('#QuitNow').val(0);
		$('#DialogEmail').html('<p id="text">To continue with your purchase, please enter your email address. This will enable us to save your quotation, so that if you abandon the process for any reason, you can come back later and retrieve all the details.</p><table><tr><td class="caption"><label for="Email">Email address</label></td><td><input type="text" id="Email" name="Email" maxlength="80" class="short" /></td></tr></table><p style="margin-top:10px;">We will use your email address in accordance with our<br /><a href="'+SITEPATH+'privacy" title="privacy policy" target="_blank">privacy policy</a></p>');
		$('#DialogEmail').dialog('open');
		event.preventDefault();
	});
	
	// terminal months popout question
	$('#TerminalRow').hide();
	$('#Med4').change(function(){
		$('#Med4 option:selected').each(function(){
			if ($(this).text() == 'Yes')
				$('#TerminalRow').show();
			else
				$('#TerminalRow').hide();
		});
	});

	// made a mistake, return to beginning link
	$('#QuoteList').each(function(){
		$(this).after('<a href="#" id="MistakeTrigger">Made a mistake?</a>');
		$('.mainWrapper').after('<div id="DialogMistake" title="Made a mistake"><p>In order to correct a mistake in the quote you must start the process again.</p></div>');
		$('#DialogMistake').dialog({
			bgiframe: true, autoOpen: false, modal: true, width: 350, resizable: false,
			buttons: {
				'Restart': function(){
					window.location = SITEPATH;
				},
				'Continue': function(){
					$(this).dialog('close');
				}
			}			
		});
		$('#MistakeTrigger').click(function(){
			$('#DialogMistake').dialog('open');
		});
	});

	// medical condition warning
	$('#MedFormSubmit').click(function(event){
		if ($('#NumConditions').val() == 0)
		{
			$('.mainWrapper').after('<div id="DialogMedCheck" title="Medical Conditions"><p>To confirm, you have not added a medical condition. By clicking continue, a full pre-existing medical condition exclusion will apply.</p></div>');
			$('#DialogMedCheck').dialog({
				bgiframe: true,	autoOpen: true,	modal: true, width: 400, resizable: false,
				buttons: {
					'Continue': function(){
						$('#MedForm').submit();
					},
					'Add Condition': function(){
						$('#MedFormSubmit').show();
						$('.ajaxloader2').each(function(){
							$(this).hide();
						});
						$(this).dialog('close');
					}
				}
			});
			event.preventDefault();
		}
		else
		{
			$('#MedForm').submit();
		}
	});

	//login form
	if ($('#QuoteRef').length)
	{
		var value;

		$('#QuoteRef').val('enter your quote reference');

		$('#QuoteRef').focus(function() {
			if ($(this).val() == 'enter your quote reference')
			{
				value=$(this).val();
				$(this).val("");
			}
			else if ($(this).val() == '')
			{
				$(this).val("enter your quote reference");
			}
			
		});
		$('#QuoteRef').blur(function() {
			if ($(this).val() == 'enter your quote reference')
			{
				if($(this).val()=="") {
					$(this).val(value);
				}
			}
			else if ($(this).val() == '')
			{
				$(this).val("enter your quote reference");
			}
		});

	}
	if ($('#Email').length && $('#Save').val() != '10')
	{
		var value;

		if ($('#Email').val() == '')
			$('#Email').val('enter your email address');

		$('#Email').focus(function() {
			if ($(this).val() == 'enter your email address')
			{
				value=$(this).val();
				$(this).val("");
			}
			else if ($(this).val() == '')
			{
				$(this).val('enter your email address');
			}
			
		});
		$('#Email').blur(function() {
			if ($(this).val() == 'enter your email address')
			{
				if($(this).val()=="") {
					$(this).val(value);
				}
			}
			else if ($(this).val() == '')
			{
				$(this).val("enter your email address");
			}
		});

	}
	

	// help buttons
	$('.help').each(function(){
		var helpText = '';
		var target = $(this);
		var id = $(this).attr('rel');
		$.ajax({
			url: SITEPATH + 'Ajax/Help/' + id,
			success: function (html){
				helpText = html;
				id = id.replace('&', '');
				id = id.replace('=', '');
				target.append('<span class="ui-icon-info clickableHelp HelpTrigger' + id + '" style="background-image: url(' + SITEPATH + 'images/jqueryui/ui-icons_000000_256x240.png); width:16px;" rel="' + id + '">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>');
				$('.mainWrapper').after('<div class="DialogHelp' + id + '" title="Help">' + helpText + '</div>');

				$('.DialogHelp' + id).dialog({
					bgiframe: true,	autoOpen: false, modal: true, width: 500, resizable: false,
					buttons: {
						'Close': function() {
							$(this).dialog('close');
						}
					}
				});
				$('.HelpTrigger' + id).unbind('click');
				$('.HelpTrigger' + id).click(function(e){
					$('.DialogHelp' + $(this).attr('rel') + ':first').dialog('open');
				});
			}
		});
	});
	$('.helpLink').each(function(){
		var helpText = '';
		var target = $(this);
		var id = $(this).attr('rel');
		$.ajax({
			url: SITEPATH + 'Ajax/Help/' + id,
			success: function (html){
				helpText = html;
				id = id.replace('&', '');
				id = id.replace('=', '');
				//target.append('<span class="ui-icon-info clickableHelp HelpTrigger' + id + '" style="background-image: url(' + SITEPATH + 'images/jqueryui/ui-icons_000000_256x240.png); width:16px;" rel="' + id + '">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>');
				$('.mainWrapper').after('<div class="DialogHelp' + id + '" title="Help">' + helpText + '</div>');

				$('.DialogHelp' + id).dialog({
					bgiframe: true,	autoOpen: false, modal: true, width: 500, resizable: false,
					buttons: {
						'Close': function() {
							$(this).dialog('close');
						}
					}
				});
				$('.HelpTrigger' + id).unbind('click');
				$('.HelpTrigger' + id).click(function(e){
					$('.DialogHelp' + $(this).attr('rel') + ':first').dialog('open');
				});
			}
		});
	});
	$('.helpUnsecure').each(function(){
		var helpText = '';
		var target = $(this);
		var id = $(this).attr('rel');
		$.ajax({
			url: SITEPATHU + 'Ajax/Help/' + id,
			success: function (html){
				helpText = html;
				id = id.replace('&', '');
				id = id.replace('=', '');
				target.append('<span class="ui-icon-info clickableHelp HelpTrigger' + id + '" style="background-image: url(' + SITEPATH + 'images/jqueryui/ui-icons_000000_256x240.png); width:16px;" rel="' + id + '">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>');
				$('.mainWrapper').after('<div class="DialogHelp' + id + '" title="Help">' + helpText + '</div>');

				$('.DialogHelp' + id).dialog({
					bgiframe: true, autoOpen: false, modal: true, width: 500, resizable: false,
					buttons: {
						'Close': function() {
							$(this).dialog('close');
						}
					}
				});
				$('.HelpTrigger' + id).unbind('click');
				$('.HelpTrigger' + id).click(function(e){
					$('.DialogHelp' + $(this).attr('rel') + ':first').dialog('open');
				});
			}
		});
	});	

	// change card number fields on maestro selection
	if ($('#CardNumber5').length)
	{
		$('#RowCardMaestro').hide();
		$('#CardType').change(function(){
			$('#CardType option:selected').each(function(){
				if ($(this).text() == 'Maestro')
				{
					$('#RowCardNormal').hide();
					$('#RowCardMaestro').show();
				}
				else
				{
					$('#RowCardNormal').show();
					$('#RowCardMaestro').hide();
				}
			});
		});
	}

	// auto set end date for annual policies
	if ($('#TripType').length)
	{
		$('#TripType option:selected').each(function(){
			if ($(this).text() == 'Annual multi-trip')
			{
				$('#DivEndDate').hide();
				$('#MultipleInfo').show();
				$('#FamilyGroup option:last-child').hide();
				$('#DestinationRowAnnual').show();
				$('#DestinationRowSingle').hide();
				$('#LabelText').text('When would you like your cover to start?');
				$('#TripAge').text(''); //(at the date the cover commences)');
			}
			else 
			{
				$('#MultipleInfo').hide();
				$('#DivEndDate').show();
				$('#FamilyGroup option:last-child').show();
				$('#DestinationRowAnnual').hide();
				$('#DestinationRowSingle').show();
				$('#LabelText').text('Tell us the dates of your holiday:');
				$('#TripAge').text(''); //(at the date the trip commences)');
			}
		});

		$('#TripType').change(function(){
			$('#TripType option:selected').each(function(){
				if ($(this).text() == 'Annual multi-trip')
				{
					//$('#RowTripLength').hide();
					if ($('#StartDate').val() == '')
					{
						var d = new Date();
						var day = String(d.getDate());
						if (day.length == 1)
							day = '0' + day;
						day += '/';
						var month = String(d.getMonth() + 1);
						if (month.length == 1)
							month = '0' + month;
						month += '/';
						$('#StartDate').val(day + month + String(d.getFullYear()));
					}
					$('#DivEndDate').hide();
					$('#MultipleInfo').show();
					$('#LabelText').text('When do you need the cover to start?');
					$('#FamilyGroup option:last-child').hide();
					$('#DestinationRowAnnual').show();
					$('#DestinationRowSingle').hide();
					$('#TripAge').text('(at the date the cover commences)');
				}
				else
				{
					//$('#RowTripLength').show();
					$('#MultipleInfo').hide();
					$('#DivEndDate').show();
					$('#LabelText').text('Tell us the dates of your holiday:');
					$('#FamilyGroup option:last-child').show();
					$('#DestinationRowAnnual').hide();
					$('#DestinationRowSingle').show();
					$('#TripAge').text('(at the date the trip commences)');
				}
			});
		});

		$('#Page0Submit').click(function(e){
			// check the destination
			e.preventDefault();
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=DestinationCheck&DestinationName=' + $('#DestinationName').val(),
				success: function (result){
					result = $.trim(result)
					if (result == 'Failure')
					{
						$('#DialogDestinationError').html('<p>The destination you entered was not found.</p><p>Please indicate the region you will be travelling to based on the definitions below.</p><p><select name="DestinationOverride" id="DestinationOverride" style="width:450px"><option value="21">Europe 1</option><option value="22">Europe 2</option><option value="16">United Kingdom</option><option value="27">Worldwide 1</option><option value="28">Worldwide 2</option></select></p><p><strong>United Kingdom:</strong> Is United Kingdom of Great Britain and Northern Ireland and the Scilly Isles.</p><p><strong>Europe 1:</strong> means the continent of Europe west of the Ural Mountains including the Republic of Ireland and the Channel Islands all Countries bordering the Mediterranean Sea, Madeira and the Azores excluding Spain, the Canary Islands, Turkey, Cyprus, Malta and Switzerland</p><p><strong>Europe 2:</strong> means all of the countries in Europe 1 and including Spain, the Canary Islands, Turkey, Cyprus, Malta and Switzerland</p><p><strong>Worldwide 1:</strong> means anywhere in the world excluding the USA, Canada and The Caribbean.</p><p><strong>Worldwide 2:</strong> means anywhere in the world including USA, Canada and The Caribbean.</p><p>If you are going to more than one country, take care to select the region that will cover all of the countries you are visiting. For example, if you are travelling to France and Spain you should select Europe 2.</p>');
						$('#DialogDestinationError').dialog('open');
					}
					else
					{
						$('#FormStart').submit();
					}
				}
			});
		});

		$('#DialogDestinationError').dialog({
			bgiframe: true, autoOpen: false, modal: true, width: 500, resizable: true,
			buttons: {
				'Continue': function() {
					$('#AltDestination').val($('#DestinationOverride').val());
					$('#DestinationName').val('');
					$('#FormStart').submit();
				}
			}
		});
		
	}

	// show age rows for the number of people travelling
	if ($('#RowAges').length)
	{
		if ($('#Age0').val() == '')
			$('#RowAges').hide();
		for (i = 0; i < MAXTRAVELLERS; i++)
		{
			if ($('#Age' + i).val() == '')
				$('#AgeRow' + i).hide();
		}

		$('#NumberTravellers').keyup(function(){
			var num = parseInt($('#NumberTravellers').val());
			if (isNaN(num)){num = '';}
			if (num < 0){num = 0;}
			if (num > MAXTRAVELLERS)
			{
				$('#DialogMaxTravellers').text('The maximum number of travellers you can have is ' + MAXTRAVELLERS); 
				$('#DialogMaxTravellers').dialog('open');
				num = MAXTRAVELLERS;
			}
			$('#NumberTravellers').val(num);

			for (i = 0; i < MAXTRAVELLERS; i++)
			{
				if (i < num)
				{
					$('#AgeRow' + i).show();
				}
				else
				{
					$('#Age' + i).val('');
					$('#Months' + i).val('');
					$('#AgeRow' + i).hide();
				}
			}
			$('#RowAges').show();
		});

		$('#DialogMaxTravellers').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 400, resizable: true,
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});
	}

	// auto set number of travellers for different family group types
	if ($('#FamilyGroup').length)
	{
		$('#RowNumPeople').hide();

		$('#FamilyGroup').change(function(){
			$('#FamilyGroup option:selected').each(function(){
				if ($(this).text() == 'Individual')
				{
					$('#NumberTravellers').val(1);
					$('#RowNumPeople').hide();
				}
				else if ($(this).text() == 'Couple')
				{
					$('#NumberTravellers').val(2);
					$('#RowNumPeople').hide();
				}
				else if ($(this).text() == 'Family')
				{
					$('#NumberTravellers').val(3);
					$('#RowNumPeople').show();
				}
				else
					$('#RowNumPeople').show();
				$('#NumberTravellers').keyup();
			});
		}).change();
	}

	// condition autocomplete
	if ($('#ConditionSearch').length)
	{
		$('#ConditionSearch').autocomplete({
			source: SITEPATH + 'index.php?a=Ajax&b=AutoComplete&c=Conditions',
			minLength: 3
		});
	}

	// activity autocomplete
	if ($('#JSResponse').length)
	{
		$('#JSResponse').html('<td class="caption"><label for="Activity">Activity:</label> <a href="#" id="ActivityHelp"><img src="' + SITEPATH + 'images/info2.gif" alt="Help" title="Help" /></a></td><td><div style="position:relative;"><input type="text" id="Activity" name="Activity" class="short2" /><a href="#" id="AddActivity"><img src="' + SITEPATH + 'images/button_add.png" alt="Add Activity" title="Add Activity" /></a></div></td>');
		$('#JSCell').html('Selected Activities:');
		$('#ActivityShowHide').hide();

		$('#Activity').autocomplete({
			source: SITEPATH + 'index.php?a=Ajax&b=AutoComplete&c=Activity',
			minLength: 3
		});

		//add activity to policy
		$('#AddActivity').click(function(){
			var ActivityName = $('#Activity').val();
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=Activity',
				type: 'POST',
				data: {name:ActivityName},
				success: callback,
				error: err
			});

			$('#ActivityError').dialog({
				bgiframe: true,	autoOpen: false, modal: true, width: 400, resizable: true,
				buttons: {
					'Close': function() {
						$(this).dialog('close');
					}
				}
			});
			
			function callback(data,status)
			{
				data = $.trim(data);
				if (data == 'fail')
				{
					$('#ActivityError').html('<p>Your activity does not match our list. Please click the <img src="'+SITEPATH+'images/info.gif" /> next to Activity to see a full list of accepted values.</p>');
					$('#ActivityError').dialog('open');
				}
				else
				{
					$('<input type="hidden" name="Activity[]" value="'+data+'" id="Activity_'+data+'" />').appendTo('form');

					$('#ActivityShowHide').show();

					var ActivityResponse = $('#ActivityResponse').html();

					var NewResponse = ActivityResponse + '<p class="ajaxDataParent">' + ActivityName + ' <a href="#" class="AjaxDataChild" rel="'+data+'">delete (x)</a></p>';

					$('#ActivityResponse').html(NewResponse);
					$('#Activity').val('');
					
				}
				
			}
			
			function err(xhr,reason,ex)
			{
				$('#ActivityError').html('<p>There has been a problem adding your activity. Please try again.</p>');
				$('#ActivityError').dialog('open');
			}

			return false;
		});

		//remove activity from dom if clicked
		$('.AjaxDataChild').live('click', function(){
			
			var inputID = $(this).attr('rel');
			$(this).parents('.ajaxDataParent').remove();
			
			$('#Activity_'+inputID).remove();

			if ($('#ActivityResponse').html() == '')
			{
				$('#ActivityShowHide').hide();
			}
			return false;
		});

		$('#ActivityHelp').live('click',function(){
			$.ajax({
			url: SITEPATH + 'Ajax/Help/Activity',
			success: function (html) {
				$('#DialogHelp5000').dialog({
					bgiframe: true,	autoOpen: false, modal: true, width: 900, height: 500, resizable: false,
					buttons: {
						'Close': function() {
							$(this).dialog('close');
						}
					}
				});
				$('#DialogHelp5000').html(html);
				$('#DialogHelp5000').dialog('open');
				}
			});
		});

	}

	// destination autocomplete
	if ($('#DestinationName').length)
	{
		$('#DestinationName').autocomplete({
			source: SITEPATH + 'index.php?a=Ajax&b=AutoComplete&c=Destinations',
			minLength: 2
		});
	}

	


	// option page
	if ($('#DivFinalPremium').length || $('#DivFinalPremium2').length || $('#DivFinalPremium3').length)
	{
		
		$('.optionChange').click(function(){
			premiumCalculate();
		});


		function premiumCalculate()
		{
			var premium = parseFloat($('#BasePremium').val());
			$('.optionChange').each(function(){
				if ($(this).attr('checked'))
				{
					parts = $(this).attr('rel').split('_');
					premium += parseFloat($('#Gross' + parts[0]).val());
				}
			});
			$('#DivFinalPremium').html('<h2>' + premiumFormatHTML(premium) + '</h2>');
			$('#DivFinalPremium2').html('Premium <span style="color:#000;font-size:14pt;font-weight:bold;">' + premiumFormatHTML(premium) + '</span>');
			$('#DivFinalPremium3').html('Premium ' + premiumFormatHTML(premium));
			$('#HiddenPremium').val(parseFloat(premium));
		}
		if ($('.optionChange').length)
		{
			premiumCalculate();
		}
		
	}

	//highlight selected fields
	$('.silver').click(function(){
		$('.tickBox').removeClass('tickChecked');
		$('.tickBox0').addClass('tickChecked');
		$('table td').each(function() {
			if ($(this).hasClass('highlight'))
			{
				$(this).removeClass('highlight');
			}
				
		});
		
		$('.silver').each(function(){
			$(this).addClass('highlight');
		});
		$('#SchemeID').val($('#Scheme0').val());
		$('.JSPremium').html($('.PremAmount0').html());
		return false;
		
	});
	$('.gold').click(function(){
		$('.tickBox').removeClass('tickChecked');
		$('.tickBox1').addClass('tickChecked');
		$('table td').each(function() {
			if ($(this).hasClass('highlight'))
			{
				$(this).removeClass('highlight');
			}
				
		});
		
		$('.gold').each(function(){
			$(this).addClass('highlight');
		});
		$('#SchemeID').val($('#Scheme1').val());
		$('.JSPremium').html($('.PremAmount1').html());
		return false;
	});
	$('.platinum').click(function(){
		$('.tickBox').removeClass('tickChecked');
		$('.tickBox2').addClass('tickChecked');
		$('table td').each(function() {
			if ($(this).hasClass('highlight'))
			{
				$(this).removeClass('highlight');
			}
				
		});
		
		$('.platinum').each(function(){
			$(this).addClass('highlight');
		});
		$('#SchemeID').val($('#Scheme2').val());
		$('.JSPremium').html($('.PremAmount2').html());
		return false;
	});

	if ($('.selectButton').length)
	{
		$('.selectButton').click(function(){
			$('#SchemeID').val($(this).attr('rel'));
			$('form#FormSchemes').submit();
			return false;
		});
	}


	if ($('.AddRemoveCover').length)
	{
		$('.AddRemoveCover').click(function() {
			var TravellerID = $(this).attr('rel');
			var LinkText = $(this).text();
			if ($('#Traveller'+TravellerID).val() == 0)
			{
				$('#Traveller'+TravellerID).val(1);
				$('#TravellerCondition'+TravellerID).val(1);
				$.ajax({
					url: SITEPATH + 'index.php?a=Ajax&b=CalculatePremium&c=' + TravellerID,
					data: $('#FormSchemes').serialize(),
					dataType: 'json',
					success: function (data){
						$.each(data, function(i,item){
							 var instring = strpos(i,'-');
							 if (instring)
							 {
									var element = $('#'+i);
									currentclass = element.attr('class');
									element.removeClass(currentclass).addClass(item);


							 }
							 else
							 {
								$('.' + i).html(premiumFormatHTML(item));
							 }
						});
					}
				});

				if (LinkText == 'Remove Cover')
				{
					
					$(this).text('Add Cover');
				}
				else
				{
					$(this).text('Remove Cover');
				}
			}
			else
			{
				$('#DialogMedical').html('<p>Are you sure you want to remove cover for this medical condition?</p><p>If you do go ahead and remove the cover, any medical treatment resulting from or associated with the condition will be excluded from your policy.</p>');
				$('#DialogMedical').dialog('open');

				$('#DialogMedical').dialog({
					bgiframe: true,	autoOpen: true,	modal: false, width: 400, height: 200, resizable: true,
					buttons: {
						'No': function(){
							$(this).dialog('close');
							$('#Traveller'+TravellerID).val(1);
						},
						'Yes': function(){
							$('#Traveller'+TravellerID).val(0);
							$('#TravellerCondition'+TravellerID).val(0);
							//make an ajax call to work out new premiums
							$.ajax({
								url: SITEPATH + 'index.php?a=Ajax&b=CalculatePremium&c=' + TravellerID,
								data: $('#FormSchemes').serialize(),
								dataType: 'json',
								success: function (data){
									$.each(data, function(i,item){
										 var instring = strpos(i,'-');
										 if (instring)
										 {
												var element = $('#'+i);
												currentclass = element.attr('class');
												element.removeClass(currentclass).addClass(item);
										 }
										 else
										 {
											$('.' + i).html(premiumFormatHTML(item));
										 }
									});
								}
							});
							$('.AddRemoveCover').each(function(){
								if ($(this).attr('rel') == TravellerID)
								{
									if ($(this).text() == 'Remove Cover')
									{
										
										$(this).text('Add Cover');
									}
									else
									{
										$(this).text('Remove Cover');
									}
								}
							});
							//change text of link
							
							$(this).dialog('close');
						
					}
					
				}
				});
							
			}
							
			return false;
		});
	}

	// trip dates
	if ($('#StartDate').length || $('#EndDate').length || $('#NewsDate').length)
	{
		$('#StartDate').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			minDate: '+0d',
			maxDate: '+1y',
			hideIfNoPrevNext: true,
			onSelect: function(dateText, inst){
				$('#EndDate').datepicker('option', 'defaultDate', dateText);
			}
		});
		$('#EndDate').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			minDate: '+1d',
			maxDate: '+2y',
			hideIfNoPrevNext: true
		});
		$('#NewsDate').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			minDate: '-1y',
			maxDate: '+6m',
			hideIfNoPrevNext: true
		});
	}


	if ($('#StartDate2').length || $('#EndDate2').length)
	{
		$('#StartDate2').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			hideIfNoPrevNext: true,
			onSelect: function(dateText, inst){
				$('#EndDate2').datepicker('option', 'defaultDate', dateText);
			}
		});
		$('#EndDate2').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			hideIfNoPrevNext: true
		});
	}

	if ($('#StartDate3').length || $('#EndDate3').length)
	{
		$('#StartDate3').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			hideIfNoPrevNext: true,
			onSelect: function(dateText, inst){
				$('#EndDate3').datepicker('option', 'defaultDate', dateText);
			}
		});
		$('#EndDate3').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			hideIfNoPrevNext: true
		});
	}

	// traveller date of birth
	if ($('.shortDob').length)
	{
		$('.shortDob').each(function(){
			var id = $(this).attr('rel');
			$('#DobDay' + id).keyup(function(){
				if ($(this).val().length == 2)
					$('#DobMonth' + id).focus();
			});
			$('#DobMonth' + id).keyup(function(){
				if ($(this).val().length == 2)
					$('#DobYear' + id).focus();
			});
		});
	}

	// adding time to news article
	if ($('#NewsTime1').length)
	{
		$('#NewsTime1').keyup(function(){
			if ($(this).val().length == 2)
				$('#NewsTime2').focus();
		});
	}

	// credit card number
	if ($('#CardNumber1').length)
	{
		$('#CardNumber1').keyup(function(){
			if ($(this).val().length == 4)
				$('#CardNumber2').focus();
		});
		$('#CardNumber2').keyup(function(){
			if ($(this).val().length == 4)
				$('#CardNumber3').focus();
		});
		$('#CardNumber3').keyup(function(){
			if ($(this).val().length == 4)
				$('#CardNumber4').focus();
		});

		$('#CardStartDate1').keyup(function(){
			if ($(this).val().length == 2)
				$('#CardStartDate2').focus();
		});
		$('#CardEndDate1').keyup(function(){
			if ($(this).val().length == 2)
				$('#CardEndDate2').focus();
		});
	}
	
	// facebook bookmark link
	if ($('#BookmarkFacebook').length)
	{
		$('#BookmarkFacebook').click(function(){
			window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(location.href) + '&amp;t=' + encodeURIComponent(document.title));
			return false;
		});
	}

	// news article content links
	if ($('#ImageLink').length)
	{

		$('#ImageLink').live('click', function() {
		  // Live handler called.
		//});
		//$('#ImageLink').click(function(){
			$('#ImagePreviewLeftDialog').dialog('open');
			return false;
		});

		$('a.imageLeft').click(function(e) {

				e.preventDefault();
				var imgID = $(this).attr('id');
				var alt = $(this).attr('title');
				var portrait = $(this).attr('href');

				if (portrait == 0)
				{
					$('#Content').text('<img src="' + SITEPATHU + 'images/news/'+ imgID+'.jpg" alt="'+ alt +'" style="margin:0 0 10px;" />' + $('#Content').text());
				}
				else 
				{
					$('#Content').text('<img src="' + SITEPATHU + 'images/news/'+ imgID+'.jpg" alt="'+ alt +'" style="margin:0 10px 10px 0;float:left;" />' + $('#Content').text());
				}

				$('#Content').focus();
				$('#ImagePreviewLeftDialog').dialog('close');

				
		});

		$('#ImageLink2').live('click', function() {
			$('#ImagePreviewRightDialog').dialog('open');

		});

		$('a.imageRight').click(function() {

				var imgID = $(this).attr('id');
				var alt = $(this).attr('title');
				
				$('#Content').text($('#Content').text() + '<img src="' + SITEPATHU + 'images/news/'+ imgID+'.jpg" alt="'+ alt +'" class="newsPhotoRight" />');
				$('#Content').focus();
				$('#ImagePreviewRightDialog').dialog('close');
			});
		
		$('#ImagePreviewLeftDialog').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 600, resizable: true,
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});

		$('#ImagePreviewRightDialog').dialog({
			bgiframe: true, autoOpen: false, modal: true, width: 600, resizable: true,
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});

		$('#LinkLink').live('click', function() {
			$('#Content').text($('#Content').text() + '<a href="http://www.google.co.uk" rel="external">Google</a>');
			$('#Content').focus();
		});
		$('#ParaLink').live('click', function() {
			$('#Content').text($('#Content').text() + '<p></p>');
			$('#Content').focus();
		});
		$('#ListLink').live('click', function() {
			$('#Content').text($('#Content').text() + '<ul><li>item 1</li><li>item 2</li></ul>');
			$('#Content').focus();
			
			return false;

		});

		$('#PreviewLink').live('click', function() {
			// escape any html

			
			$('#Content').text($('#Content').text().replace(/ & /g," &amp; "));
			//$('#Content').text($('#Content').text().replace(/ > /g," &gt; "));
			//$('#Content').text($('#Content').text().replace(/ < /g," &lt; "));
			//$('#Content').text($('#Content').text().replace(/\"/g,"&quot;"));
			$('#Content').text($('#Content').text().replace(/£/g,"&pound;"));

			var htmlval = $('#Content').text();

			//$('<div style="width:600px;background:red;position:absolute;top:500px;left:400px;" id="preview">'+htmlval+'</div>').appendTo('body');
			
			$('#PreviewDialog').dialog('open');
			$('#PreviewDialog').html('<div style="height:500px;overflow:auto;">' + $('#Content').text() + '</div>');
			
		});

		$('#PreviewDialog').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 600, resizable: true,
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});
	}
	


	$('#DialogNonDeclaration').dialog({
		bgiframe: true,	autoOpen: false, modal: true, width: 500, resizable: false,
		buttons: {
			'Yes': function(){
				$(this).dialog('close');
			},
			'No': function(){
				window.location = SITEPATH;
			}
		}
	});
	$('#LinkNonDeclaration').click(function(e){
		e.preventDefault();
		var phone = '0844 257 9857 ';
		if ($('#AffiliatePhone').length)
		{
			phone = $('#AffiliatePhone').val();
		}
		$('#DialogNonDeclaration').html('<p>Unfortunately you cannot proceed with issuing this policy unless the declaration is agreed.</p><p>Please call ' + phone + ' is you have any queries.</p><p>Do you wish to continue?</p>');
		$('#DialogNonDeclaration').dialog('open');
	});

	$('#DialogPhone').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 300, resizable: false,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});
	$('#LinkPhone').click(function(e){
		$('#DialogPhone').dialog('open');
	});

	$('#DialogDeleteEndorsement').dialog({
		bgiframe: true,	autoOpen: false, modal: false, width: 300, resizable: false,
		buttons: {
			'No': function(){
				$(this).dialog('close');
			},
			'Yes': function(){
				window.location = SITEPATH + 'index.php?a=Internal&b=Endorsements&c=DeleteEndorsement&EndorsementID=' + $('#SelectedID').val();
			}
		}
	});
	$('.linkDeleteEndorsement').click(function(e){
		e.preventDefault();
		$('#SelectedID').val($(this).attr('rel'));
		$('#DialogDeleteEndorsement').dialog('open');
	});




	//page content links

	if ($('#ParaLinkP').length)
	{

		$('#BreadLink').live('click', function() {
			$('#LinkPreviewDialog2').dialog('open');
			return false;
		});

		$('a.linkselect2').click(function() {

				var linkName = $(this).attr('title');

				if ($('#Breadcrumbs').val() == '')
				{
					$('#Breadcrumbs').val(linkName);
				}
				else
				{
					$('#Breadcrumbs').val($('#Breadcrumbs').val() + ',' + linkName);
				}
				
				$('#Breadcrumbs').focus();
				$('#LinkPreviewDialog2').dialog('close');

				return false;
		});

		
		$('#LinkPreviewDialog2').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 600, resizable: true,
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});

		$('#IntLink').live('click', function() {
			$('#LinkPreviewDialog').dialog('open');
			return false;
		});

		$('a.linkselect').click(function() {

				var linkName = $(this).attr('title');
				
				$('#Content').text($('#Content').text() + '[[Link:'+linkName+']]');
				$('#Content').focus();
				$('#LinkPreviewDialog').dialog('close');
		});

		
		$('#LinkPreviewDialog').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 600, resizable: true,
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});

		$('#ExtLink').live('click', function() {
			$('#Content').text($('#Content').text() + '<a href="http://www.google.co.uk" rel="external">Google</a>');
			$('#Content').focus();
			return false;
		});

		$('#ParaLinkP').live('click', function() {
			$('#Content').text($('#Content').text() + '<p></p>');
			$('#Content').focus();
			return false;
		});

		$('#ListLinkP').live('click', function() {
			$('#Content').text($('#Content').text() + '<ul><li>item 1</li><li>item 2</li></ul>');
			$('#Content').focus();
			return false;
		});

		$('#PreviewLinkP').live('click', function() {
			// escape any html

			
			$('#Content').text($('#Content').text().replace(/ & /g," &amp; "));
			//$('#Content').text($('#Content').text().replace(/ > /g," &gt; "));
			//$('#Content').text($('#Content').text().replace(/ < /g," &lt; "));
			//$('#Content').text($('#Content').text().replace(/\"/g,"&quot;"));
			$('#Content').text($('#Content').text().replace(/£/g,"&pound;"));

			var htmlval = $('#Content').text();

			$('#PreviewDialog').dialog('open');
			$('#PreviewDialog').html('<div style="height:500px;overflow:auto;">' + $('#Content').text() + '</div>');

			return false;
			
		});

		$('#PreviewDialog').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 600, resizable: true,
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});
	}

	// dialog for policy options description
	if ($('.dialogpol').length)
	{
		$('.dialogpol').dialog({
			bgiframe: true,	autoOpen: false, modal: false, width: 500, resizable: false,
			buttons: {
				'OK': function(){
					$(this).dialog('close');
				}
			}
		});

		$('.policyoptionstrigger').click(function(){
			var SchemeID = $(this).attr('rel');

			$('#PolicyDialog' + SchemeID).dialog({
			bgiframe: true,	autoOpen: false, modal: false, width: 500, resizable: false,
			buttons: {
				'OK': function(){
					$(this).dialog('close');
				}
			}
			});

			$('#PolicyDialog' + SchemeID).dialog('open');

			return false;

		});

		$('.schemeoptionstrigger').click(function(){
			var SchemeID = $(this).attr('rel');

			$('#SchemeDialog' + SchemeID).dialog({
			bgiframe: true,	autoOpen: false, modal: false, width: 500, resizable: false,
			buttons: {
				'OK': function(){
					$(this).dialog('close');
				}
			}
			});

			$('#SchemeDialog' + SchemeID).dialog('open');

			return false;

		});

	}
	

});

function premiumFormatHTML(pAmount)
{
	pAmount = parseFloat(pAmount);
	if (!isNaN(pAmount))
		return '&pound;' + commaFormat(pAmount.toFixed(2));
	else
		return '';
}

function commaFormat(pAmount)
{
	var delimiter = ","; // replace comma if desired
	var a = pAmount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { pAmount = n; }
	else { pAmount = n + '.' + d; }
	pAmount = minus + pAmount;
	return pAmount;
}

function strpos (haystack, needle, offset) {
  var i = (haystack+'').indexOf(needle, (offset || 0));
  return i === -1 ? false : i;
}
