/* manage_topics.php */

var inEdit = false;
var undoText = '';

function showAddTopic()
{
  if (inEdit)
    return;
  
  document.getElementById("input_topic_title").value="";  
  document.getElementById("add_topic_title").style.display="block";
   
  document.getElementById("input_topic_title").focus();
  inEdit = true; 
}

function showAddChapter( topicrow )
{
  if (inEdit)
    return;
  
  document.getElementById("input_add_chapter_heading_"+topicrow).value="";  
  document.getElementById("add_chapter_heading_"+topicrow).style.display="block";
   
  document.getElementById("input_add_chapter_heading_"+topicrow).focus();
  inEdit = true; 
}

function cancelAddChapter(topicrow)
{
  document.getElementById("add_chapter_heading_"+topicrow).style.display="none"; 
  
  inEdit = false; 
}



function cancelAddTopic()
{
  document.getElementById("add_topic_title").style.display="none"; 
  
  inEdit = false; 
}

function editTopic(topic_num)
{
  if (inEdit)
    return;
    
  undoText = document.getElementById("topic_title_text"+topic_num).innerHTML; 
  document.getElementById("topic_title_"+topic_num).style.display="none";
  document.getElementById("edit_topic_title_"+topic_num).style.display="block"; 
  document.getElementById("input_topic_title_"+topic_num).focus();
  inEdit = true; 
}

function cancelEditTopic(topic_num)
{
  document.getElementById("topic_title_"+topic_num).style.display="block";
  document.getElementById("edit_topic_title_"+topic_num).style.display="none";
  document.getElementById("input_topic_title_"+topic_num).value=undoText.replace("&amp;","&"); 
  inEdit = false; 
}

function applyEditTopic(topic_id, topic_num, keypress, e)
{
  if ( keypress )
  {
    if (!checkEnter(e))
    {
      return true;
    }
  }
  myConn.connect("ajax/ajax_applyEditTopic.php", "POST", "topic_id="+topic_id+"&topic_title="+escape(document.getElementById("input_topic_title_"+topic_num).value), applyEditTopicCallback, topic_num);    
}

function applyAddTopic(keypress, e)
{
  if ( keypress )
  {
    if (!checkEnter(e))
    {
      return true;
    }
  }
  
  
  topicname = document.getElementById("input_topic_title").value;
  if (topicname.replace(' ','') == '')
  {
    cancelAddTopic();
    return;
  }
  
  myConn.connect("manage_topics.php", "POST", "m_id="+m_id+"&action=newtopic&topic_title="+escape(topicname), applyAddTopicCallback);   
}

function applyAddChapter(topicrow, keypress, e)
{
  if ( keypress )
  {
    if (!checkEnter(e))
    {
      return true;
    }
  }
  
  
  chaptername = document.getElementById("input_add_chapter_heading_"+topicrow).value;
  if (chaptername.replace(' ','') == '')
  {
    cancelAddChapter();
    return;
  }
  
  myConn.connect("manage_topics.php", "POST", "m_id="+m_id+"&topic_number="+topicrow+"&action=newchapter&chapter_heading="+escape(chaptername), applyAddChapterCallback);     
}

function applyAddChapterCallback( oXML )
{
  response = oXML.responseText;
  document.getElementById("topics").innerHTML = response;
  inEdit = false;   
}


function applyAddTopicCallback( oXML )
{
  response = oXML.responseText;
  document.getElementById("topics").innerHTML = response;
  cancelAddTopic();
}

function applyEditTopicCallback( oXML, topic_num )
{
  response = oXML.responseText;
  if ( response != "")
  {
    alert(response);
    return;
  }
  document.getElementById("topic_title_"+topic_num).style.display="block";
  document.getElementById("edit_topic_title_"+topic_num).style.display="none";
  document.getElementById("topic_title_text"+topic_num).innerHTML = document.getElementById("input_topic_title_"+topic_num).value;
  inEdit = false;
}

function editChapter(chapter_id)
{
  if (inEdit)
    return;

  undoText = document.getElementById("chapter_heading_text"+chapter_id).innerHTML; 
  document.getElementById("chapter_heading_"+chapter_id).style.display="none";
  document.getElementById("edit_chapter_heading_"+chapter_id).style.display="block"; 
  document.getElementById("input_chapter_heading_"+chapter_id).focus();
  inEdit = true; 
}

function cancelEditChapter(chapter_id)
{
  document.getElementById("chapter_heading_"+chapter_id).style.display="block";
  document.getElementById("edit_chapter_heading_"+chapter_id).style.display="none";
  document.getElementById("input_chapter_heading_"+chapter_id).value=undoText.replace("&amp;","&"); 
  inEdit = false; 
}

function applyEditChapter(chapter_id, keypress, e)
{
  if ( keypress )
  {
    if (!checkEnter(e))
    {
      return true;
    }
  }
  myConn.connect("ajax/ajax_applyEditChapter.php", "POST", "chapter_id="+chapter_id+"&chapter_heading="+escape(document.getElementById("input_chapter_heading_"+chapter_id).value), applyEditChapterCallback, chapter_id);    
}

function applyEditChapterCallback( oXML, chapter_id )
{
  response = oXML.responseText;
  if ( response != "")
  {
    alert(response);
    return;
  }
  document.getElementById("chapter_heading_"+chapter_id).style.display="block";
  document.getElementById("edit_chapter_heading_"+chapter_id).style.display="none";
  
  document.getElementById("chapter_heading_text"+chapter_id).innerHTML = document.getElementById("input_chapter_heading_"+chapter_id).value;
  inEdit = false;
}

function movechapterdown( topicnumber, chapternumber )
{
  showBusy( "cdn"+topicnumber+"_"+chapternumber );
  myConn.connect("manage_topics.php", "POST", "m_id="+m_id+"&action=movechapter&dir=d&topicrow="+topicnumber+"&chapternum="+chapternumber, applyAddChapterCallback);
}

function movechapterup( topicnumber, chapternumber )
{
  showBusy( "cup"+topicnumber+"_"+chapternumber );
  myConn.connect("manage_topics.php", "POST", "m_id="+m_id+"&action=movechapter&dir=u&topicrow="+topicnumber+"&chapternum="+chapternumber, applyAddChapterCallback);
}

 
function movetopicup( topicnumber )
{
  showBusy( "tup"+topicnumber );
  myConn.connect("manage_topics.php", "POST", "m_id="+m_id+"&action=movetopic&dir=u&topicrow="+topicnumber, applyAddChapterCallback);
}

function movetopicdown( topicnumber )
{
  showBusy( "tdn"+topicnumber );
  myConn.connect("manage_topics.php", "POST", "m_id="+m_id+"&action=movetopic&dir=d&topicrow="+topicnumber, applyAddChapterCallback);
}

function deletetopic( topicnumber, topicname )
{
  if (confirm("Are you sure you wish to delete the topic '" + topicname + "'?"))
  {
    myConn.connect("manage_topics.php", "POST", "m_id="+m_id+"&action=deletetopic&topicrow="+topicnumber, applyAddChapterCallback);
  }
}

function deletechapter( topicnumber, chapterid, chaptername )
{
  if (confirm("Are you sure you wish to delete the chapter '" + chaptername + "'?"))
  {
    myConn.connect("manage_topics.php", "POST", "m_id="+m_id+"&action=deletechapter&topicrow="+topicnumber+"&chapterid="+chapterid, applyAddChapterCallback);
  }
}

function showBusy( idnumber )
{
  document.getElementById(idnumber).src="images/mozilla_blu.gif";
  document.getElementById(idnumber).onclick = '';
}
  
function checkEnter(e){ //e is event object passed from function invocation
var characterCode //literal character code will be stored in this variable

if(e && e.which){ //if which property of event object is supported (NN4)
e = e
characterCode = e.which //character code is contained in NN4's which property
}
else{
e = event
characterCode = e.keyCode //character code is contained in IE's keyCode property
}

if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
return true; 
}
else{
return false;
}

}

posX = 0;
posY = 0;

popuptimeout = null;

function popup($term, $def)
{
	
	document.getElementById("glossaryterm").innerHTML = $term;
	document.getElementById("glossarydef").innerHTML = $def;
popuptimeout = null;
	panel = document.getElementById("glossarypanel");
	
	
	panel.style.top = posY + "px";
	panel.style.left = posX + "px";
	panel.style.display="block";
}

function hidepopup()
{
	popuptimeout = setTimeout("hidepopupto()",500);

}
function hidepopupto()
{
	panel = document.getElementById("glossarypanel");
	panel.style.display="none";
}

document.onmousemove = function(evt) {
if (typeof evt == 'undefined') { 
myEvent = window.event; 
} else {
myEvent = evt;
} 
posX = myEvent.clientX;
posY = myEvent.clientY;
}


function editGlossary(id, term, def)
{
	document.getElementById("glossaryid").value = id;
	document.getElementById("glossaryeditterm").value = term;
	
	document.getElementById("glossaryeditdef").value = def;
	document.getElementById("glossaryeditform").style.display = "block";
	document.getElementById("glossaryeditform").style.top = posY + 10 +"px";
	document.getElementById("glossaryeditterm").focus();
	
}

function hideEditGlossary()
{
document.getElementById("glossaryeditform").style.display = "none";
}

function deleteGlossary(id)
{
	if (confirm("Are you sure you wish to delete this glossary entry?"))
	{
		location.href = "glossary.php?action=delete&id="+id;
	}
}

function activatemodule( m_id, module_name, username)
{
	if (document.getElementById("module_rad_curr"+m_id).value == "on")
	{
		return;
	}
	
	
	
	if (confirm("Are you sure you wish to activate module '"+module_name+"' for this user?"))
	{
		document.getElementById("module_rad_curr"+m_id).value = "on";
		
		myConn.connect("ajax/ajax_toggleModule.php", "POST", "m_id="+m_id+"&active=y&username="+username,	 activatemoduleCallback);
		
	}
	else
	{

		document.getElementById("moduleoff_"+m_id).checked = true;
	}
}


function deactivatemodule( m_id, module_name, username)
{
	if (document.getElementById("module_rad_curr"+m_id).value == "off")
	{
		return;
	}

	if (confirm("Are you sure you wish to suspend module '"+module_name+"' for this user?"))
	{
		document.getElementById("module_rad_curr"+m_id).value = "off";
		myConn.connect("ajax/ajax_toggleModule.php", "POST", "m_id="+m_id+"&active=n&username="+username, activatemoduleCallback);
	}
	else
	{
		document.getElementById("moduleon_"+m_id).checked = true;
	}
}

function activatemoduleCallback(oXML)
{
	response = oXML.responseText;
	alert(response);
	location.reload();
}