// 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:'48px 0 50px 0'});
			$('#containerOverlayBtn').after('<a id="closeBtn">Schlie&szlig;en</a>');
			$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);
			
			$.getScript("../scripts/jquery.validate.js", function() {
																														
				$("#passwordFrm").validate({
					
					rules: {
						password: {
							required: true,
							passComplexity: true
						},
						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:'48px 0 50px 0'});
							$('.overlay p').after('<a id="closeBtn">Schlie&szlig;en</a>');
							$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);	
							
						}).fadeIn('slow');
						
						return false; 
						
					}
				
				});
				
				$("#contactFrm").validate({
					
					rules: {
						SALUTATION: "required",
						CHRNAME: "required",
						NAME: "required",
						STREET_NAME: "required",
						STREET_REMAINDER: "required",
						ZIPCODE: "required",
						CITY: "required",
                                                EMAIL: {
                                                        required: true,
                                                        email: true
                                                }
					},
					
					errorContainer: "#containerErrors",
					
					errorLabelContainer: "#containerErrors ul",
					
					errorElement: "li",
					
					messages: {
						SALUTATION: "- Anrede",
						CHRNAME: "- Vorname",
						NAME: "- Name",
						STREET_NAME: "- Stra&szlig;e",
						STREET_REMAINDER: "- Hausnummer",
						ZIPCODE: "- PLZ",
						CITY: "- Ort",
                                                EMAIL: {
                                                        required: "- E-Mail-Adresse",
                                                        email: "- Bitte geben Sie eine g&uuml;ltige E-Mail-Adresse an."
                                                }
					},
					
					submitHandler: function() {
						
						jQuery.post('change-contact-details.php', {
							'urn': $("#urn").attr("value"),
							'SALUTATION': $("#SALUTATION").attr("value"),
							'CHRNAME': $("#CHRNAME").attr("value"),
							'NAME': $("#NAME").attr("value"),
							'STREET_NAME': $("#STREET_NAME").attr("value"),
							'STREET_REMAINDER': $("#STREET_REMAINDER").attr("value"),
							'ZIPCODE': $("#ZIPCODE").attr("value"),
							'CITY': $("#CITY").attr("value"),
							'EMAIL': $("#EMAIL").attr("value"),
							'MOB_NUM': $("#MOB_NUM").attr("value")
						});
						$('#lightBox').html('');
                                                $('#lightBox').css({display:'none'}).load('change-contact-details-confirm.php .overlay', function() {

                                                        //$('.overlay').css({padding:'48px 0 50px 0'});
                                                        $('.overlay p').after('<a id="closeBtn">Schlie&szlig;en</a>');
                                                        $("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);

                                                }).fadeIn('slow');

                                                return false;		
					}
				
				});
				
				$("#subscriptionFrm").validate({
					
					submitHandler: function() {
										
						jQuery.post('change-email-subscriptions.php', {
							'PG_BRAND_OPTOUT': $("input[@name='PG_BRAND_OPTOUT']:checked").val(),
							'PG_CORPORATE_OPTOUT': ($("#PG_CORPORATE_OPTOUT").attr("checked") ? 'I' : 'O'),
							'PG_FORME_OPTOUT': ($("#PG_FORME_OPTOUT").attr("checked") ? 'I' : 'O')
						});
						
						$('#lightBox').html('');
						$('#lightBox').css({display:'none'}).load('change-email-subscriptions-confirm.php .overlay', function() {
							
							//$('.overlay').css({padding:'48px 0 50px 0'});
							$('.overlay p').after('<a id="closeBtn">Schlie&szlig;en</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:'48px 0 50px 0'});
			$('#containerOverlayBtn').after('<a id="closeBtn">Schlie&szlig;en</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: "- Name Ihres Freundes",
						friendemail: {
							required: "- E-Mail-Adresse Ihres Feundes",
							email: "- Bitte geben Sie eine g&uuml;ltige E-Mail-Adresse an."
						},
						verify: "- Bitte best&auml;tigen Sie, dass Ihr Freund einverstanden ist, eine E-Mail zu erhalten."
					},
					
					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:'48px 0 50px 0'});
							$('#containerOverlayBtn').after('<a id="closeBtn">Schlie&szlig;en</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:'48px 0 50px 0'});
											$('.overlay p').after('<a id="closeBtn">Schlie&szlig;en</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">Schlie&szlig;en</a>');
			$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);
			
			$.getScript("../scripts/jquery.validate.js", function() {
																														
				$("#forgotPasswordFrm").validate({
					
					rules: {
						passwordEmail: {
							required: true,
							email: true
						},
						security_question: {
							required: true,
						},
						security_answer: {
							required: true,
						},
						security_question2: {
							required: true,
						},
						security_answer2: {
							required: true,
						},
					},
					
					errorContainer: "#containerErrors",
					
					errorLabelContainer: "#containerErrors ul",
					
					errorElement: "li",
					
					messages: {
						passwordEmail: {
							required: "",
							email: "- Bitte geben Sie eine g&uuml;ltige E-Mail-Adresse an."
						},
						security_question: {
							required: "",
							email: "- Bitte w&auml;hlen Sie die erste Sicherheitsfrage aus."
						},
						security_answer: {
							required: "",
							email: "- Bitte beantworten Sie die erste Sicherheitsfrage."
						},
						security_question2: {
							required: "",
							email: "- Bitte w&auml;hlen Sie die zweite Sicherheitsfrage aus."
						},
						security_answer2: {
							required: "",
							email: "- Bitte beantworten Sie die zweite Sicherheitsfrage."
						},
					},
					
					submitHandler: function() {
							
						jQuery.post('forgot-password.php', {
							'passwordEmail': $("#forgotPasswordFrm").find('input[name=passwordEmail]').val(),
							'security_question': $("#forgotPasswordFrm").find('select[name=security_question]').val(),
							'security_answer': $("#forgotPasswordFrm").find('input[name=security_answer]').val(),
							'security_question2': $("#forgotPasswordFrm").find('select[name=security_question2]').val(),
							'security_answer2': $("#forgotPasswordFrm").find('input[name=security_answer2]').val(),
							},
							function(data)
							{
								if (data.length > 3) {
									$('#lightBox').html('');
									$('#lightBox').css({display:'none'}).load('forgot-password-decline.php .overlay', function() {
																																																						 
										$('.overlay p').after('<a id="closeBtn">Schlie&szlig;en</a>');
										$("#closeBtn").css({cursor:'pointer'}).click(closeLightBox);	
										
									}).fadeIn('slow');
								} else {
									$('#lightBox').html('');
									$('#lightBox').css({display:'none'}).load('forgot-password-confirm.php .overlay', function() {
																																																						 
										$('.overlay p').after('<a id="closeBtn">Schlie&szlig;en</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;
		
	});
	
});


function fixedIE(tl, n, el){
var sc = 'scroll'+tl, d = document, c = 'compatMode', b = d[c]&&d[c]=='CSS1Compat'? d.documentElement : d.body;
n = n-0; if(typeof n!='number')return 0;
if(/^(Top|Left)$/.test(tl))
return b[sc]+n+'px';
if(/^(Bottom|Right)$/.test(tl)&&typeof el=='object'){
tl = tl=='Right'? 'Left' : 'Top', sc = 'scroll'+tl;
var dim = 'client' + (tl=='Top'? 'Height' : 'Width');
return b[sc]+b[dim]-el[dim]-n+'px';
}
return 0;
}

