$(document).ready(function() {
    $("#apartment-search-form").validate({
        submitHandler: function(form) {
            $("#state").removeClass("error");
            $("#city").removeClass("error");
            $("#zip").removeClass("error");

            var submit = true;
            if ($("#zip").val().length < 1) {
                if ($("#state").val().length < 1) {
                    $("#state").addClass("error");
                    submit = false;
                }
                if ($("#city").val().length < 1) {
                    $("#city").addClass("error");
                    submit = false;
                }
                if (!submit) $("#zip").addClass("error");
            }

            if (submit) form.submit();
        }
    });
});

function saveLocation()
    {
        var city = document.getElementById("city").value;
        var state = document.getElementById("state").value;
        var zip = document.getElementById("zip").value;
        
        if (document.getElementById("remember").checked)
        {
            var value = city + "," + state + "," + zip;
            createCookie("camden-remember",value,14);
        }
        else
        {
            createCookie("camden-remember","",-1);
        }
        try 
        {
        document.apartment-search-form.submit();
        }
        catch (Exception) { }
    }
function UpdateList() {

	city = document.getElementById("city");
	state = document.getElementById("state");
	element = document.getElementById("zip");
	remember = document.getElementById("remember");
	
	var cookie = readCookie('camden-remember');
        if (cookie)
        {
		    var data = cookie.split(",");
		    
		    
		    if (data[0].length > 0 && data[1].length > 0) 
		    {
		        state.value = data[1];
		        poplist();
		        city.value = data[0];
		    }
		    else if(data[2].length > 0)  
		    {
		        element.value = data[2];
		    }
		    remember.checked = true;
		    
        }
        else
        {
	        poplist();
        }
}

function poplist()
{
     // Remove all existing items from city list
        city.options.length = 0;
        var defaultSelected = false;
	    var selected = false;

	// add items based on the selected option of state

	// nothing selected
	if (state.options[state.selectedIndex].value == "") {
		var optionName = new Option("Default", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
	}

	// arizona
	if (state.options[state.selectedIndex].value == "AZ") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Phoenix", "AZP", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// california
	if (state.options[state.selectedIndex].value == "CA") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Inland Empire", "CAIE", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Orange County", "CALA", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("San Diego", "CASD", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// colorado
	if (state.options[state.selectedIndex].value == "CO") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Denver", "CODV", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		
		element.setAttribute("class", "optional"); 
	}

	// florida
	if (state.options[state.selectedIndex].value == "FL") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Orlando", "FLO", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Southeast Florida", "FLSE", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Tampa", "FLTA", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// georgia
	if (state.options[state.selectedIndex].value == "GA") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Atlanta Metro", "GAAM", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// kentucky
	if (state.options[state.selectedIndex].value == "KY") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Louisville", "KYLO", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// missouri
	if (state.options[state.selectedIndex].value == "MO") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Kansas City", "MIKC", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("St. Louis", "MISL", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// nevada
	if (state.options[state.selectedIndex].value == "NV") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Las Vegas", "NVLV", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// dc
	if (state.options[state.selectedIndex].value == "DC") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("D.C. Metro", "DCWM", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// Maryland MD
	if (state.options[state.selectedIndex].value == "MD") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("D.C. Metro", "DCWM", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// Virginia VA
	if (state.options[state.selectedIndex].value == "VA") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("D.C. Metro", "DCWM", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// north carolina
	if (state.options[state.selectedIndex].value == "NC") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Charlotte", "NCCH", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Raleigh", "NCRL", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// pennsylvania
	if (state.options[state.selectedIndex].value == "PA") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Chadds Ford", "PNCF", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// texas
	if (state.options[state.selectedIndex].value == "TX") {
		var optionName = new Option("Select a City", "", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Austin", "TXAU", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Corpus Christi", "TXCC", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Dallas/Ft. Worth", "TXDFW", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		var optionName = new Option("Houston", "TXHOU", defaultSelected, selected);
		var length = city.length;
		city.options[length] = optionName;
		// make the zip not required
		element.setAttribute("class", "optional"); 
	}

	// Do the next line to make sure the top item is selected
	city.options[0].selected = true;
	}

