// product_registration.js

function moveOnMax(field,nextFieldID){
  if(field.value.length >= field.maxLength){
    document.getElementById(nextFieldID).focus();
  }
}

function checkProductSerial() {
	
	var noSerial = $('#noserial_input').attr('checked');
	
	$('#serial_error').remove();
	
	var serialNotRecognised = '<div id="serial_error" class="error"><p><strong>Your product serial number cannot be recognised, please check it again.</strong></p><p>If you think this is a mistake, or that your product might be counterfeit, please <a href="/uk/customer-care/contact-us">contact us</a> immediately.</p></div>';
	
/*	$('#new_form').append('<input type="hidden" name="serialExists" id="serialExists" value="timeout" />');
	
	$.ajax({
		type: "POST",
		url: "/ajax/product-validation.php",
		data: "part1=" + $('#serial_part1_input').val() + "&part2=" + $('#serial_part2_input').val() + "&part3=" + $('#serial_part3_input').val(),
		success: function(msg){
			$('input#serialExists').val( msg.replace(/^\s+|\s+$/g, '') );
		},
		error: function(msg) {
			$('input#serialExists').val( msg.replace(/^\s+|\s+$/g, '') );
		}
	}); */

}
	
function sendAllData() {

	$('#content input, #content textarea, #content select').each(function(i) {
		var value = $(this).val();
		var name = $(this).attr('name');
		
		if( name != 'submit' ) {
			if( $(this).attr('type') == 'radio' && !$(this).attr('checked') ) {
				name = 'ignore';
			}
			if( $(this).attr('type') == 'checkbox' ) {
				value = $(this).attr('checked') ? 'on' : 'off';
			}
			if( name != 'ignore' ) {
				$('#new_form').append('<input type="hidden" name="' + name + '" value="' + value + '" />');
			}
		}
	});
	
	$('#new_form').submit();
	
}

function checkAge() {
	// get today's date vars
	var thedate = new Date();
	var day = thedate.getDate();
	var month = thedate.getMonth()+1;
	var year = thedate.getFullYear();
	// add preceding zero
	if (day < 10) {
		day = "0"+day;	
	}
	if (month < 10) {
		month = "0"+month;
	}
	var sixteenYears = year - 16;
	var fourteenYears = year - 14;
	var sixteenYearsAgo = sixteenYears + "-" + month  + "-" + day;
	var fourteenYearsAgo = fourteenYears + "-" + month  + "-" + day;
	// get user data
	var dob_day = $("#dob_day").val();
	var dob_month = $("#dob_month").val();
	var dob_year = $("#dob_year").val();
	var userDOB = dob_year  + "-" + dob_month + "-" + dob_day;
	if (userDOB > sixteenYearsAgo && userDOB < fourteenYearsAgo) {
		$("#consent_checkbox").show();
	} else {
		$("#consent_checkbox").hide();
	}	
}

$(document).ready(function() {

	// append new form to end of page to store all data from 3 forms
	$('body').append('<form id="new_form" action="'+location.href+'" method="post" style="display:none;"></form>');

	$('#noserial_input').click(function() {
		if(	$('#noserial_input').attr('checked') == true ) {
			$('#serial_part1_input').attr('disabled', 'disabled');
			$('#serial_part2_input').attr('disabled', 'disabled');
			$('#serial_part3_input').attr('disabled', 'disabled');
		} else {
			$('#serial_part1_input').removeAttr('disabled');
			$('#serial_part2_input').removeAttr('disabled');
			$('#serial_part3_input').removeAttr('disabled');
		}
	});

	// hide steps 2 and 3 on load
	$("#step_2").hide();
	$("#step_3").hide();
	// hide select variables on step 2
	$("#consent_checkbox").hide();
	$("#dob_day").change(function() {
		checkAge()				   
	});
	$("#dob_month").change(function() {
		checkAge()				   
	});
	$("#dob_year").change(function() {
		checkAge()				   
	});
	// hide select variables on step 3
	$("#salon_details").hide();
	$("#please_specify").hide();
	$("#bought_from").change(function() {
		if ($(this).val() == 'In a Salon') {
			$("#salon_details").show();
		} else {
			$("#salon_details").hide();
		}
		if ($(this).val() == 'On another website' || $(this).val() == 'Other') {
			$("#please_specify").show();
		} else {
			$("#please_specify").hide();
		}
									  
	});
	// step 1
	$("#step_1_form").validate({
		rules: {
			serial_part1: {
				required: function(element) {
					if(!$("#noserial_input").is(":checked")) {
						return true;
					}
				},
				minlength: function(element) {
					if(!$("#noserial_input").is(":checked")) {
						return 5;
					}
				}
			},
			serial_part2: {
				required: function(element) {
					if(!$("#noserial_input").is(":checked")) {
						return true;
					}
				},
				minlength: function(element) {
					if(!$("#noserial_input").is(":checked")) {
						return 5;
					}
				}
			},
			serial_part3: {
				required: function(element) {
					if(!$("#noserial_input").is(":checked")) {
						return true;
					}
				},
				minlength: function(element) {
					if(!$("#noserial_input").is(":checked")) {
						return 5;
					}
				}
			}
			//,
			//batch_number: {
			//	required: true
			//}
		},
		messages: {
			serial_part1: "Venligst indtast de f&oslash;rste 5 cifre.",
			serial_part2: "Venligst indtast de n&aelig;ste 5 cifre.",
			serial_part3: "Venligst indtast de sidste 5 cifre.",
			batch_number: "Indtast dit batch-nummer"
		},
		errorElement: "em",
		errorContainer: $("#error_message"),
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("p").prev("p") );	
		},
		highlight: function(element, errorClass) {
			$(element).addClass('hasError').parent().parent("div").addClass(errorClass);
		},		
		unhighlight: function(element, errorClass) {
			$(element).removeClass('hasError');
			if( !$(element).siblings().hasClass('hasError') ) {
				$(element).parent().parent("div").removeClass(errorClass);
			}
		},		
		onfocusout: false,
		onkeyup: false,
		submitHandler: function() {
			checkProductSerial();
			$('#step_1').hide();
			$('#step_2').show();
		}
	})
	// step 2
	$("#step_2_submit").click(function() {
		// trim email address
		var str = $("input[@name='email_address']").val();
		str = $.trim(str);
		$("input[@name='email_address']").val(str);						  
	});
	$("#step_2_form").validate({
		rules: {
			title: {
				required: true
			},
			firstname: {
				required: true
			},
			lastname: {
				required: true
			},
			dob_day: "required",
			dob_month: "required",
			dob_year: "required",
			parental_consent: {
				required: function(element) {
					// get today's date vars
					var thedate = new Date();
					var day = thedate.getDate();
					var month = thedate.getMonth()+1;
					var year = thedate.getFullYear();
					// add preceding zero
					if (day < 10) {
						day = "0"+day;	
					}
					if (month < 10) {
						month = "0"+month;
					}
					var sixteenYears = year - 16;
					var fourteenYears = year - 14;
					var sixteenYearsAgo = sixteenYears + "-" + month  + "-" + day;
					var fourteenYearsAgo = fourteenYears + "-" + month  + "-" + day;
					// get user data
					var dob_day = $("#dob_day").val();
					var dob_month = $("#dob_month").val();
					var dob_year = $("#dob_year").val();
					var userDOB = dob_year  + "-" + dob_month + "-" + dob_day;
					if (userDOB > sixteenYearsAgo && userDOB < fourteenYearsAgo) {
						return true;
					} else {
						return false;
					}
				}
			},
			gender: "required",
			email_address: {
				required: true,
				email: true
			},
			telephone: {
				required: true,
				minlength: 3			
			},
			street_address: {
				required: true,
				minlength: 5			
			},
			/*City: {
				required: true,
				minlength: 3			
			},

			state: {
				required: true,			    
			},
			postcode: {
				required: true,
				minlength: 4
			},*/
			country: {
				required: true
			}
			
		},
		messages: {
			title: "V&aelig;lg venligst din titel.",
			firstname: "Skriv venligst dit fornavn.",
			lastname: "Skriv venligst dit efternavn.",
			dob_day: "V&aelig;lg venligst dag. ",
			dob_month: " V&aelig;lg venligst m&aring;ned.",
			dob_year: "V&aelig;lg venligst &aelig;r.",
			parental_consent: "`&aring; grund af din alder kr&aelig;ves godkendelse af en for&aelig;lder.",
			gender: "V&aelig;lg venligst k&oslash;n.",
			email_address: "Skriv venligst en gyldig email adresse.",
			telephone: "Dit telefonnummer skal indeholde mindst 8 tal. ",
			street_address: "Din adresse skal indeholde mindst 5 bogstaver.", 
			postcode: "Dit postnummer skal indeholde 4 tal.", 
			City: "Your city must contain a minimum of 3 characters.", 
			state: "Please select the State.",
			country: "Du skal v&aelig;lge det land hvor levering &oslash;nskes til under lande menuen."
		},
		errorElement: "em",
		errorContainer: $("#error_message"),
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("div").parent("div").children("p") );	
		},
		highlight: function(element, errorClass) {
			$(element).addClass('hasError').parent().parent("div").addClass(errorClass);
		},		
		unhighlight: function(element, errorClass) {
			$(element).removeClass('hasError');
			if( !$(element).siblings().hasClass('hasError') ) {
				$(element).parent().parent("div").removeClass(errorClass);
			}
		},
		onfocusout: false,
		onkeyup: false,
		submitHandler: function() { 
			$("#step_2").hide();
			$("#step_3").show();
		}
	})
	// step 3
	$("#step_3_form").validate({
		rules: {
			product_purchased: "required",
			purchase_day: "required",
			purchase_month: "required",
			purchase_year: "required",
			bought_from: "required",
			salon_name: {
				required: function(element) {
					if($("#bought_from").val() == 'In a Salon') {
						return true;
					} else {
						return false;
					}
				}/*,
				minlength: function(element) {
					if($("#bought_from").val() == 'In a Salon') {
						return 2;
					}
				}*/			
			},
			salon_address: {
				required: function(element) {
					if($("#bought_from").val() == 'In a Salon') {
						return true;
					} else {
						return false;
					}
				}/*,
				minlength: function(element) {
					if($("#bought_from").val() == 'In a Salon') {
						return 2;
					}
				}*/			
			},
			salon_country: {
				required: function(element) {
					if($("#bought_from").val() == 'In a Salon') {
						return true;
					} else {
						return false;
					}
				}
			},
			other: {
				required: function(element) {
					if($("#bought_from").val() == 'On another website') {
						return true;
					} else if ($("#bought_from").val() == 'Other') {
						return true;
					} else {
						return false;
					}
				}
			},
			problem: {
				required: true
			},
			tandc: {
				required: true
			}
			
		},
		messages: {
			purchase_day: "V&aelig;lg venligst dag.",
			purchase_month: "V&aelig;lg venligst m&aring;ned. ",
			purchase_year: " V&aelig;lg venligst &aring;r. ",
			bought_from: "V&aelig;lg venligst hvor du har k&oslash;bt dit produkt.",
			salon_name: "Skriv venligst salon navn.",
			salon_address: "Skriv venligst adressen til salonen.",
			salon_country: "V&aelig;lg venligst land.",
			other: "Indtast venligst hvor du har købt dit produkt.",
			tandc: "Du skal godkende vores betingelser og vilk&aring;r.",
			problem: "Beskriv venligst problemet."
		},
		errorElement: "em",
		errorContainer: $("#error_message"),
		errorPlacement: function(error, element) {
			error.appendTo( element.parent("div").parent("div").children("p") );	
		},
		highlight: function(element, errorClass) {
			$(element).addClass('hasError').parent().parent("div").addClass(errorClass);
		},		
		unhighlight: function(element, errorClass) {
			$(element).removeClass('hasError');
			if( !$(element).siblings().hasClass('hasError') ) {
				$(element).parent().parent("div").removeClass(errorClass);
			}
		},
		onfocusout: false,
		onkeyup: false,
		submitHandler: function() { sendAllData(); }
	})

});