// utility functions

// sets the text of any one of the 3 text fields of the form
// input: textindex - index of text field (1,2 or 3)
// 	  txt - the text to set

function SearchTextFromBox(id) {
	var term = new Array();
	var counter = 0;
	var headings = document.getElementById("box" + id).childNodes;
	for (var i=0;i<headings.length;i++) {

		if (headings[i].nodeName.substr(0,2)=="H3" || headings[i].nodeName.substr(0,2)=="H5") { //kk - anti H5 --> H3
			term[counter] = "\"" + headings[i].lastChild.data + "\"";
			counter++;
//			if (term=="") term += "\"" + headings[i].lastChild.data + "\"";
		}  
	}
	return term;
}


function OldSearchTextFromBox(code) {
/* Afti einai i deprecated SearchTextFromBox pou ypostirizei ti sxesi inContextOf
*/
	if (document.getElementById("box" + code).getAttribute('sxesi')=='inContextOf') {	
		SearchTextFromBox(code-1);
		search_query = replace_quot(search_query);
		if (search_query!="") search_query += " AND ";
	}
//	else if (document.getElementById("box" + code).getAttribute('sxesi')=='seeAlso') {
//		SearchTextFromBox(code-1);
//		search_query = replace_quot(search_query);
//		if (search_query!="") search_query += " OR  ";
//	}
		search_query = elaborate_search(search_query, code);

	return search_query;
}


function Oldelaborate_search(term, id, flag) {
/* Afti einai i deprecated elaborate_search pou ypostirizei ti sxesi inContextOf
*/
	var headings = document.getElementById("box" + id).childNodes;
	var flag = false;
	for (var i=0;i<headings.length;i++) {
		//kk - anti H5 --> H3
		if (headings[i].nodeName.substr(0,2)=="H3") {
			flag=true;
			//alert("shit!");
			if (term=="") term += "\"" + headings[i].lastChild.data + "\""; else term += "(" + headings[i].lastChild.data + ")";
		}  
	}
	if (flag) {
//		if ((term.substr(term.length-5, term.length)==" OR  ")||(term.substr(term.length-5, term.length)==" AND ")) term = term.substr(0, term.length-5);
		if (term.substr(term.length-5, term.length)==" AND ") term = term.substr(0, term.length-5);
	}
	return term;
}

function replace_quot(term){
	if (term!="") {
		term = term.substr(1);
		term = "(" + term;
		term = term.substr(0, term.length-1);
		term = term + ")";
	}
	return term;
}

function check_fields() {
/* deprecated. kaleitai apo tin index.html kai frontizei etsi wste na yparxei mono ena zevgari quotations sto query string. Afto apaiteitai apo ton OPAC, oxi to dspace*/
	if ((document.getElementById("entry2").value!="")||(document.getElementById("entry3").value!="")) {
		document.getElementById("entry1").value = replace_quot(document.getElementById("entry1").value);
	}
}

function setSearchText(textindex,txt) {
	if ((textindex<1)||(textindex>3)) return;
	textid = "entry"+textindex;
	if (this.location.href.substring(location.href.lastIndexOf('/')+1)!="included.html") {
		textel = document.getElementById(textid);
	}else {
		textel = window.parent.document.getElementById(textid);
	}
	textel.value = txt;  
}

function delSearchText(textindex) {
	if ((textindex<1)||(textindex>3)) return;
	textid = "entry"+textindex;
	//alert(textid);
	if (this.location.href.substring(location.href.lastIndexOf('/')+1)!="included.html") {
		textel = document.getElementById(textid);
		entry2 = document.getElementById("entry2");
		entry3 = document.getElementById("entry3");
	}else {
		textel = window.parent.document.getElementById(textid);
		entry2 = window.parent.document.getElementById("entry2");
		entry3 = window.parent.document.getElementById("entry3");
	}
	textel.value = ""; 
	entry2.value = "";
	entry3.value = "";
}

function format_query(term) {    //obsolete..  Tin afinoume mpas kai xreiastei sti synexeia....
	term = term.replace(/_dash_/g, ' - ');
	term = term.replace(/_/g, ' ');
	term = term.replace(/comma/g, '');
	return term;
}

var utl_logic_indices = { 'and':0,'or':1,'andnot':2 };
// sets the logical operation to be performed between keywords
// input: logicindex - index of logic element (1 or 2)
// 	  logic - one of 'and', 'or', 'andnot'. Anything else defaults to 'and'
function setLogicalOp(logicindex,op) {
  if ((logicindex<1)||(logicindex>2)) return;
  logicid = "logic"+logicindex;
  logicel = document.getElementById(logicid);
  if (utl_logic_indices[op]) selindex = utl_logic_indices[op];
  else selindex=0;
  logicel.selectedIndex = selindex;
}

var utl_menu_indices = { 'any':0,'title':1,'titleseries':2,'author':3,'subject':4,'isbn':5,'issn':6,'year':7,'lang':8,'notes':9};
// sets the menu selection for keyword search
// input: menuindex - index of menu element (1,2 or 3)
// 	  menu - see list above
function setSearchPosition(menuindex,position) {
  if ((menuindex<1)||(menuindex>3)) return;
  menuid = "menu"+menuindex;
  menuel = document.getElementById(menuid);
  if (utl_menu_indices[position]) selindex = utl_menu_indices[position];
  else selindex=0;
  menuel.selectedIndex = selindex;
}