// JavaScript Document
jQuery(function($){ 
	$(document).mousemove(function(e){
		if ($('#float_col2').is(':hidden')) {
			$('#float_col2').css('left',e.pageX-150);
			$('#float_col2').css('top',e.pageY);
		}
	}); 
	
	$('.icon_send').click(function(e){
		if ($('#float_col2').is(':hidden')) { $('#float_col2').fadeIn('slow'); } else { $('#float_col2').fadeOut('fast'); 
			setTimeout("jQuery('#float_col').fadeIn('slow');jQuery('#float_col2').css('top',"+e.pageY+"); jQuery('#float_col2').css('left',"+(e.pageX-150)+");",600); 
		}
		return false;
	});
	
	$(".closeme2").click(function(){
		if (!$('#float_col2').is(':hidden')) { 
			$('#float_col2').fadeOut('fast'); 
		}
		return false;
	});
	
	// ===== validation =====
	var errors = [];
	var borderColor = $('#your_email').css('borderColor');
	var STD_ERROR_PREFIX = "There were one or more problems with the form values you entered.\nPlease check the following and try submitting the form again:\n====================================\n";
	
	
	function isValidEmailAddress( strValue ) {
	/************************************************
	REMARKS: Accounts for email with country appended
	  does not validate that email contains valid URL
	  type (.com, .gov, etc.) or valid country suffix.
	*************************************************/
	var objRegExp  = /(^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$)/i;
	  //check for valid email
	  return objRegExp.test(strValue);
	}
	
	$("#submit_friend").click(function(){
		if (!$('#float_col2').is(':hidden')) {
			errors = []; // reset
			$('#preload').removeClass('hide').fadeIn('fast');
			var dataString = 'fromEmail='+ encodeURIComponent($('#your_email').attr('value')) + '&toEmail=' + encodeURIComponent($('#your_friends_email').attr('value')) + '&notes=' + encodeURIComponent($('#notes').val()) + '&page=' + encodeURIComponent(window.location.href);
			$.ajax({
				type: "POST",
				url: "submit_SendToFriend.aspx",
				data: dataString,
				success: function(msg) {
					$('#preload').fadeOut('fast').toggleClass('hide');
					if(msg == "Success"){
						var newsletterRedirect = ($('#interest').attr('checked') === true)? true : false;
						// == reset fields ==
						$('#your_email').attr('value', '');
						$('#your_friends_email').attr('value','');
						$('#notes').val('');
						$('#interest').attr('checked',false);
						// == close ==
						$('#float_col2').fadeOut('fast'); 
						if(newsletterRedirect){
							window.location.href = "/my_racetrack_signup.aspx";
						};
					} else {
						if (!isValidEmailAddress($('#your_email').attr('value'))){
							errors.push("\t - Your Email Address");
							$('#your_email').css('border','1px solid #F00');
						} else {
							$('#your_email').css('border','1px solid #'+borderColor);
						}
						if (!isValidEmailAddress($('#your_friends_email').attr('value'))){
							errors.push("\t - Your Friend's Email Address");
							$('#your_friends_email').css('border','1px solid #F00');
						} else {
							$('#your_friends_email').css('border','1px solid #'+borderColor);
						}
						if (errors.length > 0) {
							alert(STD_ERROR_PREFIX + errors.join("\n"))
							return false;
						}
					};
				}
			});
		}
		return false;
	});
});