function saveListing(l){
	$("#wait").fadeIn('fast');
	$.getJSON('/nyc/listing/' + l + '/json/save/', function(data){
		e = $("#save_listing_" + l + " img");
		a = e.parent();
		if(data.saved){
			e.attr('src','/assets/images/icons/16x16/pin_on.png');
			//alert(a);
			//alert(a.text());
			//alert(a.html().replace(' Save',' Un-save'));
			if(a.text()==' Save'){a.html(a.html().replace(' Save',' Un-save'));}
		} else {
			e.attr('src','/assets/images/icons/16x16/pin_off.png');
			if(a.text()==' Un-save'){a.html(a.html().replace(' Un-save',' Save'));}
		}
	});
	$("#wait").fadeOut('fast');
	return false;
}
function moreListingInfo(l){
	$("#wait").fadeIn('fast');
	$("#popupRequestListingInfo").load('/nyc/listing/' + l + '/json/request_info_form/', function(){
		$('input:visible:first', this).focus();
	});
	centerPopup('RequestListingInfo');
	loadPopup('RequestListingInfo');
	$("#wait").fadeOut('fast');
	return false;
}
function processMoreListingInfo(l){
	var validate=true;
	var name = $("#id_name").val();
	var email = $("#id_email").val();
	var phone = $("#id_phone").val();
	var message = $("#id_message").val();
	if (!validateLength(name)){
		$("#name_error").html('* required field');
		validate=false;
	} else {
		$("#name_error").html('');
	}
	if (!validateEmail(email)){
		$("#email_error").html('* invalid email address');
		validate=false;
	} else {
		$("#email_error").html('');
	}
	if (!validateLength(message)){
		$("#message_error").html('* required field');
		validate=false;
	} else {
		$("#message_error").html('');
	}
	if (validateLength(phone) && !validatePhone(phone)){
		$("#phone_error").html('* use XXX-XXX-XXXX format');
		validate=false;
	} else {
		$("#phone_error").html('');
	}
	if (validate){
		$("#wait").fadeIn('fast');
		$.post('/nyc/listing/' + l + '/json/request_info/', {name: name, email: email, phone: phone, message: message},function(data){
			if (data.sent){
				$("#popupDone").html('<h1>Message Sent</h1>Your message has been sent.');
				centerPopup('Done');
				$("#popupRequestListingInfo").hide();
				$("#popupDone").show();
				current_popup = 'Done';
				window.setTimeout(function() {
					disablePopup();
				}, 1000);
			} else {
				
			}
		}, "json");
		$("#wait").fadeOut('fast');
	}
	return false;
}
$(document).ready(function(){
	$("#email_listing_lnk").click(function(){
		centerPopup('EmailListing');
		loadPopup('EmailListing');
		current_popup = 'EmailListing';
		return false;
	});
	$("#email_listing_form").submit(function(){
		var validate = true;
		var sender_name = $("#sender_name");
		var sender_email = $("#sender_email");
		var recipient_name = $("#recipient_name").val();
		var recipient_email = $("#recipient_email").val();
		var email_listing_memo = $("#email_listing_memo").val();
		
		if (sender_email.length) {
			sender_name = sender_name.val();
			if (!validateEmail(sender_email.val())){
				$("#sender_email_error").html('* invalid email address');
				validate=false;
			} else {
				sender_email = sender_email.val();
				$("#sender_email_error").html('');
			}
		} else {
			sender_name = '';
			sender_email = '';
		}
		
		if (!validateEmail(recipient_email)){
			$("#recipient_email_error").html('* invalid email address');
			validate=false;
		} else {
			$("#recipient_email_error").html('');
		}
		if (validate){
			$("#wait").fadeIn('fast');
			$.post('json/email/', {sender_name:sender_name, sender_email:sender_email,recipient_name:recipient_name,recipient_email:recipient_email, memo:email_listing_memo}, function(data){
				if (data.sent){
					centerPopup('EmailListingDone');
					$("#popupEmailListing").hide();
					$("#popupEmailListingDone").show();
					current_popup = 'EmailListingDone';
					window.setTimeout(function() {
						$("#popupEmailListingDone").show();
						disablePopup();
					}, 1000);
				} else {
					//TODO: do this
				}
			}, "json");
			$("#wait").fadeOut('fast');
		}
		return false;
	});
});
