<!--
$(document).ready(function (){
	init_expand();
	init_ct_change();
	init_ct_remove();
	init_col_change();
	init_cat_change();
	load_vet();
	init_vet_links();
	load_meta();
	init_user();
	init_user_edit();
	$("#vet").change(function(){load_vet();});
	$("#expandcats").click(function(){expand_all('cat');});
	$("#expandcts").click(function(){expand_all('ct');});
	$('#ex2').jqm({ajax: 'inc/cat_form.php?col_id='+$("#colony_id").val(), trigger: 'button#addcat',modal:true}); 
	$('#addcaretaker').click(function(){add_caretaker($(this).val())});
});

function load_vet(){
	$.post("inc/db/get_vet.php",{
			ID:$("#vet").val()
	},function(data){
		$("#vetinfo").html(data);
	});
}


function init_expand(){
	ctexp=$("div[class^=ctexp]");
	for(var i=0;i<ctexp.length;i++){
		ctexp[i].onclick=new Function("evt","expand_box('"+ctexp[i].getAttribute('id').substr(0,2)+"','"+ctexp[i].getAttribute('id').substr(5)+"')");
	}
	catexp=$("div[class^=catexp]");
	for(var i=0;i<catexp.length;i++){
		catexp[i].onclick=new Function("evt","expand_box('"+catexp[i].getAttribute('id').substr(0,3)+"','"+catexp[i].getAttribute('id').substr(6)+"')");
	}
		
}


function init_cat_change(){
	$(".catput").keyup(function(){
		cat_update($(this).closest('fieldset'));
	});
	$(".catput2").change(function(){
		cat_update($(this).closest('fieldset'));
	});
	$(".catput3").click(function(){
		cat_update($(this).closest('fieldset'));
	});
}


function init_col_change(){
	$(".colinput").keyup(function(){
		col_update($(this).closest('div'));
	});
	$(".colinput4").keyup(function(){
		col_update($(this).closest('div'));
	});
	$(".colinput4").change(function(){
		col_update($(this).closest('div'));
	});
	$(".colinput2").change(function(){
		col_update($(this).closest('div'));
		
	});
	$(".colinput3").click(function(){
		col_update($("#colony"+$("#colony_id").val()));
	});
	
}

function cat_update(catdiv){
	catid=catdiv.attr('id');
	if (catdiv.attr('status')=='unchanged'){
			oldshadow=catdiv.css("-moz-box-shadow");
			catdiv.css("-moz-box-shadow","0 0 5px 5px #f00");
			catdiv.css("-webkit-box-shadow","0 0 5px 5px #f00");
			catdiv.css("box-shadow","0 0 5px 5px #f00");
			catdiv.effect("highlight",1000);
			catdiv.attr('status','changed');
			catbutton=$("#updatecat"+catid);
			catbutton.attr('disabled','');
			catbutton.attr('class','update');
			catbutton.click(function(){
				if ($("#rabies"+catid).attr('checked')){rabies=1;}else{rabies=0;}
				if ($("#distemper"+catid).attr('checked')){distemper=1;}else{distemper=0;}				
				if ($("#fiv_status"+catid).attr('checked')){fiv=1;}else{fiv=0;}
				if ($("#ear_tip"+catid).attr('checked')){ear_tip=1;}else{ear_tip=0;}	
				Trap_date=$("#trap_date"+catid+"-y").val()+'-'+$("#trap_date"+catid+"-m").val()+'-'+$("#trap_date"+catid+"-d").val();				
				SN_date=$("#sn_date"+catid+"-y").val()+'-'+$("#sn_date"+catid+"-m").val()+'-'+$("#sn_date"+catid+"-d").val();				
				$.post("inc/db/updatedb.php",{
					table:'cat',
					ID:catid,
					Name:$("#catname"+catid).val(),
					Color:$("#color"+catid).val(),
					Markings:$("#markings"+catid).val(),
					Sex:$("#sex"+catid).val(),
					Age:$("#age"+catid).val(),
					Trap_Date:Trap_date,
					SN_Date:SN_date,
					FIV_Status:fiv,
					Rabies:rabies,
					Distemper:distemper,
					Ear_Tip:ear_tip,
					Foster_Notes:$("#foster_notes"+catid).val(),
					Status:$("#status"+catid).val(),
					Status_Notes:$("#status_notes"+catid).val(),
					Health_Notes:$("#health_notes"+catid).val()
				},function(data){
					if (data!=''){
						alert("There was a problem updating the database: "+data);
					} else {
						load_meta();
					}							
				});
				catdiv.css("-moz-box-shadow","5px 5px 5px #888");
				catdiv.css("-webkit-box-shadow","5px 5px 5px #888");
				catdiv.css("box-shadow","5px 5px 5px #888");
				catdiv.effect("highlight",1000);
				catdiv.attr('status','unchanged');
				catbutton.attr('disabled','disabled');
				catbutton.attr('class','updatedisabled');
				catbutton.unbind('click');
			});
		}
}

function col_update(coldiv){
if (coldiv.attr('status')=='unchanged'){
			coldiv.css("-moz-box-shadow","0 0 5px 5px #f00");
			coldiv.css("-webkit-box-shadow","0 0 5px 5px #f00");
			coldiv.css("box-shadow","0 0 5px 5px #f00");
			coldiv.effect("highlight",500);
			coldiv.attr('status','changed');
			colbutton=$("#updatecolony");
			colbutton.attr('disabled','');
			colbutton.attr('class','update');
			colbutton.click(function(){
				if ($("#removal").attr('checked')){removal=1;}else{removal=0;}
				colony_ID=$("#colony_id").val();
				Date_TNR=$("#date_tnr"+colony_ID+"-y").val()+'-'+$("#date_tnr"+colony_ID+"-m").val()+'-'+$("#date_tnr"+colony_ID+"-d").val();
				$.post("inc/db/updatedb.php",{
					table:'colony',
					ID:colony_ID,
					Lead_Trapper:$("#lead_trapper").val(),
					Asst_Trapper:$("#asst_trapper").val(),
					Setting:$("#colony_location").val(),
					Street:$("#colony_street").val(),
					Cross_Street:$("#cross_street").val(),
					Ward:$("#ward").val(),
					County:$("#county").val(),
					Zip:$("#colony_zip").val(),
					Location_Notes:$("#location_notes").val(),
					Date_TNR:Date_TNR,
					Year_Formed:$("#year_formed").val(),
					Vet_ID:$("#vet").val(),
					Removal:removal,
					Removal_Date:$("#removal_date").val(),
				},function(data){
					if (data!=''){
						alert("There was a problem updating the database: "+data);
					}			
				});
				coldiv.css("-moz-box-shadow","");
				coldiv.css("-webkit-box-shadow","");
				coldiv.css("box-shadow","");
				coldiv.effect("highlight",1000);
				coldiv.attr('status','unchanged');
				colbutton.attr('disabled','disabled');
				colbutton.attr('class','updatedisabled');
				colbutton.unbind('click');
			});
		}
}


function init_ct_change(){
	$(".ctinput").keyup(function(){
		ctdiv=$(this).closest('div').parent('div');
		ctid=ctdiv.attr('ID').substr(9);
		if (ctdiv.attr('status')=='unchanged'){
			ctdiv.css("-moz-box-shadow","0 0 5px 5px #f00");
			ctdiv.css("-webkit-box-shadow","0 0 5px 5px #f00");
			ctdiv.css("box-shadow","0 0 5px 5px #f00");
			ctdiv.effect("highlight",1000);
			ctdiv.attr('status','changed');
			ctbutton=$("#updatecaretaker"+ctid);
			ctbutton.attr('disabled','');
			ctbutton.attr('class','update');
			ctbutton.click(function(){
				$.post("inc/db/updatedb.php",{
				table:'caretaker',
				ID:ctid,
				Name:$("#ctname"+ctid).val(),
				Street:$("#ctstreet"+ctid).val(),
				City:$("#ctcity"+ctid).val(),
				State:$("#ctstate"+ctid).val(),
				Zip:$("#ctzip"+ctid).val(),
				Phone:$("#ctphone"+ctid).val(),
				Email:$("#ctemail"+ctid).val(),
				},function(data){
					if (data!=''){
						alert("There was a problem updating the database");
					}
				});
				ctdiv.css("-moz-box-shadow","");
				ctdiv.css("-webkit-box-shadow","");
				ctdiv.css("box-shadow","");
				ctdiv.effect("highlight",1000);
				ctdiv.attr('status','unchanged');
				ctbutton.attr('disabled','disabled');
				ctbutton.attr('class','updatedisabled');
				ctbutton.unbind('click');
			});
			
		}
	});
}

function init_ct_remove(){
	$(".ctremove").mouseup(function(){
		var answer= confirm('Remove this caretaker from this colony?');
		if (answer){
			ctid=$(this).attr('id');
			$.post("inc/db/deldb.php",{
							table:'colony_caretakers',
							Colony_ID:$("#colony_id").val(),
							Caretaker_ID:ctid,
						},function(){
							$("#caretaker"+ctid).fadeOut();
						});
		}
	});
}


function expand_box (type,itemId){
	$("#"+type+'exp'+itemId).html('-');
	document.getElementById(type+'exp'+itemId).onclick='';
	document.getElementById(type+'exp'+itemId).onclick=new Function("evt","close_box('"+type+"','"+itemId+"')");
	$("#"+type+'close'+itemId).hide();
	$("#"+type+'open'+itemId).fadeIn('slow');
	
}

function close_box (type,itemId) {
	if ($("#"+type+'exp'+itemId).length==0){ 
	alert("there is no link, so we're adding one right after #ctclose"+itemId);
		var closelink='<div class="ctexp" id="ctexp'+itemId+'">-</div>';
		$("#ctclose"+itemId).after(closelink);
	} 
	$("#"+type+'exp'+itemId).html('+');
	document.getElementById(type+'exp'+itemId).onclick='';
	document.getElementById(type+'exp'+itemId).onclick=new Function("evt","expand_box('"+type+"','"+itemId+"')");
	$("#"+type+'open'+itemId).fadeOut('fast',function(){
		$("#"+type+'close'+itemId).fadeIn();
	});
}	

function expand_all (type){
	exp=$("div[class^="+type+"open]");
	if (type=='ct'){
	$("#expandcts").unbind('click');
	$("#expandcts").click(function(){collapse_all('ct');});	
	$("#expandcts").html("Collapse All");
		for(var i=0;i<exp.length;i++){
			itemId=exp[i].getAttribute('id').substr(6);
			expand_box("ct",itemId); 
		}
	}
	if (type=='cat'){
	$("#expandcats").unbind('click');
	$("#expandcats").click(function(){collapse_all('cat');});	
	$("#expandcats").html("Collapse All");
		for(var i=0;i<exp.length;i++){
			itemId=exp[i].getAttribute('id').substr(7);
			expand_box("cat",itemId);
		}
	}
	
} 
	

function collapse_all (type){
	exp=$("div[class^="+type+"close]");
	if (type=='ct'){
	$("#expandcts").unbind('click');
	$("#expandcts").click(function(){expand_all('ct');});	
	$("#expandcts").html("Expand All");
		for(var i=0;i<exp.length;i++){
			itemId=exp[i].getAttribute('id').substr(7);
			close_box("ct",itemId); 
		}
	}
	if (type=='cat'){
	$("#expandcats").unbind('click');
	$("#expandcats").click(function(){expand_all('cat');});	
	$("#expandcats").html("Expand All");
		for(var i=0;i<exp.length;i++){
			itemId=exp[i].getAttribute('id').substr(8);
			close_box("cat",itemId);
		}
	}
}

function add_caretaker(id){
	ctform='<div class="caretaker" id="caretaker'+id+'" status="new">\
			<div id="ctclose'+id+'" class="ctclose"></div><div class="ctexp" id="ctexp'+id+'">-</div>\
			<div id="ctopen'+id+'" class="ctopen">\
			<b><label class="right" for="exct">Existing Caretaker:</b><br /><select id="exct">\
			</select><br />\
			<b>New Caretaker:</b><br />\
			<label class="right" for="ctname'+id+'">Name</label> <input type="text" size="20" name="caretaker_name" class="ctinput" id="ctname'+id+'" /><span class="notice2">Required</span><br />\
			<label class="right" for="ctstreet'+id+'">Street</label> <input type="text" size="20" name="street" class="ctinput" id="ctstreet'+id+'" /><br />\
			<label class="right" for="ctcity'+id+'">City</label> <input type="text" size="14" name="city" class="ctinput" id="ctcity'+id+'" /> <label for="ctstate'+id+'">State</label> <input type="text" size="3" name="state" class="ctinput" id="ctstate'+id+'" /><br />\
			<label class="right" for="ctzip'+id+'">Zip</label> <input type="text" size="6" name="zip" class="ctinput" id="ctzip'+id+'"/> <label for="ctphone'+id+'">Phone</label> <input type="text" size="10" name="phone" class="ctinput" id="ctphone'+id+'" /><br />\
			<label class="right" for="ctemail'+id+'">Email</label> <input type="email" size="28" name="email" class="ctinput" id="ctemail'+id+'" /><br /><br />\
			<button id="'+id+'" class="ctremove">remove caretaker</button>\
			<button id="savecaretaker'+id+'" class="updatedisabled" disabled="disabled">Save</button>\
			</div>\
			</div>';
	$(ctform).insertBefore($("#addcaretaker"));
	expand_box ("ct",id);
	init_ct_remove();
	$.get("inc/db/ct.php",{},function(data){
				$("#exct").html(data);
					document.getElementById('exct').onchange=function() {
					// this will fill out the form automatically, add the ct to the colony
					var ct_id=$(this).val();
					$.get("inc/db/get_ct.php",{ID:ct_id},function(data){
						var ct=eval ('('+data+')');
						$("#ctname"+id).val(ct.Name),
						$("#ctstreet"+id).val(ct.Street),
						$("#ctcity"+id).val(ct.City),
						$("#ctzip"+id).val(ct.Zip),
						$("#ctphone"+id).val(ct.Phone),
						$("#ctemail"+id).val(ct.Email)
					});
					$('#savecaretaker'+id).attr("disabled","");
					$('#savecaretaker'+id).attr("class","update");
					$('#savecaretaker'+id).unbind('click');
					$('#savecaretaker'+id).click(function(){
						$.post("inc/db/insertdb.php",{
							table:'caretaker_colony',
							Colony_ID:$("#colony_id").val(),
							Caretaker_ID:ct_id,
						},function(){
							$("#savecaretaker"+id).attr("disabled","disabled");
							$("#savecaretaker"+id).attr("class","updatedisabled");
							$("#ctclose"+id).html($("#ctname"+id).val());
							close_box('ct',id);
							init_ct_change();
							newacval=$("#addcaretaker").val()+1;
							$("#addcaretaker").val(newacval);
							$("#addcaretaker").show();
						});
					});
				};
			});
	
	$("#ctname"+id).keyup(function(){
		$("#savecaretaker"+id).attr("disabled","");
		$("#savecaretaker"+id).attr("class","update");
	});
	$("#addcaretaker").hide();
	$('#savecaretaker'+id).click(function(){
		$.post("inc/db/insertdb.php",{
			table:'caretaker',
			Colony_ID:$("#colony_id").val(),
			Name:$("#ctname"+id).val(),
			Street:$("#ctstreet"+id).val(),
			City:$("#ctcity"+id).val(),
			State:$("#ctstate"+id).val(),
			Zip:$("#ctzip"+id).val(),
			Phone:$("#ctphone"+id).val(),
			Email:$("#ctemail"+id).val()
		},function(data){
			var status = eval('(' + data + ')');
			if (status.status=='Y'){
				$("#savecaretaker"+id).attr("disabled","disabled");
				$("#savecaretaker"+id).attr("class","updatedisabled");
				$("#ctclose"+id).html($("#ctname"+id).val());
				close_box('ct',id);
				init_ct_change();
				$("#addcaretaker").show();
				newacval=$("#addcaretaker").val()+1;
				$("#addcaretaker").val(newacval);
			} else {
				alert ('there was a problem updating the database: '+data);
			}
		});
				
	});
}	

function init_vet_links(){
	vetedit=$("span[id^=edit]");
	for(var i=0;i<vetedit.length;i++){
		vetid=vetedit[i].getAttribute('id').substr(4);
		vetedit[i].onclick=new Function("evt","edit_vet("+vetid+")");
	}
	vetdel=$("span[id^=delete]");
		for(var i=0;i<vetdel.length;i++){
		vetid=vetdel[i].getAttribute('id').substr(6);
		vetdel[i].onclick=new Function("evt","delete_vet("+vetid+")");
	}
}

function edit_vet (id){
	editform='<label for="form_vetname"'+id+'">Name</label> <input type="text" size="20" name="vetname" id="form_vetname'+id+'" value="'+$('#vetname'+id).html()+'"><br />\
	<label for="form_clinic"'+id+'">Clinic</label> <input type="text" size="20" name="clinic" id="form_clinic'+id+'" value="'+$('#clinic'+id).html()+'"><br />\
	<label for="form_phone"'+id+'">Phone</label> <input type="text" size="20" name="phone" id="form_phone'+id+'" value="'+$('#phone'+id).html()+'">\
	<button id="vet_submit'+id+'" class="update">Save</button>';
	$("#vet_form"+id).fadeOut(200,function(){;
		$("#vet_form"+id).after(editform);
		$("#vet_submit"+id).click(function(){
			$.post("inc/db/updatedb.php",{
				table:'vet',
				ID:id,
				Name:$("#form_vetname"+id).val(),
				Clinic:$("#form_clinic"+id).val(),
				Phone:$("#form_phone"+id).val(),
			},function(data){
				if (data==''){
					window.location = document.location.href;
				} else {
					alert ('there was a problem updating the database: '+data);
				}
			});
		});
	});
}

function delete_vet(id){
	var delete_check=confirm("Really delete this vet?  This action cannot be undone");
	if (delete_check){
		$.post("inc/db/deldb.php",{
			table:'vet',
			ID:id},function(data){
				if (data==''){	
					$("#outer_vet_form"+id).fadeOut();
				} else {
					alert ("There was a problem updating the database: "+data);
				}
			});
		}
}

function load_meta(){
	colony_id=$("#colony_id").val();
	$.get("inc/db/load_meta.php",{
				ID:colony_id},function(data){
		var meta = eval('(' + data + ')');
		$("#meta1").html(meta.meta1);
		$("#meta2").html(meta.meta2);
		$("#meta3").html(meta.meta3);
		$("#meta4").html(meta.meta4);
		$("#meta5").html(meta.meta5);
		$("#meta6").html(meta.meta6);
		$("#meta7").html(meta.meta7);
	});
}

function init_user(){
	$("#usersave").click(function(){
		pass=1;
		trap=0;
		volun=0;
		admin=0;
		if ($("#trap").attr('checked')){trap=1;}
		if ($("#admin").attr('checked')){admin=1;}
		if ($("#volun").attr('checked')){volun=1;}
		if ($("#name").val()==''){pass=0;}
		if ($("#username").val()==''){pass=0;}
		if ($("#password")==''){pass=0;}
		if ($("#password").val()!=$("#checkpassword").val()){pass=0;}
		if (trap==0&&admin==0&&volun==0){pass=0;}
		if (pass==1){
			$.post("inc/save_user.php",{
				name:$("#name").val(),
				username:$("#username").val(),
				password:$("#password").val(),
				type:$("#usertype").val(),
				trap:trap,
				volun:volun,
				admin:admin,
				email:$("#email").val()},function(data){
					if (data==''){	
						window.location = document.location.href;
					} else {
						alert ("There was a problem updating the database: "+data);
					}
			});
		} else {
			if ($("#name").val()==''){ $("#name").effect("highlight","1000"); }
			if ($("#username").val()==''){ $("#username").effect("highlight","1000"); }
			if ($("#password").val()==''){$("#password").effect("highlight","1000"); }
			if ($("#password").val()!=$("#checkpassword").val()){ alert('Passwords do not match.');}
			if (trap==0&&volun==0&&admin==0){ alert ("You must choose at least one User Type");}
		}
	});
}

function init_user_edit(){
	$("#useredit").click(function(){
		trap=0;
		volun=0;
		admin=0;
		if ($("#trap").attr('checked')){trap=1;}
		if ($("#admin").attr('checked')){admin=1;}
		if ($("#volun").attr('checked')){volun=1;}
		if (t=='a'){
			if ($("#password").val()!=''){
				if ($("#name").val()!=''&&$("#username").val()!=''&&$("#password")!=''&&$("#password").val()==$("#checkpassword").val()){
					$.post("inc/update_user.php",{
					name:$("#name").val(),
					username:$("#username").val(),                                                                                                 
					password:$("#password").val(),
					utype:t,
					trap:trap,
					volun:volun,
					admin:admin,
					ID:ID,
					email:$("#email").val()},function(data){
						if (data==''){	
							$("#status").html('User Updated');
						} else {
							alert ("There was a problem updating the database: "+data);
						}
					});
				} else {
					if ($("#name").val()==''){ $("#name").effect("highlight","1000"); }
					if ($("#username").val()==''){ $("#username").effect("highlight","1000"); }
					if ($("#password").val()==''){$("#password").effect("highlight","1000"); }
					if ($("#password").val()!=$("#checkpassword").val()){ alert('Passwords do not match.');}
				};
			} else {
				if ($("#name").val()!=''&&$("#username").val()){
					$.post("inc/update_user.php",{
						name:$("#name").val(),
						username:$("#username").val(),
						trap:trap,
						volun:volun,
						admin:admin,
						utype:t,
						ID:ID,
						email:$("#email").val()},function(data){
							if (data==''){	
								$("#status").html('User Updated');
							} else {
								alert ("There was a problem updating the database: "+data);
							}
					});
				} else {
					if ($("#name").val()==''){ $("#name").effect("highlight","1000"); }
					if ($("#username").val()==''){ $("#username").effect("highlight","1000"); }
				}
			}
		} else {
			if ($("#password").val()!=''){
				if ($("#name").val()!=''&&$("#username").val()!=''&&$("#password")!=''&&$("#password").val()==$("#checkpassword").val()){
					$.post("inc/update_user.php",{
						name:$("#name").val(),
						username:$("#username").val(),
						password:$("#password").val(),
						ID:ID,
						email:$("#email").val()},function(data){
							if (data==''){	
								$("#status").html('User Updated');
							} else {
								alert ("There was a problem updating the database: "+data);
							}
					});
				} else {
					if ($("#name").val()==''){ $("#name").effect("highlight","1000"); }
					if ($("#username").val()==''){ $("#username").effect("highlight","1000"); }
					if ($("#password").val()==''){$("#password").effect("highlight","1000"); }
					if ($("#password").val()!=$("#checkpassword").val()){ alert('Passwords do not match.');}
				};
			} else {
				if ($("#name").val()!=''&&$("#username").val()){
					$.post("inc/update_user.php",{
						name:$("#name").val(),
						username:$("#username").val(),
						ID:ID,
						email:$("#email").val()},function(data){
							if (data==''){	
								$("#status").html('User Updated');
							} else {
								alert ("There was a problem updating the database: "+data);
							}
					});
				} else {
					if ($("#name").val()==''){ $("#name").effect("highlight","1000"); }
					if ($("#username").val()==''){ $("#username").effect("highlight","1000"); }
				}
			}
		}
	});
}
-->


