$(function()
{
	//home page banner
	if ($('#promoHome').length)
	{
		function showImages() {
			$('#promoHome').hide();
			$('#promoHome').html('');
			
			var TextHTML = '<p>14 Day Money Back</p><h3>Guarantee</h3>';

			$('#promoHome').html(TextHTML);
			
			$('#promoHome').fadeIn(200, function() {

				$('#promoHome').delay(3000).fadeOut(200, function() {
					TextHTML = '<p class="small" style="top:20px;">All types of</p><h3 style="top:40px;font-size:170%;right:10px;">Medical Conditions</h3><p style="top:70px;font-size:120%;">can be covered</p>';
					$('#promoHome').html(TextHTML);
					$('#promoHome').fadeIn(200, function() {

						$('#promoHome').delay(3000).fadeOut(200, function() {
							TextHTML = '<h3 style="top:25px;font-size:200%;right:15px;">No Age Limits</h3><p style="top:57px;right:15px;font-size:120%;">on single trip policies</p>';
							$('#promoHome').html(TextHTML);
							$('#promoHome').fadeIn(200, function() {

								$('#promoHome').delay(3000).fadeOut(200, function() {
									TextHTML = '<h3 style="top:25px;font-size:200%;right:15px;">Annual Cover</h3><p style="top:57px;right:15px;font-size:120%;">available up to age 65</p>';
									
									$('#promoHome').html(TextHTML);
									$('#promoHome').fadeIn(200, function() {

										$('#promoHome').delay(3000).fadeOut(200, function() {
											TextHTML = '<h3 style="top:23px;font-size:150%;right:10px;">24 Hour Medical</h3><h3 style="top:43px;font-size:150%;right:10px;">Emergency Support</h3><p style="top:70px;font-size:120%;right:10px;">just a phone call away!</p>';
											$('#promoHome').html(TextHTML);
											$('#promoHome').fadeIn(200, function() {

												$('#promoHome').delay(3000).fadeOut(200, function() {
													showImages();
												});


											});
										});


									});
								});


							});
						});

					});
				});
			});
		}

		showImages();
	}
	/*
	//members area form
	if ($('#Username').length)
	{
		var value;

		$('#Username').focus(function() {
			if ($(this).val() == 'enter your email...')
			{
				value=$(this).val();
				$(this).val("");
			}
			else if ($(this).val() == '')
			{
				$(this).val("enter your email...");
			}
			
		});
		$('#Username').blur(function() {
			if ($(this).val() == 'enter your email...')
			{
				if($(this).val()=="") {
					$(this).val(value);
				}
			}
			else if ($(this).val() == '')
			{
				$(this).val("enter your email...");
			}
		});

	}

	if ($('#RetrievePassword').length)
	{
		var value;

		$('#RetrievePassword').focus(function() {
			if ($(this).val() == '123456')
			{
				value=$(this).val();
				$(this).val("");
			}
			else if ($(this).val() == '')
			{
				$(this).val("123456");
			}
			
		});
		$('#RetrievePassword').blur(function() {
			if ($(this).val() == '123456')
			{
				if($(this).val()=="") {
					$(this).val(value);
				}
			}
			else if ($(this).val() == '')
			{
				$(this).val("123456");
			}
		});

	}*/

	//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');
				}
			});
		});
	}

	//medical page banner
	if ($('#promoMedical').length)
	{
		function showImages2() {
			$('#promoMedical').hide();
			$('#promoMedical').html('');
			
			var TextHTML = '<p>14 Day Money Back</p><h3>Guarantee</h3>';

			$('#promoMedical').html(TextHTML);
			
			$('#promoMedical').fadeIn(200, function() {

				$('#promoMedical').delay(3000).fadeOut(200, function() {
					TextHTML = '<p class="small" style="top:20px;">All types of</p><h3 style="top:40px;font-size:170%;right:10px;">Medical Conditions</h3><p style="top:70px;font-size:120%;">can be covered</p>';
					
					$('#promoMedical').html(TextHTML);
					$('#promoMedical').fadeIn(200, function() {

						$('#promoMedical').delay(3000).fadeOut(200, function() {
							TextHTML = '<p style="right:80px;font-size:120%;">Extra cover for</p><h3 style="font-size:160%;right:15px;">Loss of Medication</h3>';
							
							$('#promoMedical').html(TextHTML);
							$('#promoMedical').fadeIn(200, function() {

								$('#promoMedical').delay(3000).fadeOut(200, function() {
									TextHTML = '<h3 style="top:23px;font-size:150%;right:10px;">24 Hour Medical</h3><h3 style="top:43px;font-size:150%;right:10px;">Emergency Support</h3><p style="top:70px;font-size:120%;right:10px;">just a phone call away!</p>';
									
									$('#promoMedical').html(TextHTML);
									$('#promoMedical').fadeIn(200, function() {

										$('#promoMedical').delay(3000).fadeOut(200, function() {
											showImages2();
										});


									});
								});


							});
						});

					});
				});
			});
		}

		showImages2();
	}

	//over 65 page banner
	if ($('#promoOver65').length)
	{
		function showImages3() {
			$('#promoOver65').hide();
			$('#promoOver65').html('');
			
			var TextHTML = '<h3 style="top:25px;font-size:200%;right:15px;">No Age Limits</h3><p style="top:57px;right:15px;font-size:120%;">on single trip policies</p>';

			$('#promoOver65').html(TextHTML);
			
			$('#promoOver65').fadeIn(200, function() {

				$('#promoOver65').delay(3000).fadeOut(200, function() {
					TextHTML = '<p>14 Day Money Back</p><h3>Guarantee</h3>';
					$('#promoOver65').html(TextHTML);
					$('#promoOver65').fadeIn(200, function() {

						$('#promoOver65').delay(3000).fadeOut(200, function() {
							TextHTML = '<p class="small" style="top:20px;">All types of</p><h3 style="top:40px;font-size:170%;right:10px;">Medical Conditions</h3><p style="top:70px;font-size:120%;">can be covered</p>';
							
							$('#promoOver65').html(TextHTML);
							$('#promoOver65').fadeIn(200, function() {

								$('#promoOver65').delay(3000).fadeOut(200, function() {
									TextHTML = '<h3 style="top:23px;font-size:150%;right:10px;">24 Hour Medical</h3><h3 style="top:43px;font-size:150%;right:10px;">Emergency Support</h3><p style="top:70px;font-size:120%;right:10px;">just a phone call away!</p>';
									
									$('#promoOver65').html(TextHTML);
									$('#promoOver65').fadeIn(200, function() {

										$('#promoOver65').delay(3000).fadeOut(200, function() {
											showImages3();
										});


									});
								});


							});
						});

					});
				});
			});
		}

		showImages3();
	}
	
	
	//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);
			}
		});
	});

	//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');
						}
						
					}
				}
			}


			

		}
		

	});



	$('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;
	});

	// 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();
		}

		
	}
	
	//show reference for internal users for travel counsellors affiliate
	if ($('#Affiliate').length)
	{
		if ($('#Affiliate').val() == 62)
		{
			$('#ExtrasRefRow').show();
		}
		else
		{
			$('#ExtrasRefRow').hide();
		}
		$('#Affiliate').change(function(){
			$('#Affiliate option:selected').each(function(){
				if ($(this).text() == 'Travel Counsellors')
				{
					$('#ExtrasRefRow').show();
				}
				else
				{
					$('#ExtrasRefRow').hide();
				}
			});
		});
	}
	

	// add text link for affiliate
	if ($('#addtextlink').length)
	{
		$('#addtextlink').click(function(){
			var textLink = $('#TextLink').val();
			var affiliateID = $('#ArticleID').val();
			var affiliateName = $('#Name').val().replace(/\s/g,'');
		
			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;

		});
	}

	if ($('#addtextlinkQuote').length)
	{
		$('#addtextlinkQuote').click(function(){
			var textLink = $('#TextLink').val();
			var affiliateID = $('#ArticleID').val();
			var affiliateName = $('#Name').val().replace(/\s/g,'');
			
			var newValue = '<a href="https://www.goodtogoinsurance.com/public/Quote,Page0&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

	if ($('#DialogPDFWarning').length)
	{
		$('#DialogPDFWarning').dialog({
				bgiframe: true,	autoOpen: false, modal: false, width: 400, resizable: false,
				buttons: {
					'Close': function(){
						$(this).dialog('close');
					}
				}
		});

		$('#RenewalPDFLink').click(function(){
			$('#DialogPDFWarning').html('Your PDF is being created. This may take a few minutes depending on the number of letters to be created');
			$('#DialogPDFWarning').dialog('open');
			$('#RenewalPDFLink').hide();
			name = $(this).attr('name');
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=RenewalPDF&c='+name,
				success: function(){
					$('#DialogPDFWarning').dialog('close');
					$('#RenewalPDFLink').show();
					$('#DialogRenewalPDF').html('<embed src="' + SITEPATH + 'temp/renewals'+name+'.pdf" href="' + SITEPATH + 'temp/renewals'+name+'.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
					$('#DialogRenewalPDF').dialog({
						bgiframe: true, autoOpen: true,	modal: false, width: 700, height: 500, resizable: true,
						buttons: {
							'Close': function(){
								$(this).dialog('close');
							}
						}
					});
				}
			});

			return false;
		});

		$('#MegaPDFLink').click(function(){
			$('#DialogPDFWarning').html('Your PDF is being created. This may take a few minutes depending on the number of policies in the list');
			$('#DialogPDFWarning').dialog('open');
			$('#MegaPDFLink').hide();
			name = $(this).attr('name');
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=MegaPDF&c='+name,
				success: function(){
					$('#DialogPDFWarning').dialog('close');
					$('#MegaPDFLink').show();
					$('#DialogMegaPDF').html('<embed src="' + SITEPATH + 'temp/unsent'+name+'.pdf" href="' + SITEPATH + 'temp/unsent'+name+'.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');
							}
						}
					});
				}
			});

			return false;
		});
		$('#MegaPDFLink2').click(function(){
			$('#DialogPDFWarning').html('Your PDF is being created. This may take a few minutes depending on the number of policies in the list');
			$('#DialogPDFWarning').dialog('open');
			$('#MegaPDFLink2').hide();
			name = $(this).attr('name');
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=MegaPDF2&c='+name,
				success: function(){
					$('#DialogPDFWarning').dialog('close');
					$('#MegaPDFLink2').show();
					$('#DialogMegaPDF2').html('<embed src="' + SITEPATH + 'temp/unsent'+name+'.pdf" href="' + SITEPATH + 'temp/unsent'+name+'.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');
							}
						}
					});
				}
			});
			return false;
		});

		//renewal schedule pdfs
		$('#MegaPDFLink3').click(function(){
			$('#DialogPDFWarning').html('Your PDF is being created. This may take a few minutes depending on the number of renewals in the list');
			$('#DialogPDFWarning').dialog('open');
			$('#MegaPDFLink3').hide();
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=MegaPDF3',
				success: function(){
					$('#DialogPDFWarning').dialog('close');
					$('#MegaPDFLink3').show();
					$('#DialogMegaPDF3').html('<embed src="' + SITEPATH + 'temp/unsent3.pdf" href="' + SITEPATH + 'temp/unsent3.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
					$('#DialogMegaPDF3').dialog({
						bgiframe: true, autoOpen: true,	modal: false, width: 700, height: 500, resizable: true,
						buttons: {
							'Close': function(){
								$(this).dialog('close');
							}
						}
					});
				}
			});
			return false;
		});
		//renewal letter pdfs
		$('#MegaPDFLink4').click(function(){
			$('#DialogPDFWarning').html('Your PDF is being created. This may take a few minutes depending on the number of renewals in the list');
			$('#DialogPDFWarning').dialog('open');
			$('#MegaPDFLink4').hide();
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=MegaPDF4',
				success: function(){
					$('#DialogPDFWarning').dialog('close');
					$('#MegaPDFLink4').show();
					$('#DialogMegaPDF4').html('<embed src="' + SITEPATH + 'temp/unsent4.pdf" href="' + SITEPATH + 'temp/unsent4.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
					$('#DialogMegaPDF4').dialog({
						bgiframe: true, autoOpen: true,	modal: false, width: 700, height: 500, resizable: true,
						buttons: {
							'Close': function(){
								$(this).dialog('close');
							}
						}
					});
				}
			});
			return false;
		});
		
	}

	// 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 = 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');
						}
					}
				});

				var hiddenVars = '<input type="checkbox" class="checkbox" id="LinkOption' + optionArray[i] + '" rel="' + optionArray[i] + '"';
				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"';
					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 += ' 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');
					}
				});

			}
		}
	}

	

	//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 + 'templates/personalbelongings' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/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 + 'templates/cancellation' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/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');
			}
		}
	});

	$('.linkLegalDoc').click(function(e){
		e.preventDefault();
		$('#DialogLegalDoc').html('<embed src="' + SITEPATH + 'templates/legal' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/legal' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogLegalDoc').dialog('open');
	});

	$('#DialogLegalDoc').dialog({
		bgiframe: true, autoOpen: false, modal: false, width: 700, height: 500,	resizable: true,
		buttons: {
			'Close': function(){
				$(this).dialog('close');
			}
		}
	});

	$('.linkPersonalLiabilityDoc').click(function(e){
		e.preventDefault();
		$('#DialogPersonalLiability').html('<embed src="' + SITEPATH + 'templates/personalliability' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/personalliability' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogPersonalLiability').dialog('open');
	});

	$('#DialogPersonalLiability').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 + 'templates/curtailment' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/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 + 'templates/luggagedelay' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/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 + 'templates/traveldelay' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/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 + 'templates/medicalexpenses' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/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 + 'templates/' + affiliate + 'wording' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/' + 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();
		var affiliate = '';
		if ($('#Affiliate').length && !$('#Skip').val())
			affiliate = 'affiliates/' + $('#Affiliate').val() + '/';
		$('#DialogKeyFactsDoc').html('<embed src="' + SITEPATH + 'templates/' + affiliate + 'keyfacts' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'templates/' + affiliate + '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');
			}
		}
	});
	$('.linkSuperTagDoc').click(function(e){
		e.preventDefault();
		$('#DialogTagDoc').html('<embed src="' + SITEPATH + 'temp/supertagletter' + $(this).attr('rel') + '.pdf" href="' + SITEPATH + 'temp/supertagletter' + $(this).attr('rel') + '.pdf" style="width:100%; height:100%"><noembed>Your browser does not support embedded PDF files.</noembed></embed>');
		$('#DialogTagDoc').dialog('open');
	});

	$('#DialogTagDoc').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 + '/emp/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 buttons after click
	

	// 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;" />');
		$('#leftWrapper').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');
				}
			}
		});
		if ($('#Postcode').val() != '')
		{
			$('#RowAddress').show();
		}
		else
		{
			$('#RowAddress').hide();
		}
		$('#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 ($('#Affiliate').val() > 0)
			$(this).after('<br /><br /><input type="image" id="SubmitSave" src="' + SITEPATH + 'images/affiliates/' + $('#Affiliate').val() + '/button_savequotation.png" style="height:39px; width:169px; background-color:transparent" class="submit" alt="Save Quotation" />');
		else
			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" />');

		$('#leftWrapper').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="'+SITEPATHU+'privacy" title="privacy policy" target="_blank">privacy policy</a></p></div>');
		
		$('#DialogEmail').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)
					{
						$('#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>');
					}
				},
				'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();
					}
				}
			}
			
		});

		$('#DialogContactName').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 400, resizable: false,
			buttons: {
				'Continue': function(){
					if ($('#ContactName').val() != '')
					{
						$('#HiddenContactName').val($('#ContactName').val());
						$('#FormSchemes').submit();
					}
				}
			}
		});
	});

	// save or buy button
	$('#SubmitBuyNowRen').each(function(){
		if ($('#Affiliate').val() > 0)
			$(this).after('<br /><br /><input type="image" id="SubmitSave2" src="' + SITEPATH + 'images/affiliates/' + $('#Affiliate').val() + '/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="SubmitSave2" src="' + SITEPATH + 'images/button_savequotation.png" style="height:39px; width:169px; background-color:transparent" class="submit" alt="Save Quotation" />');

		$('#SubmitBuyNowRen').click(function(){
			$('#FormSchemes').submit();
		});

	});

	//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>');
				}
			}
		}
	});
	$('#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="'+SITEPATHU+'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="'+SITEPATHU+'privacy" 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="'+SITEPATHU+'privacy" 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();
	});

	// 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>');
		$('#leftWrapper').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 + 'public/Quote,Page0';
				},
				'Continue': function(){
					$(this).dialog('close');
				}
			}			
		});
		$('#MistakeTrigger').click(function(){
			$('#DialogMistake').dialog('open');
		});
	});

	// annual to single trip switch
	$('#DialogSwitchSingle').dialog({
		bgiframe: true,	autoOpen: false, modal: true, width: 450, resizable: false,
		buttons: {
			'Continue': function(){
				$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=DateCheck',
				data: $('#FormSwitchSingle').serialize(),
				success: function (result){
					$.trim(result);
					if (result == '')
					{
						$('#FormSwitchSingle').submit();
						
					}
					else
					{
						$('#DialogSwitchSingleError').html('<p class="error">'+result+'</p>');
					}
				}
				});
				
			},
			'Cancel': function(){
				$(this).dialog('close');
			}
		}
	});
	$('#LinkSwitchSingle').click(function(){
		$('#DialogSwitchSingle').dialog('open');
	});

	// medical condition warning
	$('#MedFormSubmit').click(function(event){
		if ($('#NumConditions').val() == 0)
		{
			$('#leftWrapper').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
		{
			var QuoteID = $('#QuoteID').val();
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=CheckMedical',
				data: $('#MedForm').serialize(),
				success: function (result){
					$.trim(result);
					if (result != 'OK')
					{
						$('#leftWrapper').after('<div id="DialogMedCheck" title="Medical Conditions"><p>You haven\'t added cover for all of the medical conditions that were listed on your previous policy.</p><p>Would you like to go back and add cover for these conditions or continue with out cover?</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');
								}
							}
						});
					}
					else
					{
						$('#MedForm').submit();
					}

				}
			});
			event.preventDefault();
		}
	});

	//telephone dialog
	if ($('.phoneLink').length)
	{
		$('#leftWrapper').after('<div class="DialogPhone" title="Our Opening Hours"><p><strong>Quotations, Sales &amp; Renewals</strong><br />Monday to Friday - 9am to 5pm<br />Saturday - 9am to 4pm<br />Sunday - 10am to 4pm</p><p><strong>Customer Service</strong><br />Monday to Friday - 9am to 5pm</p></div>');
		$('.DialogPhone').dialog({
			bgiframe: true,	autoOpen: false, modal: true, width: 300, resizable: false ,position: [1145,60],
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});
		$('.phoneLink').click(function() {
			$('.DialogPhone').dialog('open');
			return false;
		});
	}

	// 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>');
				$('#leftWrapper').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>');
				$('#leftWrapper').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>');
				$('#leftWrapper').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();
				//$('#TripAge').html('How old are the people travelling?<br /><span class="subExplanation">(at the date the cover commences)</span>');
			}
			else 
			{
				$('#MultipleInfo').hide();
				$('#DivEndDate').show();
				$('#FamilyGroup option:last-child').show();
				$('#DestinationRowAnnual').hide();
				$('#DestinationRowSingle').show();
				//$('#TripAge').html('Enter the travellers ages, as of today:');
			}
		});

		$('#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();
					$('#TravellingText').text('When do you need the cover to start?');
					$('#FamilyGroup option:last-child').hide();
					$('#DestinationRowAnnual').show();
					$('#DestinationRowSingle').hide();
					//$('#TripAge').html('How old are the people travelling?<br /><span class="subExplanation">(at the date the cover commences)</span>');
				}
				else
				{
					//$('#RowTripLength').show();
					$('#MultipleInfo').hide();
					$('#DivEndDate').show();
					$('#TravellingText').text('Tell us the dates of your holiday');
					$('#FamilyGroup option:last-child').show();
					$('#DestinationRowAnnual').hide();
					$('#DestinationRowSingle').show();
					//$('#TripAge').html('Enter the travellers ages, as of today:');
				}
			});
		});

		$('#Page0Submit').click(function(e){
			// check the destination
			e.preventDefault();
			$.ajax({
				url: SITEPATH + 'index.php?a=Ajax&b=DestinationCheck&DestinationName=' + $('#DestinationName').val(),
				success: function (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 <span class="highlight">excluding</span> Spain, the Canary Islands, Turkey, Cyprus, Malta and Switzerland</p><p><strong>Europe 2:</strong> means all of the countries in Europe 1 and <span class="highlight">including</span> Spain, the Canary Islands, Turkey, Cyprus, Malta and Switzerland</p><p><strong>Worldwide 1:</strong> means anywhere in the world <span class="highlight">excluding</span> the USA, Canada and The Caribbean.</p><p><strong>Worldwide 2:</strong> means anywhere in the world <span class="highlight">including</span> 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();
				}
			}
		});

		/*$('#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: 200, resizable: true,
			buttons: {
				'Close': function() {
					$(this).dialog('close');
				}
			}
		});*/
	}

	// 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: 200, 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();
	}

	// broker autocomplete
	if ($('#CompanyName').length)
	{
		$('#CompanyName').autocomplete({
			source: SITEPATH + 'index.php?a=Ajax&b=AutoComplete&c=Brokers',
			minLength: 3
		});
	}

	// condition autocomplete
	if ($('#ConditionSearch').length)
	{
		$('#ConditionSearch').autocomplete({
			source: SITEPATH + 'index.php?a=Ajax&b=AutoComplete&c=Conditions',
			minLength: 3
		});
	}

	// 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();
		}
		
	}
	
	//scheme select page
	//condition confirm exlusion dialog

	//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();
		});
	}


	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;
		});
	}

	//change premium if medical condition not included
	if ($('.travellerChange').length)
	{
	}

	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');
								}
								
							}
						});
					}
				});
			}
		}
		
	}

	

	// trip dates
	if ($('#StartDate').length || $('#EndDate').length || $('#NewsDate').length)
	{
		if ($('#TripType').val() == 1)
		{
			$('#StartDate').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			minDate: '+0d',
			maxDate: '+1y',
			hideIfNoPrevNext: true,
			onSelect: function(dateText, inst){
				$('#EndDate').datepicker('option', 'defaultDate', dateText);
			}
			});
		}
		else if ($('#TripType').val() == 2)
		{
			$('#StartDate').datepicker({
			dateFormat: "dd/mm/yy",
			gotoCurrent: true,
			minDate: '+0d',
			maxDate: '+90d',
			hideIfNoPrevNext: true,
			onSelect: function(dateText, inst){
				$('#EndDate').datepicker('option', 'defaultDate', dateText);
			}
			});
		}
		$('#TripType').change(function(){
			
			$('#TripType option:selected').each(function(){
				if ($(this).text() == 'Annual multi-trip')
				{
					$( "#StartDate").datepicker( "option", "maxDate", '+90d');
				}
				else
				{
					$( "#StartDate").datepicker( "option", "maxDate", '+1y');

				}

			});
		});
		
		$('#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 334 0160';
		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');
	});


	// retrieve quote dialog
	$('.linkRetrieveDetails').click(function(){
		var id = $(this).attr('rel');
		$('#DialogRetrieveDetails').dialog({
			bgiframe: true, autoOpen: true, modal: false, width: 350, resizable: false,
			buttons: {
				'Close': function(){
					$(this).dialog('close');
				},
				'Retrieve': function(){
					window.location = SITEPATH + 'Internal/RetrieveQuote&QuoteID=' + id;
				}
			}
		});
		$.ajax({
			url: SITEPATH + 'index.php?a=Ajax&b=RetrieveDetails&ID=' + $(this).attr('rel'),
			success: function(data){
				$('#DialogRetrieveDetails').html(data);
			}
		});

	});

	// retrieve renewal dialog
	$('.linkRetrieveDetails2').click(function(){
		var id = $(this).attr('rel');
		$('#DialogRetrieveDetails').dialog({
			bgiframe: true, autoOpen: true, modal: false, width: 350, resizable: false,
			buttons: {
				'Close': function(){
					$(this).dialog('close');
				},
				'Retrieve': function(){
					window.location = SITEPATH + 'Internal/RetrieveRenewal&QuoteID=' + id;
				}
			}
		});
		$.ajax({
			url: SITEPATH + 'index.php?a=Ajax&b=RetrieveDetails2&ID=' + $(this).attr('rel'),
			success: function(data){
				$('#DialogRetrieveDetails').html(data);
			}
		});

	});



	//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;

		});

	}




	// dialog for confirming policy lapse at renewal
	if ($('#DialogLapse').length)
	{
		$('#DialogLapse').dialog({
			bgiframe: true, autoOpen: false, modal: true, resizable: false,
			buttons: {
				'Lapse Renewal': function(){
					window.location = SITEPATH + 'index.php?a=Internal&b=Renewals&Lapse=1&QuoteID=' + $('#HiddenQuote').val();
				},
				'Keep Renewal': function(){
					$(this).dialog('close');
				}
			}
		});


		$('.linkLapse').click(function(){
			parts = $(this).attr('rel').split('~');
			j = parts[0];
			k = parts[1];

			$('#HiddenQuote').val(j);
			$('#PolicyNumber').html(k);

			$('#DialogLapse').dialog('open');
		});
	}
	if ($('#DialogLapseCustomer').length)
	{
		$('#DialogLapseCustomer').dialog({
			bgiframe: true, autoOpen: false, modal: true, resizable: false,
			buttons: {
				'Yes': function(){
					window.location = SITEPATH + 'index.php?a=Customer&b=Index&c=Lapse&QuoteID=' + $('#HiddenQuote').val();
				},
				'No': function(){
					$(this).dialog('close');
				}
			}
		});


		$('.linkLapse').click(function(){
			parts = $(this).attr('rel').split('~');
			j = parts[0];
			k = parts[1];

			$('#HiddenQuote').val(j);
			//$('#PolicyNumber').html(k);

			$('#DialogLapseCustomer').dialog('open');
		});
	}

});

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;
}

