$(document).ready(function(){
	$( ".datepicker" ).datepicker({ dateFormat: 'yy-mm-dd', maxDate: '+0d' });
	
	$('select#country').change(function() {
		//Clear any errors
		$("#country").removeClass('error');	
		$.post("ajax_get_dealers.php", { region: $("select#country").val() },
			function(xml){
				if($("status",xml).text() != "0") {
					//Sucessful response from server, so put the names into the dealer select DDL
					var options = '<option value=\"\" selected>Please select</option>';
					$("dealer",xml).each(function(id) { 
						dealer = $("dealer",xml).get(id);  
						//alert($("name",dealer).text());
						options += '<option value=\"'+$("name",dealer).text()+'\">'+$("name",dealer).text()+', '+$("city",dealer).text()+'</option>\n';
					 });
					$("#dealer").removeClass('error');						 
					$('select#dealer').html(options);
				}
		});	
	});
	
	//Add clause in case the page is refreshed without changing the country. Need to display the correct dealers.
	if ($('select#country').val() != "Please select") {
		$.post("ajax_get_dealers.php", { region: $("select#country").val() },
			function(xml){
				if($("status",xml).text() != "0") {
					//Sucessful response from server, so put the names into the dealer select DDL
					var options = '<option value=\"\" selected>Please select</option>';
					$("dealer",xml).each(function(id) { 
						dealer = $("dealer",xml).get(id);  
						//alert($("name",dealer).text());
						options += '<option value=\"'+$("name",dealer).text()+'\">'+$("name",dealer).text()+', '+$("city",dealer).text()+'</option>\n';
					 });
					$('select#dealer').html(options);
				}
		});		
	}	
		
});

//AJAX for catching form submission
$(function() {
	$(".bt_login").unbind("click").click(function() {
		//Reset the error messages and classes
		$('div.error').html("");
		$("#first-name").removeClass('error');
		$("#last-name").removeClass('error');	
		$("#address").removeClass('error');
		$("#city").removeClass('error');
		$("#state").removeClass('error');
		$("#postcode").removeClass('error');
		$("#all-countries").removeClass('error');
		$("#phone-number").removeClass('error');	
		$("#email").removeClass('error');
		$("#confirm-email").removeClass('error');
		$("#country").removeClass('error');
		$("#dealer").removeClass('error');
		$("#purchase-date").removeClass('error');
		$("#serial1").removeClass('error');
		$("#serial2").removeClass('error');
		$("#terms").removeClass('error');
		fail = 0;
					
		//Check for required values
		if($("#first-name").val() == "") {
			fail = 1;
			$("#first-name").addClass('error');
		}

		if($("#address").val() === "") {
			fail = 1;
			$("#address").addClass('error');
		}
		
		if($("#city").val() === "") {
		  fail = 1;
		  $("#city").addClass('error');
		}
		
		if($("#postcode").val() === "") {
		  fail = 1;
		  $("#postcode").addClass('error');
		}
		
		if($("#all-countries").val() == "Please select") {
		  fail = 1;
		  $("#all-countries").addClass('error');
		}
		
		if($("#phone-number").val() === "") {
		  fail = 1;
		  $("#phone-number").addClass('error');
		}
		
		if($("#last-name").val() === "") {
		  fail = 1;
		  $("#last-name").addClass('error');
		}

			if($("#email").val() === "") {
				fail = 1;
				$("#email").addClass('error');
			}
			
			if($("#country").val() == "Please select") {
				fail = 1;
				$("#country").addClass('error');
			}

			if(($("#dealer").val() == "Please select a country first")||($("#dealer").val() === "")) {
				fail = 1;
				$("#dealer").addClass('error');
			}

			if($("#purchase-date").val() === "") {
				fail = 1;
				$("#purchase-date").addClass('error');
			}

			if($("#serial1").val() === "") {
				fail = 1;
				$("#serial1").addClass('error');
			}
			
			if(($("#serial2").val() === "")&&($("#model").val() == "opal")) {
				fail = 1;
				$("#serial2").addClass('error');
			}		

			if(fail == 1) {
				//Something is blank, print the error and quit
				$('div.error').html("<p>Error: Please ensure you complete every field.<br />Unable to complete your registration? <a href=\"contact.php?contact=website\">Get some help</p>");
				return;
			}
			
			if($("#address").val() === "") {
				fail = 1;
				$("#address").addClass('error');
				$('div.error').html("<p>Error: Address and phone number are required for fulfillment of StudioEQ delivery. <br />Unable to complete your registration? <a href=\"contact.php?contact=website\">Get some help</p>");
				return;
			}
			
			//Terms & Conditions checkbox must be checked
			if ($("#terms").is(':checked')) {
			
			} else {
				fail = 1;
				$("#terms").addClass('error');
				$('div.error').html("<p>Error: You must read and accept the terms &amp; conditions of warranty to proceed. <br />Unable to complete your registration? <a href=\"contact.php?contact=website\">Get some help</p>");
				return;
			 }			
			
			 
			//Check emails match
			if ($("#email").val() !=  $("#confirm-email").val()) {
				fail = 1;
				$("#email").addClass('error');
				$("#confirm-email").addClass('error');
				$('div.error').html("<p>Error: Please ensure you enter and confirm a valid email address. <br />Unable to complete your registration? <a href=\"contact.php?contact=website\">Get some help</p>");
				return;
			}
			
			if (fail === 0) {
				// disable the login button and change its value
				$('.bt_login').attr('disabled', 'disabled');
				$('.bt_login').val('Registering...');
			//alert('First Name: '+$("#first-name").val()+'\nLast Name: '+$("#last-name").val()+'\nEmail: '+$("#email").val()+'\nConfirm Email: '+$("#confirm-email").val()+'\nCountry: '+$("#country").val()+'\nDealer: '+$("#dealer").val()+'\nMethod: '+$("#method").val()+'\nModel: '+$("#model").val()+'\nPurchase Date: '+$("#purchase-date").val()+'\nSerial 1: '+$("#serial1").val()+'\nSerial 2: '+$("#serial2").val()+'\nSubscribe: '+$("#subscribe").is(':checked'));
			//$('#error').html("");
				$.post("ajax_register_speakers.php", { 
					firstname : $("#first-name").val(), 
					lastname : $("#last-name").val(),  
					address : $("#address").val(), 
					city : $("#city").val(), 
					state : $("#state").val(), 
					postcode : $("#postcode").val(), 
					homecountry : $("#all-countries").val(), 
					phone : $("#phone-number").val(),
					email : $("#email").val(), 
					country : $("#country").val(), 
					dealer : $("#dealer").val(), 
					method : $("#method").val(), 
					model : $("#model").val(), 
					purchasedate : $("#purchase-date").val(), 
					serial1 : $("#serial1").val(), 
					serial2 : $("#serial2").val(), 
					subscribe : $("#subscribe").is(':checked') },
					function(xml){
						if($("status",xml).text() != "1") {
							$('.bt_login').removeAttr('disabled');
							$('.bt_login').val('Register');
							$('div.error').html("<p>Error: "+$("error",xml).text()+"</p>");
							if($("status",xml).text() == "2") {
								$('.bt_login').removeAttr('disabled');
								$('.bt_login').val('Register');
								//Duplicate serial number or invalid format
								$("#serial1").addClass('error');
								$("#serial2").addClass('error');
							}
							if($("status",xml).text() == "3") {
								$('.bt_login').removeAttr('disabled');
								$('.bt_login').val('Register');
								//Change class if address is PO Box.
								$("#address").addClass('error');
							}
							return;
						} else {
							//Registered successfully
							message = "<p>Thank you for registering. You should receive an email confirmation shortly.</p><p>If you don't receive an email from Event please check spam and junk mail filters. If you are still experiencing issues please <a href=\"contact.php\">contact us here</a>.</p>";
							$('div#panel').html("<div class=\"content clearfix\"><div><h1>Register your speakers for warranty here.</h1></div><div>"+message+"</div></div>");
							
							
						}			
				});
			}
			
		});
});


$('.terms-conditions').popupWindow({ 
	height:500, 
	width:800, 
	top:50, 
	left:50,
	scrollbars: 1
}); 

