/* ########################################################################### *
/* ***** DOCUMENT INFO  ****************************************************** *
/* ########################################################################### *
 * ##### NAME:  email.js
 * ##### VERSION: v1.0
 * ##### UPDATED: 3 October 2008 (Damian Keeghan, Eclipse Group)
/* ########################################################################### *

/* ########################################################################### *
/* ***** INDEX *************************************************************** *
/* ########################################################################### *
/* ##### GLOBAL VARIABLES
/* ##### EMAIL FORM
/* ########################################################################### */

/* ########################################################################### *
/* ##### GLOBAL VARIABLES
/* ########################################################################### */

//test mode for email form, set to true for local test, false for server
var ecl_staticTestMode = false;

/* ########################################################################### *
/* ##### EMAIL FORM
/* ########################################################################### */

function ecl_emailToAFriend()
{
	if($(".oneColLayout2").find("a#emailCancel").length>0){
		pageTracker._trackPageview(SWFAddress.getPath()+"/email");
		
		$(".oneColLayout2 a#emailCancel").click(function(){
			hideProductEmailOverlay();
			return false;
		});
	}
	
	if($(".oneColLayout3").find("a#emailCancel").length>0){
		pageTracker._trackPageview(SWFAddress.getPath()+"/email");
		
		$(".oneColLayout3 a#emailCancel").click(function(){
			hideProductEmailOverlay();
			return false;
		});
	}
	
	if($("#emailSubmit").length>0){
		var name = ($.cookie("cr_prepopulation_name_cookie")!=null) ? $.cookie("cr_prepopulation_name_cookie") : "";
		var email = ($.cookie("cr_prepopulation_email_cookie")!=null) ? $.cookie("cr_prepopulation_email_cookie") : "";
		var toname = ($.cookie("cr_prepopulation_toname_cookie")!=null) ? $.cookie("cr_prepopulation_toname_cookie") : "";
		var toemail = ($.cookie("cr_prepopulation_toemail_cookie")!=null) ? $.cookie("cr_prepopulation_toemail_cookie") : "";
		
		$("#yourname").val(name);
		$("#youremail").val(email);
		
		$("#friendname").val(toname);
		$("#toemail").val(toemail);
		
		$("#emailSubmit").click(function(){
			$('#emailPage #errorPanel').remove();
			$('#emailPage .error').removeClass('error');
			$("#emailPage .error").hide();
			
			// Set position static so that form fields animate in IE
			$("#emailPage").find(".ctrlHolder").css("position","static");
			$("#emailPage").find(".buttonHolder").css("position","static");
			
			var hasError = false;
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			var errorMessages = "";
			
			var fromNameVal = $("#yourname").val();
			if(fromNameVal == '') {
				errorMessages += '<li>Your name is required.</li>';
				$("#yourname").parents(".ctrlHolder").addClass("error");
				hasError = true;
			}
			
			var emailFromVal = $("#youremail").val();
			if(emailFromVal == '') {
				errorMessages += '<li>Your email address is required.</li>';
				$("#youremail").parents(".ctrlHolder").addClass("error");
				hasError = true;
			} else if(!emailReg.test(emailFromVal)) {	
				errorMessages += '<li>Please review your email address as it appears to be in an incorrect format.</li>';
				$("#youremail").parents(".ctrlHolder").addClass("error");
				hasError = true;
			}
			
			var toNameVal = $("#friendname").val();
			if(toNameVal == '') {
				errorMessages += '<li>Friend\'s name is required.</li>';
				$("#friendname").parents(".ctrlHolder").addClass("error");
				hasError = true;
			}
					
			var emailToVal = $("#toemail").val();
			if(emailToVal == '') {
				errorMessages += '<li>Friend\'s email address is required.</li>';
				$("#toemail").parents(".ctrlHolder").addClass("error");
				hasError = true;
			} else if(!emailReg.test(emailToVal)) {	
				errorMessages += '<li>Please review the recipient\'s email address as it appears to be in an incorrect format.</li>';
				$("#toemail").parents(".ctrlHolder").addClass("error");
				hasError = true;
			}		
							
			var messageVal = $("#emailMessage").val();
			if(messageVal == '') {
				/*errorMessages += '<li>A message is required.</li>';
				$("#emailMessage").parents(".ctrlHolder").addClass("error");
				hasError = true;*/
				messageVal = " ";
			}
		
			if(messageVal.length > 1000) {
				errorMessages += '<li>The message cannot be longer than 1000 characters.</li>';
				$("#emailMessage").parents(".ctrlHolder").addClass("error");
				hasError = true;
			}
			
			var messageType = "promo";
			if(currentThumbnail!=null){
				messageType="keylook";	
			}
			
			if(hasError == false) {
				//do cookie
				$.cookie("cr_prepopulation_name_cookie", $("#yourname").val(), { path: '/', expires: 999 });	
				$.cookie("cr_prepopulation_email_cookie", $("#youremail").val(), { path: '/', expires: 999 });
				$.cookie("cr_prepopulation_toname_cookie", $("#friendname").val(), { path: '/', expires: 999 });	
				$.cookie("cr_prepopulation_toemail_cookie", $("#toemail").val(), { path: '/', expires: 999 });
				
				//send email
				var title = $("title").html();
				var url 	= window.location.href;
				$("#emailPage input, #emailPage textarea").attr("disabled", "disabled");
				jQuery.browser.msie ? $("#emailPage .crForm").hide() : $("#emailPage .crForm").fadeOut(_speed);

				$('#emailPage').fadeOut(_speed, function(){
					$("#emailPage .crForm").hide();
					$('#emailPage').prepend("<p class='ecl_sendingEmailNow'>Sending now, please wait a moment...</p>");
					
					update_scrollpane();
					$('#emailPage').fadeIn(_speed);		
					// If in ecl_staticTestMode then do not attempt to send via server side script, instead execute dummy client side test
					if (ecl_staticTestMode)
					{
						ecl_ajaxUrl_emailToAFriend = $('#emailPage form').attr("action");
						
						alert('url: '+ecl_ajaxUrl_emailToAFriend+', friendname: '+toNameVal+', toEmail: '+emailToVal+', yourname '+fromNameVal+', youremail '+emailFromVal+', emailMessage '+messageVal+', pageTitle: '+title+', pageUrl: '+url+', type: '+messageType+', currentthumbnail: '+currentThumbnail);
						
						setTimeout ( ecl_emailToAFriend_success, _emailTimeout);
					} else {
						ecl_ajaxUrl_emailToAFriend = $('#emailPage form').attr("action");
						
						$.post(ecl_ajaxUrl_emailToAFriend, { 'friendname': toNameVal,'toEmail': emailToVal, 'yourname': fromNameVal, 'youremail': emailFromVal, 'emailMessage': messageVal, 'pageTitle':title, 'pageUrl':encodeURI(url), 'type':messageType, 'currentthumbnail':currentThumbnail }, function(data){
							if (data.indexOf("success") != -1)
							{
								ecl_emailToAFriend_success();	
							}
							else {
								ecl_emailToAFriend_fail();
							}
						});
					}
					
				});
			} else {
				// If errors exist show the feedback
				$('#emailPage div.errors').remove();
				$('#emailPage p.mandatory').before('<div class="errors" style="display:none"><p>Sorry, the following issues were found. Please correct the information before submitting the form again.</p><ul id="errorPanel" class="errors">' + errorMessages + '</ul></div>');
				$('#emailPage div.errors').fadeIn(_speed, function(){
					// Reset position relative so that helptext appears properly
					$("#emailPage").find(".ctrlHolder").css("position","relative");
					$("#emailPage").find(".buttonHolder").css("position","relative");
					
					update_scrollpane();													 
				});
				return false;
			}
			
			return false;
		});
	}
}

function ecl_emailToAFriend_success()
{
	pageTracker._trackPageview(SWFAddress.getPath()+"/email_sent");
	
	$('#emailPage .ecl_sendingEmailNow').remove();
	
	if (jQuery.browser.msie)
	{
		$("#emailPage").hide();
		$("#emailPage").prepend('<p class="success">Thank you, your email has been sent.</p>');
		$("#emailPage").show();
		setTimeout(function(){
			hideProductEmailOverlay();
		}, _emailTimeout);
	}
	else
	{
		$("#emailPage").fadeOut(_speed,function()
				{
					$("#emailPage").prepend('<p class="success">Thank you, your email has been sent.</p>');
					$("#emailPage").fadeIn(_speed,function()
						{
							setTimeout(function() 
								{
									hideProductEmailOverlay();
								}, _emailTimeout);
						});
				});
	}
}

function ecl_emailToAFriend_fail()
{
	$('#emailPage .ecl_sendingEmailNow').remove();
	
	if (jQuery.browser.msie)
	{
		$("#emailPage").hide();
		$("#emailPage").prepend('<p class="errors">Unable to send email. Please try again.</p>');
		$("#emailPage").show();
		setTimeout(function() 
			{
				$("#emailPage .errors").remove();
				$("#emailPage input, #emailPage textarea").removeAttr("disabled");
				$("#emailPage .crForm").show();
			}, _emailTimeout);
	}
	else
	{
		$("#emailPage").fadeOut(_speed,function()
				{
					$("#emailPage").prepend('<p class="errors">Unable to send email. Please try again.</p>');
					$("#emailPage").fadeIn(_speed,function()
						{
							setTimeout(function() 
								{
									$("#emailPage .errors").remove();
									$("#emailPage input, #emailPage textarea").removeAttr("disabled");
									$("#emailPage .crForm").fadeIn(_speed);
								}, _emailTimeout);
						});
				});
	}
}

function hideProductEmailOverlay(){
	currentThumbnail = null;
	
	if($('#overlay').hasClass('oneColLayout2')){
		$("#overlay").fadeOut(_speed, function(){
			//remove styling
			$("#overlay").removeClass();
			//remove content from the overlay
			$("#overlay div:first").empty();
			
			getFlashMovieFromId("CountryRoad").fl_resetProductNavigation();
		});
		return false;
	} else if($('#overlay').hasClass('oneColLayout3')){
		loadOverlay_RightColumn(currentHref);
		
		setTimeout(function(){
			$('.overlayFooter').fadeIn(_speed);
		}, _speed);
		
		return false;
  	} else {
	 	return false; 
	}
}