function openPopBox(content) {
	$('#pop_box').html(content);
	$('body').click(closePopBox);
	$('#pop_box #close_button').focus();
}

function prepPopBox() {
	$('#pop_box').html("<link href='/global/containers/ajax_pop_box/popbox.css' type='text/css' rel='stylesheet'>"
	+ "</link><img id='top' src='/global/containers/ajax_pop_box/images/pop_box_border_top.png'/><div id='left'>"
	+ "<div id='right'><div id='content'><img alt='wait' src='/global/images/wait30trans.gif' style='margin: 80px auto; display: block;'/></div>"
	+ "</div></div><img id='bottom' src='/global/containers/ajax_pop_box/images/pop_box_border_bottom.png'/>");
	$('#pop_box').css('top', $(window).scrollTop());
	$('#pop_box').css('left', $(window).width()/2 - 786/2);
	$('#pop_box').fadeIn("normal");
}

function closePopBox() {   
	$('#pop_box').fadeOut("normal");
	$('body').unbind('click', closePopBox);
}

function loadPopBoxByPageName(pageName){
	loadPopBox('/global/containers/ajax_pop_box/ajax-pop-box.dot?name=' + pageName);
}

function loadPopBox(url) {
	prepPopBox();
	$.ajax({
		url: url,
		cache: true,
		success: function(html){
			openPopBox(html);
		}
	}); 	
}

function loadPlan(marketChannel) { 
	prepPopBox();
	$.ajax({
		url: '/global/containers/ajax_pop_box/ajax-pop-box.dot?name=' + marketChannel + '_plan',
		cache: true,
		success: function(html){
			if (marketChannel == 'AKC' || marketChannel == 'CFA') {
				html = html.replace(/;;ap_quote;;/, getPlanQuote(marketChannel, 'Accident Plus (accident / injury only)'));
				html = html.replace(/;;e_quote;;/, getPlanQuote(marketChannel, 'Essential'));
				html = html.replace(/;;ep_quote;;/, getPlanQuote(marketChannel, 'Essential Plus'));
				html = html.replace(/;;w_quote;;/, getPlanQuote(marketChannel, 'Wellness'));
				html = html.replace(/;;wp_quote;;/, getPlanQuote(marketChannel, 'Wellness Plus'));
				html = html.replace(/;;disclaimer;;/, quote.disclaimer);
			}			
			openPopBox(html);
		}
	});  	
}

function openNewsletterSubscriptionBox(emailObj, marketChannel, ipAddress) {
	prepPopBox();
	$.ajax({
		type: "GET",
		url: '/global/containers/ajax_pop_box/ajax-pop-box.dot?name=newsletter_signup',
		cache: true,
		success: function(retPage){
			retPage = retPage.replace(/email address/, emailObj.value);
			retPage = retPage.replace(/;;marketChannel;;/, marketChannel);
			retPage = retPage.replace(/;;ipAddress;;/, ipAddress);
			openPopBox(retPage);
		}
	});  	
}

function sendNewsletterSubscription(emailObject, firstNameObject, lastNameObject, marketChannel, ipAddress) {	
	
	loadPopBoxByPageName('wait_page');
	
	var data = {
		email: emailObject.value, 
		lastName: lastNameObject.value, 
		firstName: firstNameObject.value, 
		ipAddress: ipAddress, 
		marketChannel: marketChannel
	};

	$.ajax({
		url: '/PPIServices/NewsletterSubscribeServlet',
		type: "POST",
		data:  data,
		dataType: "json",
		cache: false,
		success: function(retVal){
			if (retVal.success == 1) {
				loadPopBoxByPageName('subscription_confirmed');
			} else {
				loadPopBoxByPageName('error');
			}
		},
		error: function () {
			loadPopBoxByPageName('error');
		}
	});  
}
