// JavaScript Document for Club Olay pages.

$(document).ready(function(){
													 
	// Applying negative margins to legend element for IE
	if (navigator.userAgent.indexOf("MSIE") !=-1) {
		$("legend").css('margin-left', '-8px');
	}
	
	// Toggle more messages
	$('#containerMoreMsgs').css('display', 'none');
	$('#containerMoreMsgsLink h5').css('cursor', 'pointer').click(function() {
			
		$('#containerMoreMsgs').toggle();
		return false;
		
	});
	
	// Subsequent code generates a lightbox to display a confirmation page.
	if ($('div').hasClass('profile')) {
		
		$('#id').after(
			'<input type="hidden" name="js" value="true" />'				 
		);
		
		if ($('div').hasClass('confirm')) {
			
			$('#lightBox').load('confirm.php .overlay');
			$('#lightBoxBg').height($('#containerMain').height());
			$(document).scrollTop(0);
			$('#lightBoxBg').css('opacity', '0.85').fadeIn(1500);
			$('#lightBoxContainer').fadeIn('slow');
				
		};
		
	}
	
	// Subsequent code generates a lightbox to display an underage page.
	if ($('div').hasClass('register')) {
		
		$('#first_name').after(
			'<input type="hidden" name="js" value="true" />'				 
		);
		
		if ($('div').hasClass('underage')) {
			
			$('#lightBox').load('underage.php .overlay');
			$('#lightBoxBg').height($('#containerMain').height());
			$(document).scrollTop(0);
			$('#lightBoxBg').css('opacity', '0.85').fadeIn(1500);
			$('#lightBoxContainer').fadeIn('slow');
				
		};
		
	}
	
	// Subsequent code generates a lightbox to display various forms lauunched from the Club Olay home page.
	$('#leftColumnClubHome li a').click(function() {
	
		$('#quickLinks').css({visibility: 'hidden'});
		var url = this.href;
		
		$('#lightBox').html('');
		$('#lightBox').load(url + ' .overlay', function() {
			
			$('.overlay').css({padding:'108px 0 50px 0'});
			$('#containerOverlayBtn').after('<a id="closeBtn">Chiudi</a>');
			$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);
			
			$.getScript("../scripts/jquery.validate.js", function() {
																														
				$("#passwordFrm").validate({
					
					rules: {
						password: "required",
						passwordrepeat: {
							required: true,
							equalTo: "#password"
						}
					},
					
					errorContainer: "#containerErrors",
					
					messages: {
						password: "",
						passwordrepeat: ""
					},
					
					submitHandler: function() {
						
						jQuery.post('change-password.php', {
							'password': $("#password").attr("value"),
							'passwordrepeat': $("#password").attr("value")
						});
						
						$('#lightBox').html('');
						$('#lightBox').css({display:'none'}).load('change-password-confirm.php .overlay', function() {
							
							$('.overlay').css({padding:'108px 0 50px 0'});
							$('.overlay p').after('<a id="closeBtn">Chiudi</a>');
							$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);	
							
						}).fadeIn('slow');
						
						return false; 
						
					}
				
				});
				
				$("#contactFrm").validate({
					
					rules: {
						email: {
							required: true,
							email: true
						},
						phone_number: {
							number: true
						}
					},
					
					errorContainer: "#containerErrors",
					
					errorLabelContainer: "#containerErrors ul",
					
					errorElement: "li",
					
					messages: {
						email: {
							required: "- Email",
							email: "- Bitte geben Sie eine g&uuml;ltige E-Mail Adresse ein"
						},
						phone_number: "- Bitte geben Sie eine g&uuml;ltige Telefonnummer ein"
					},
					
					submitHandler: function() {
						
						jQuery.post('change-contact-details.php', {
							'email': $("#contactEmail").attr("value"),
							'address_1': $("#address_1").attr("value"),
							'city': $("#city").attr("value"),
							'county': $("#county").attr("value"),
							'zipcode': $("#zipcode").attr("value"),
							'country': $("#country").attr("value"),
							'phone_number': $("#phone_number").attr("value"),
							'json': 'true'
						}, function(data) {
							
							if (data != null && data.status == 'ok') {
								
								// Submitted okay
								$('#lightBox').html('');
								$('#lightBox').css({display:'none'}).load('change-contact-details-confirm.php .overlay', function() {
									
									$('.overlay').css({padding:'108px 0 50px 0'});
									$('.overlay p').after('<a id="closeBtn">Chiudi</a>');
									$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);	
									
								}).fadeIn('slow');
								
							}
							else if (data != null && data.status == 'email-already-registered') {
								
								// Submitted, but email is already in the DB.
								$('#containerErrors').css({display:'block'});
								$('#containerErrors h2').after('<ul><li>- Diese E-Mail Adresse ist bereits registriert</li></ul>');
								
							}
							else {
								
								// Some other error. Shouldn't happen.
								$('#containerErrors').css({display:'block'});
								$('#containerErrors h2').after('<ul><li>- Server response is not what was expected</li></ul>');
								
							}
							
						}, "json");
						
						return false; 
						
					}
				
				});
				
				$("#subscriptionFrm").validate({
					
					submitHandler: function() {
						
						jQuery.post('change-email-subscriptions.php', {
							'brand_optin': $("#brand_optin").attr("checked"),
							'research_optin': $("#research_optin").attr("checked"),
							'pg_optin': $("#pg_optin").attr("checked")
						});
						
						$('#lightBox').html('');
						$('#lightBox').css({display:'none'}).load('change-email-subscriptions-confirm.php .overlay', function() {
							
							$('.overlay').css({padding:'108px 0 50px 0'});
							$('.overlay p').after('<a id="closeBtn">Chiudi</a>');
							$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);	
							
						}).fadeIn('slow');
						
						return false; 
						
					}
				
				});
					
			});
			
		});
		$('#lightBoxBg').height($('#containerMain').height());
		$(document).scrollTop(0);
		$('#lightBoxBg').css('opacity', '0.85').fadeIn('slow');
		$('#lightBoxContainer').fadeIn('slow');
		
		return false;
			
	});
	
	$('#submitBtn .btnLink a').click(function() {
		
		$('#quickLinks').css({visibility: 'hidden'});
		var action = $("#tafFrm").attr("action");
		var n = $('#name').val();
		
		$('#lightBox').html('');
		$('#lightBox').load(action + ' .overlay', function() {
			
			$('.overlay').css({padding:'108px 0 50px 0'});
			$('#containerOverlayBtn').after('<a id="closeBtn">Chiudi</a>');
			$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);
			$('#friendname').val(n);
			
			$.getScript("../scripts/jquery.validate.js", function() {
																														
				$("#tafDetailsFrm").validate({
					
					rules: {
						friendname: "required",
						friendemail: {
							required: true,
							email: true
						},
						verify: "required"
					},
					
					errorContainer: "#containerErrors",
					
					errorLabelContainer: "#containerErrors ul",
					
					errorElement: "li",
					
					messages: {
						friendname: "- Friend&#39;s name",
						friendemail: {
							required: "- Friend&#39;s email",
							email: "- Bitte geben Sie eine g&uuml;ltige E-Mail Adresse ein"
						},
						verify: "- Please confirm your friend would be happy to receive an email."
					},
					
					submitHandler: function() {
						
						var n = $('#friendname').val();
						var e = $('#friendemail').val();
						
						$('#lightBox').html('');
						$('#lightBox').css({display:'none'}).load('spread-the-word-preview.php .overlay', function() {
							
							$('.overlay').css({padding:'108px 0 50px 0'});
							$('#containerOverlayBtn').after('<a id="closeBtn">Chiudi</a>');
							$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);
							$('#friendname').val(n);
							$('#friendemail').val(e);
							
							$.getScript("../scripts/jquery.validate.js", function() {
							
								$("#tafPreviewFrm").validate({
					
									submitHandler: function() {
										
										jQuery.post('spread-the-word-preview.code.php', {
											'friendname': $("#friendname").attr("value"),
											'friendemail': $("#friendemail").attr("value"),
											'verify': true
										});
										
										$('#lightBox').html('');
										$('#lightBox').css({display:'none'}).load('spread-the-word-confirm.php .overlay', function() {
											
											$('.overlay').css({padding:'108px 0 50px 0'});
											$('.overlay p').after('<a id="closeBtn">Chiudi</a>');
											$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);	
											
										}).fadeIn('slow');
										
										return false; 
										
									}
								
								});
							
							});
							
						}).fadeIn('slow');
						
						return false; 
						
					}
				
				});
					
			});
			
		});
		$('#lightBoxBg').height($('#containerMain').height());
		$(document).scrollTop(0);
		$('#lightBoxBg').css('opacity', '0.85').fadeIn('slow');
		$('#lightBoxContainer').fadeIn('slow');
		
		return false;
		
	});
	
	$('#forgotPassword').click(function() {
		
		$('#quickLinks').css({visibility: 'hidden'});
		var url = this.href;
		
		$('#lightBox').html('');
		$('#lightBox').load(url + ' .overlay', function() {
			
			$('#containerOverlayBtn').after('<a id="closeBtn">Chiudi</a>');
			$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);
			
			$.getScript("../scripts/jquery.validate.js", function() {
																														
				$("#forgotPasswordFrm").validate({
					
					rules: {
						email: {
							required: true,
							email: true
						}
					},
					
					errorContainer: "#containerErrors",
					
					errorLabelContainer: "#containerErrors ul",
					
					errorElement: "li",
					
					messages: {
						email: {
							required: "- Your email address",
							email: "- Please enter a valid email address"
						}
					},
					
					submitHandler: function() {
							
						jQuery.post('forgot-password.php', {
							'email': $("#passwordEmail").attr("value")
						});
						
						$('#lightBox').html('');
						$('#lightBox').css({display:'none'}).load('forgot-password-confirm.php .overlay', function() {
																																																			 
							$('.overlay p').after('<a id="closeBtn">Chiudi</a>');
							$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);	
							
						}).fadeIn('slow');
						
						return false; 
						
					}
				
				});
					
			});
			
		});
		$('#lightBoxBg').height($('#containerMain').height());
		$(document).scrollTop(0);
		$('#lightBoxBg').css('opacity', '0.85').fadeIn('slow');
		$('#lightBoxContainer').fadeIn('slow');
		
		return false;
		
	});
	
});
