function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}


$(document).ready(function() {
	
	//$("#menu").css("opacity", 0);
	$(".text").hide();
	$("#menu ul").hide();
	
	var _currentPos = $("#image").position();
	var _wrapperPos = $("#wrapper").position();
	var _bodyPos = $("body").position();
	_bodyPos = _bodyPos.left - _wrapperPos.left;
	_currentPos = _currentPos.left;
	
	$("#menu").hide();
	$("#image").hide();
	
	$("#image").css({
		height: "630px",
		position: "absolute",
		left: _bodyPos-85,
		top:0
	}).fadeIn().animate({
		left: _currentPos
	}, 1000, "swing", function(){
		//$("#menu").css("opacity", 100);
		$("#menu").fadeIn("slow");
		$(".text").fadeIn("slow");
		$(this).css({
			position: "",
			height: ""
		});
		var _height = $(this).height();
		if (_height < 630) {
			$(this).css("height", "630px");
		}
	});
	
	
	$("#menu p a").hover(function(){
		$(this).parent().animate({
			marginLeft: "+=10px"
		}, 300);
		//$("#menu ul").hide();
		//$(this).parent().next("ul").fadeIn();
	}, function(){
		$(this).parent().stop().animate({
			marginLeft: "0px"
		}, 300);
	});
	/*
	$("#menu ul a").hover(function(){
		$(this).parent().parent().stop().show();
	}, function() {
		$(this).parent().parent().animate({
			opacity: 1
		}, 1000, function(){
			$(this).fadeOut();
		});
	});
	
	$("#menu p#signUp a").click(function(){
		return false;
	});
	*/
	
	$(".fieldset").hide();
	$(".fieldset:first").show();
	
	var _count = 1;
	var _totalCount = 0;
	
	$("form .fieldset").each(function(){
		_totalCount ++;
	});
	
	$(".totalCount").html(_totalCount);
		
	function checkCount() {
				
		if(_count >= _totalCount) {
			$(".next").hide();
		} else {
			$(".next").show();
		}
		
		if(_count < 2) {
			$(".prev").hide();
		} else {
			$(".prev").show();
		}
		
		$(".currentCount").html(_count);
	}
	
	$(".prev").hide();
	$(".next a").click(function(){
		_count++;
		$(".fieldset:visible").fadeOut(500, function(){
			$("#fieldset_"+_count).fadeIn();
		});
		checkCount();
		return false;
	});
	$(".prev a").click(function(){
		_count--;
		$(".fieldset:visible").fadeOut(500, function(){
			$("#fieldset_"+_count).fadeIn();
		});
		checkCount();
		return false;
	});
	
	$(".close a").click(function(){
		$("#theForm").slideUp();
		$("#overlay").fadeOut();
		return false;
	});
	
	function panel_reset()
	{
		$(".fieldset:visible").fadeOut(500, function(){
			$("#fieldset_1").fadeIn();
		});
		
		_count = 1;
		checkCount();
	}
	
	
	
	$("#submitSmallform").submit(function(){
		
		if(document.getElementById('billing_first_name').value == "")
		{
			alert('Please fill out your first name field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('billing_last_name').value == "")
		{
			alert('Please fill out your last name field in order to continue.');
			panel_reset();
			return false;
		}
		
		if(document.getElementById('email_address').value == "")
		{
			alert('Please fill out your email address field in order to continue.');
			panel_reset();
			return false;
		}
		
		if(document.getElementById('email_address').value != "")
		{
			
			//alert(isValidEmailAddress(document.getElementById('email_address').value));
			
			if(!isValidEmailAddress(document.getElementById('email_address').value))
			{
				alert('Your Email Address seems to be invalid, please enter a valid email address.');
				panel_reset();
				return false;
			}
			
			
		}
		
		if(document.getElementById('email_address').value != document.getElementById('confirm_email_address').value)
		{
			alert('Please confirm your email address');
			panel_reset();
			return false;
		}
		
		$(this).attr("action","/handlers/send/sign_up_info.html");
		
		$(this).unbind("submit");
		
		$(this).submit(function(){
			return true;
			
		});
		
		this.submit();
		
		return false;
		
	});
	
	
	$("#submitform").submit(function(){
		
		//alert("submitting form...");
		
		
		if(document.getElementById('billing_first_name').value == "")
		{
			alert('Please fill out your billing first name field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('billing_last_name').value == "")
		{
			alert('Please fill out your billing last name field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('billing_street_address').value == "")
		{
			alert('Please fill out the billing address field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('billing_city').value == "")
		{
			alert('Please fill out the billing city field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('billing_state').value == "")
		{
			alert('Please fill out the billing state field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('billing_zip_code').value == "")
		{
			alert('Please fill out the billing zip code field in order to continue.');
			
			panel_reset();
			return false;
		}
		if(document.getElementById('billing_phone_number').value == "")
		{
			alert('Please fill out the billing phone number field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('email_address').value == "")
		{
			alert('Please fill out the billing email address field in order to continue.');
			panel_reset();
			return false;
		}

		if(document.getElementById('email_address').value != document.getElementById('confirm_email_address').value)
		{
			alert('Please confirm your email address');
			panel_reset();
			return false;
		} 
		
		if(document.getElementById('email_address').value != "")
		{
			
			
			if(!isValidEmailAddress(document.getElementById('email_address').value))
			{
				alert('Your Email Address seems to be invalid, please enter a valid email address.');
				panel_reset();
				return false;
			}
			
			
		}
		
		/* Shipping Information --- */
		if(document.getElementById('shipping_first_name').value == "")
		{
			alert('Please fill out your shipping first name field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('shipping_last_name').value == "")
		{
			alert('Please fill out your shipping last name field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('shipping_street_address').value == "")
		{
			alert('Please fill out the shipping address field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('shipping_city').value == "")
		{
			alert('Please fill out the shipping city field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('shipping_state').value == "")
		{
			alert('Please fill out the shipping state field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('shipping_zip_code').value == "")
		{
			alert('Please fill out the shipping zip code field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('shipping_phone_number').value == "")
		{
			alert('Please fill out the shipping phone number field in order to continue.');
			panel_reset();
			return false;
		}

		
		if($(this).hasClass("interest_form"))
		{
			$(this).attr("action","/handlers/send/sign_up_interest.html");

		} else {
			
		if(document.getElementById('visa_card_type').checked == false && 
		   document.getElementById('mc_card_type').checked == false && 
		   document.getElementById('ae_card_type').checked == false && 
		   document.getElementById('disc_card_type').checked == false)
		{
			alert('Please fill out the credit card type field in order to continue.');
			panel_reset();
			return false;
		}

		if(document.getElementById('card_Number').value == "")
		{
			alert('Please fill out the credit card number field in order to continue.');
			panel_reset();
			return false;
		}
		if(document.getElementById('expiration_date_mo').value == "" || document.getElementById('expiration_date_yr').value == "")
		{
			alert('Please fill out the credit card expiration in order to continue.');
			panel_reset();
			return false;
		}
		
			$(this).attr("action","/handlers/send/sign_up.html");

		}
		
			$(this).unbind("submit");

			$(this).submit(function(){
				return true;

			});

			this.submit();

			return false;
	});
	
	
});


function copyBilling() {
	if (document.getElementById('same_as_billing').checked == true) {
		document.getElementById('shipping_first_name').value = document.getElementById('billing_first_name').value;
		document.getElementById('shipping_last_name').value = document.getElementById('billing_last_name').value;
		document.getElementById('shipping_street_address').value = document.getElementById('billing_street_address').value;
		document.getElementById('shipping_city').value = document.getElementById('billing_city').value;
		document.getElementById('shipping_state').value = document.getElementById('billing_state').value;
		document.getElementById('shipping_zip_code').value = document.getElementById('billing_zip_code').value;
		document.getElementById('shipping_phone_number').value = document.getElementById('billing_phone_number').value;	
	}
}


