function calendar(id) {
	win = window.open("./" + document.getElementById(id).value + "?id=" + id + "&ac_adm=Calendar","_blank","width=210,height=180");
}
	
function calcOffsetLeft(r) {
	return calcOffset(r,"offsetLeft")
}
	
function calcOffsetTop(r) {
	return calcOffset(r,"offsetTop")
}
	
function calcOffset(element,attr) {
	var offset=0;
	while(element) {
		offset+=element[attr];
		element=element.offsetParent;
	}
	return offset;
}

var keywords_index = {};
var keywords = new Array();
	
function init_keyword_index(){
	keywords_index= {};
	for(var i=0; i<keywords.length; i++) {
		keywords_index[keywords[i]['id']]=i;
	}
}
		
function update_key_word_list(){
	var input=document.getElementById(input_id);
	var container=document.getElementById('keyword_list_container');
	var div=document.getElementById('keyword_list');
	
	document.getElementById('hidden_keyword').value= input.value;
	
	var searchFor= input.value;
	var list="";
	var list_end="";
	var type='confirmed';
	if(searchFor.length>0){
		for(var i=0; i<keywords.length; i++){
			var keyword= keywords[i]['value'];
			var pos=  keyword.toLowerCase().indexOf(searchFor.toLowerCase());
			if(pos!=-1){
				keyword= keyword.substr(0, pos) +"<strong>"+ keyword.substr(pos, searchFor.length) +"</strong>"+ keyword.substr(pos+searchFor.length);
				if(show_class){
					if(keywords[i]['class']=="standard")
						list+="<a class='keyword_in_list "+ keywords[i]['class'] +"' onclick='select_keyword("+ keywords[i]['id'] +")'> "+keyword+"</a>";
					else
						list_end+="<a class='keyword_in_list "+ keywords[i]['class'] +"' onclick='select_keyword("+ keywords[i]['id'] +")'> "+keyword+"</a>";
				}else{					
					list+="<a class='keyword_in_list' onclick='select_keyword("+ keywords[i]['id'] +")'> "+keyword+"</a>";
				}
			}
		}
	}
	if(list_end.length>0){
		if(list.length>0)
			list+= "<hr />"+list_end;
		else
			list= list_end;
	}
	// alert(list);
	if(list=="")
		hide_list();
	else
		show_list();
		
	// update the content of the highlight div by first updating a clone node (as there is no display in the same time for this node it's quite faster (5*))
	var new_Obj= div.cloneNode(false);
	new_Obj.innerHTML= list;			
	div.parentNode.insertBefore(new_Obj, div);
	div.parentNode.removeChild(div);			
	// div.innerHTML=list;	
	container.style.height=Math.min(200, new_Obj.offsetHeight)+"px";
	
			 	
}
	
function hide_list(){
	container= document.getElementById('keyword_list_container');
	container.style.display='none';
	container.list_shown= false;
}
	
function show_list(time){
	var container= document.getElementById('keyword_list_container');
	var hide=true;
	
	if(time && container.last_click && time<=container.last_click)
		hide=false;
		
	if(!container.list_shown && hide)
	{
		container.onclick= click_container;
		var input=document.getElementById(input_id);
		container.style.left= calcOffsetLeft(input)+"px";
		var top= input.offsetHeight + calcOffsetTop(input);
		container.style.top= top+"px";
		container.style.width= (input.offsetWidth -2 )+"px";
		container.style.display='block';
		container.list_shown= true;
	}
}
	
function select_keyword(i) {
	var keyword= keywords[keywords_index[i]]['value'].replace(/&#039;/g,"'").replace(/&quot;/g, "\"").replace(/&amp;/g, "&");
	document.getElementById(input_id).value= keyword;
	document.getElementById('hidden_keyword').value= keyword;
	hide_list();
}

function click_container() {
	var d= new Date();
	container.last_click= d.getTime();
}

function input_blur() {
	var d= new Date();
	setTimeout("hide_list('"+d.getTime()+"');", 500);
}

function see_full_keywords_list(){
	win= window.open("", "keywords", "width=650,height=600,scrollbars=yes,");

	content="<html style='background: #FFFFFF;'><head><title>Full keyword list</title><link rel='stylesheet' type='text/css' href='/?ac_style' /></head><body style='background: #FFFFFF;text-align: left;'>";
	content+="<h2 align='center'>Full keyword list</h2>";
	content+="<h4>Keyword status:</h4>"
	+ "<blockquote>&bull; <span class='standard'>Standard keywords appear in black</span><br />"
	+ "&bull; <span class='free'>Free keywords appear in grey.</span>"
	+ "(&quot;Free&quot; keywords are ones suggested by users. They may integrate the standard list when proved widely used)."
	+ "</blockquote>";
	content+="<div id='keyword_list'>";
	content+="<table><tr><td style='width: 50%'>";
	var nb=0;
	var middle= Math.ceil(keywords.length/2);
	for(var i=0; i<keywords.length; i++){
		if(nb++==middle)
			content+="</td><td style='width: 50%'>";
		content+="<a class='keyword_in_list "+ keywords[i]['class'] +"' onclick='opener.select_keyword("+ keywords[i]['id'] +");window.close();return false;'>&bull; "+ keywords[i]['value'] +"</a>\n";
	}
	content+="</td></tr></table><\div>\n</body></html>";
	win.document.open();
	win.document.write(content);
	win.document.close();
	win.focus();
}

/** Open and update a popup to display stamp data
 * TODO: we can save "moduleURL" on a static constant generated by the module on the header
 */
function stampPopup(stampId, moduleURL) {
	url = moduleURL + 'special?action=stampDescription&stamp=' + stampId + '&type=popup';
	popup = window.open(url, 'Stamp description', 'width=280, height=150, toolbar=no, scrollbars=yes, resizable=yes, status=no, location=no, dependent=yes');
	popup.parent = window;
	popup.focus();
}

/** Open and update a popup to change owner of a resource
 * TODO: we can save "moduleURL" on a static constant generated by the module on the header
 */
function editOwnerPopup(resourceId, moduleURL) {
	url = moduleURL + 'special?action=editResourceOwner&resource=' + resourceId + '&type=popup';
	popup = window.open(url, 'Edit resource owner', 'width=280, height=200, toolbar=no, scrollbars=yes, resizable=yes, status=no, location=no, dependent=yes');
	popup.parent = window;
	popup.focus();
}
/** use a dedicated button to submit a form
 * @exemple onkeydown="return onReturnSubmitButtonById(event, 'buttonSumbit4')"
 */
function onReturnSubmitButtonById(event, id) {
	if (event.keyCode != 13) return true;
	button = document.getElementById(id);
	try {
		// p***n de boggue de FF maintant : "Permission denied to get property XULElement.selectedIndex when calling method: [nsIAutoCompletePopup::selectedIndex]"
		button.click();
	} catch (e) {}
	return false
}
