$(function() {
	$.ajaxSetup ({
		cache: false
	});
	
			$('#logging_in_username, #logging_in_password').keyup(function(e) {
				if (e.keyCode == 13) {
				   $('#loginUserSubmit').trigger('click');
				}
			});
 
			$('#loginUserSubmit').click(function() {
				if ($('#logging_in_username').val().length > 2 && $('#logging_in_password').val().length > 2) {
					  $('#login_status').css('color', '').css('line-height', '27px').css('padding-top', '');
					  $('#login_status').html("<img src='/includes/js/jquery-tooltip/images/loader.gif' />");
					  $('#login_status').show();

					  var url='/user_actions/login';

					  $.post(url, { email: $('#logging_in_username').val(), pass: $('#logging_in_password').val() }, function(data) {
							JSONObject = eval( "(" + data + ")" );

															if (JSONObject.success=='false') {
																$('#login_status').css('color', '#fb8585').html(JSONObject.msg);
															} else {
																if (JSONObject.success=='block') {
																	$('#login_status')
																		.css('color', '#fb8585')
																		.css('line-height', '1')
																		.css('padding-top', '2px')
																		.html(JSONObject.msg);
																} else {
															    	$('#login_status').html("<img src='/includes/js/jquery-tooltip/images/loader.gif' />");
																	$('#login_status').show();

																	location.reload();
																}												
															}
					});
				}
			});

			$('#forgot_psw_cont a').click(function() {
						
							$("#password_reminder_dialog").siblings('div.ui-dialog-titlebar').remove();
							$("#password_reminder_dialog").html("<center><img src='/includes/js/jquery-tooltip/images/loader.gif' style='padding: 30px;' /></center>");
							$("#password_reminder_dialog").dialog("open");
							
							var loadurl='/user_actions/load_password_reminder_form';
							$.post(loadurl, function(data) {
								$("#password_reminder_dialog").html(data);
							});						
						
			});

			$("#log_in_area .other_cities_btn").click(function(event) {
					event.preventDefault();
				    $("#city_selection").siblings('div.ui-dialog-titlebar').remove();
				    $("#city_selection").html("<center><img src='/includes/js/jquery-tooltip/images/loader.gif' style='padding: 30px;' /></center>");
					$("#city_selection").dialog("open");
					
					var loadurl='/user_actions/load_other_cities_view';
					$.post(loadurl, function(data) {
						$("#city_selection").html(data);
					});	
			  });

			$('#subscribe_dialog_opener').click(function() {
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				
						if ($('#subscribers_email').val()!='' || !emailReg.test($('#subscribers_email').val())) {
							$("#subscribe_dialog").siblings('div.ui-dialog-titlebar').remove();
							$("#subscribe_dialog").html("<center><img src='/includes/js/jquery-tooltip/images/loader.gif' style='padding: 30px;' /></center>");
							$("#subscribe_dialog").dialog("open");

							var loadurl='/user_actions/load_subscription_form/';
							$.post(loadurl, { email: $('#subscribers_email').val() }, function(data) {
								$("#subscribe_dialog").html(data);
							});	
						} else {
							alert ('Please provide a valid email address');
						}
			});
			
			if ($('#latest_poster').length>0) {
				OutputFPPoster ();
			}
});

function jump_to_url (id_to_fetch) {
	url_to_jump=document.getElementById(id_to_fetch).value;
	window.location=url_to_jump;
}

function final_poster_price(base_price) {
	var postage = document.getElementById('postage_options').value;
	var new_final_price = parseFloat(base_price) + parseFloat(postage);
	
	//WP
	if (document.buy_poster_form) {
		document.buy_poster_form.amount.value = new_final_price;
	}
	
	//PP
	if (document.buy_poster_form_pp) {
		document.buy_poster_form_pp.amount.value = new_final_price;
	}
	
	fetch_address();
	
	$("#price_to_pay").hide();
	$("#price_to_pay").html("<h1>&pound;"+new_final_price.toFixed(2)+"</h1>");
	$("#price_to_pay").show();
}

function fetch_address() {
	// WP fields
	if (document.buy_poster_form) {
		document.buy_poster_form.MC_delivery_name.value=document.buy_poster_form.delivery_name.value;
		document.buy_poster_form.MC_delivery_address.value=document.buy_poster_form.delivery_address.value;
		document.buy_poster_form.MC_delivery_pcode.value=document.buy_poster_form.delivery_pcode.value;
		document.buy_poster_form.MC_delivery_city.value=document.buy_poster_form.delivery_city.value;
		document.buy_poster_form.MC_delivery_country.value=document.buy_poster_form.delivery_country.value;
	}
	
	// PP fields
	if (document.buy_poster_form_pp) {
		var customString;
		customString = document.buy_poster_form.delivery_name.value+'***'+document.buy_poster_form.delivery_address.value+'***'+document.buy_poster_form.delivery_pcode.value+'***'+document.buy_poster_form.delivery_city.value+'***'+document.buy_poster_form.delivery_country.value;
		document.buy_poster_form_pp.custom.value=customString;
	}
}

function OutputFPPoster () {
	var loadurl='/shop/LatestPosterFP';
	
	$('#latest_poster').html("<img src='/includes/js/jquery-tooltip/images/loader.gif' />");
	
	$.post(loadurl, function(data) {
		if (data!='false') {
			$("#latest_poster").html(data).fadeIn();
		} else {
			$("#latest_poster").hide();
		}
	});
}
