// JavaScript Document

$(function() {
	
	$(document).ready(function() {
    	$("#buy").validate({
			rules: {
    			email_address: {
      				email: true
    			},
    			quantity: {
    				number: true
    			}
    			
  			}
		});
		
		
		$("#newsletter").validate({		
			rules: {
    			email: {
      				email: true
    			}
  			}
		 });
		$("#trial").validate({		
			rules: {
    			email: {
      				email: true
    			}
  			}
		 });
		 
		 
		$("#buy_step_one").click(function() {
			$.post('/ajax/cupon/check_discount_code.php', $("#buy").serialize(), function(xml) {
				var product_type_id = $("product_type_id",xml).text();
				switch(product_type_id) {
					//Standard
					case '1':
/* 						$("#product_type").html('<label class="block">Sessions are &pound;'+ $("session_price",xml).text() +' each<br />Please enter the number of sessions you require:</label><input type="text" class="text required" value="" name="quantity" id="quantity" />'); */
						$("#product_type").html('<p>1-4 Sessions - &pound;20 per session*<br />5 Sessions or more - &pound;18 per session*<br />15 Sessions or more - &pound;15 per session*</p><label class="block">Please enter the number of sessions you require:</label><input type="text" class="text required" value="" name="quantity" autocomplete="off" id="quantity" />');

						$("#session_price").val($("session_price",xml).text());
						$("#total_price").val("");
						break;
					case '2':
					//Fixed Number
						$("#product_type").html('<label class="block">You are entitled to '+ $("quantity",xml).text() +' sessions* at a total price of &pound;'+ $("flat_rate_price",xml).text() +'</label><input type="hidden" value="'+ $("quantity",xml).text() +'" name="quantity" id="quantity" />');						
						$("#total_price").val($("flat_rate_price",xml).text());
						break;
					case '3':
					//Unlimited Discount
						$("#product_type").html('<label class="block">Sessions* are &pound;'+ $("session_price",xml).text() +' each<br />Please enter the number of sessions you require:</label><input type="text" class="text required" value="" name="quantity" id="quantity" />');						
						$("#session_price").val($("session_price",xml).text());
						$("#total_price").val("");
						break;
				}
				$("#product_type label").append('<br /><small>*Each session lasts for one hour</small>');
				$("#product_type_id").val(product_type_id);
				$("#total_price_display").empty();
				
				$("#quantity").keyup(function() {
					if (product_type_id == 1) {
						if ($(this).val() >= 1 && $(this).val() < 5) {
							$("#session_price").val('20');
						} else if ($(this).val() >= 5 && $(this).val() < 15) {
							$("#session_price").val('18');
						} else if ($(this).val() >= 15) {
							$("#session_price").val('15');
						}
					}
					$("#total_price").val($(this).val() * $("#session_price").val());
					if (isNaN($("#total_price").val())) {
						$("#total_price_display").html('Please enter whole numbers only');
					} else {
						$("#total_price_display").html('Total Price: ' + $().number_format($("#total_price").val(),{numberOfDecimals:2,decimalSeparator:'.',thousandSeparator:',',symbol:'&pound;'}));
					}					
		
				});

				$("#discount_code_container").hide();
				$("#step_two").show();
				
			});
		});
		
		$("#back_to_step_one").click(function() {
			$("#discount_code_container").show();
			$("#step_two").hide();	
		});
		

		
		 	
  	});
	

});

