ContentOrderEditor = {
		
	restoreBillingAddress: function() {
		var jqxhr,
			returndata,
			addressdata;
		
		jqxhr = $.ajax({
			url: window.location.href,
			type: 'POST',
			data: 'action=get_order_address&address_type=billing'
		});
		
		jqxhr.success(function(response) {
			try {
				returndata = $.parseJSON(response);
				
				if (returndata.status == 'success') {
					addressdata = returndata.data;
					
					for (prop in addressdata) {
						$(':input[name="billing_' + prop +'"]').val(addressdata[prop]);
					}
				} else {
					alert('Error: no success');
				}
			} catch (e) {
				alert('Error: no json')
			}
		});
		
		jqxhr.error(function() {
			alert('Error: no server');
		});
	},
	
	updateBillingAddress: function(choice) {
		var jqxhr,
			returndata,
			addressdata;
		
		if (choice == '-1') {
			return;
		} else if (choice == '-2') {
			return;
		} else if (choice == '0') {
			ContentOrderEditor.restoreBillingAddress();
		} else {
			jqxhr = $.ajax({
				url: window.location.href,
				type: 'POST',
				data: 'action=get_address&address_id=' + choice
			});
			
			jqxhr.success(function(response) {
				try {
					returndata = $.parseJSON(response);
					
					if (returndata.status == 'success') {
						addressdata = returndata.data;
						
						for (prop in addressdata) {
							$(':input[name="billing_' + prop +'"]').val(addressdata[prop]);
						}
					} else {
						alert('Error: no success');
					}
				} catch (e) {
					alert('Error: no json')
				}
			});
			
			jqxhr.error(function() {
				alert('Error: no server');
			});
		}
	},
	
	restoreShippingAddress:function() {
		var jqxhr,
			returndata,
			addressdata;
		
		jqxhr = $.ajax({
			url: window.location.href,
			type: 'POST',
			data: 'action=get_order_address&address_type=shipping'
		});
		
		jqxhr.success(function(response) {
			try {
				returndata = $.parseJSON(response);
				
				if (returndata.status == 'success') {
					addressdata = returndata.data;
					
					for (prop in addressdata) {
						$(':input[name="shipping_' + prop +'"]').val(addressdata[prop]);
					}
				} else {
					alert('Error: no success');
				}
			} catch (e) {
				alert('Error: no json')
			}
		});
		
		jqxhr.error(function() {
			alert('Error: no server');
		});
	},
	
	updateShippingAddress: function(choice) {
		var jqxhr,
			returndata,
			addressdata;
		
		if (choice == '-1') {
			return;
		} else if (choice == '-2') {
			return;
		} else if (choice == '0') {
			ContentOrderEditor.restoreShippingAddress();
		} else {
			jqxhr = $.ajax({
				url: window.location.href,
				type: 'POST',
				data: 'action=get_address&address_id=' + choice
			});
			
			jqxhr.success(function(response) {
				try {
					returndata = $.parseJSON(response);
					
					if (returndata.status == 'success') {
						addressdata = returndata.data;
						
						for (prop in addressdata) {
							$(':input[name="shipping_' + prop +'"]').val(addressdata[prop]);
						}
					} else {
						alert('Error: no success');
					}
				} catch (e) {
					alert('Error: no json')
				}
			});
			
			jqxhr.error(function() {
				alert('Error: no server');
			});
		}
	}
	
};

$(document).ready(function() {
	$('h2 > a').click(function(e) {
		e.preventDefault();
		
		$($(this).attr('href')).slideToggle();
	});
	
	$('#select_shipping_address').change(function() {
		var choice;
		
		choice = $(this).val();
		
		ContentOrderEditor.updateShippingAddress(choice);
	});
	
	$('#select_billing_address').change(function() {
		var choice;
		
		choice = $(this).val();
		
		ContentOrderEditor.updateBillingAddress(choice);
	});
});