// JavaScript Document


var display;

(function()
{

	
display = function(id){
	if(id == "iphone"){
	document.getElementById(id + "_dis").style.display = "block";
	document.getElementById(id).style.color = "#fff";
	document.getElementById(id + "_td").className = 'new-subscription1';
	document.getElementById(id+"_td").style.borderBottom = "hidden";
	document.getElementById("email").style.color = "#77cff8";
//	document.getElementById("email_td").style.borderBottom = "solid 1px #494b51";
//	document.getElementById("email_td").style.borderWidth = "1px";
	document.getElementById("email_td").className = 'new-subscription2';
	document.getElementById("rss_td").className = 'new-subscription2';
	document.getElementById("rss").style.color = "#77cff8";
//	document.getElementById("rss_td").style.borderBottom = "solid 1px #494b51";
//	document.getElementById("rss_td").style.borderWidth = "1px";
	document.getElementById("email_dis").style.display = "none";
	document.getElementById("rss_dis").style.display = "none";
/*document.getElementById("mobile_dis").style.display = "none";
	document.getElementById("mobile").style.background = "url('/static/set/default/img/bg__tab_image.gif')";*/
	}
	if(id == "email"){
	
		
	document.getElementById(id + "_dis").style.display = "block";
	document.getElementById(id).style.color = "#fff";
	document.getElementById(id + "_td").className = 'new-subscription1';
	document.getElementById(id+"_td").style.borderBottom = "hidden";
	document.getElementById("iphone").style.color = "#77cff8";
	document.getElementById("iphone_td").className = 'new-subscription2';
//	document.getElementById("iphone_td").style.borderBottom = "solid #494b51";
//	document.getElementById("iphone_td").style.borderWidth = "1px";
	document.getElementById("rss").style.color = "#77cff8";
	document.getElementById("rss_td").className = 'new-subscription2';
//	document.getElementById("rss_td").style.borderBottom = "solid #494b51";
//	document.getElementById("rss_td").style.borderWidth = "1px";
	document.getElementById("iphone_dis").style.display = "none";
	document.getElementById("rss_dis").style.display = "none";
/*	document.getElementById("mobile_dis").style.display = "none";
	document.getElementById("mobile").style.background = "url('/static/set/default/img/bg__tab_image.gif')";*/
	}
	if(id == "rss"){
	document.getElementById(id + "_dis").style.display = "block";
	document.getElementById(id).style.color = "#fff";
	document.getElementById(id + "_td").className = 'new-subscription1';
	document.getElementById(id+"_td").style.borderBottom = "hidden";
	document.getElementById("email").style.color = "#77cff8";
//	document.getElementById("email_td").style.borderBottom = "solid #494b51";
//	document.getElementById("email_td").style.borderWidth = "1px";
	document.getElementById("email_td").className = 'new-subscription2';
	document.getElementById("iphone").style.color = "#77cff8";
	document.getElementById("iphone_td").className = 'new-subscription2';
//	document.getElementById("iphone_td").style.borderBottom = "solid #494b51";
//	document.getElementById("iphone_td").style.borderWidth = "1px";
	document.getElementById("email_dis").style.display = "none";
	document.getElementById("iphone_dis").style.display = "none";
/*	document.getElementById("mobile_dis").style.display = "none";
	document.getElementById("mobile").style.background = "url('/static/set/default/img/bg__tab_image.gif')";*/
	}
/*	if(id == "mobile"){
	document.getElementById(id + "_dis").style.display = "block";
	document.getElementById(id).style.color = "#fff";
	document.getElementById(id).style.background = "url('/static/set/default/img/bg_image.gif')";
	document.getElementById("email_dis").style.display = "none";
	document.getElementById("email").style.background = "url('/static/set/default/img/bg__tab_image.gif')";
	document.getElementById("rss_dis").style.display = "none";
	document.getElementById("rss").style.background = "url('/static/set/default/img/bg__tab_image.gif')";
	document.getElementById("iphone_dis").style.display = "none";
	document.getElementById("iphone").style.background = "url('/static/set/default/img/bg__tab_image.gif')";
	}
	*/
}

/**
 * Check if value is a valid zip code
 * @param	value
 */
function isValidZipCode(value) {
		//make sure we only have positive integers beteween 0 and 9
	if(!value.match(/^[0-9]+$/)) {
		return false;
	}
		//only want 5 digit US zip code
	if(value.length != 5) {
		return false;
	}

	return true;
						}
						

/**
 * Check if value is a valid email address
 * @param	value
 */
function isValidEmail(value) {
	
	var expr = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
	if(!value.match(expr)) {
		return false;
					}

	return true;
}


/**
 *
 * @param	nHood	The neighborhood that the user already signed up for (if any)
 */
function ShowSubscriptionPart2(nHood) {



}


	//clear form elements when they get focus
$('input[name=email]').focus(function() {
	if($(this).val() == 'Email Address') {
		$(this).val('');
	}
					});
						
$('input[name=zipcode]').focus(function() {
	if($(this).val() == 'Zip Code') {
		$(this).val('');
	}
});
						
$('input[name=zip_code]').focus(function() {
	if($(this).val() == 'Zip Code') {
		$(this).val('');
	}
});


$('#subscribe_bar .click_here img').click(function(event) {
	event.preventDefault();

	$('#subscribe_bar .click_here').hide();
	$('#subscribe_bar .form_elements').css('display', 'inline');

});

// Bottom bar subscription form action
$('#subscribe_bar_form_1').submit(function(event) {
	event.preventDefault();

	var form = $(this);

	var email = $('input[name=email]', form).val();
	var zip = $('input[name=zipcode]', form).val();

	if(!isValidEmail(email)) {
		alert('Invalid Email Address');
		$('input[name=email]', form).focus();
					}
	else if(!isValidZipCode(zip)) {
		alert('Invalid Zip Code');
		$('input[name=zipcode]', form).focus();
	}
	else {
		$.ajax({
			url: '/account/new-subscription-part-one',
			type: 'POST',
			data: form.serialize(),
			success: function() {
				//set the cookie that we added what we needed
				$.cookie('subscription_bar_submit', 1);
				$('#subscribe_bar .form_part_1').hide();
				$('#subscribe_bar .thank_you').show();
					//hide bar after 7 seconds
				$('#subscribe_bar').animate({opacity: 1.0}, 7000).hide('slow');
			}
		});
	}
});
		
// Father's Day specials page subscription form
$('#deals_subscribe_form').submit(function(event) {
    event.preventDefault();
    var form = $(this);
    var email = $('input[name=email]', form).val();
    var zip = $('input[name=zip_code]', form).val();
	var url = '/account/news-letter';
	if(typeof(domain) != 'undefined' && domain != NULL) {
		url = domain+url;
	}
    if(!isValidEmail(email)) {
        alert('Invalid Email Address');
        $('input[name=email]', form).focus();
    } 
    else if(!isValidZipCode(zip)) {
        alert('Invalid Zip Code');
        $('input[name=zip_code]', form).focus();
    }
    else {
        $.ajax({
            url: url,
            type: 'POST',
            data: form.serialize(),
            success: function() {
                $('#deals_subscribe_form').hide();
                $('#deals_subscribe .thank_you').show();
            }
        });
    }
});

$('#subscribe_bar .close_btn').click(function() {
	$('#subscribe_bar').hide('slow');
        var hideDate = new Date();
        hideDate.setTime(hideDate.getTime() + (171 * 1000))
        $.cookie('subscription_bar_hide', 1, {expires: hideDate});
});

$('#b_submit').click(function() {
    var pathname = window.location.pathname;
    pageTracker._trackEvent('Bottom Bar Newsletter Widget', 'click', pathname);
});

	//side bar subscription
$('#newsLetter_subscription').submit(function(event) {
	event.preventDefault();
	
	var form = $(this);

	var email = $('input[name=email]', form).val();
	var zip = $('input[name=zip_code]', form).val();
	var nHood = $('input[name=nHood]', form).val();
	var url = '/account/news-letter';
	if(typeof(postUrl) != 'undefined' && postUrl != null) {
		url = postUrl+url;
	}
	if(!isValidEmail(email)) {
		alert('Invalid Email Address');
		$('input[name=email]', form).focus();
		
			}
	else if(!isValidZipCode(zip)) {
		alert('Invalid Zip Code');
		$('input[name=zip_code]', form).focus();
			}
	else {

		if($("#newsLetter_neighborhoods").hasClass("neighborhoods_closed")) {

			pageTracker._trackEvent(nHood, "submit 1", location.pathname);

			$.ajax({
				url: url,
				type: 'POST',
				data: form.serialize(),
				success: function(result) {
					if(result && (result != '')) {
						services = result.split(',');
						//lets make sure we only hide the one we added and not any others
							//remove preset one incase this changed
						$("div", form).removeClass("neighborhoods_closed");
							//hide each one
						for(index=0; index < services.length; index++) {
							$("#"+services[index], form).addClass("neighborhoods_closed");
						};
			}
			else {
						//ignoring errors for now...
			}
					//ShowSubscriptionPart2
					$("#newsLetter_neighborhoods").removeClass("neighborhoods_closed");
					$("#newsLetter_neighborhoods").addClass("neighborhoods_open");
		}
			});
		}
		else {
			pageTracker._trackEvent(nHood, "submit 2", location.pathname);
		 
			$("#newsLetter_neighborhoods").addClass("neighborhoods_open");	
			$("#newsLetter_neighborhoods").removeClass("neighborhoods_closed");
		 
			$.ajax({
				url: '/account/news-letter',
				type: 'POST',
				data: form.serialize(),
				success: function() {
					$("#newsLetter_neighborhoods").removeClass("neighborhoods_open");
					$("#newsLetter_neighborhoods").addClass("neighborhoods_closed");

					$("#newsLetter_neighborhoods_thanks").removeClass("neighborhoods_closed");
					$("#newsLetter_neighborhoods_thanks").addClass("neighborhoods_open");
					$("#formsubmit").addClass("neighborhoods_closed");
					}
			});
			}
	}
	

});



}()); //end of closure


