var $j = jQuery.noConflict();
var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

$j(document).ready(function(){
	$j('ul.dblist a').click(function() {
		var t = $j(this).attr('id');
		//var db = t.substring(0, t.indexOf("_"));
		var db = $j(this).attr('class');

		$j.post("/php/" + db, { stage: "table", table: t }, function(data) {
			if(data.substr(0, 6) == "Error:") {
				alert(data);
			} else {
				$j("#ajax_container").html(data);
			}
		});
	});

	$j("div.ajax_load").ajaxStart(function(){
   		$j(this).show();
 	});

	$j("div.ajax_load").ajaxStop(function(){
   		$j(this).hide();
 	});

	$j('#ajaxdiv a').live("click", catch_clicks);
	$j('div.form_page_nav select').live("change", select_change);

	$j('#jsddm > li').bind('mouseover', jsddm_open);
	$j('#jsddm > li').bind('mouseout',  jsddm_timer);

	$j(".puButton").click(function() {
		var button = $j(this);
    	var box = $j(this).parent('.puContainer').children('.puBox');
    	var form = $j(this).parent('.puContainer').children('.puForm');
    	button.removeAttr('href');
		
		box.toggle();
		button.toggleClass('active');

		$j('.puBox').each(function() {
			if($j(this).attr("id") != box.attr("id")) {
				$j(this).hide();
			}
		});

    	form.mouseup(function() { 
			return false;
    	});

        //if(!($j(login.target).parent('.puButton').length > 0)) {
            //button.removeClass('active');
           // box.hide();
       // }
	});

	$j("input.extratext[type='radio']").live("change", function() {
		var id = $j(this).attr('id');
		var n = $j(this).attr('name');
		//alert(id + ":" + n + "  checked");
		if(id == n){
			$j("#" + n + "_EXTRA").show();
		} else {
			$j("#" + n + "_EXTRA").hide();
		}
	});

	$j("input.parent[type='radio']").live("change", function() {
		var val = $j(this).attr('value');
		var n = $j(this).attr('name');
		//alert(val + ":" + n + "  checked");
		if(val.substr(0, 3) == "Yes"){
			$j("." + n + "_child").show();
		} else {
			$j("." + n + "_child").hide();
		}
	});

	$j("input.parent[type='checkbox']").live("change", function() {
		var val = $j(this).attr('checked');
		var n = $j(this).attr('name');
		//alert(val + ":" + n);
		if(val == "checked"){
			$j("." + n + "_child").show();
		} else {
			$j("." + n + "_child").hide();
		}
	});

	$j("select.extratext").live("change", function() {
		var id = $j(this).attr('id');
		var n = $j(this).attr('name');
		var v = $j(this).val();
		//alert(id + ":" + v + "  checked");
		if(v == "Other"){
			$j("#" + n + "_EXTRA").show();
		} else {
			$j("#" + n + "_EXTRA").hide();
		}
	});
});

function catch_clicks (e) {
	e.preventDefault();
	var s = $j(e.target).attr('class');
	var id = $j(e.target).attr('id');
	var t = $j("div#ajaxdiv").attr('class');
	var db = t.substring(0, t.indexOf("_"));
	//alert("Class: " + s + " id: " + id + " db: " + db);
	if(s == "add") {
		//alert($j("#table_edit").serialize());
		$j.post("/php/" + db + "db.php", $j("#table_edit").serialize(), function(data) {
			if(data.substr(0, 6) == "Error:") {
				alert(data);
			} else {
				$j("#ajax_container").html(data);
			}
 		});
	} else if (s == "group_display") {
		$j("div#qgroups").children().each(function(i) {
			var shown = $j(this).attr('id');
			if (shown == id) {
				$j(this).addClass("Current");
				//alert("SHOW: id: " + id + " shown: " + shown);
			} else {
				$j(this).removeClass("Current");
				$j("." + shown).hide();
				//alert("HIDE: id: " + id + " shown: " + shown);
			}
		});
		$j("." + id).show();
	} else if (id == "profile") {
		$j.post("/php/session.php", $j("#profile_edit").serialize(), function(data) {
			if(data.substr(0, 6) == "Error:") {
				alert(data);
			} else {
				$j("#ajax_container").html(data);
			}
		});
	} else if(id == "filter") {
		$j.post("/php/" + db + "db.php", $j("#db_sf_form").serialize(), function(data) {
			//$j("div.ajax_load").hide();
			if(data.substr(0, 6) == "Error:") {
				alert(data);
			} else {
				$j("#ajax_container").html(data);
			}
 		});
	} else { //if (s == "delete" || s == "view" || s == "edit" || s == "table" || s == "report" || s == "charts")
		if (s == "delete") {
			if(confirm("Are you sure you want to delete this record?")) {
				s = "godelete";
			} else {
				return;
			}
		}
		//alert("Class: " + s + " id: " + id + " db: " + db);
		var p = $j("#page").attr('value');
		//alert(p);
		$j.post("/php/" + db + "db.php", { stage: s, table: t, id: id, page: p }, function(data) {
			if(data.substr(0, 6) == "Error:") {
				alert(data);
			} else {
				$j("#ajax_container").html(data);
			}

			if ($j("#map_canvas").length != 0) {
				initialize_map($j("#map_canvas").html());
			}

			$j(".extratext").each(function() {
				var id = $j(this).attr('id');

				if($j(this).is(":checked")) {
				} else {
					$j("#" + id + "_EXTRA").hide();
				}
			});	
		});
	}
}

function select_change (e) {
	var id = $j(this).val();

	$j("#page option").each(function() {
		c = $j(this).val();
		$j("." + c).hide();
	});

	$j("." + id).show();
}

function jsddm_open()
{  jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $j(this).find('ul').css('visibility', 'visible');
}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;
	}
}

function check_required_fields(formid)
{
	var failed = 0;
	for (var i = 0; i < (document.getElementById(formid).elements.length); i++) {
		if (document.getElementById(formid).elements[i].type == "text" && document.getElementById(formid).elements[i].className.indexOf("required") != -1 && document.getElementById(formid).elements[i].value =="") {
			if (document.getElementById(document.getElementById(formid).elements[i].id + "label").innerHTML.indexOf("REQUIRED") == -1) { 
				document.getElementById(document.getElementById(formid).elements[i].id + "label").innerHTML += " <b style='color: red'>REQUIRED</b>";
			}
			failed = 1;
		}
	}
	if(failed) {
		alert("Please fill out all required fields");
	} else {
		document.getElementById(formid).submit();
	}
}


