// Author : jeremy.k@cs.brandeis.edu
//this function allows the user to hide or show comment
function affCacheHidden() { 
	
	var link = document.getElementById("showcomment");
	//get the name of the link and change it
	if (link.innerHTML == 'Show Comments')
	link.innerHTML = 'Hide Comments';
	else
	link.innerHTML = 'Show Comments';
	
	//loop that hide/show comment 
	for (var i = 1; i < 100; i++) {

idDiv = "wikicomments" + i; 
var div = document.getElementById(idDiv);
if (div.style.display == "")
div.style.display = "none";
else
div.style.display = "";
}

}

//Author : jeremy.k@cs.brandeis.edu
//this function erase a comment when you click on it 
//Second version : javascript remove the comment without reloading the page
function erase(a) {
	//ask confirmation to remove comment
	//if cancel don't do anything
	if (confirm('Do you really want to remove this comment?') == false)
	{return}
	//else go on
	var link = document.getElementById(a);
	//get the link
	str = link.innerHTML;
	//erase comment
	link.style.display = "none";
	//format the comment to erase
	var j = str.search(/<\/A><B>/i);
	str = str.substring(j+8);
	var k = str.search(/\)/i);
	var user = str.slice(1,k+1);
	user = "[/{" + user + "/}"
	//alert(user);
	var l = str.search(/<BR>/i);
	str = str.substring(l+4);
	var m = str.search(/</i);
	str = str.slice(0,m);
	//alert(str);
	com = user + str + "/]";
	//alert(com);
	//get the url of the page by removing all the string between the first /
	var urlPage = window.location.pathname;
	var urlPagebis = urlPage.slice(1);
	var n = urlPagebis.search(/\//)
	var url = urlPagebis.slice(n+1);
	//create the querystring
	var querystring =  url + "?" + "action=copyedit&url=" + url + "&comment=" + com;
	//alert(querystring)
	
	//execute the remove comment action
	var xhr=null;
    
    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.open("GET", querystring, false);
    xhr.send(null);	
}

/*
 * Buffer used to temporarily store link changes
 * Used because not all request were going thorugh and
 * Updating the database correctly.
 */
var link_buffer = null;

/*
 * Create thte XML http object to perform the AJAX calls.
 */
function getXmlHttpObject(handler)
{ 
	var objXMLHttp = null;
	if(window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return objXMLHttp;
}

/*
 * This function is called whenever the user drags N drops the customized 
 * link boxes. This function makes an AJAX call to the linkform action
 * which takes care of updating the database.
 */
function updateLinks(element) {
	str = element.innerHTML;
	str = str.replace(/<li.+?>/g,"")
	result = str.split(/<\/li>/g)
	
	if(link_buffer == null) {
		link_buffer = "&cat1=" + element.id + "&links1=" + result
	}
	else {
		link_new = "&cat2=" + element.id + "&links2=" + result
		//Get the search results
		var pars="action=linkform" + link_buffer + link_new;
		link_buffer = null;
		var url = "http://localhost/dev";
		xmlHttp = getXmlHttpObject();
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
		xmlHttp.send(pars);
	}
}

/*****************************************************/
/* Select link category
/* 
/* This function changes the UI so user gets an input box
/* where he can type in a link category name
/*
/*	Author: johann@cs.brandeis.edu
/*
/*****************************************************/
function selectCategory(ops) {
	var value = ops.options[ops.selectedIndex].value; 
	document.linkform.submit();
}

/**
 * This function is based on Andreas Gohr <andi@splitbrain.org>
 * worcounter script.
 *
 * This function counts the words in the editor as the user types
 * and shows the wordcount next to the buttons.
 */
 
function wordcounter(){
	text = document.getElementById('editor-textarea').value;
	var list = text.split(/[^\w\-_]+/);
	var len  = list.length;
	if(list[len-1] == '') len--;
	if(list[0] == '') len--;
	if(len < 0) len=0;
	
	//Update the counter div
	document.getElementById('wordcounter').innerHTML = 'Word count: ' + len;
}

/*****************************************************/
/*  Insert WikiSticky
/* 
/* This function is called when the user clicks on the
/* WikiSticky when editing a page. The function will insert 
/* the WikiSticky syntax into the editor at the carat location.
/*
/*	Author: johann@cs.brandeis.edu
/*
/*****************************************************/
function insertWikiSticky() { 
	
	myField = document.getElementById('editor-textarea');
	
	myValue = '{sticky}all: {/sticky}';
	
	//IE support, aka "tribute to the worst browser in the world"
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}

}

/*****************************************************/
/*  Navigate between "viewed" pages only
/* 
/* This function is used to navigate the wiki using
/* the Back/Next links. This works similar to a 
/* regular browser activity except only pages that
/* were viewed are available for navigation. Not
/* edited pages.
/*
/*	Author: johann@cs.brandeis.edu
/*
/*****************************************************/
function navigateHistory(what) {

	if(what == 'Back') {
		document.Back_history.submit();
	}
	else if(what == 'Next') {
		document.Next_history.submit();
	}
}

/*****************************************************/
/*  Navigate the CVSViewer 
/* 
/* This function sets the hidden fields with the 
/* data that the user selected and submits the form
/* so we can navigate the cvs repository without
/* lentgty querystrings.
/*
/*	Author: johann@cs.brandeis.edu
/*
/*****************************************************/
function navigateCVSViewer(event,item,revision) {
	_form = document.cvsviewer;
	
	_form.event.value = event;
	_form.path_to_item.value = item;
	_form.revision.value = revision;
	
	document.cvsviewer.submit();
}


/*****************************************************/
/*  Navigate the WikiEye 
/* 
/*	Author: mvirnik@brandeis.edu
/*
/*****************************************************/
function navigateWikiEye(timeframe,user,projectname,frombox) {
	_form = document.wikieyeviewer;
	
	_form.timeframe.value = timeframe;
	_form.user.value = user;
	_form.projectname.value = projectname;
	_form.frombox.value = frombox;
	
	document.wikieyeviewer.submit();
}


/*****************************************************/
/*  Spawn design
/* 
/* This function changes the UI so user gets an input box
/* where he can type in a ring name
/*
/*	Author: johann@cs.brandeis.edu
/*
/*****************************************************/
function newring(ops) {
	var value = ops.options[ops.selectedIndex].value; 
	
	if(value == "NewRing") {
		document.spawnform.submit();
	}
		
}

/*****************************************************/
/*  EmailThreads page.
/* 
/* This function shows/hides the messages that the user
/* clicks on.
/*
/*	Author: johann@cs.brandeis.edu
/*
/*****************************************************/
function toggleMessage(id) {

	_form = document.emailthreads;	
	_form.messageid.value = id;
	
	document.emailthreads.submit();
}

/*
 * This function is called by the EmailThreads page and move the browser down to the email message that was selected to display.
 * This is only done when the user clicks on a email message in the GotLight search results
 */
function moveScrollbarToEmail(id) {
	alert(id);
	if(id != NULL && id != "") {
		document.location = "#" + id;
	}
}
//Dummy function for transcription on email inbox.
function response() {
}

/*****************************************************/
/*  Tag management
/*
/*	Author: johann@cs.brandeis.edu
/*
/*****************************************************/

/*
 * This function is called when a user clicks a [tag] link to open up the tag management window.
 */
function openTagWindow(attrib) {
	window.open("?action=tag_view&attrib="+attrib,"mywindow","location=0,status=1,scrollbars=0,width=400,height=400,resizable=0"); 
}

/*
 * This function is called from the tag management window when the user wants to 
 * assign a tag to an object.
 */
function assignTag(obj) {
	
	var taglist = obj
	
	for(i=taglist.length-1; i>=0; i--)
	{
		if(taglist.options[i].selected)
		{
			value = taglist.options[i].value;
		}
	} 
	
	title = taglist.options[0].text;
	
	if(value != title) {	
		window.open("?action=tag_assign&" + value,"mywindow","location=0,status=1,scrollbars=0,width=400,height=400,resizable=0"); 	
	}
}

/* 
 * This function is called from the tag management window when a user wants to 
 * remove an object from a tag.
 */
function removeTagObject(tag_object_id,tag_type,attributes,tag) {
	window.open("?action=tag_object_remove&tag_type=" + tag_type + "&tag_object_id=" + tag_object_id + "&tag_name=" + tag + "&attrib=" + attributes ,"mywindow","location=0,status=1,scrollbars=0,width=400,height=400,resizable=0"); 	
}

/*
 * This function is called from the tag management window when a user wants to 
 * remove a tag from an object
 */
function removeTag(tag_id,tag_object_id,tag_type,attributes) {
	window.open("?action=tag_remove&tag_id=" + tag_id + "&tag_type=" + tag_type + "&tag_object_id=" + tag_object_id + "&attrib=" + attributes ,"mywindow","location=0,status=1,scrollbars=0,width=400,height=400,resizable=0"); 	
}

/*
 * This function is called from the tag management window when a user wants to create a 
 * new tag.
 */
function createNewTag(attrib) {
	var tag = document.getElementById("new_tag").value;
	
	var tag_type = "public";

	if(tag != null && tag != "") {
		window.open("?action=tag_create&new_tag="+tag+"&tag_type=" + tag_type + "&attrib="+attrib,"mywindow","location=0,status=1,scrollbars=0,width=400,height=400,resizable=0");  
	}
}

/*
 * This function inits the search box. This is necessary to fool Safari because
 * Safari does not allow modifying the look-and-feel of input boxes by default.
 */
var DEF_VAL   = "search here..."; // Default Value
var isSafari = (navigator.userAgent.indexOf("AppleWebKit") !=-1); // Detecting not only Safari but WebKit-based browsers
function initGotLightSearchBox() {
	if (!document.getElementById) return;
	var theSearchField = document.getElementById('gotlightbox'); // Enter search box's id attribute here
	if (isSafari) {
        // Changing type to 'search' from 'text'
        // and inserting 'autosave,' 'results,' 'placeholder' values for Safari and WebKit-based browsers
		theSearchField.setAttribute('type', 'search');
		theSearchField.setAttribute('autosave', 'saved.data');
		theSearchField.setAttribute('results', '5');
		theSearchField.setAttribute('placeholder', DEF_VAL);
	}
}

/*
 * This function submits the search when the user presses the enter button
 */
function performGotLightSearch(e) {
	//Check the key pressed, only submit the search when the user hits enter.
	var key=e.keyCode || e.which;
	if (key==13) {
		gotlight_box = document.getElementById("gotlightbox");
        //Get the search string
		search_string = gotlight_box.value;
		window.location.href = "?action=gotlightsearch&text=" + search_string;
	}
}

/*
 * Executed when the user clicks on the button to export BibTeX citations
 */
function exportCitations() {
	var expBib = 0;
	
	//Check if we need to export raw BibTeX
	var group = document.getElementsByName("expBib");
	for( i = 0; i < group.length; i++ ) {
		if( group[i].checked == true ) {
			expBib = group[i].value;
			break;
		}
	}
	
	//Get tag name
	var tag_name = document.getElementById("exp_tag_name").value;
	
	window.location = '?action=bibtex&export=' + tag_name + '&expbib=' + expBib;
}