/*===================================
	Dealer Signup Functions
	Patrick Bennett
	Showroom Logic
====================================*/
$(document).ready(function() {
	//update city list
	
	$("#showroomstate").click(function(){
		var stateid = $(this).val();
	
		if(!isNaN(stateid)) {
		
			$.ajax({
				type: "GET",
				url: "js/api",
				cache: true,
				data: "action=getcities&stateid="+stateid,
				dataType: "json",
				success: function(json) {
					//update the cities
					var j = json;
					var options = '<option value="">-- select city--</option>';
					for (var i=0; i<j.length; i++) {
						options += '<option value="' + j[i].id + '">' + j[i].c + '</option>';
					}
					options += '<option value="700">-- Not Listed --</option>';
					$('#showroomcityinput').hide();
					$('#showroomcity').show();
					$('#showroomcity').html(options);				
					$('#showroomcity').attr("disabled",false);
				}
			});
		
		} else {
			loadCityField();			
		}
			
	});
	
	//update spots sentance
	$("#showroomcity").click(function(){
		var cityid = $(this).val();
		
		if(cityid > 0 && cityid != 700) {
			$.ajax({
				type: "GET",
				url: "js/api",
				cache: true,
				data: "action=getcitylimit&cityid="+cityid,
				dataType: "json",
				success: function(json) {
					//update the cities
					var j = json;
					$('#dlimit')
					.html('We currently have <span>'+j.dtotal+'</span> of <span>'+j.l+'</span> spots available.')
					.css("display","inline-block");
				}
			});
		}
		
		if(cityid == 700) {
			loadCityField();				
		}
		
	});	
	
});

var loadCityField = function() {
	//update spots for typed in ones
	$('#showroomcity').hide();
	$('#showroomcityinput').show();
	$('#showroomcityinput').html(
		'<input type="text" name="showroomcity" id="showroomcitytext" class="input" />'
	);
	$("#dlimit").hide();
	
	//update spots for typed in ones
	$("#showroomcitytext").blur(function(){
		$('#dlimit')
		.html('We currently have <span>10</span> spots available.')
		.css("display","inline-block");				 
	});	
}
