	/*
	**  Functions for manupulating and rendering the data
	**  irv-software c 2009
	**  version 0.3 - 25.06.2009
	**  frameworks: jQuery 1.3.2
	**  frameworks: jQuey.autocomplete 
	*/
	
	//var initialization
	var aktuelleorggroup="0";					//actual organism group index - 0...Macro-invertebrates
																		//															1...Fish
      															//															2...Phytoplankton
      															//															3...Macrophytes
      															//															4...Diatoms
	var tettlcount=new Array();       //array for count of taxa for each organism group
	tettlcount[0]=0;
	tettlcount[1]=0;
	tettlcount[2]=0;
	tettlcount[3]=0;
	tettlcount[4]=0;
	var tetsamplecount=0;             //count of samples

 	//ajax functions
 	$(document).ready(function(){

		tettlcount[0]=0;
		tettlcount[1]=0;
		tettlcount[2]=0;
		tettlcount[3]=0;
		tettlcount[4]=0;
		tetsamplecount=1;
		aktuelleorggroup="0";
				
		$("#txtgenus_id").val("0");
		$("#txtspecies_id").val("0");
		$("#samplecount").val("1");
		$("#txtspecies").val("");
		$("#txtgenus").val("");

 		for (var j=0;j<5;j++) {
			//tettlsetsamplecols(document.forms["tetsample"].samplecount.value, document.forms["tetsample"].sampleunit.value,j)
			//tettlsetsamplecols(document.forms["tetsample"].samplecount.value, "Insert unit",j);
			tettlsetsamplecols(document.forms["tetsample"].samplecount.options[document.forms["tetsample"].samplecount.selectedIndex].text, "Insert unit",j);
			
		}
		rendertabheader(aktuelleorggroup);

  });
	//$.ready - Ende 


	function txtspeciesas (aindex) {
		$("#txtspecies"+aindex).flushCache().autocomplete("as_taxa.php", {
				minChars: 1,
				width: 200,
				cacheLength:1,
				matchSubset:1,
				matchContains:1,
				max:100,
				extraParams: {
					getSpeciesByLetters: 1,
					orggroup: function() { return aktuelleorggroup; },
					ID_GEN: function() { return $("#txtgenus_id"+aindex).val(); }
   			}
		});
		$("#txtspecies"+aindex).result(function(event, data, formatted) {
			if (data) {
				$("#txtspecies_id"+aindex).val(data[1]);
				if (aindex != "") {
					$("#chkExclude"+aindex).val("1");
				}
			}
		});
	}

	function txtgenusas (aindex) {
			$("#txtgenus"+aindex).autocomplete("as_taxa.php", {
				minChars: 3,
				width: 200,
				cacheLength:1,
				matchSubset:1,
				matchContains:1,
				max:100,
				extraParams: {
					getGenusByLetters: 1,
					orggroup: function() { return aktuelleorggroup; }
   			}
		});
		$("#txtgenus"+aindex).result(function(event, data, formatted) {
			if (data) {
				$("#txtspecies_id"+aindex).val("0");
				$("#txtspecies"+aindex).val("");
				$("#txtgenus_id"+aindex).val(data[1]);
			}
		});
	}	

	function getTaxonByID () {
		if ($("#txtspecies_id").val() > 0) {
			$.ajax({
		  	type: "GET",
		  	async: false,
		  	url: "as_taxa.php",
		  	data: "getTaxonByID=1&orggroup="+aktuelleorggroup+"&ID_ART="+$("#txtspecies_id").val(),
		  	success: function(data){
		    	tettaxon=unserialize(data);
		    	sertaxon=data;
		  	}
			});
			var actorggroup=aktuelleorggroup;
			//alert(sertaxon);
			//alert(tettaxon["Author"]);
			//Values for organism group specific
			switch (actorggroup) {
				case "0":
					//mzb
					tettaxalist[actorggroup][tettlcount[actorggroup]] = new Object();
					tettaxalist[actorggroup][tettlcount[actorggroup]]["taxagroup_rf"] = tettaxon["TaxaGroup_RF"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["taxagroup"] = tettaxon["TaxaGroup"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["family"] = tettaxon["Family"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["genus"] = tettaxon["Genus"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["species"] = tettaxon["Species"];
					if (tettaxon["Author"] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = html_entity_decode(tettaxon["Author"]);}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"]      = new Array();
					tettaxalist[actorggroup][tettlcount[actorggroup]]["samplelist"] = new Array();
		  		for (var j=0;j<tetsamplecount;j++) {
						tettaxalist[actorggroup][tettlcount[actorggroup]]["samplelist"][j] = "";
					}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["subfamily"] = '';
					if ((tettaxon["TaxaGroup"] != "Ephemeroptera") &&
						 (tettaxon["TaxaGroup"] != "Plecoptera") &&
						 (tettaxon["TaxaGroup"] != "Trichoptera")) {
						tettaxalist[actorggroup][tettlcount[actorggroup]]["subfamily"] = tettaxon["Subfamily"];
					}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["shortcode"] = tettaxon["Shortcode"];
					for (var i=0;i<=tetmzbcodelist.length-1;i++) {
						if (tettaxon[tetmzbcodelist[i]["fieldname"]] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"][i] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"][i] = tettaxon[tetmzbcodelist[i]["fieldname"]];}
					}
					break;
				case "1":
					//fish
					tettaxalist[actorggroup][tettlcount[actorggroup]] = new Object();
					tettaxalist[actorggroup][tettlcount[actorggroup]]["taxagroup_rf"] = tettaxon["TaxaGroup_RF"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["taxagroup"] = tettaxon["TaxaGroup"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["family"] = tettaxon["Family"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["genus"] = tettaxon["Genus"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["species"] = tettaxon["Species"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = "";
					if (tettaxon["Author"] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = html_entity_decode(tettaxon["Author"]);}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"]      = new Array();
					tettaxalist[actorggroup][tettlcount[actorggroup]]["samplelist"] = new Array();
		  		for (var j=0;j<tetsamplecount;j++) {
						tettaxalist[actorggroup][tettlcount[actorggroup]]["samplelist"][j] = "";
					}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["shortcode"] = "";
					if (tettaxon["ID_ART"] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["shortcode"] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["shortcode"] = tettaxon["ID_ART"];}
					for (var i=0;i<=tetfishcodelist.length-1;i++) {
						if (tettaxon[tetfishcodelist[i]["fieldname"]] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"][i] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"][i] = tettaxon[tetfishcodelist[i]["fieldname"]];}
					}
					break;
				case "2":
					//phytoplankton
					tettaxalist[actorggroup][tettlcount[actorggroup]] = new Object();
					tettaxalist[actorggroup][tettlcount[actorggroup]]["taxagroup_rf"] = 0;
					tettaxalist[actorggroup][tettlcount[actorggroup]]["taxagroup"] = tettaxon["Class"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["family"] = tettaxon["Order"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["genus"] = tettaxon["Genus"];
					if (tettaxon["Variety"] === null) {	
						tettaxalist[actorggroup][tettlcount[actorggroup]]["species"] = tettaxon["Species"];
					} else {
						tettaxalist[actorggroup][tettlcount[actorggroup]]["species"] = tettaxon["Species"]+' '+tettaxon["Variety"];
					}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = "";
					if (tettaxon["Author"] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = html_entity_decode(tettaxon["Author"]);}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"]      = new Array();
					tettaxalist[actorggroup][tettlcount[actorggroup]]["samplelist"] = new Array();
		  		for (var j=0;j<tetsamplecount;j++) {
						tettaxalist[actorggroup][tettlcount[actorggroup]]["samplelist"][j] = "";
					}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["shortcode"] = tettaxon["RebeccaID"];
					//for (var i=0;i<=tetphpcodelist.length-1;i++) {
					//	if (tettaxon[tetphpcodelist[i]["fieldname"]] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"][i] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"][i] = tettaxon[tetphpcodelist[i]["fieldname"]];}
					//}
					break;
				case "3":
					//macrophyten
					tettaxalist[actorggroup][tettlcount[actorggroup]] = new Object();
					tettaxalist[actorggroup][tettlcount[actorggroup]]["taxagroup_rf"] = 0;
					tettaxalist[actorggroup][tettlcount[actorggroup]]["taxagroup"] = "";
					tettaxalist[actorggroup][tettlcount[actorggroup]]["family"] = tettaxon["Family"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["genus"] = tettaxon["Genus"];
					if (tettaxon["Subspecies"] === null) {	
						tettaxalist[actorggroup][tettlcount[actorggroup]]["species"] = tettaxon["Species"];
					} else {
						tettaxalist[actorggroup][tettlcount[actorggroup]]["species"] = tettaxon["Species"]+' '+tettaxon["Subspecies"];
					}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = "";
					if (tettaxon["Authority"] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = html_entity_decode(tettaxon["Authority"]);}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"]      = new Array();
					tettaxalist[actorggroup][tettlcount[actorggroup]]["samplelist"] = new Array();
		  		for (var j=0;j<tetsamplecount;j++) {
						tettaxalist[actorggroup][tettlcount[actorggroup]]["samplelist"][j] = "";
					}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["shortcode"] = "";
					tettaxalist[actorggroup][tettlcount[actorggroup]]["shortcode"] = tettaxon["TaxonCode"];
					//for (var i=0;i<=tetmphcodelist.length-1;i++) {
					//	if (tettaxon[tetmphcodelist[i]["fieldname"]] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"][i] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"][i] = tettaxon[tetmphcodelist[i]["fieldname"]];}
					//}
					break;
				case "4":
					//diatomens
					tettaxalist[actorggroup][tettlcount[actorggroup]] = new Object();
					tettaxalist[actorggroup][tettlcount[actorggroup]]["taxagroup_rf"] = tettaxon["TaxaGroup_RF"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["taxagroup"] = tettaxon["Taxagroup"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["family"] = tettaxon["Family"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["genus"] = tettaxon["Genus"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["species"] = tettaxon["Species"];
					tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = "";
					//alert(tettaxon["Author"]);
					if (tettaxon["Author"] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = html_entity_decode(tettaxon["Author"]);}
					//alert(tettaxalist[actorggroup][tettlcount[actorggroup]]["author"]);
					if (tettaxon["ssp_var_mor_fo"] === null) {
					} else {	
						tettaxalist[actorggroup][tettlcount[actorggroup]]["species"] = tettaxalist[actorggroup][tettlcount[actorggroup]]["species"]+' '+tettaxon["ssp_var_mor_fo"]+' '+tettaxon["ssp_var_mor_fo_aff_name"];
						tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = tettaxalist[actorggroup][tettlcount[actorggroup]]["author"]+' '+html_entity_decode(tettaxon["Author_ssp"]);
					}
					if (tettaxon["2nd_var_mor_fo"] === null) {
					} else {	
						tettaxalist[actorggroup][tettlcount[actorggroup]]["species"] = tettaxalist[actorggroup][tettlcount[actorggroup]]["species"]+' '+tettaxon["2nd_var_mor_fo"]+' '+tettaxon["2nd_var_mor_fo_name"];
						tettaxalist[actorggroup][tettlcount[actorggroup]]["author"] = tettaxalist[actorggroup][tettlcount[actorggroup]]["author"]+' '+html_entity_decode(tettaxon["Author_2nd"]);
					}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"]      = new Array();
					tettaxalist[actorggroup][tettlcount[actorggroup]]["samplelist"] = new Array();
		  		for (var j=0;j<tetsamplecount;j++) {
						tettaxalist[actorggroup][tettlcount[actorggroup]]["samplelist"][j] = "";
					}
					tettaxalist[actorggroup][tettlcount[actorggroup]]["shortcode"] = tettaxon["Valid_Code"];
					//for (var i=0;i<=tetfishcodelist.length-1;i++) {
					//	if (tettaxon[tetfishcodelist[i]["fieldname"]] === null) {tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"][i] = "";} else {tettaxalist[actorggroup][tettlcount[actorggroup]]["codelist"][i] = tettaxon[tetfishcodelist[i]["fieldname"]];}
					//}
					break;
			}
			tettaxalist[actorggroup][tettlcount[actorggroup]]["cf"]  = "0";
			tettaxalist[actorggroup][tettlcount[actorggroup]]["juv"] = "0";
			tettlcount[actorggroup]++;
			tettaxalist[actorggroup].sort(sorttaxalist);
			rendertabtl();
		}
	}

	function DeleteTaxonByID(aindex) {
		var actorggroup=aktuelleorggroup;
		tettaxalist[actorggroup].splice(aindex,1);		
		rendertabtl();
	}


	// var initialization
	var Zeile = 1;

	var tetmzbcodelist = new Array();										//Codelist for mzb
	tetmzbcodelist[0]   					 = new Object();
	tetmzbcodelist[0]["title"]   	 = "ID-AQEM";
	tetmzbcodelist[0]["fieldname"] = "ID_ART";
	tetmzbcodelist[1]   					 = new Object();
	tetmzbcodelist[1]["title"]   	 = "DV-No";
	tetmzbcodelist[1]["fieldname"] = "DINNo";
	tetmzbcodelist[2]   					 = new Object();
	tetmzbcodelist[2]["title"]   	 = "TCM-Code";
	tetmzbcodelist[2]["fieldname"] = "TCM_Code";
	tetmzbcodelist[3]   					 = new Object();
	tetmzbcodelist[3]["title"]   	 = "Furse-Code";
	tetmzbcodelist[3]["fieldname"] = "Furse_Code";
	tetmzbcodelist[4]   					 = new Object();
	tetmzbcodelist[4]["title"]   	 = "Perla-Code";
	tetmzbcodelist[4]["fieldname"] = "Perla_Code";

	var tetfishcodelist  = new Array();										//Codelist for fish
	tetfishcodelist[0]   					  = new Object();
	tetfishcodelist[0]["title"]     = "Shortcode-WISER";
	tetfishcodelist[0]["fieldname"] = "Shortcode_wiser";
	tetfishcodelist[1]   					  = new Object();
	tetfishcodelist[1]["title"]     = "Shortcode-AT";
	tetfishcodelist[1]["fieldname"] = "Shortcode_AT";
	tetfishcodelist[2]   					  = new Object();
	tetfishcodelist[2]["title"]   	= "Shortcode-FR";
	tetfishcodelist[2]["fieldname"] = "Shortcode_FR";
	tetfishcodelist[3]   					  = new Object();
	tetfishcodelist[3]["title"]   	= "German-Name";
	tetfishcodelist[3]["fieldname"] = "German_name";
	tetfishcodelist[4]   					  = new Object();
	tetfishcodelist[4]["title"]   	= "English-name";
	tetfishcodelist[4]["fieldname"] = "English_name";

	var tetphpcodelist  = new Array();										//Codelist for phytoplankton
	tetphpcodelist[0]   					  = new Object();
	//tetphpcodelist[0]["title"]     = "REBECCA-ID";
	//tetphpcodelist[0]["fieldname"] = "RebeccaID";

	var tetmphcodelist  = new Array();										//Codelist for macrophyten
	tetmphcodelist[0]   					  = new Object();
	//tetmphcodelist[0]["title"]     = "Taxoncode";
	//tetmphcodelist[0]["fieldname"] = "TaxonCode";

	var tetdiacodelist  = new Array();										//Codelist for diatomens
	tetdiacodelist[0]   					  = new Object();

	var tettaxalist = new Array();												//array taxalist for each organism group
	tettaxalist[0] = new Array();
	tettaxalist[1] = new Array();
	tettaxalist[2] = new Array();
	tettaxalist[3] = new Array();
	tettaxalist[4] = new Array();

	var tettlheader = new Array();												//array taxalist header for each organism group

	tettlheader[0]                  = new Object();
	tettlheader[0]["taxon"]         = "Taxon";
	tettlheader[0]["author"]        = "Author";
	tettlheader[0]["shortcode"]     = "Shortcode";
	tettlheader[0]["codelist"]      = new Array();
	for (var i=0;i<=tetmzbcodelist.length-1;i++) {
		tettlheader[0]["codelist"][i]   = tetmzbcodelist[i]["title"];
	}
	tettlheader[0]["cf"]            = "cf.";
	tettlheader[0]["juv"]           = "juv.";
	tettlheader[0]["samplelist"]    = new Array();
	tettlheader[0]["samplelist"][0] = new Array();
	tettlheader[0]["samplelist"][0][0] = "Sample 1";
	tettlheader[0]["samplelist"][0][1] = "Insert unit";
	
	tettlheader[1]                  = new Object();
	tettlheader[1]["taxon"]         = "Taxon";
	tettlheader[1]["author"]        = "Author";
	tettlheader[1]["shortcode"]     = "ID-EL";
	tettlheader[1]["codelist"]      = new Array();
	for (var i=0;i<=tetfishcodelist.length-1;i++) {
		tettlheader[1]["codelist"][i]   = tetfishcodelist[i]["title"];
	}
	tettlheader[1]["cf"]            = "cf.";
	tettlheader[1]["juv"]           = "juv.";
	tettlheader[1]["samplelist"]    = new Array();
	tettlheader[1]["samplelist"][0] = new Array();
	tettlheader[1]["samplelist"][0][0] = "Sample 1";
	tettlheader[1]["samplelist"][0][1] = "Insert unit";

	tettlheader[2]                  = new Object();
	tettlheader[2]["taxon"]         = "Taxon";
	tettlheader[2]["author"]        = "Author";
	tettlheader[2]["shortcode"]     = "REBECCA-ID";
	//tettlheader[2]["codelist"]      = new Array();
	//for (var i=0;i<=tetmzbcodelist.length-1;i++) {
	//	tettlheader[2]["codelist"][i]   = tetphpcodelist[i]["title"];
	//}
	tettlheader[2]["cf"]            = "cf.";
	tettlheader[2]["juv"]           = "juv.";
	tettlheader[2]["samplelist"]    = new Array();
	tettlheader[2]["samplelist"][0] = new Array();
	tettlheader[2]["samplelist"][0][0] = "Sample 1";
	tettlheader[2]["samplelist"][0][1] = "Insert unit";

	tettlheader[3]                  = new Object();
	tettlheader[3]["taxon"]         = "Taxon";
	tettlheader[3]["author"]        = "Author";
	tettlheader[3]["shortcode"]     = "Taxoncode";
	//tettlheader[3]["codelist"]      = new Array();
	//for (var i=0;i<=tetmzbcodelist.length-1;i++) {
	//	tettlheader[3]["codelist"][i]   = tetmphcodelist[i]["title"];
	//}
	tettlheader[3]["cf"]            = "cf.";
	tettlheader[3]["juv"]           = "juv.";
	tettlheader[3]["samplelist"]    = new Array();
	tettlheader[3]["samplelist"][0] = new Array();
	tettlheader[3]["samplelist"][0][0] = "Sample 1";
	tettlheader[3]["samplelist"][0][1] = "Insert unit";

	tettlheader[4]                  = new Object();
	tettlheader[4]["taxon"]         = "Taxon";
	tettlheader[4]["author"]        = "Author";
	tettlheader[4]["shortcode"]     = "Validcode";
	//tettlheader[4]["codelist"]      = new Array();
	//for (var i=0;i<=tetdiacodelist.length-1;i++) {
	//	tettlheader[4]["codelist"][i]   = tetdiacodelist[i]["title"];
	//}
	tettlheader[4]["cf"]            = "cf.";
	tettlheader[4]["juv"]           = "juv.";
	tettlheader[4]["samplelist"]    = new Array();
	tettlheader[4]["samplelist"][0] = new Array();
	tettlheader[4]["samplelist"][0][0] = "Sample 1";
	tettlheader[4]["samplelist"][0][1] = "Insert unit";

	var tettaxagrouplast = "";														//last taxagroup for table rendering
	var tetfamilylast = "";																//last family for table rendering
	var tetsubfamilylast = "";														//last subfamily for table rendering
	var tetrowoffset = 0;

//function for deleting all rows of the table
function tabdelallrows(actorggroup) {
	tabelle=document.getElementById('tabtaxalist'+actorggroup);
	for (var i=tabelle.rows.length-1;i>=0;i--) {
		tabelle.deleteRow(i);
	}
	
	Zeile=0;
}

function foo() {alert("Test");}
function foo1(a) {alert(a);}

function valuetoarr(tabcode) {
	//alert(tabcode);
	if ((tabcode=="sampleunit") || (tabcode=="samplename")) {
		for (var i=0;i<=4;i++) {	//organism group
			for (var j=0;j<tettlheader[i]["samplelist"].length;j++) {	//samplelist
				obj = document.getElementById(tabcode + i + "_" + j);
				if (tabcode=="samplename") {tettlheader[i]["samplelist"][j]["0"]=obj.value;}
				if (tabcode=="sampleunit") {tettlheader[i]["samplelist"][j]["1"]=obj.value;}
			}
		}
	}
	if (tabcode=="sample_juv") {
		for (var i=0;i<=4;i++) {	//organism group
			for (var j=0;j<=tettaxalist[i].length-1;j++) {	//taxalist
				obj = document.getElementById(tabcode + i + "_" + j);
				//if (tabcode=="sample_cf") {if (obj.checked) {tettaxalist[i][j]["cf"]="1";} else {tettaxalist[i][j]["cf"]="0";}}
				if (tabcode=="sample_juv") {if (obj.checked) {tettaxalist[i][j]["juv"]="1";} else {tettaxalist[i][j]["juv"]="0";}}
			}
		}
	}
	if (tabcode=="sample_cf") {
		for (var i=0;i<=4;i++) {	//organism group
			for (var j=0;j<=tettaxalist[i].length-1;j++) {	//taxalist
				obj = document.getElementById(tabcode + i + "_" + j);
				if (tabcode=="sample_cf") {if (obj.checked) {tettaxalist[i][j]["cf"]="1";} else {tettaxalist[i][j]["cf"]="0";}}
				//if (tabcode=="sample_juv") {if (obj.checked) {tettaxalist[i][j]["juv"]="1";} else {tettaxalist[i][i]["juv"]="0";}}
			}
		}
	}
	if (tabcode=="samplevalue") {
		for (var i=0;i<=4;i++) {	//organism group
			for (var j=0;j<=tettaxalist[i].length-1;j++) {	//taxalist
				for (var k=0;k<tetsamplecount;k++) {	//samplelist
					obj = document.getElementById(tabcode + i + "_" + j + "_" + k);
					tettaxalist[i][j]["samplelist"][k] = obj.value;
				}
			}
		}
	}
}

//functions for rendering data
function rendertabtl() {
	for (var j=0;j<5;j++) {
		rendertabheader(j);
		tabdelallrows(j);
		tettaxagrouplast = "";
		tetfamilylast = "";
		tetsubfamilylast = "";
		tetrowoffset = 0;
		tettable=document.getElementById('tabtaxalist'+j);
		for (var i=0;i<=tettaxalist[j].length-1;i++) {
			rendertaxonrow(tettable, i, j);
		}
	}
}

function rendertaxonrow(objtab, index, actorggroup) {
	var i = index+tetrowoffset;
	var TR = objtab.insertRow(i);
	if (tettaxagrouplast != tettaxalist[actorggroup][index]["taxagroup"]) {
	  var TD = document.createElement("td");
  	var TDtext = document.createTextNode(tettaxalist[actorggroup][index]["taxagroup"]);
  	TD.appendChild(TDtext);
  	TR.appendChild(TD);
		tettaxagrouplast = tettaxalist[actorggroup][index]["taxagroup"];
		tetfamilylast = "";
		tetsubfamilylast = "";
		tetrowoffset++;
	}
	var i = index+tetrowoffset;
	var TR = objtab.insertRow(i);
	if (tetfamilylast != tettaxalist[actorggroup][index]["family"]) {
	  var TD = document.createElement("td");
  	var TDtext = document.createTextNode(tettaxalist[actorggroup][index]["family"]);
  	TD.appendChild(TDtext);
  	TR.appendChild(TD);
		tetfamilylast = tettaxalist[actorggroup][index]["family"];
		tetsubfamilylast = "";
		tetrowoffset++;
	}
	
	if (actorggroup == "0") {
		var i = index+tetrowoffset;
		var TR = objtab.insertRow(i);
		if (tetsubfamilylast != tettaxalist[actorggroup][index]["subfamily"]) {
  		var TD = document.createElement("td");
	 		var TDtext = document.createTextNode(tettaxalist[actorggroup][index]["subfamily"]);
 			TD.appendChild(TDtext);
 			TR.appendChild(TD);
			tetsubfamilylast = tettaxalist[actorggroup][index]["subfamily"];
			tetrowoffset++;
		}
	}
	
	var i = index+tetrowoffset;
	var TR = objtab.insertRow(i);
	//Taxon
	var TD = document.createElement("td");
  var TDtext = document.createTextNode(tettaxalist[actorggroup][index]["genus"]+" "+tettaxalist[actorggroup][index]["species"]);
  TD.appendChild(TDtext);
  TR.appendChild(TD);
	var TD = document.createElement("td");
  var TDtext = document.createTextNode(tettaxalist[actorggroup][index]["author"]);
  TD.appendChild(TDtext);
  TR.appendChild(TD);
	var TD = document.createElement("td");
  var TDtext = document.createTextNode(tettaxalist[actorggroup][index]["shortcode"]);
  TD.appendChild(TDtext);
  TR.appendChild(TD);

	//Codelist
	if ($('#chkShowCodelist').attr('checked')) {
	  for (var j=0;j<tettaxalist[actorggroup][index]["codelist"].length;j++) {
			var TD = document.createElement("td");
	  	var TDtext = document.createTextNode(tettaxalist[actorggroup][index]["codelist"][j]);
	  	TD.appendChild(TDtext);
	  	TR.appendChild(TD);
	  }
	}

	//cf-Status
	var TD = document.createElement("td");
	var TDInput = null; 
	TDInput = document.createElement("input");
	var elatt = document.createAttribute("type");
	elatt.nodeValue = "checkbox";
	TDInput.setAttributeNode(elatt);
	var elatt = document.createAttribute("name");
	TDIname2 = "sample_cf"+actorggroup+"_"+index;
	elatt.nodeValue = TDIname2;
	TDInput.setAttributeNode(elatt);
	var elatt = document.createAttribute("id");
	elatt.nodeValue = TDIname2;
	TDInput.setAttributeNode(elatt);
	var elatt = document.createAttribute("value");
	elatt.nodeValue = "1";
	TDInput.setAttributeNode(elatt);

	if (tettaxalist[actorggroup][index]["cf"]=="1") {
		var elatt = document.createAttribute("checked");
		elatt.nodeValue = "checked";
		TDInput.setAttributeNode(elatt);
	}

	TDInput.onblur = function(){ valuetoarr("sample_cf");};
  TD.appendChild(TDInput);
  TR.appendChild(TD);


	//juv-Status
	var TD = document.createElement("td");
	var TDInput = document.createElement("input");
	var elatt = document.createAttribute("type");
	elatt.nodeValue = "checkbox";
	TDInput.setAttributeNode(elatt);
	var elatt = document.createAttribute("name");
	TDIname1 = "sample_juv"+actorggroup+"_"+index;
	elatt.nodeValue = TDIname1;
	TDInput.setAttributeNode(elatt);
	var elatt = document.createAttribute("id");
	elatt.nodeValue = TDIname1
	TDInput.setAttributeNode(elatt);
	var elatt = document.createAttribute("value");
	elatt.nodeValue = "1";
	TDInput.setAttributeNode(elatt);
	
	if (tettaxalist[actorggroup][index]["juv"]=="1") {
		var elatt = document.createAttribute("checked");
		elatt.nodeValue = "checked";
		TDInput.setAttributeNode(elatt);
	}
	
	TDInput.onblur = function(){ valuetoarr("sample_juv");};
  TD.appendChild(TDInput);
  TR.appendChild(TD);

  //Samples
  for (var j=0;j<tetsamplecount;j++) {
		var TD = document.createElement("td");
  	
		var TDInput = document.createElement("input");
		var elatt = document.createAttribute("type");
		elatt.nodeValue = "text";
		TDInput.setAttributeNode(elatt);
		var elatt = document.createAttribute("name");
		var TDIname = "samplevalue"+actorggroup+"_"+index+"_"+j;
		elatt.nodeValue = TDIname;
		TDInput.setAttributeNode(elatt);
		var elatt = document.createAttribute("class");
		elatt.nodeValue = "tet_text";
		TDInput.setAttributeNode(elatt);					

		var elatt = document.createAttribute("size");
		elatt.nodeValue = "10";
		TDInput.setAttributeNode(elatt);					
		var elatt = document.createAttribute("maxlength");
		elatt.nodeValue = "50";
		TDInput.setAttributeNode(elatt);					

		var elatt = document.createAttribute("id");
		elatt.nodeValue = TDIname;
		TDInput.setAttributeNode(elatt);
		var elatt = document.createAttribute("value");
		elatt.nodeValue = tettaxalist[actorggroup][index]["samplelist"][j];
		TDInput.setAttributeNode(elatt);
		TDInput.onblur = function(){ valuetoarr("samplevalue");};
	  TD.appendChild(TDInput);
  	TR.appendChild(TD);
  }
  
  //Delete-Button
  //	<input type="button" name="cmdtaxonenter" value="Enter" onClick="javascript:getTaxonByID();">
	var TD = document.createElement("td");
  	
	var TDInput = document.createElement("input");
	var elatt = document.createAttribute("type");
	elatt.nodeValue = "button";
	TDInput.setAttributeNode(elatt);
	var elatt = document.createAttribute("name");
	elatt.nodeValue = "taxondelete"+actorggroup+"_"+index;
	TDInput.setAttributeNode(elatt);
	var elatt = document.createAttribute("value");
	elatt.nodeValue = "Delete";
	TDInput.setAttributeNode(elatt);
	TDInput.onclick = function(){DeleteTaxonByID(index);};
	TD.appendChild(TDInput);
  TR.appendChild(TD);
}

function rendertabheader(aktorggroup) {
	var tabelle=document.getElementById('tabtl'+aktorggroup);
  tabelle.deleteTHead();
  var TH = tabelle.createTHead();
  var TR = TH.insertRow(0);
  for (var prop in tettlheader[aktorggroup]) {
  	if (typeof(tettlheader[aktorggroup][prop])=="object") {
  		if (prop=="samplelist") {
	  		for (i=0;i<tettlheader[aktorggroup][prop].length;i++) {
					var TD = document.createElement("th");
					var TDInput = document.createElement("input");
					var elatt = document.createAttribute("type");
					elatt.nodeValue = "text";
					TDInput.setAttributeNode(elatt);
					var elatt = document.createAttribute("name");
					var TDIname1 = "samplename"+aktorggroup+"_"+i;
					elatt.nodeValue = TDIname1;
					TDInput.setAttributeNode(elatt);
					var elatt = document.createAttribute("class");
					elatt.nodeValue = "tet_text";
					TDInput.setAttributeNode(elatt);					

					var elatt = document.createAttribute("size");
					elatt.nodeValue = "10";
					TDInput.setAttributeNode(elatt);					
					var elatt = document.createAttribute("maxlength");
					elatt.nodeValue = "50";
					TDInput.setAttributeNode(elatt);					

					var elatt = document.createAttribute("id");
					elatt.nodeValue = TDIname1;
					TDInput.setAttributeNode(elatt);
					var elatt = document.createAttribute("value");
					elatt.nodeValue = tettlheader[aktorggroup][prop][i][0];
					TDInput.setAttributeNode(elatt);
	  			TD.appendChild(TDInput);
	  			
					var TDInput = document.createElement("br");
	  			TD.appendChild(TDInput);

					var TDInput = document.createElement("input");
					var elatt = document.createAttribute("type");
					elatt.nodeValue = "text";
					TDInput.setAttributeNode(elatt);
					var elatt = document.createAttribute("name");
					var TDIname = "sampleunit"+aktorggroup+"_"+i;
					elatt.nodeValue = TDIname;
					TDInput.setAttributeNode(elatt);
					var elatt = document.createAttribute("class");
					elatt.nodeValue = "tet_text";
					TDInput.setAttributeNode(elatt);					

					var elatt = document.createAttribute("size");
					elatt.nodeValue = "10";
					TDInput.setAttributeNode(elatt);					
					var elatt = document.createAttribute("maxlength");
					elatt.nodeValue = "50";
					TDInput.setAttributeNode(elatt);					

					var elatt = document.createAttribute("id");
					elatt.nodeValue = TDIname;
					TDInput.setAttributeNode(elatt);
					var elatt = document.createAttribute("value");
					elatt.nodeValue = tettlheader[aktorggroup][prop][i][1];
					TDInput.setAttributeNode(elatt);
	  			TD.appendChild(TDInput);

	  			TR.appendChild(TD);
	  			//Event-Handler
					var obj = document.getElementById(TDIname);
					obj.onblur=function(){ valuetoarr("sampleunit");};
					var obj = document.getElementById(TDIname1);
					obj.onblur=function(){ valuetoarr("samplename");};
	  		}
	  	} else {

				if ($('#chkShowCodelist').attr('checked')) {
		  		for (i=0;i<tettlheader[aktorggroup][prop].length;i++) {
						var TD = document.createElement("th");
		  			var TDtext = document.createTextNode(tettlheader[aktorggroup][prop][i]);
		  			TD.appendChild(TDtext);
		  			TR.appendChild(TD);
		  		}
				}
	  	}
  	} else {
			var TD = document.createElement("th");
  		var TDtext = document.createTextNode(tettlheader[aktorggroup][prop]);
  		TD.appendChild(TDtext);
  		TR.appendChild(TD);
  	}
  }
  //Spalte für Delete-Button
	var TD = document.createElement("th");
  var TDtext = document.createTextNode("");
  TD.appendChild(TDtext);
  TR.appendChild(TD);
  
}

//function for setting the sample cols of the taxalist table
function tettlsetsamplecols(colcount, unitvalue, actorggroup) {

	if (typeof(tettlheader[actorggroup]["samplelist"])=='object') {
		var sc = tettlheader[actorggroup]["samplelist"].length;
	} else {
		var sc = 0;
	}
	if (colcount > sc) {
		for (i=sc;i<colcount;i++) {
			if (sc == 0) {
				tettlheader[actorggroup]["samplelist"]    = new Array();
				tettlheader[actorggroup]["samplelist"][0] = new Array();
				tettlheader[actorggroup]["samplelist"][0][0] = "Sample "+(i+1);
				tettlheader[actorggroup]["samplelist"][0][1] = unitvalue;
			} else {
				var arr = new Array();
				arr[0] = "Sample "+(i+1);
				arr[1] = unitvalue;
				var el = tettlheader[actorggroup]["samplelist"].push(arr);
				for (j=0;j<tettlcount[actorggroup];j++) {
					var el = tettaxalist[actorggroup][j]["samplelist"].push("");
				}
			}
		}
	}
	if (colcount < sc) {
		tettlheader[actorggroup]["samplelist"]=tettlheader[actorggroup]["samplelist"].slice(0,colcount);
		for (j=0;j<tettlcount[actorggroup];j++) {
			tettaxalist[actorggroup][j]["samplelist"].slice(0,colcount);
		}
	}
	//Unit for sample 1
	if (typeof(tettlheader[actorggroup]["samplelist"])=='object') {
		if (tettlheader[actorggroup]["samplelist"][0][1] == 'Insert unit') {
			tettlheader[actorggroup]["samplelist"][0][1] = unitvalue;
		}
	}
	
	tetsamplecount=colcount;
	rendertabtl();

}

//function for setting the up/down value
function inputupdown(objinput, min, max, step) {
	var i=parseInt(objinput.value);
	if (((i+step) >= min) && ((i+step) <= max)) {objinput.value=i+step;}
	//objinput.value=i+step;
}

function serialize( mixed_value ) {
    // Returns a string representation of variable (which can later be unserialized)  
    // 
    // version: 812.3015
    // discuss at: http://phpjs.org/functions/serialize
    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // +   improved by: Dino
    // +   bugfixed by: Andrej Pavlovic
    // +   bugfixed by: Garagoth
    // %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'
    var _getType = function( inp ) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            if (match = cons.match(/(\w+)\(/)) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';
    
    switch (type) {
        case "function": 
            val = ""; 
            break;
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
            val = "s:" + mixed_value.length + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            /*
            if (type == "object") {
                var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {
                    return;
                }
                objname[1] = serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }
            */
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") { 
                    continue; 
                }
                
                okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") val += ";";
    return val;
}

function unserialize(data){
    // Takes a string representation of variable and recreates it  
    // 
    // version: 903.3016
    // discuss at: http://phpjs.org/functions/unserialize
    // +     original by: Arpad Ray (mailto:arpad@php.net)
    // +     improved by: Pedro Tainha (http://www.pedrotainha.com)
    // +     bugfixed by: dptr1988
    // +      revised by: d3x
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brettz9.blogspot.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %            note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %            note: Aiming for PHP-compatibility, we have to translate objects to arrays 
    // *       example 1: unserialize('a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}');
    // *       returns 1: ['Kevin', 'van', 'Zonneveld']
    // *       example 2: unserialize('a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}');
    // *       returns 2: {firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'}
    
    var error = function (type, msg, filename, line){throw new window[type](msg, filename, line);};
    var read_until = function (data, offset, stopchr){
        var buf = [];
        var chr = data.slice(offset, offset + 1);
        var i = 2;
        while (chr != stopchr) {
            if ((i+offset) > data.length) {
                error('Error', 'Invalid');
            }
            buf.push(chr);
            chr = data.slice(offset + (i - 1),offset + i);
            i += 1;
        }
        return [buf.length, buf.join('')];
    };
    var read_chrs = function (data, offset, length){
        var buf;
        
        buf = [];
        for(var i = 0;i < length;i++){
            var chr = data.slice(offset + (i - 1),offset + i);
            buf.push(chr);
        }
        return [buf.length, buf.join('')];
    };
    var _unserialize = function (data, offset){
        var readdata;
        var readData;
        var chrs = 0;
        var ccount;
        var stringlength;
        var keyandchrs;
        var keys;

        if(!offset) offset = 0;
        var dtype = (data.slice(offset, offset + 1)).toLowerCase();
        
        var dataoffset = offset + 2;
        var typeconvert = new Function('x', 'return x');
        
        switch(dtype){
            case "i":
                typeconvert = new Function('x', 'return parseInt(x)');
                readData = read_until(data, dataoffset, ';');
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "b":
                typeconvert = new Function('x', 'return (parseInt(x) == 1)');
                readData = read_until(data, dataoffset, ';');
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "d":
                typeconvert = new Function('x', 'return parseFloat(x)');
                readData = read_until(data, dataoffset, ';');
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 1;
            break;
            case "n":
                readdata = null;
            break;
            case "s":
                ccount = read_until(data, dataoffset, ':');
                chrs = ccount[0];
                stringlength = ccount[1];
                dataoffset += chrs + 2;
                
                readData = read_chrs(data, dataoffset+1, parseInt(stringlength));
                chrs = readData[0];
                readdata = readData[1];
                dataoffset += chrs + 2;
                if(chrs != parseInt(stringlength) && chrs != readdata.length){
                    error('SyntaxError', 'String length mismatch');
                }
            break;
            case "a":
                readdata = {};
                
                keyandchrs = read_until(data, dataoffset, ':');
                chrs = keyandchrs[0];
                keys = keyandchrs[1];
                dataoffset += chrs + 2;
                
                for(var i = 0;i < parseInt(keys);i++){
                    var kprops = _unserialize(data, dataoffset);
                    var kchrs = kprops[1];
                    var key = kprops[2];
                    dataoffset += kchrs;
                    
                    var vprops = _unserialize(data, dataoffset);
                    var vchrs = vprops[1];
                    var value = vprops[2];
                    dataoffset += vchrs;
                    
                    readdata[key] = value;
                }
                
                dataoffset += 1;
            break;
            default:
                error('SyntaxError', 'Unknown / Unhandled data type(s): ' + dtype);
            break;
        }
        return [dtype, dataoffset - offset, typeconvert(readdata)];
    };
    return _unserialize(data, 0)[2];
}

//function for sorting the taxalist
function sorttaxalist(a,b) {
	
	//1.) Sortierung nach Taxagroup-RF
	if (a["taxagroup_rf"]==b["taxagroup_rf"]) {
		//2.) Sortierung nach Taxagroup
		if (a["taxagroup"]==b["taxagroup"]) {
			//3.) Sortierung nach Family
			if (a["family"]==b["family"]) {
				//4.) Sortierung nach Subfamily
				if (a["subfamily"]==b["subfamily"]) {
					//5.) Sortierung nach Species
					if (a["species"]==b["species"]) {
						//6.) Sortierung nach Genus
						if (a["genus"]==b["genus"]) {
							return 0;
						} else {
							return(a["genus"]>b["genus"])?1:-1;
						}
					} else {
						return(a["species"]>b["species"])?1:-1;
					}
				} else {
					return(a["subfamily"]>b["subfamily"])?1:-1;
				}
			} else {
				return(a["family"]>b["family"])?1:-1;
			}
		} else {
			return(a["taxagroup"]>b["taxagroup"])?1:-1;
		}
	} else {
		return(a["taxagroup_rf"]>b["taxagroup_rf"])?1:-1;
	}

	
}

//function for setting the data for exporting
function setvalue(idheader, idtl, idog) {
	var objheader = document.getElementById(idheader)
	objheader.value="";
	var objtl = document.getElementById(idtl)
	objtl.value="";
	for (j=0;j<5;j++) {
		var obj1 = document.getElementById(idheader+j)
		obj1.value=serialize(tettlheader[j]);
		var obj2 = document.getElementById(idtl+j)
		obj2.value=serialize(tettaxalist[j]);
		if (document.getElementById('tabtl'+j).style.display=="") {
			objheader.value=obj1.value;	
			objtl.value=obj2.value;	
			var obj = document.getElementById(idog)
			obj.value=j;
		}
	}

	return true;
}

function cboorggroup_onChange(aValue) {
	
	aktuelleorggroup = aValue;
	for (j=0;j<5;j++) {
		var tabelle=document.getElementById('tabtl'+j).style.display="none";
		var lbl=document.getElementById('lblTLAccording'+j).style.display="none";
	}
	var tabelle=document.getElementById('tabtl'+aktuelleorggroup).style.display="";
	var lbl=document.getElementById('lblTLAccording'+aktuelleorggroup).style.display="";
				
	$("#txtspecies_id").val("0");
	$("#txtspecies").val("");
	$("#txtgenus_id").val("0");
	$("#txtgenus").val("");

}

function get_html_translation_table(table, quote_style) {
    // Returns the internal translation table used by htmlspecialchars and htmlentities 
    //
    // version: 906.401
    // discuss at: http://phpjs.org/functions/get_html_translation_table
    // +   original by: Philip Peterson
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: noname
    // +   bugfixed by: Alex
    // +   bugfixed by: Marco
    // +   bugfixed by: madipta
    // +   improved by: KELAN
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %          note: It has been decided that we're not going to add global
    // %          note: dependencies to php.js. Meaning the constants are not
    // %          note: real constants, but strings instead. integers are also supported if someone
    // %          note: chooses to create the constants themselves.
    // *     example 1: get_html_translation_table('HTML_SPECIALCHARS');
    // *     returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}
     
    var entities = {}, histogram = {}, decimal = 0, symbol = '';
    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};
     
    // Translate arguments
    constMappingTable[0]      = 'HTML_SPECIALCHARS';
    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';
 
    useTable       = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
    useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';
 
    if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
        throw new Error("Table: "+useTable+' not supported');
        // return false;
    }
 
    if (useTable === 'HTML_ENTITIES') {
        entities['160'] = '&nbsp;';
        entities['161'] = '&iexcl;';
        entities['162'] = '&cent;';
        entities['163'] = '&pound;';
        entities['164'] = '&curren;';
        entities['165'] = '&yen;';
        entities['166'] = '&brvbar;';
        entities['167'] = '&sect;';
        entities['168'] = '&uml;';
        entities['169'] = '&copy;';
        entities['170'] = '&ordf;';
        entities['171'] = '&laquo;';
        entities['172'] = '&not;';
        entities['173'] = '&shy;';
        entities['174'] = '&reg;';
        entities['175'] = '&macr;';
        entities['176'] = '&deg;';
        entities['177'] = '&plusmn;';
        entities['178'] = '&sup2;';
        entities['179'] = '&sup3;';
        entities['180'] = '&acute;';
        entities['181'] = '&micro;';
        entities['182'] = '&para;';
        entities['183'] = '&middot;';
        entities['184'] = '&cedil;';
        entities['185'] = '&sup1;';
        entities['186'] = '&ordm;';
        entities['187'] = '&raquo;';
        entities['188'] = '&frac14;';
        entities['189'] = '&frac12;';
        entities['190'] = '&frac34;';
        entities['191'] = '&iquest;';
        entities['192'] = '&Agrave;';
        entities['193'] = '&Aacute;';
        entities['194'] = '&Acirc;';
        entities['195'] = '&Atilde;';
        entities['196'] = '&Auml;';
        entities['197'] = '&Aring;';
        entities['198'] = '&AElig;';
        entities['199'] = '&Ccedil;';
        entities['200'] = '&Egrave;';
        entities['201'] = '&Eacute;';
        entities['202'] = '&Ecirc;';
        entities['203'] = '&Euml;';
        entities['204'] = '&Igrave;';
        entities['205'] = '&Iacute;';
        entities['206'] = '&Icirc;';
        entities['207'] = '&Iuml;';
        entities['208'] = '&ETH;';
        entities['209'] = '&Ntilde;';
        entities['210'] = '&Ograve;';
        entities['211'] = '&Oacute;';
        entities['212'] = '&Ocirc;';
        entities['213'] = '&Otilde;';
        entities['214'] = '&Ouml;';
        entities['215'] = '&times;';
        entities['216'] = '&Oslash;';
        entities['217'] = '&Ugrave;';
        entities['218'] = '&Uacute;';
        entities['219'] = '&Ucirc;';
        entities['220'] = '&Uuml;';
        entities['221'] = '&Yacute;';
        entities['222'] = '&THORN;';
        entities['223'] = '&szlig;';
        entities['224'] = '&agrave;';
        entities['225'] = '&aacute;';
        entities['226'] = '&acirc;';
        entities['227'] = '&atilde;';
        entities['228'] = '&auml;';
        entities['229'] = '&aring;';
        entities['230'] = '&aelig;';
        entities['231'] = '&ccedil;';
        entities['232'] = '&egrave;';
        entities['233'] = '&eacute;';
        entities['234'] = '&ecirc;';
        entities['235'] = '&euml;';
        entities['236'] = '&igrave;';
        entities['237'] = '&iacute;';
        entities['238'] = '&icirc;';
        entities['239'] = '&iuml;';
        entities['240'] = '&eth;';
        entities['241'] = '&ntilde;';
        entities['242'] = '&ograve;';
        entities['243'] = '&oacute;';
        entities['244'] = '&ocirc;';
        entities['245'] = '&otilde;';
        entities['246'] = '&ouml;';
        entities['247'] = '&divide;';
        entities['248'] = '&oslash;';
        entities['249'] = '&ugrave;';
        entities['250'] = '&uacute;';
        entities['251'] = '&ucirc;';
        entities['252'] = '&uuml;';
        entities['253'] = '&yacute;';
        entities['254'] = '&thorn;';
        entities['255'] = '&yuml;';
    }
 
    if (useQuoteStyle !== 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';
    }
    if (useQuoteStyle === 'ENT_QUOTES') {
        entities['39'] = '&#39;';
    }
    entities['60'] = '&lt;';
    entities['62'] = '&gt;';
 
    // ascii decimals for better compatibility
    entities['38'] = '&amp;';
 
    // ascii decimals to real symbols
    for (decimal in entities) {
        symbol = String.fromCharCode(decimal);
        histogram[symbol] = entities[decimal];
    }
     
    return histogram;
}

function html_entity_decode( string, quote_style ) {
    // Convert all HTML entities to their applicable characters 
    //
    // version: 906.401
    // discuss at: http://phpjs.org/functions/html_entity_decode
    // +   original by: john (http://www.jd-tech.net)
    // +      input by: ger
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: marc andreu
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // -    depends on: get_html_translation_table
    // *     example 1: html_entity_decode('Kevin &amp; van Zonneveld');
    // *     returns 1: 'Kevin & van Zonneveld'
    // *     example 2: html_entity_decode('&amp;lt;');
    // *     returns 2: '&lt;'
    var histogram = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();
     
    if (false === (histogram = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {
        return false;
    }
 
    for (symbol in histogram) {
        entity = histogram[symbol];
        tmp_str = tmp_str.split(entity).join(symbol);
    }
    tmp_str = tmp_str.split('&#039;').join("'");
     
    return tmp_str;
}
//file end