var editing = false;
var currentEditType = null;
var currentEditOldData = null;
var typesstopicsel = 0;

function removeChildren(s)
{
	while (s.hasChildNodes())
		s.removeChild(s.childNodes[0]);
}

function appendOptionLast(sel,text,value)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = text;
  elOptNew.value = value;

  try {
    sel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    sel.add(elOptNew); // IE only
  }
}

function ajax_get_stopic()
{
   if (typesstopicsel)
  	{
		topicobject = fetch_object('ltopicid');
	}
	else
	{
		topicobject = fetch_object('topicid');	
	}
	
	/* Retrieves data only if different from -1 */
	if(topicobject.options[topicobject.selectedIndex].value != -1)
	{
      stopicAJAX = new vB_AJAX_Handler(true);
      stopicAJAX.onreadystatechange(ajax_wait_stopic);
      stopicAJAX.send('mgc_gloss_ajax.php', 'do=ajax_getstopic&topicid=' + topicobject.options[topicobject.selectedIndex].value + '&securitytoken=' + SECURITYTOKEN);		
	}
}

function ajax_wait_stopic()
{
   if(stopicAJAX.handler.readyState == 4 && stopicAJAX.handler.status == 200 && stopicAJAX.handler.responseXML)
   {
   	if (typesstopicsel)
   	{
   		stopicobj = fetch_object('lstopicid');
   		sstopicobj = fetch_object('lsstopicid');   		
   	}
   	else
   	{
   		stopicobj = fetch_object('stopicid');
   		sstopicobj = fetch_object('sstopicid');   
   	}
   	
   	removeChildren(stopicobj);
		appendOptionLast(stopicobj,phrase_select_inlist,-1);   	
   	
   	removeChildren(sstopicobj);
   	appendOptionLast(sstopicobj,phrase_select_inlist,-1);   	
	      	
		var selitems = fetch_tags(stopicAJAX.handler.responseXML, 'item');
		for (i=0;i<selitems.length;i++)
		{
         stopicid = selitems[i].getAttribute('id');  
         stopictitle = stopicAJAX.fetch_data(selitems[i]);
			appendOptionLast(stopicobj,stopictitle,stopicid);
      }
      stopicobj.disabled = "";

      /* Reset sstopic select */
   	if (typesstopicsel == 1)
   	{
   		sstopicobj = fetch_object('lsstopicid');
   		typesstopicsel = 0;
   	}
   	else
   	{
   		sstopicobj = fetch_object('sstopicid');
   	}      

   	removeChildren(sstopicobj);
		appendOptionLast(sstopicobj,phrase_select_inlist,-1);   	
		sstopicobj.disabled ="disabled";
   }
}   

function ajax_get_sstopic(type)
{
   if (typesstopicsel)
  	{
		stopicobject = fetch_object('lstopicid');
	}
	else
	{
		stopicobject = fetch_object('stopicid');	
	}
	
	/* Retrieves data only if different from -1 */
	if(stopicobject.options[stopicobject.selectedIndex].value != -1)
	{
      sstopicAJAX = new vB_AJAX_Handler(true);
      sstopicAJAX.onreadystatechange(ajax_wait_sstopic);
      sstopicAJAX.send('mgc_gloss_ajax.php', 'do=ajax_getsstopic&stopicid=' + stopicobject.options[stopicobject.selectedIndex].value + '&securitytoken=' + SECURITYTOKEN);		
	}
}

function ajax_wait_sstopic()
{
   if(sstopicAJAX.handler.readyState == 4 && sstopicAJAX.handler.status == 200 && sstopicAJAX.handler.responseXML)
   {
      /* Reset sstopic select */
   	if (typesstopicsel == 1)
   	{
   		sstopicobj = fetch_object('lsstopicid');
   		typesstopicsel = 0;
   	}
   	else
   	{
   		sstopicobj = fetch_object('sstopicid');
   	}      
        
   	removeChildren(sstopicobj);
		appendOptionLast(sstopicobj,phrase_select_inlist,-1);   	
   	
		var selitems = fetch_tags(sstopicAJAX.handler.responseXML, 'item');
		for (i=0;i<selitems.length;i++)
		{
         sstopicid = selitems[i].getAttribute('id');
         sstopictitle = stopicAJAX.fetch_data(selitems[i]);
			appendOptionLast(sstopicobj,sstopictitle,sstopicid);
      }
      sstopicobj.disabled = "";      
   }
}   

function ajax_get_seealso_for_edit(itemid)
{
	if (editing)
	{
		alert(phrase_edit_only_one_item);
	}
	else
	{
		editing 					= true;
		currentEditType 		= "seealso";
		currentItemid 			= itemid;
		currentEditOldData = fetch_object('seealso').innerHTML;
      getSeealsoDataForEditAJAX = new vB_AJAX_Handler(true);
      getSeealsoDataForEditAJAX.onreadystatechange(ajax_wait_seealso_for_edit);
      getSeealsoDataForEditAJAX.send('mgc_gloss_ajax.php', 'do=ajax_getseealsoeditdata&itemid=' + itemid + '&securitytoken=' + SECURITYTOKEN);
	}
}

function ajax_wait_seealso_for_edit()
{
   if(getSeealsoDataForEditAJAX.handler.readyState == 4 && getSeealsoDataForEditAJAX.handler.status == 200 && getSeealsoDataForEditAJAX.handler.responseXML)
   {
      /* Get xml answer content */
		var content_table = fetch_tags(getSeealsoDataForEditAJAX.handler.responseXML, 'content');
		editData = getSeealsoDataForEditAJAX.fetch_data(content_table[0]);

		/* Let's put in the field content ! */
		eval('editObj = fetch_object(\'' + currentEditType + '\');');
		editObj.innerHTML = editData;
   }
}

function ajax_get_data_for_edit(itemid,type,big,withbbcode,withlatex,latextype)
{
	if (editing)
	{
		alert(phrase_edit_only_one_item);
	}
	else
	{
		editing 					= true;
		currentEditType 		= type;
		currentItemid 			= itemid;
		currentEditBBcode 	= withbbcode;
		currentEditLatex		= withlatex;
		currentEditLatexType = latextype;	
		eval('currentEditOldData = fetch_object(\'' + type + '\').innerHTML;');
      getDataForEditAJAX = new vB_AJAX_Handler(true);
      getDataForEditAJAX.onreadystatechange(ajax_wait_data_for_edit);
      getDataForEditAJAX.send('mgc_gloss_ajax.php', 'do=ajax_geteditdata&itemid=' + itemid + '&type=' + type + '&big=' + big + '&securitytoken=' + SECURITYTOKEN);
	}
}

function ajax_wait_data_for_edit()
{
   if(getDataForEditAJAX.handler.readyState == 4 && getDataForEditAJAX.handler.status == 200 && getDataForEditAJAX.handler.responseXML)
   {   
      /* Get xml answer content */
		var content_table = fetch_tags(getDataForEditAJAX.handler.responseXML, 'content');
		editData = getDataForEditAJAX.fetch_data(content_table[0]);
		
		/* Let's put in the field content ! */
		eval('editObj = fetch_object(\'' + currentEditType + '\');');
		editObj.innerHTML = editData;
   }
}

function cancel_edit()
{
	editing = false;
	eval('fetch_object(\'' + currentEditType + '\').innerHTML = currentEditOldData;');
}

function ajax_save_edit()
{
	/* Empty field is forbidden */
	if (PHP.trim(fetch_object('edit').value) == "")
	{
		alert(phrase_empty_field);
		cancel_edit();
	}
	else
	{
		editData = PHP.urlencode(fetch_object('edit').value);		
      saveEditDataAJAX = new vB_AJAX_Handler(true);
      saveEditDataAJAX.onreadystatechange(ajax_wait_save_edit);
      saveEditDataAJAX.send('mgc_gloss_ajax.php', 'do=ajax_saveeditdata&itemid=' + currentItemid + '&type=' + currentEditType + '&data=' + editData + '&bbcode=' + currentEditBBcode + '&latex=' + currentEditLatex + '&ltype=' + currentEditLatexType + '&securitytoken=' + SECURITYTOKEN);	
	}
}

function ajax_wait_save_edit()
{
   if(saveEditDataAJAX.handler.readyState == 4 && saveEditDataAJAX.handler.status == 200 && saveEditDataAJAX.handler.responseXML)
   {
      /* Get xml answer content */
		var content_table = fetch_tags(saveEditDataAJAX.handler.responseXML, 'content');
		editData = saveEditDataAJAX.fetch_data(content_table[0]);
		
		if (currentEditLatex)
		{
			location.reload();
		}
		
		/* Let's put in the field content ! */
		eval('editObj = fetch_object(\'' + currentEditType + '\');');
		editObj.innerHTML = editData;
		editing = false;
   }
}

function ajax_get_breakdown_data_for_edit(itemid)
{
	if (editing)
	{
		alert(phrase_edit_only_one_item);
	}
	else
	{
		editing 				= true;
		currentEditType 	= 'breakdown';
		currentItemid 		= itemid;
		eval('currentEditOldData = fetch_object(\'breakdown\').innerHTML;');
      getBreakdownDataForEditAJAX = new vB_AJAX_Handler(true);
      getBreakdownDataForEditAJAX.onreadystatechange(ajax_wait_breakdown_data_for_edit);
      getBreakdownDataForEditAJAX.send('mgc_gloss_ajax.php', 'do=ajax_getbreakdowneditdata&itemid=' + itemid + '&securitytoken=' + SECURITYTOKEN);
	}
}

function ajax_wait_breakdown_data_for_edit()
{
   if(getBreakdownDataForEditAJAX.handler.readyState == 4 && getBreakdownDataForEditAJAX.handler.status == 200 && getBreakdownDataForEditAJAX.handler.responseXML)
	{
      /* Get xml answer content */
		var content_table = fetch_tags(getBreakdownDataForEditAJAX.handler.responseXML, 'content');
		editData = getBreakdownDataForEditAJAX.fetch_data(content_table[0]);
		
		/* Let's put in the field content ! */
		fetch_object('breakdown').innerHTML = editData;		
	}
}

function ajax_save_breakdownedit()
{
	/* Verify topics are selected ! */
	topicval   = fetch_object('topicid').options[fetch_object('topicid').selectedIndex].value;
	stopicval  = fetch_object('stopicid').options[fetch_object('stopicid').selectedIndex].value;
	sstopicval = fetch_object('sstopicid').options[fetch_object('sstopicid').selectedIndex].value;
	
	if (topicval == -1 || stopicval == -1 || sstopicval == -1)
	{
		alert(phrase_must_select_topic);
		return false;
	}
	   
	breakdownSaveEditAJAX = new vB_AJAX_Handler(true);
	breakdownSaveEditAJAX.onreadystatechange(ajax_wait_breakdownedit);
	breakdownSaveEditAJAX.send('mgc_gloss_ajax.php', 'do=ajax_savebreakdownedit&itemid=' + currentItemid + '&topicid=' + topicval + '&stopicid=' + stopicval + '&sstopicid=' + sstopicval + '&securitytoken=' + SECURITYTOKEN);	
}

function ajax_wait_breakdownedit()
{
   if(breakdownSaveEditAJAX.handler.readyState == 4 && breakdownSaveEditAJAX.handler.status == 200 && breakdownSaveEditAJAX.handler.responseXML)
   {
      /* Get xml answer content */
		var content_table = fetch_tags(breakdownSaveEditAJAX.handler.responseXML, 'content');
		editData = breakdownSaveEditAJAX.fetch_data(content_table[0]);

		/* Let's put in the field content ! */
		fetch_object('breakdown').innerHTML = editData;
		editing = false;
   }
}

function ajax_get_comment_data_for_edit(icommentid)
{
	if (editing)
	{
		alert(phrase_edit_only_one_item);
	}
	else
	{
		editing 				= true;
		currentItemid 		= icommentid;
		currentEditType	= 'comment_' + currentItemid;
		eval('currentEditOldData = fetch_object(\'comment_' + currentItemid + '\').innerHTML;');
      getCommentDataForEditAJAX = new vB_AJAX_Handler(true);
      getCommentDataForEditAJAX.onreadystatechange(ajax_wait_comment_data_for_edit);
      getCommentDataForEditAJAX.send('mgc_gloss_ajax.php', 'do=ajax_getcommenteditdata&icommentid=' + currentItemid + '&securitytoken=' + SECURITYTOKEN);
	}
}

function ajax_wait_comment_data_for_edit()
{
   if(getCommentDataForEditAJAX.handler.readyState == 4 && getCommentDataForEditAJAX.handler.status == 200 && getCommentDataForEditAJAX.handler.responseXML)
	{
      /* Get xml answer content */
		var content_table = fetch_tags(getCommentDataForEditAJAX.handler.responseXML, 'content');
		editData = getCommentDataForEditAJAX.fetch_data(content_table[0]);
		
		/* Let's put in the field content ! */
		fetch_object('comment_' + currentItemid).innerHTML = editData;		
	}
}

function ajax_save_commentedit()
{
	/* Empty field is forbidden */
	if (PHP.trim(fetch_object('edit').value) == "")
	{
		alert(phrase_empty_field);
		cancel_edit();
	}
	else
	{
		editData = PHP.urlencode(fetch_object('edit').value);		
      saveCommentEditDataAJAX = new vB_AJAX_Handler(true);
      saveCommentEditDataAJAX.onreadystatechange(ajax_wait_save_commentedit);
      saveCommentEditDataAJAX.send('mgc_gloss_ajax.php', 'do=ajax_saveecommentditdata&icommentid=' + currentItemid + '&data=' + editData + '&securitytoken=' + SECURITYTOKEN);	
	}
}

function ajax_wait_save_commentedit()
{
   if(saveCommentEditDataAJAX.handler.readyState == 4 && saveCommentEditDataAJAX.handler.status == 200 && saveCommentEditDataAJAX.handler.responseXML)
   {
		/* Comment contains latex => page reload */
		location.reload();
   }
}

function ajax_save_name_edit(itemid)
{
	/* Empty field is forbidden */
	if (PHP.trim(fetch_object('nameedit').value) == "")
	{
		alert(phrase_empty_field);
		cancel_edit();
	}
	else
	{
		editing 				= true;	
		editData = PHP.urlencode(fetch_object('nameedit').value);		
      saveTitleEditDataAJAX = new vB_AJAX_Handler(true);
      saveTitleEditDataAJAX.onreadystatechange(ajax_wait_save_nameedit);
      saveTitleEditDataAJAX.send('mgc_gloss_ajax.php', 'do=ajax_savenameditdata&itemid=' + itemid + '&data=' + editData + '&securitytoken=' + SECURITYTOKEN);	
	}	
}

function ajax_wait_save_nameedit()
{
   if(saveTitleEditDataAJAX.handler.readyState == 4 && saveTitleEditDataAJAX.handler.status == 200 && saveTitleEditDataAJAX.handler.responseXML)
	{
      /* Get xml answer content */
		var content_table = fetch_tags(saveTitleEditDataAJAX.handler.responseXML, 'content');
		editData = saveTitleEditDataAJAX.fetch_data(content_table[0]);
		
		/* Let's put in the field content ! */
		fetch_object('name').innerHTML = editData;
		fetch_object('edit_name').style.display='none';
		fetch_object('name').style.display='';		
		editing 				= false;		
	}
}

function ajax_save_seealsoedit()
{
	editing 				= true;	

	seealsotitle1 = PHP.urlencode(fetch_object('seealso_title1').value);
	seealsotitle2 = PHP.urlencode(fetch_object('seealso_title2').value);	
	seealsotitle3 = PHP.urlencode(fetch_object('seealso_title3').value);
	seealsotitle4 = PHP.urlencode(fetch_object('seealso_title4').value);	
	seealsotitle5 = PHP.urlencode(fetch_object('seealso_title5').value);	
	seealsourl1 = PHP.urlencode(fetch_object('seealso_url1').value);
	seealsourl2 = PHP.urlencode(fetch_object('seealso_url2').value);	
	seealsourl3 = PHP.urlencode(fetch_object('seealso_url3').value);
	seealsourl4 = PHP.urlencode(fetch_object('seealso_url4').value);	
	seealsourl5 = PHP.urlencode(fetch_object('seealso_url5').value);	

	saveSeealsoEditDataAJAX = new vB_AJAX_Handler(true);
	saveSeealsoEditDataAJAX.onreadystatechange(ajax_wait_save_seealsoedit);
	saveSeealsoEditDataAJAX.send(
		'mgc_gloss_ajax.php', 'do=ajax_saveseealsoditdata&itemid=' + currentItemid
			+ '&seealso_title1=' + seealsotitle1
			+ '&seealso_title2=' + seealsotitle2
			+ '&seealso_title3=' + seealsotitle3
			+ '&seealso_title4=' + seealsotitle4
			+ '&seealso_title5=' + seealsotitle5
			+ '&seealso_url1=' + seealsourl1
			+ '&seealso_url2=' + seealsourl2
			+ '&seealso_url3=' + seealsourl3
			+ '&seealso_url4=' + seealsourl4
			+ '&seealso_url5=' + seealsourl5	 + '&securitytoken=' + SECURITYTOKEN																
		);	
}

function ajax_wait_save_seealsoedit()
{
   if(saveSeealsoEditDataAJAX.handler.readyState == 4 && saveSeealsoEditDataAJAX.handler.status == 200 && saveSeealsoEditDataAJAX.handler.responseXML)
	{
      /* Get xml answer content */
		var content_table = fetch_tags(saveSeealsoEditDataAJAX.handler.responseXML, 'content');
		editData = saveSeealsoEditDataAJAX.fetch_data(content_table[0]);

		/* Let's put in the field content ! */
		fetch_object('seealso').innerHTML = editData;	
		editing 				= false;		
	}
}


function go_to_sstopic_page()
{
	topicval   = fetch_object('ltopicid').options[fetch_object('ltopicid').selectedIndex].value;
	stopicval  = fetch_object('lstopicid').options[fetch_object('lstopicid').selectedIndex].value;
	sstopicval = fetch_object('lsstopicid').options[fetch_object('lsstopicid').selectedIndex].value;
	
	if (topicval == -1)
	{
		alert(phrase_select_topics_first);
		return false;
	}
	window.location = "library.php?do=navigate&topicid=" + topicval + "&stopicid=" + stopicval + "&sstopicid=" + sstopicval;
}

function check_search()
{
	texttosearch = fetch_object('text').value;
	
	if (PHP.trim(texttosearch) == "")
	{
		alert(phrase_need_search_text);
		return false;		
	}
}