function hit(page) {
	$.get(page, {
		'randid' : Math.random()
	});
}

function hit_and_redirect(page, redirect_page) {
	$.ajax({
		url: page,
		data: {
			'randid' : Math.random()
		},
		success: function(data) {
			document.location = redirect_page;
		}
	});
}

function set_wishlist_price(price, select_id, target_id) {
	var quantity = $('#' + select_id).val();
	var new_pennies = convert_to_pennies(price) * quantity;
	var new_dollars = convert_to_dollars(new_pennies, true, true);
	$('#' + target_id).text('$' + new_dollars);
}

function toggle_share_wishlist_checkbox(form_id, checkbox_id, value) {
	if ($('#' + checkbox_id).is(':checked')) {
		append_hidden_field_with_id(form_id, 'agent_sku_list_item_ids', value,
				'item_' + value);
	} else {
		$('#item_' + value).remove();
	}
}

function submit_email_ariat(context){
	$.ajax({ 
		url: context+'/lead/add_do',
		data: get_form_values('email_ariat_form'), 
		success: function(response){
			$('#email-us').html(response);
      	}
	});
}

function swap_press_gallery_image(images, captions, index) {
	$('#lg-view').attr('src', images[index]);
	$('#caption').html('<em>' + captions[index] + '</em>');
}

function show_in_the_news_media_login_form() {
	$.ajax({
		url: pathInfo['contextPath'] + '/lead/add?type=IN_THE_NEWS_MEDIA_LOGIN',
		data: {
			'randid' : Math.random()
		},
		success: function(data) {
			$('#in_the_news_media_login_div').html(data);
		}
	});
}

function show_in_the_news_press_requests_form() {
	$.ajax({
		url: pathInfo['contextPath'] + '/lead/add?type=IN_THE_NEWS_PRESS_REQUESTS',
		data: {
			'randid' : Math.random()
		},
		success: function(data) {
			$('#press-request').html(data);
		}
	});
}

function submit_in_the_news_press_requests_form() {
	$.post(pathInfo['contextPath'] + '/lead/add_do', get_form_values('in_the_news_press_requests_form'), function(data, textStatus, xhr) {
		$('#press-request').html(data);
	});
}

function submit_in_the_news_media_login_form() {
	$.post(pathInfo['contextPath'] + '/lead/add_do', get_form_values('in_the_news_media_login_form'), function(data, textStatus, xhr) {
		if ((xhr.getResponseHeader('ACDC_ERROR') == 't')) {
			$('#in_the_news_media_login_div').html(data);
		} else {
			document.location = pathInfo['contextPath'] + '/pages/media.jsp';
		}
	});
}

function generateReturnLabel() {
	$.post(pathInfo.contextPath + '/return/return_label_do',
		get_form_values('returnLabel'), 
		function(data, textStatus, xhr) {
			if (xhr.getResponseHeader('ACDC_ERROR') == 't') {
				alert('There was an error.');
			} else {
				var url = pathInfo.contextPath + '/util/return_label_render.jsp?imageURLS=' + escape(data);
				window.open(url, "UPSReturnLabel", "height=400,width=660,scrollbars=1,resizable=0,menubar=yes,toolbar=yes,status=0").focus();
			}
		}
	);
}

function format_price(price) {
	price = price.toFixed(2) + '';
	x = price.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function submitExchangeOrder(landing){
	$.post(pathInfo.contextPath + '/return/exchange_order',
		get_form_values('exchange_order'), 
		function(data, textStatus, xhr) {
			if (xhr.getResponseHeader('ACDC_ERROR') == 't') {
				alert('There was an error.');
			} else {
				window.location=landing;
			}
		}
	);
}

function submitResendReturnLabel(landing){
	$.post(pathInfo.contextPath + '/return/resend_label',
		get_form_values('resend_label'), 
		function(data, textStatus, xhr) {
			if (xhr.getResponseHeader('ACDC_ERROR') == 't') {
				alert('There was an error.');
			} else {
				alert('Please check your email.');
			}
		}
	);
}

function submitEmailSignupHomepage(field_value){
	$('#home_signup_success').val('/util/email_subscription.jsp?update=' + encodeURIComponent($('#email-term').val()));
	$.post(pathInfo.contextPath + '/servlet/EmailSubscriptionServlet', 
		get_form_values('home_page_email_signup'), 
		function(data, textStatus, xhr) {
			//HACK:TopServlet and Controller Servlet should install error messages in addition to a header representing error. Since it does not I heuristically determine if the email already exists sort of like a virus scanner
			//and its use heuristic signatures.
			if ((xhr.getResponseHeader('ACDC_ERROR') == 't') && (data.indexOf('Email address already exists') == -1)) {			
				email_subscription_Popup(pathInfo.contextPath + '/util/email_subscription.jsp');
			} else {
				$('#email-term').val(field_value);
				$('.popup').html(data);
				show_popup();
			}
			
		}
	);
}

function get_onload_solr_perpage_count(){
	if (readCookie('view_all_onload')=='true'){
		return 1000;	// assume there would be no more than that
	}else{
		return 10;	// use default
	}
}
