var GStandardVersion= -1;
var GLanguage= "";
var new_point= 0;
var new_tags = [];
var current_point = null;
var current_tag= -1;
var point_list = [];


class CPointList
	{
		constructor(id, type)
		{
			this.ID= id;
			this.Type= type;				// 0 : point,   1 : title
		}
	};

function InitAuditStandardPage ()
{
	console.log ( "InitAuditStandardPage..." );

	let ss = sessionStorage.getItem ( 'SettingsAuditStandardPageInitialized' );

	if ( ss == 'true' )
		return;

	sessionStorage.setItem ( 'SettingsAuditStandardPageInitialized', true );


	$(document).ready(function () 
	{
		current_point = new CPointList(-1, 0);

		IdcSASplitter1_2 	= new C_Splitter ( "IdcSASplitter1_2" );

		IdcSASplitter1_2.LoadUserCfg ( G_USE_ID );
		document.getElementById ('IdcSASStandardsTab' 	  ).addEventListener	( 'shown.bs.tab', () => { SASStandardsTabShown (); 	   } 		);
		document.getElementById ('IdcSASStandardsEditTab' ).addEventListener	( 'shown.bs.tab', () => { SASStandardsEditTabShown (); } 		);

		FillAuditTypesList		( 'IdcSASStandardsList' );

		switchLanguage ( CurrentLanguage );
	});
}

function SASStandardsTabShown ()
{

}

function SASStandardsEditTabShown ()
{
	// let standardID = $('#IdcSASStandardsList').val ();
	let standardID = 4;

	console.log ( " standardID : " + standardID );

	RenderPage ( "SubFW/html/standards-config.php?standard_version="+standardID+"&language=FR", "IdcSASStandardsEditContainer" );
}

function SASStandardChanged()
{
	console.log ( "SASStandardChanged..." );

	let standardID = $('#IdcSASStandardsList').val ();

	console.log ( " standardID : " + standardID );

	RenderPage ( "SubFW/html/standards-config.php?standard_version="+standardID+"&language=FR", "IdcSASStandardsEditContainer" );

	// IdcSASplitter3_4 	= new C_Splitter ( "IdcSASplitter3_4" );
}





	

	
	function SASSelectPoint(idpoint, type)
	{
		new_tags.splice(0);
		
		if ( current_point.ID != -1 )
		{	
			var tr_sumelement = document.getElementById("summary_"+current_point.ID);
					
			tr_sumelement.style.fontWeight = "normal";		
			tr_sumelement.style.fontStyle = "normal";
		}
		
		current_point.ID = idpoint;
		current_point.Type= type;
		
		if(current_point.ID != -1)
		{	
			var tr_sumelement = document.getElementById("summary_"+current_point.ID);
					
			tr_sumelement.style.fontWeight = "bold";
			tr_sumelement.style.fontStyle = "italic";
		}

		SASFillQuaadPoint();
		
		SASFillQuaadTag();
		
		return;
	}

	function SASSelectPoint1(point)
	{
		SASSelectPoint(point.ID, point.ChapterTitle, point.Type);
	}

	function SASFillPointsList ()
	{
		console.log ( "SASFillPointsList..." );

		last_proc_checked = -1;
		
		var xhra = getxhr();
		
		var param_c = "standard_version="+GStandardVersion+"&language="+GLanguage;

		console.log ( " param_c : " + param_c );
		
		xhra.onreadystatechange = function f()
		{
			if(xhra.readyState == 4 && xhra.status == 200) 
			{
				var  response = xhra.responseText;
				reponse = clean(xhra.responseXML.documentElement);
				
				var points = reponse.getElementsByTagName('point');

				var table_points = document.getElementById('table_points');
				
				var tmp = 		"<tbody>";
				
				if(new_point == 1)
				{
					id = -2;
					description = "New point";
					level= 1;
					
					
					tmp += 	"<tr>";
						tmp +=		"<td style=\"width: 100%; \">";
						// tmp +=			"<div id=\"summary_"+id+"\" onclick=\"selectpoint("+id+", 0);\" style=\"border-bottom: 1px solid gray;  width: 100%; font-size:12px; float: left; color:#484848; margin-left:10px; margin-top:2px; margin-bottom:1px;\">";
						tmp +=			"<div id=\"summary_"+id+"\" style=\"border-bottom: 1px solid gray;  width: 100%; font-size:12px; float: left; color:#484848; margin-left:10px; margin-top:2px; margin-bottom:1px;\">";
						tmp +=				"<table> ";
						tmp += 					"<tr>";
						tmp +=						"<td onclick=\"SASSelectPoint("+id+", 0);\" style=\"min-width: 40px; padding-left: 10px; padding-top: 0px; padding-right: 10px; padding-bottom: 0px;\">"+level+"</td>";
						tmp += 						"<td onclick=\"SASSelectPoint("+id+", 0);\" style=\"width: 100%; padding-left: 10px; padding-top: 0px; padding-right: 10px; padding-bottom: 0px;\">" + description + "</td>";
						tmp +=						"<td><img src=\"./SubFW/bmp/trash_white.png\" title=\"Delete point\" class=\"clickablepoint\" style=\"width:18px;vertical-align:middle;\" 	onclick=\"DeleteQuaadPoint("+id+")\"></td>";
						tmp += 					"</tr>";
						tmp += 				"</table>";
						tmp +=			"</div>";
						tmp += 		"</td>";
						tmp += "</tr>";
				}
				
				for(var j = 0; j<points.length && points.length;j++)
				{
					var id = points.item(j).getAttribute("id");
					var istitle = points.item(j).getAttribute("istitle");
					var description= points.item(j).getAttribute("description");
					var level = points.item(j).getAttribute("level");
					
					if(istitle == "t")
					{
						point_list.push(new CPointList(id, 1));
								
						tmp += 	"<tr>";								
						tmp +=		"<td>";
						tmp +=			"<div id=\"summary_" + id + "\" onclick=\"SASSelectPoint("+ id + ", 1);\" style=\"font-weight: bold; font-size:14px; float: left; color:#484848; margin-left:10px; margin-top:10px; margin-bottom:3px;\"><b>"+ description +"</b></div>";
						tmp +=		"</td>";
						tmp +=						"<td><img src=\"./SubFW/bmp/trash.png\" title=\"Delete point\" class=\"clickablepoint\" style=\"width:18px;vertical-align:middle;\" 	onclick=\"DeleteQuaadPoint("+id+")\"></td>";
						tmp +=	"</tr>";								
					}
					else
					{
						point_list.push(new CPointList(id, 0));
									
						tmp += 	"<tr>";
						tmp +=		"<td style=\"width: 100%; \">";
						tmp +=			"<div id=\"summary_"+id+"\" style=\"border-bottom: 1px solid gray;  width: 100%; font-size:12px; float: left; color:#484848; margin-left:10px; margin-top:2px; margin-bottom:1px;\">";
						tmp +=				"<table> ";
						tmp += 					"<tr>";
						tmp +=						"<td onclick=\"SASSelectPoint("+id+", 0);\" style=\"min-width: 40px; padding-left: 10px; padding-top: 0px; padding-right: 10px; padding-bottom: 0px;\">"+level+"</td>";
						tmp += 						"<td onclick=\"SASSelectPoint("+id+", 0);\" style=\"width: 100%; padding-left: 10px; padding-top: 0px; padding-right: 10px; padding-bottom: 0px;\">" + description + "</td>";
						tmp +=						"<td><img src=\"./SubFW/bmp/trash.png\" title=\"Delete point\" class=\"clickablepoint\" style=\"width:18px;vertical-align:middle;\" 	onclick=\"DeleteQuaadPoint("+id+")\"></td>";
						tmp += 					"</tr>";
						tmp += 				"</table>";
						tmp +=			"</div>";
						tmp += 		"</td>";
						tmp += "</tr>";
					}
				}
				
				tmp += 		"</tbody>";
				
				table_points.innerHTML = tmp;
			}
		};
	
		xhra.open("POST","./SubFW/database/referentiel/getpointslist.php",false);
		
		xhra.overrideMimeType('text/xml');
		
		xhra.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhra.send(param_c);
	}


function SASFillQuaadPoint()
	{
		console.log ( "SASFillQuaadPoint..." );

		var quaadpoint_div=document.getElementById('quaadpoint_div');
		
		quaadpoint_div.innerHTML = "";
		
		var xhra = getxhr();
		
		var param_c = "id="+current_point.ID + "&type="+current_point.Type;
		xhra.onreadystatechange = function f()
		{
			if(xhra.readyState == 4 && xhra.status == 200) 
			{
				var  response = xhra.responseText;
				reponse = clean(xhra.responseXML.documentElement);

				var nb_elem = reponse.getElementsByTagName('id').length;
				
				if(nb_elem > 0)
				{
					var id= reponse.getElementsByTagName('id').item(0).firstChild.nodeValue;		
					var description = "";		
					var name = "";		

					var level1 = reponse.getElementsByTagName('level1').item(0).firstChild.nodeValue;
					var level2 = reponse.getElementsByTagName('level2').item(0).firstChild.nodeValue;
					var level3 = reponse.getElementsByTagName('level3').item(0).firstChild.nodeValue;
					var level4 = reponse.getElementsByTagName('level4').item(0).firstChild.nodeValue;
					var level5 = reponse.getElementsByTagName('level5').item(0).firstChild.nodeValue;
					var inLevelOrder= reponse.getElementsByTagName('inlevelorder').item(0).firstChild.nodeValue;
					var isTitle= reponse.getElementsByTagName('istitle').item(0).firstChild.nodeValue;
					var dateBeg= reponse.getElementsByTagName('datebeg').item(0).firstChild.nodeValue;
					var dateEnd;
					if(reponse.getElementsByTagName('dateend').item(0).firstChild != null) 
						dateEnd = reponse.getElementsByTagName('dateend').item(0).firstChild.nodeValue;
					else
						dateEnd = "2999-12-31";
					var quaadVersion= reponse.getElementsByTagName('quaadversion').item(0).firstChild.nodeValue;
				}
				else
				{
					if(new_point == 1)
					{
						var id= -2;		
						var description = "New point";		
						var name = "New point";		

						var level1 = 1;
						var level2 = 1;
						var level3 = 0;
						var level4 = 0;
						var level5 = -1;
						var inLevelOrder= -1;
						var isTitle= false;
						var dateBeg= CurrentDate();
						var dateEnd = "2999-12-31";
						var quaadVersion = GStandardVersion;
					}
				}
				
				if(nb_elem > 0 || new_point == 1)
				{
					tmp  = "<table id=\"IdcQuaadPointTable\" style=\"font-size: 12px; width: 100%;\" cellspacing=\"0\" cellpadding=\"5\" border=\"0\">";
					tmp += 		"<tbody>";
				
					tmp += 		"<tr >";
					tmp += 			"<td >";
					tmp += 				"<table width=\"100%\" >";
					tmp += 					"<tr >";
					tmp += 						"<td >";
					tmp += 							"<table >";
					tmp += 								"<tr>";		
					tmp += 									"<td >Level 1 :</td><td><input id=\"IdcLevel1\" type=\"text\" style=\"text-align: right; max-width:30px;\" value=\""+level1+"\"></td>";
					tmp += 								"</tr>";		
					tmp += 								"<tr>";
					tmp += 									"<td >Level 2 :</td><td><input id=\"IdcLevel2\" type=\"text\" style=\"text-align: right; max-width:30px;\" value=\""+level2+"\"></td>";
					tmp += 								"</tr>";		
					tmp += 								"<tr>";
					tmp += 									"<td >Level 3 :</td><td><input id=\"IdcLevel3\" type=\"text\" style=\"text-align: right; max-width:30px;\" value=\""+level3+"\"></td>";
					tmp += 								"</tr >";													
					tmp += 							"</table >";				
					tmp += 						"</td >";
					tmp += 						"<td >";	
					tmp += 							"<table >";
					tmp += 								"<tr>";
					tmp += 									"<td >Level 4 :</td><td><input id=\"IdcLevel4\" type=\"text\" style=\"text-align: right; max-width:30px;\" value=\""+level4+"\"></td>";
					tmp += 								"</tr >";	
					tmp += 								"<tr>";		
					tmp += 									"<td >Level 5 :</td><td><input id=\"IdcLevel5\" type=\"text\" style=\"text-align: right; max-width:30px;\" value=\""+level5+"\"></td>";
					tmp += 								"</tr>";		
					tmp += 								"<tr>";
					tmp += 									"<td >Order in level :</td><td><input id=\"IdcOrderInLevel\" type=\"text\" style=\"text-align: right; max-width:30px;\" value=\""+inLevelOrder+"\"></td>";
					tmp += 								"</tr>";		
					tmp += 							"</table >";				
					tmp += 						"</td >";
					tmp += 						"<td >";	
					tmp += 							"<table >";
					tmp += 								"<tr>";
					if(isTitle == "t" || isTitle == "true") check = "checked"; else check = "";
					tmp += 									"<td >Is title :</td><td><input id=\"IdcIsTitle\" type=\"checkbox\" style=\"text-align: right; max-width:30px;\" "+check+"></td>";
					tmp += 								"</tr >";
					tmp += 								"<tr>";
					tmp += 									"<td >Quaadril version :</td><td><input id=\"IdcQuaadVersion\" type=\"text\" style=\"text-align: right; max-width:30px;\" value=\""+quaadVersion+"\" readonly></td>";
					tmp += 								"</tr>";						
					tmp += 							"</table >";				
					tmp += 						"</td >";
					tmp += 						"<td >";		
					tmp += 							"<table >";
					tmp += 								"<tr>";		
					tmp += 									"<td >Begin date :</td><td><input id=\"IdcDateBegin\" type=\"date\" style=\"text-align: right; max-width:140px;\" value=\""+dateBeg+"\"></td>";
					tmp += 								"</tr>";		
					tmp += 								"<tr>";
					tmp += 									"<td >End date :</td><td><input id=\"IdcDateEnd\" type=\"date\" style=\"text-align: right; max-width:140px;\" value=\""+dateEnd+"\"></td>";
					tmp += 								"</tr>";		
					tmp += 							"</table >";				
					tmp += 						"</td >";
					tmp += 					"</tr >";
					tmp += 				"</table>";
					tmp += 			"<td >";
					tmp += 		"</tr >";

					tmp += 		"<tr>";
					tmp +=			"<table>";
					tmp +=				"<tr>";
					tmp += 					"<td style=\"width: 70px;\">Language : </td>";
					tmp += 					"<td>";
					tmp += 						"<select id=\"IdcLanguage\">";
					var selected = "";
					console.log("GLanguage : "+GLanguage);
					if(GLanguage == "FR") 	selected = "selected";
					tmp += 							"<option value=\"FR\" "+selected+">French</option>";
					if(GLanguage == "NL") 	selected = "selected";	else selected = "";
					tmp += 							"<option value=\"NL\" "+selected+">Dutch</option>";
					tmp += 						"</select>";
					tmp += 					"</td>";
					tmp +=				"</tr>";
					tmp +=			"</table>";
					tmp += 		"</tr>";
				
					tmp += 		"<tr >";
					tmp +=			"<td>";
					tmp +=				"<table style=\"width: 100%;\">";
					tmp +=					"<tr>";
					tmp += 						"<td style=\"width: 50px;\">Name : </td>";
					tmp += 						"<td><input id=\"IdcPointName\" type=\"text\" value=\""+name+"\" style = \"width:99%; border: solid 1px lightgray; \"></td>";
					tmp +=					"</tr>";
					tmp +=				"</table>";
					tmp +=			"</td>";
				
					tmp += 		"</tr >";
				
					tmp += 		"<tr >";
					tmp += 			"<td><textarea id=\"IdcPointDescription\" contenteditable=\"true\" style = \"width:99%; border: solid 1px lightgray; height:160px;\">"+description+"</textarea></td>";
					tmp += 		"</tr>";
				
					tmp += 		"</tbody>";
					tmp += 	"</table>";
				
					quaadpoint_div.innerHTML = tmp;
				
					var elt = document.querySelector('#IdcLanguage');
					elt.addEventListener('change', SASLanguageChanged);
				
					SASLanguageChanged();
				}
			}
		};
	
		xhra.open("POST","./SubFW/database/referentiel/getfullquaadpoint.php",false);
		
		xhra.overrideMimeType('text/xml');
		
		xhra.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhra.send(param_c);
	}



	function SASFillQuaadTag()
	{
		var xhra = getxhr();
		
		var param_c = "id="+current_point.ID;
		xhra.onreadystatechange = function f()
		{
			if(xhra.readyState == 4 && xhra.status == 200) 
			{
				var  response = xhra.responseText;
				
				reponse = clean(xhra.responseXML.documentElement);
				
				var tags = reponse.getElementsByTagName('tag');

				var tags_div = document.getElementById('tags_div');
				
				if(tags.length > 0 || new_tags.length > 0)
				{
					console.log("TABLE BEGIN");
					tmp  = "<table id=\"IdcTagsTable\" style=\"width: 100%;\" cellspacing=\"0\" cellpadding=\"5\" border=\"0\">";
					tmp += 		"<thead>";
					tmp += 			"<tr>";
					tmp += 				"<th style=\"text-align:left; display:none;\">tag_id</th>";
					tmp += 				"<th style=\"text-align:left;\">Scope</th>";
					tmp += 				"<th style=\"text-align:left;\">Scope 2</th>";
					tmp += 				"<th style=\"text-align:left;\" >Global scope</th>";
					tmp += 				"<th style=\"text-align:left; display:none;\" >Proc. needed</th>";
					tmp += 				"<th style=\"text-align:left;\" >Doc. needed</th>";
					tmp += 				"<th style=\"text-align:left;\" >Add. param.</th>";
					tmp += 				"<th style=\"text-align:left;\" >Where no tag_id</th>";
					tmp += 				"<th style=\"text-align:left;\" >Document Scope</th>";
					tmp += 				"<th style=\"text-align:left;\" >Tag name</th>";
					tmp += 				"<th style=\"text-align:left;\" ></th>";		// Delete button
					tmp += 			"</tr>";
					tmp += 		"</thead>";
					tmp += 		"<tbody>";
				
					var first_tag = -1;
				
					for(var j = 0; j<tags.length; j++)
					{
						var tag_id = tags.item(j).getAttribute("id");			// console.log(" id : " + tag_id);
						var scope = tags.item(j).getAttribute("scope");			// console.log(" scope : " + scope);
						var scope2 = tags.item(j).getAttribute("scope2");			// console.log(" scope2 : " + scope2);
						var isglobalscope= tags.item(j).getAttribute("isglobalscope");			// console.log(" isglobalscope : " + isglobalscope);
						var proc_needed = tags.item(j).getAttribute("procedure_needed");			// console.log(" proc_needed : " + proc_needed);
						var doc_needed = tags.item(j).getAttribute("documents_needed");			// console.log(" doc_needed : " + doc_needed);
						var add_param = tags.item(j).getAttribute("additional_param");			// console.log(" add_param : " + add_param);
						var where_notagid= tags.item(j).getAttribute("where_notagid");			// console.log(" where_notagid : " + where_notagid);
						var doc_scope = tags.item(j).getAttribute("document_scope");
						var tag_name = tags.item(j).getAttribute("tag_name");
						if(j == 0)
							first_tag = tag_id;
					
						tmp += 		"<tr id=\"quaadtag_"+tag_id+"\" >";
						// tmp += 			"<td style=\"display:none;\">"+tag_id+"</td>";
						tmp += 			"<td style=\"display:none;\" onclick=\"SASClickOnTag("+tag_id+")\">"+tag_id+"</td>";
						tmp += 			"<td><input type=\"text\" value=\"" + scope + "\" onclick=\"SASClickOnTag("+tag_id+")\"></td>";
						tmp += 			"<td><input type=\"text\" value=\"" + scope2 + "\" onclick=\"SASClickOnTag("+tag_id+")\"></td>";
					
						if(isglobalscope == "t")
							check = "checked";		
						else 	check = "";
						tmp += 			"<td><input type=\"checkbox\" " + check + " onclick=\"SASClickOnTag("+tag_id+")\"></td>";
						tmp += 			"<td style=\"display:none;\" id=\"IdcProcNeeded_"+tag_id+"\" onclick=\"SASClickOnTag("+tag_id+")\">"+proc_needed+"</td>";
						// tmp += 			"<td id=\"IdcProcNeeded_"+tag_id+"\" onclick=\"ClickOnTag("+tag_id+")\">"+proc_needed+"</td>";
						tmp += 			"<td><input style=\"text-align:right; width: 30px;\" type=\"text\" value=\"" + doc_needed + "\" onclick=\"ClickOnTag("+tag_id+")\"></td>";
						tmp += 			"<td><input type=\"text\" value=\"" + add_param + "\" onclick=\"SASClickOnTag("+tag_id+")\"></td>";
						
						if(where_notagid == "t" || where_notagid == "true") 
							check = "checked";		
						else 	check = "";
						tmp += 			"<td><input type=\"checkbox\" " + check + " onclick=\"SASClickOnTag("+tag_id+")\"></td>";
						tmp += 			"<td><input type=\"text\" value=\"" + doc_scope + "\" onclick=\"SASClickOnTag("+tag_id+")\"></td>";
						tmp += 			"<td><input type=\"text\" value=\"" + tag_name + "\" onclick=\"SASClickOnTag("+tag_id+")\"></td>";
						tmp += 			"<td><img src=\"./SubFW/bmp/trash_white.png\" title=\"Delete tag\" class=\"clickablepoint\" style=\"width:22px;vertical-align:middle;\" 	onclick=\"DeleteQuaadTag("+tag_id+")\"></td>";
						tmp += 		"</tr>";
					}
					
					// Add new tags
					
					console.log("new_tags length : " + new_tags.length);
					for(var j = 0; j<new_tags.length; j++)
					{
						var tag_id = new_tags [j];
						var scope = "scope";
						var scope2= "scope2";
						var isglobalscope= false;
						var proc_needed= -1;
						var add_param= "";
						var tag_name="";
						
						tmp += 		"<tr id=\"quaadtag_"+tag_id+"\" >";
						tmp += 			"<td style=\"display:none;\" >"+tag_id+"</td>";
						// tmp += 			"<td >"+tag_id+"</td>";
						tmp += 			"<td><input type=\"text\" value=\"" + scope + "\" onclick=\"ClickOnTag("+tag_id+")\"></td>";
						tmp += 			"<td><input type=\"text\" value=\"" + scope2 + "\" onclick=\"ClickOnTag("+tag_id+")\"></td>";
					
						if(isglobalscope == "t") check = "checked";		else 	check = "";
						tmp += 			"<td><input type=\"checkbox\" " + check + " onclick=\"ClickOnTag("+tag_id+")\"></td>";
						// tmp += 			"<td style=\"display:none;\">"+proc_needed+"</td>";
						// tmp += 			"<td id=\"IdcProcNeeded_"+tag_id+"\" onclick=\"ClickOnTag("+tag_id+")\">"+proc_needed+"</td>";
						tmp += 			"<td id=\"IdcProcNeeded_"+tag_id+"\" style=\"display:none;\" onclick=\"ClickOnTag("+tag_id+")\">"+proc_needed+"</td>";
						tmp += 			"<td><input style=\"text-align:right; width: 30px;\" type=\"text\" value=\"" + doc_needed + "\" onclick=\"ClickOnTag("+tag_id+")\"></td>";
						tmp += 			"<td><input type=\"text\" value=\"" + add_param + "\" onclick=\"ClickOnTag("+tag_id+")\"></td>";
						if(where_notagid == "t" || where_notagid == "true") check = "checked";		else 	check = "";
						tmp += 			"<td><input type=\"checkbox\" " + check + " onclick=\"ClickOnTag("+tag_id+")\"></td>";
						tmp += 			"<td><input type=\"text\" value=\"" + doc_scope + "\" onclick=\"ClickOnTag("+tag_id+")\"></td>";
						tmp += 			"<td><input type=\"text\" value=\"" + tag_name + "\" onclick=\"ClickOnTag("+tag_id+")\"></td>";

						tmp += 			"<td><img src=\"./SubFW/bmp/trash_white.png\" title=\"Delete tag\" class=\"clickablepoint\" style=\"width:22px;vertical-align:middle;\" 	onclick=\"DeleteQuaadTag("+tag_id+")\"></td>";
						tmp += 		"</tr>";
					}
					
					tmp += 		"</tbody>";
					tmp += 		"</table>";

					tags_div.innerHTML = tmp;
				
					if(first_tag != -1)
						SASSelectTag(first_tag);
				}
				else
				{
					SASSelectTag(-1);
				}
					
			}
		};
	
		xhra.open("POST","./SubFW/database/referentiel/getquaadtags.php",false);
		
		xhra.overrideMimeType('text/xml');
		
		xhra.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhra.send(param_c);
	}


	function SASSelectTag(tag_id)
	{
		var maindiv=document.getElementById('main_div');
		var tags_div=document.getElementById('tags_div');
		
		if(tag_id != -1)
		{
			tags_div.style.display='inline';
			maindiv.style.display='inline';
			
			if(current_tag != -1)
			{
				if(document.getElementById("quaadtag_" + current_tag))
				{
					document.getElementById("quaadtag_" + current_tag).style.backgroundColor = "#ffffff";
				}
			}
		
			document.getElementById("quaadtag_" + tag_id).style.backgroundColor = "#ddddff";
		
			current_tag = tag_id;
			
			SASSetProcedureUsed(tag_id);
		}
		else
		{
			tags_div.style.display='none';
			maindiv.style.display='none';
		}
	}

	function SASSetProcedureUsed(tag_id)
	{
		if(last_proc_checked != -1)
		{	
			document.getElementById("procedure_" + last_proc_checked).checked = false;
		}

		
		var proc_type = document.getElementById("IdcProcNeeded_" + tag_id).innerText
					
		if(proc_type != -1)
		{
			var row_proc = document.getElementById("procedure_" + proc_type);
					
			row_proc.checked = true;	

			last_proc_checked = proc_type;
		}
	}


	function SASLanguageChanged()
	{
		LanguageExist = false;
		
		var s = document.getElementById("IdcLanguage");
				
		var idcPointName = document.getElementById('IdcPointName');
		var idcPointDescription = document.getElementById('IdcPointDescription');
		
		if(current_point.ID >= 0)
		{
			var param_c = "id="+current_point.ID+"&language="+s.value;
		
			var xhra = getxhr();
		
			xhra.onreadystatechange = function f()
			{
				if(xhra.readyState == 4 && xhra.status == 200) 
				{
					var  response = xhra.responseText;
									
					reponse = clean(xhra.responseXML.documentElement);
				
					var status = reponse.getElementsByTagName('quaad_description');
					
					if(status.length > 0)
					{
						var id= reponse.getElementsByTagName('id').item(0).firstChild.nodeValue;		
						var name = reponse.getElementsByTagName('name').item(0).firstChild.nodeValue;		
						var description = reponse.getElementsByTagName('description').item(0).firstChild.nodeValue;		

						idcPointName.value = name;
						idcPointDescription.value = description;
					
						LanguageExist = true;
					}
					else
					{
						idcPointName.value = "";
						idcPointDescription.value = "";
					}
				}
			};
	
			xhra.open("POST","./SubFW/database/referentiel/getpointtext.php",false);
		
			xhra.overrideMimeType('text/xml');
		
			xhra.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
			xhra.send(param_c);
		}
		else
		{
			idcPointName.value = "New point";
		}
			
	}


	function SASClickOnTag(ident)
	{
		SASSelectTag(ident);
	}


	function SASClickOnProcedure(ident)
	{
		if(last_proc_checked != -1)
		{	
			document.getElementById("procedure_" + last_proc_checked).checked = false;
		}
		
		document.getElementById("procedure_" + ident).checked = true;
		
		last_proc_checked = ident;
		
		document.getElementById("IdcProcNeeded_"+current_tag).innerText = ident;
	}


	function SASGetNext(point)
	{
		var p= SASGetPoint_Listpos(point);

		return(p>=point_list.length-1)?SASGetPoint_Listpos(current_point):(p+1);
	}

	function SASGetPrevious(point)
	{
		var p= SASGetPoint_Listpos(point);
		return(p<=0)?SASGetPoint_Listpos(current_point):(p-1);
	}

	function SASGetPoint_Listpos(point)
	{
		for(var i=0;i<point_list.length;i++)
		{
			if(point_list[i].ID==point.ID)
			{
				return i;
			}
		}
		return -2;
	}