﻿var searcher = {
    is_searching: false,
    init: function() {
	    //add hover to the form button
	    $('.form_button').hover(function() {
			    $(this).addClass('form_button_hover');
		    }, function() {
			    $(this).removeClass('form_button_hover');
	    });
	    	
	    $("#p").change(function() {
	        if($("#p :selected").val() !== "") {
    	        searcher.is_searching = true;
	            $("#city-select").hide();
	            $("#c").load("search_results.aspx?p=" + $(this).val(), function() {
	                if($("#city-select option").length > 1) {
	                    $("#city-select").show();
	                } else {
	                    $("#city-select").hide();
	                }
	                searcher.is_searching = false;
	            });
	        } else {
	            $("#city-select").hide(); 
	        }
	    });
    	
	    $("#bt-submit").click(function(e) {	
	        e.preventDefault();
	        if(searcher.is_searching) { 
	            alert("Searching: Please wait") 
	        } else {
	            if(typeof $("#city-select :selected").val() === 'undefined' || $("#city-select :selected").val() === "") {
	                if (!$("#city-select").is(":visible") && $("#p :selected").val() !== "") {
                        alert("There are currently no cities participating in this province - please check back soon!");	                
                    } else {
                        alert("You must select a province and city!");
                    }
	            } else {
	                location.href = "search.aspx?p="+$("#p").val()+"&c="+$("#c").val();
	            }
	        }
	    });
    }
}

$(function() {
    searcher.init();
});

