passMark = {}

passMark.feedbackQuotes = [
	{
		quote: 'Contacted Mark after going nowhere with a previous instructor. Very flexible to accommodate lessons around my working hours. Professional, approachable, on time and full of handy tips. Would recommend Mark to anyone wanting to pass their driving test. Thanks Mate!!',
		person:	'Paul P from Bebington'
	},
	{
		quote: 'I passed with Passmark after 7 lessons, 1 a week was enough from Mark, he flew me through my test 1st time as I failed twice before with other waste of time instuctors.... cheers mate.',
		person:	'Marc D from Bebington'
	},
	{
		quote: 'Mark is a very professional driving instructor, I found it easy to learn to drive with his help and I passed my test first time. I would recommend Mark to any new learner.',
		person:	'Mike J from Leasowe'
	}
];

passMark.currentQuote = 0;

$(document).ready( function () {
	setInterval(passMark.changeQuote, 8000);

	// Setup showquotes: 
	setupQuotesHTML = '<div class="quotesContainer">';
	for (i in passMark.feedbackQuotes) {
	setupQuotesHTML += ''
		+ '<div class="quote" ' + (i != 0 ? 'style="display: none"' : '') + '>'
		+ passMark.quoteHTML(i)
		+ '</div>'	
		
	}
	setupQuotesHTML += '</div>'

	$('.showQuotes').html(setupQuotesHTML);
	
	
});

passMark.quoteHTML = function (i) {
	html = ''
		+ '<div class="quote-text">' 
		+ passMark.feedbackQuotes[i].quote 
		+ '</div>'
		+ '<div class="quote-person">' 
		+ passMark.feedbackQuotes[i].person 
		+ '</div>'
	return html;
}

passMark.changeQuote = function () {

	if ($('.quotesContainer .quote:visible').next().length) {
		next = $('.quotesContainer .quote:visible').next();
	} else {
		next = $('.quotesContainer .quote:first');
	}

	$('.quotesContainer .quote:visible').filter(':not(:animated)').fadeOut('slow');
	next.filter(':not(:animated)').fadeIn('slow');
	
	/*, function () {
		$(this).html(passMark.quoteHTML(passMark.currentQuote)).show();
		passMark.currentQuote = (passMark.currentQuote + 1)%passMark.feedbackQuotes.length;
		$('.quotesContainer .quote2').html(passMark.quoteHTML(passMark.currentQuote));
	})
		*/
}

passMark.prices = {}
passMark.prices['1'] 	= 20;
passMark.prices['1.5'] 	= 30;
passMark.prices['2'] 	= 40;

passMark.discounts = [
	{from: 1, to: 5, discount: 0.05, suggest: 5},
	{from: 6, to: 10, discount: 0.05, suggest: 10},
	
	{from: 11, to: 15, discount: 0.075, suggest: 20},
	{from: 16, to: 20, discount: 0.075, suggest: 25},
	
	{from: 21, to: 25, discount: 0.1, suggest: 30},
	{from: 26, to: 30, discount: 0.1, suggest: 35},	

	{from: 31, to: 35, discount: 0.15, suggest: 40},
	{from: 36, to: 100, discount: 0.15, suggest: 50}
]

passMark.quotePopup = {
	errors: null,
	init: function () {
		$('#contact_submit').click(function (e){ 
			e.preventDefault();
			passMark.quotePopup.showStep2();
		});
		$('#lesson_options_submit').click(function (e){ 
			e.preventDefault();
			passMark.quotePopup.showStep3();
		});
		$('#quote_submit').click(function (e){ 
			e.preventDefault();
			passMark.quotePopup.showStep4();
		});
		$('#back_to_2').click(function (e){ 
			e.preventDefault();
			passMark.quotePopup.showStep2();
		});
		$('#back_to_1').click(function (e){ 
			e.preventDefault();
			passMark.quotePopup.showStep1();
		});
		$('#close_wizard').click(function (e){ 
			passMark.quotePopup.hide();
		});
		
		$('#quote_form').submit(function (e) {
			e.preventDefault();
		});
		
		$('#error_sending').hide();

		$('#step2_content').hide();
		$('#step3_content').hide();
		$('#step4_content').hide();
	},
	
	show: function () {
		$('#overlay').fadeIn();
		$('#quotePopupWrapper').fadeIn();
	},
	
	hide: function () {
		$('#overlay').fadeOut();
		$('#quotePopupWrapper').fadeOut();
	},
	
	showStep1: function () {
		$('.step_panel:visible').fadeOut('fast', function () {
			$('#step1_content').fadeIn();
		});
	},
	
	showStep2: function () {
		passMark.quotePopup.errors = [];
		mandatoryError = false;
		$('#step1_content .mandatory').each(function () {
			if (!$(this).val() || $(this).val() == '') {
				mandatoryError = true;
			}
		});

		if (mandatoryError)
		{
			passMark.quotePopup.errors.push('Please fill-in all the fields marked with *');
		}
	
		if (!$('#step1_content input[name="contact[postal_code]"]').val().match(/^[A-Za-z0-9]{3,4} [A-Za-z0-9]{3}$/))
		{
			passMark.quotePopup.errors.push("Please specify your postal code in the appropriate format. E.g.: CH41 8TH");
		}

		if (!$('#step1_content input[name="contact[phone]"]').val().match(/^[0-9\- ]+$/))
		{
			passMark.quotePopup.errors.push("Please check the phone number you provided.");
		}

		if (!$('#step1_content input[name="contact[mobile]"]').val().match(/^[0-9\- ]+$/))
		{
			passMark.quotePopup.errors.push("Please check the mobile number you provided.");
		}

		if (!$('#step1_content input[name="contact[email]"]').val().match(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/))
		{
			passMark.quotePopup.errors.push("Please check the email you provided.");
		}


		if (passMark.quotePopup.errors.length) {
			alert(passMark.quotePopup.errors.join("\n"));
			return;
		}
	
		$('.step_panel:visible').fadeOut('fast', function () {
			$('#step2_content').fadeIn();
		});
	},
	
	showStep3: function () {
		number_of_lessons = parseFloat($('#number_of_lessons').val(), 10);
		hours_per_lesson = parseFloat($('#hours_per_lesson').val(), 10);
		
		$('.selected_number_of_lessons').html(number_of_lessons);
		$('.selected_hours_per_lesson').html(hours_per_lesson);

		standard_price_per_hour = passMark.prices[hours_per_lesson];
		number_of_hours = parseFloat(hours_per_lesson) * number_of_lessons;
		discount_applicable = 1;
		for (i in passMark.discounts) {
			d = passMark.discounts[i];
			if (d.from <= number_of_hours && d.to >= number_of_hours) {
				discount_applicable = 1 - d.discount;
				break;
			}
		}
		
		standard_quote = number_of_lessons * standard_price_per_hour;
		$('#standard_quote').html(standard_quote);
		$('#standard_lesson_rate').html(standard_price_per_hour);

		if (discount_applicable != 1) {
			$('#your_discount_panel').show();
			$('#discount_quote').html(standard_quote * discount_applicable);
			$('#discount_lesson_rate').html(standard_price_per_hour * discount_applicable);
			$('#discount_you_save').html(standard_quote - standard_quote * discount_applicable);
		} else {
			$('#your_discount_panel').hide();
		}
		
		$('.upSell').hide();
		for (i in passMark.discounts) {
			d = passMark.discounts[i];
			if (d.from > number_of_hours) {
				suggested_lessons = Math.floor(d.suggest / parseFloat(hours_per_lesson));

				$('#value_for_suggested').html(suggested_lessons * (1-d.discount) * standard_price_per_hour);
				$('#you_save_suggested').html(suggested_lessons * (d.discount) * standard_price_per_hour);
				$('#suggested_lessons').html(suggested_lessons);
				$('#suggested_lessons_radio').val(suggested_lessons);
				$('.upSell').show();
				break;
			}
		}

		$('.step_panel:visible').fadeOut('fast', function () {
			$('#step3_content').fadeIn();
		});

		
	},
	
	showStep4: function () {
		
		$('#error_sending').hide();
		ch = $('#suggested_lessons_radio').is(':checked');
		number_of_lessons = (ch ? parseInt($('#suggested_lessons_radio').val(), 10) : parseFloat($('#number_of_lessons').val(), 10));
		total = (ch ? $('#value_for_suggested').html() : $('#discount_quote').html());
		
		$('#quote_given').val(number_of_lessons + ' lessons x ' + $('#hours_per_lesson').val() + ' hours = &pound;' + total);
		
		$('#waiting').show();
		$('#thank_you').hide();
		$('.step_panel:visible').fadeOut('fast', function () {
			$('#step4_content').fadeIn();
			
			$.post('submit_quote.php', $('#quote_form').serialize(), function (resp) {
					if (!resp.status) {
						$('#error_sending').show();
						passMark.quotePopup.showStep3();
						return;
					}
					$('#waiting').hide();
					$('#thank_you').show();
			}, 'json');
		});
	}
	
}

passMark.onSubmitInlineContact = function (e) {
	e.preventDefault();
	$('#inline_contact_form .form_errors').html('').slideUp();
	$('#inline_contact_form .form_errors').removeClass('form_errors_none');
	$('#inline_contact_form .ajax_loader').show();
	$.post('inline_contact_form.php?method=ajax', $('#inline_contact_form').serialize(), function (res) {
		if (res.status == 'ok') {
			$('#inline_contact_form .form_errors').addClass('form_errors_none').html('Thank you. We will get back to you shortly.').slideDown();
		} else {
			errors = [];
			for (i in res.errors) {
				errors.push(res.errors[i])
			}
			$('#inline_contact_form .form_errors').html(errors.join('<br />')).slideDown();
		}
		$('#inline_contact_form .ajax_loader').hide();
	}, 'json');
}

$(document).ready(function () {
	passMark.quotePopup.init();
	$('#inline_contact_form').submit(passMark.onSubmitInlineContact);
});
