$(document).ready(function() {

	if($('#franchiseContact').length > 0 && $('#franchiseContact').attr('name')) {
		var phoneNumber = $('#franchiseContact').attr('name');
		var phoneNumber1 = phoneNumber.replace('<p>', '');
		var phoneNumber2 = phoneNumber1.replace('</p>', '');
		$('.btn-contact').html(phoneNumber2);
	}

	if(($('#franchisePage').length > 0 || $('#careers').length > 0) && document.location.search !== '') {
		$('#searchForm input[type="text"]').attr('disabled', 'disabled');
		
		var city = queryString('c');
		var city2 = (city) ? city.replace('-', ' ') : '';
		var state = queryString('s');
		var zip = queryString('z');
		
		$('.addressInput').val(zip);
		$('#addressInput2').val(city2);
		$('#state').val(state);
		setTimeout('triggerLocationSubmit()', 1000);
	}
	
	if($('#findHomeCare').length > 0 && document.location.search !== '') {
		var zip = queryString('z');
		
		$('.addressInput').val(zip);
		setTimeout('triggerLocationSubmit()', 1000);
	}
	
	$('.submitMailchimp').click(function(e) {
		e.preventDefault();
		$('#mc_signup_submit').trigger('click');
	});
	
	setTimeout("setSidebarHeight()",1000);
	
	$('.triggerEmail').click(function(e) {
		e.preventDefault();
		$('.form-contact').show();
	});
	
	
//	$('.buzzLink').click(function(e) {
//		e.preventDefault();
//		$('.buzzList .active').removeClass('active');
//		$('.buzzList li ul').hide();
//		$(this).parent().addClass('active');
//		$(this).siblings('ul').show();
//		
//	});
	
	setTimeout("bindForm()",1000);
});

function setSidebarHeight() {	
	if($('.blogcol').length === 0) {
		$('#rightcol').css({minHeight : $('.columns3').height() + 'px'});
		$('.rightcolShell').css({height : $('#rightcol').height() - 100 + 'px'});
	} else {
		$('#rightcol').css({minHeight : $('.columns3').height() + 'px'});
		$('.rightcolShell').css({height : $('#rightcol').height() - 635 + 'px'});
	}
		
	if($('#rightcol').length > 0 && $('#rightcol-content').length > 0) {
		$('#rightcol-content').stickyfloat();
	}
}

function triggerLocationSubmit() {
	$('#addressSubmit').trigger('click');
	$('#searchForm input[type="text"]').removeAttr('disabled');
}

function queryString(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

function bindForm() {
	$('.submitbtn').live('click', function(e) {
		e.preventDefault();
		$('#letusCare').trigger('submit');
	});
	
	
	$('#letusCare').submit(function(e) {
		var email = ($('#franchiseEmail').attr('name') !== '') ? $('#franchiseEmail').attr('name') : '';
		e.preventDefault();
		var str = $(this).serialize();
		$.ajax({
			type: 'POST',
			url: 'http://www.nursenextdoor.com/emailScript.php',
			data: str + '&frEmail='+email,
			success: function(response) {
				alert(response);
				if(response) {
					$('#result').html(response);
				} else {
				//$('#result').html(response);
					window.location.href="http://www.nursenextdoor.com/form-thank-you/";
				//setTimeout("window.location.href='http://web2.anzleads.com/NND/dev/nursenextdoor/'",2000);
				}
			}
		});
		return false;
	});
	
	$('#homeForm .btn').live('click', function(e) {
		e.preventDefault();
		$('#homeForm').trigger('submit');
	});
	
	
	$('#homeForm').submit(function(e) {
		var email = ($('#franchiseEmail').attr('name') !== '') ? $('#franchiseEmail').attr('name') : '';
		e.preventDefault();
		var str = $(this).serialize();
		$.ajax({
			type: 'POST',
			url: 'http://www.nursenextdoor.com/emailScript.php',
			data: str + '&frEmail='+email,
			success: function(response) {
				
				if(response =='error')
				{
					$('#result').html('Please enter a valid email address.');
				}
				else
				{
				//$('#result').html(response);
				window.location.href="http://www.nursenextdoor.com/form-thank-you/";
				//setTimeout("window.location.href='http://web2.anzleads.com/NND/dev/nursenextdoor/'",2000);
				}
			}
		});
		return false;
	});
}
