$(document).ready(function(){

	if($(".success").length > 0 && $(".success").html().length > 0){
		$(".success").after("<hr />");
		$(".success").fadeIn(2000);
	}
	
	$("#refine_criteria").toggle(function(){ 
		$("#search_criteria").slideDown(1000);
		$("#refine_criteria").find("span").html("Hide Search Criteria");
	}, function(){
		$("#search_criteria").slideUp(1000);
		$("#refine_criteria").find("span").html("Change Search Criteria");
	});
	
	$("#reset_form").click(function(){ 
		Lightbox.confirm("Are you sure? All criteria will be lost.", function(){ 
			$("#frmsearch").find("input[type!=hidden]").each(function(){ 
				if($(this).attr("type") == "text"){ $(this).val("") }
				else if($(this).attr("type") == "checkbox"){ $(this).attr("checked", "") }});
			$("#frmsearch").find("select").each(function(){ $(this).attr("selectedIndex", 0); });
		});
	});
	
});

function getCheckedIds(){
	var checked = [];
	$("input[id^='check'][type='checkbox']").each(function(){ if($(this).attr("checked") === true){ checked.push($(this).val()); }});
	return checked;
}

function getCheckedCount(){
	var count = 0;
	$("input[id^='check'][type='checkbox']").each(function(){ if($(this).attr("checked") === true){ count++; }});
	return count;
}

function showAjax(key){
	if($("#ajax_"+key).length == 0){
		var ajax = $("<img></img>").attr({ id: "ajax_"+key, src: "images/ajax/small.gif" }).css({ paddingTop: "1px", paddingLeft: "5px" });
		$("#"+key).after(ajax);
	}else{
		$("#ajax_"+key).show();
	}
}

function lookupSelect(key, value, source, define, display){
	showAjax(define);
	$.get('javascript/includes/lookups.php', { key: key, value: value, source: source, define: define, display: display },
	function(data){
		$("#"+define).empty();
		$(data).find("option").each(function(element){
			var option = $("<option></option>").attr("value", $(this).find("value").text()).html($(this).find("text").text());
			$('#'+define).append(option);
		});
		$("#region_id").find("option:first").html("Any Region"); 
		$("#location_id").find("option:first").html("Any Location"); 
		$("#ajax_"+define).hide();
	});
}