var MAX_DUMP_DEPTH=10;function dumpObj(obj,name,indent,depth){if(depth>MAX_DUMP_DEPTH){return indent+name+": \x3CMaximum Depth Reached\x3E\x0A";} ;if( typeof obj=="object"){var child=null;var output=indent+name+"\x0A";indent+="	";for(var item in obj){try{child=obj[item];} catch(e){child="\x3CUnable to Evaluate\x3E";} ;if( typeof child=="object"){output+=dumpObj(child,item,indent,depth+1);} else {output+=indent+item+": "+child+"\x0A";} ;} ;return output;} else {return obj;} ;} ;

$(document).ready(function() {
	$('#button-add').click(function(e) {
		e.preventDefault();
		add_page.clear_errors();
		if (add_page.validate_local()) {
			add_page.validate_remote(
				add_page.save
			)
		}
	});
	
	$('#restaurant-country').change(function() {
		var selected_country = $('#restaurant-country option:selected').val()
		add_page.change_country(selected_country);
	});
})

var add_page = {
	
	show_country_error: function(message) {
		$('#country-error').html('<br />' + message).fadeIn(500);
	},
	
	show_error: function(message) {
		$('#page-error').html(message).fadeIn(500);
	},
	
	clear_errors: function() {
		$('.add-error').fadeOut(0);
	},
	
	validate_local: function() {
		var is_valid = true;
		if ($('#restaurant-name').val().length < 2) {
			is_valid = false;
			$('#restaurant-name-error').fadeIn(500);
		}
		if ($('#restaurant-city').val().length < 2) {
			is_valid = false;
			$('#restaurant-city-error').fadeIn(500);
		}
		if ($('#deal-description').val().length < 10) {
			is_valid = false;
			$('#deal-description-error').fadeIn(500);
		}
		var has_day = false;
		has_day = has_day || $('#sunday').attr('checked');
		has_day = has_day || $('#monday').attr('checked');
		has_day = has_day || $('#tuesday').attr('checked');
		has_day = has_day || $('#wednesday').attr('checked');
		has_day = has_day || $('#thursday').attr('checked');
		has_day = has_day || $('#friday').attr('checked');
		has_day = has_day || $('#saturday').attr('checked');
		if (!has_day) {
			is_valid = false;
			$('#days-error').fadeIn(500);
		}

		return is_valid;
	},
	
	validate_remote: function(success_callback) {
		success_callback();
	},
	
	save: function() {
		$.ajax({
		    url: '/add/save/',
			data: $('#add-form').serialize(),
		    type: 'POST',
		    dataType: 'json',
		    timeout: 10000,
		    error: function(){
		        add_page.show_error('Your deal was not saved.  There was an error saving.')
		    },
		    success: function(result){
		        if (result.length == 1) {
					var d = result[0];
					window.location.href = d.fields["url"];
				} else {
					add_page.show_error('Your deal was not saved.  There was an unexpected error.')
				}
		    }
		});
	},
	
	change_country: function(country_code) {
		if (country_code == 'US')
			$('#postal-name').html('Zip');
		else 
			$('#postal-name').html('Postal');
			
		$.ajax({
		    url: '/add/get_country/',
			data: $('#add-form').serialize(),
		    type: 'POST',
		    dataType: 'json',
		    timeout: 10000,
		    error: function(){
		        add_page.show_country_error('Failed to change the country');
		    },
		    success: function(result){
				if (result.length == 1) {
					var show_states = result[0].fields["show_states"];
					if (show_states) {
						$('#show-states').fadeIn(500);
						add_page.get_states();
					} else {
						$('#show-states').fadeOut(500);
					}
				} else {
					add_page.show_country_error('Failed to change the country.<br />There was an unexpected error.');
				}
		    }
		});
	},
	
	get_states: function() {
		$.ajax({
		    url: '/add/get_states/',
			data: $('#add-form').serialize(),
		    type: 'POST',
		    dataType: 'json',
		    timeout: 10000,
		    error: function(){
		        add_page.show_country_error('Failed to get states');
		    },
		    success: function(result){
				if (result.length > 0) {
					states_html = '';
					$.each(result, function(key, value) {
						states_html += '<option value="' + value.fields["url_name"] + '">' + value.fields["name"] + '</option>';
					});
					$('#restaurant-state').html(states_html);
				} else {
					add_page.show_country_error('Failed to get the states.<br />There was an unexpected error.');
				}
		    }
		});
	}
	
}