
var forminputs = null;


function checkItems(ids)
{
	var xmlHttp;
	try
	{
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	  // Internet Explorer
	  try
	  {
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	  catch (e)
	  {
	    try
	    {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch (e)
	    {
	        $("#dialog").dialog("open");
            $("#dialogContent").html("Your browser does not support AJAX!");
	      return false;
	    }
	  }
	}
	xmlHttp.onreadystatechange=function()
	{
	}
	document.getElementById('selectedIds').value = replaceAll(ids, ',', ',');
	var rooter = document.getElementById('root').value;
	xmlHttp.open("POST", rooter + "/setCheckedIds.do", true);
	
	xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", document.getElementById('selectedIds').value.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send('selectedIds=' + document.getElementById('selectedIds').value);
}

function replaceAll(string, oldVal, newVal)
{
	var array=string.split(oldVal);
	var ret = "";
	for (var i = 0; i < array.length; i++)
	{
		if (array[i] != '')
		{
			ret += array[i];
			ret += newVal;
		}
	}
	return ret;
}

function getObjInnerText(obj)
{
	if (obj.innerText) 
	{ // IE;
		return obj.innerText;
	}
	else
	{
		if (obj.textContent) //FF
		{
			return obj.textContent;
		}
		else
			return "";
	}
} 

function URLencode(sStr)
{
    return escape(sStr)
	       .replace(/\+/g, '%2B')
	       .replace(/\"/g,'%22')
	       .replace(/\'/g, '%27');
}

function encodeQuotes(str)
{
	return str.replace(/\+/g, '%2B')
          		.replace(/\"/g,'%22')
            	.replace(/\'/g, '%27');
}

function submit_order(id)
{
	
	// pries submit pakeiciam hidden elemento reiksme, kad Action'as suprastu, jog tai uzsakymas.
	var x=document.getElementsByName('orderStr');
	var ids = '';
  	
	for (i = 0; i < x.length; i++)
	{
		if (x[i].value == 'false')
		{
			x[i].value = 'true';
		}
	}
	
	ids = document.getElementById('selectedIds').value;
	
	if((ids.length <= 2) && ('list' == id))	// jei nera pazymetu id'u ir esame pilname aprase, tada meta klaida
		ShowRecs();
	else
	{	
		if (id == 'list')
			document.getElementById('selectedIdsOrder').value = ids;
		else
			document.getElementById('selectedIdsOrder').value = id;
		document.getElementById('detailsForm').submit();
	}
}

function saveList(id)
{
	//checkSelectedIds(id);
	
	// pries submit pakeiciam hidden elemento reiksme, kad Action'as suprastu, jog tai reiksmiu issaugojimas.
	var x=document.getElementsByName('saveMyList');
	var order = document.getElementsByName('orderStr');
	var ids = '';
	for (i = 0; i < x.length; i++)
	{
		if (x[i].value == 'false')
		{
			x[i].value = 'true';
		}
	}
	for (j = 0; j < order.length; j++)
	{
		if (order[j].value == 'true')
		{
			order[j].value = 'false'; // uzdeda false, jei pries tai buvo paspaustas uzsakymo mygtukas
		}							  // kad action'as reaguotu tinkamai
	}
	
	ids = document.getElementById('selectedIds').value;
		
	if((ids.length <= 2) && ('list' == id))	// jei nera pazymetu id'u ir esame pilname aprase, tada meta klaida
		ShowRecs();
	else
	{	
		document.getElementById('selectedIdsOrder').value = ids;
		document.getElementById('detailsForm').submit();
	}
}

function removeElemFromList(id)
{
	checkSelectedIds(id);

	// pries submit pakeiciam hidden elemento reiksme, kad Action'as suprastu, jog tai reiksmiu issaugojimas.
	var x=document.getElementsByName('removeFromList');
	for (i = 0; i < x.length; i++)
	{
		if (x[i].value == 'false')
		{
			x[i].value = 'true';
		}
	}
	document.getElementById('selectedIdsOrder').value = document.getElementById('selectedIds').value;
	document.getElementById('detailsForm').submit();
}

function select_this(name, value) {
	/*forminputs = document.getElementsByTagName('input');

	for (i = 0; i < forminputs.length; i++) {
		var regex = new RegExp(name, "i");		
		if (regex.test(forminputs[i].getAttribute('name'))) {
			
			if (value == forminputs[i].getAttribute('value')) {
				forminputs[i].checked = true;
				break;
			} else {
				forminputs[i].checked = false;
			}
		}
	}*/
}

function select_all(name, value) {
	
	forminputs = document.getElementsByTagName('input');
	
	for (i = 0; i < forminputs.length; i++) {
		var regex = new RegExp(name, "i");		
		if (regex.test(forminputs[i].getAttribute('name'))) {
			if (value == '1') {
				forminputs[i].checked = true;
			} else {
				forminputs[i].checked = false;
			}
		}
	}
	
	if (value == '1')
	{
		var allIds = document.getElementById('allIds').value;
		var currentPage = document.getElementById('hidCurrentPage').value;
		var pageSize = document.getElementById('hidPageSize').value;
		
		var allIdsArray = allIds.split(',');
		var selectedIds = document.getElementById('selectedIds').value;
		
		var i=0;
		for (i=(parseInt(currentPage)*parseInt(pageSize))-parseInt(pageSize);
			 i < parseInt(currentPage)*parseInt(pageSize); i++)
		{
			selectedIds += allIdsArray[i];
			selectedIds += ',';
		}
		
		checkItems(allIds);
	}
	else
	{
		checkItems("");
	}
}

function setCheckedItems(name, id)
{
	var bi001searchId = document.getElementById('bi001searchId').value;
	
	if (bi001searchId != 'false')
	{
		id = bi001searchId;
	}
	
	if (document.getElementById(name).checked)
	{
		if (document.getElementById('selectedIds').value.indexOf(id) == -1)
		{
			checkItems(document.getElementById('selectedIds').value + id + ',');
		}
	}
	else
	{
		if (document.getElementById('selectedIds').value.indexOf(id) != -1)
		{
			s = new String(document.getElementById('selectedIds').value);
			s = s.replace(id + ",", ',');
			checkItems(s);
		}
	}
}

function changePage(psl, catalog)
{
	var rooter = document.getElementById('root').value;
	var type = 'type';
	var pageSize = document.getElementById('hidPageSize').value;
	var currentPage = document.getElementById('hidCurrentPage').value;
	var cql = "";
	if(null != document.getElementById('CQLQuery'))
	{
		cql = document.getElementById('CQLQuery').value;
		cql = URLencode(cql);
	}
	
	if (psl == 'totalPages')
	{
		psl = document.getElementById('totalPag').value;;
	}
	if (pageSize == '1')
	{
		type = document.getElementById('marcFullType').value;
	}
	window.location=rooter + '/top100changeResultsPage.do?pageNo='+psl+'&pageSizeCh=' + pageSize + '&currentRealPage=' + currentPage + '&marcFullType=' + type + '&doBackSearch=1' + '&CQLQuery=' + cql + '&catalog=' + catalog+'&doNotHistory=1';
}

function getIds()
{
	var pageSize = document.getElementById('hidPageSize').value;
	
	if (pageSize == 1)
	{
		document.getElementById('marcFullTypeA').value = document.getElementById('marcFullType').value;
	}
	else
	{
		// virsutinio selectedIdsLaukasV lauko reiksme reik paimt tik tada, kai rodoma virsutine navigacijos eilute.
		document.getElementById('selectedIdsLaukasV').value = document.getElementById('selectedIds').value;
	}
	
	document.getElementById('selectedIdsLaukasA').value = document.getElementById('selectedIds').value;
}

function orderLoadLink(biId, recNum, catalog)
{
	var rooter = document.getElementById('root').value;
	window.location=rooter + '/orderLoadFormTop100.do?pageSize=1&gotoPage=' + recNum +
	 '&biDetLink=true' + '&biRecordIdLink=' + biId + '&doBackSearch=1' + '&catalog=' + catalog +'&doNotHistory=1';
}

function showListOrOne(type, catalog)
{
	var rooter = document.getElementById('root').value;
	var size = document.getElementById('defaultPageSize').value;
	var cql = "";
	if(null != document.getElementById('CQLQuery'))
	{
		cql = document.getElementById('CQLQuery').value;
		cql = URLencode(cql);
	}
		
	if (catalog == 'true' && cql == "" && document.getElementById('CQLQueryCatalog') != null)
		cql = document.getElementById('CQLQueryCatalog').value;
	
	if (type == 'list')
		window.location=rooter + '/changePageSizeTop100.do?pageSize=' + size + '&doBackSearch=1' + '&catalog=' + catalog +'&doNotHistory=1';
	if (type == 'one')
		window.location=rooter + '/changePageSizeTop100.do?pageSize=1&doBackSearch=1' + '&catalog=' + catalog +'&doNotHistory=1';
}

function specifyParametres()
{
	var rooter = document.getElementById('root').value;
	window.location=rooter + '/specifyParametres.do';
}

function showMarcOrFull(type, catalog)
{
	var rooter = document.getElementById('root').value;
	var pageNum = document.getElementById('fullRecNumber').value;
	var biId = document.getElementById('fullRecId').value;
	
	var cql = "";
	if(null != document.getElementById('CQLQuery'))
	{
		cql = document.getElementById('CQLQuery').value;
		cql = URLencode(cql);
	}
	
	if ((type == 'marc') || (type == 'full'))
	{
		window.location=rooter + '/recordMarcFormTop100.do?pageNo=' + pageNum + '&marcFullType=' + type + '&biId=' + biId + '&doBackSearch=1' + '&pSize=1' +  '&catalog=' + catalog+'&doNotHistory=1';
	}
}

function openPart(field001, type)
{
	var rooter = document.getElementById('root').value;
	
	if (type == 'marcFieldGroupe4')
		window.location=rooter + '/simpleSearch.do?BI001=' + field001 + '&pageSize=1&marcFullType=marc&order=true';
	else
		window.location=rooter + '/simpleSearch.do?BI001=' + field001 + '&pageSize=1&order=true';
}

function searchForAuthor(field001, text)
{
	var rooter = document.getElementById('root').value;
	var filter = document.getElementById('saveFilters').value;
	
	if (field001 != "") 
	{
		text = "";
	}
	
	window.location = rooter + '/simpleSearch.do?@creator=' + text + '&creator001=' + field001 + '&@creator.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&saveFilters=' + filter + '&saveParameters=false&mainFilterArea=true'; // + '&textsToFind-0=' + textsTofind;
}

function searchForAuthorGroup(field001, text)
{
	var rooter = document.getElementById('root').value;
	var filter = document.getElementById('saveFilters').value;
	
	if (field001 != "")
	{
		text = "";
	}
	
	window.location=rooter + '/simpleSearch.do?@authorsGroup=' + text + '&authorsGroup001=' + field001 + '&@authorsGroup.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&saveFilters=' + filter + '&saveParameters=false&mainFilterArea=true'; // + '&textsToFind-1=' + textsTofind;
}

function searchForAboutPerson(field001, text)
{
	var rooter = document.getElementById('root').value;
	var filter = document.getElementById('saveFilters').value;
	
	if (field001 != "")
	{
		text = "";
	}
	
	window.location = rooter + '/simpleSearch.do?@about.person=' + text + '&@about.person001=' + field001 + '&@about.person.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&saveFilters=' + filter + '&saveParameters=false&mainFilterArea=true';
}

function searchForSubject(field001, text)
{
	var rooter = document.getElementById('root').value;
	var filter = document.getElementById('saveFilters').value;
	
	if (field001 != "")
	{
		text = "";
	}
	
	window.location=rooter + '/simpleSearch.do?@subject=' + text + '&subject001=' + field001 + '&@subject.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&saveFilters=' + filter + '&saveParameters=false&mainFilterArea=true'; // + '&textsToFind-10=' + textsTofind;
}

function searchForUDK(fieldUDK)
{
	var rooter = document.getElementById('root').value;
	var filter = document.getElementById('saveFilters').value;
	
	window.location=rooter + '/simpleSearch.do?@udk=' + fieldUDK + '&doSearch=1&doCheck=1&searchSource=1&idsDidFind=&saveFilters=' + filter + '&saveParameters=false&mainFilterArea=true';
}

function searchByColection(catalog)
{
	var rooter = document.getElementById('root').value;
	// kadangi kolekcija yra filtras tai juos saugom
	//var filter = document.getElementById('saveFilters').value;
	var filter = true;
	window.location=rooter + '/simpleSearch.do?filter.area2=' + catalog + '&doSearch=1&doCheck=1&searchSource=1&idsDidFind=&saveFilters=' + filter + '&saveParameters=false&mainFilterArea=true';
}

function searchByAI001(field001)
{
	var rooter = document.getElementById('root').value;
	var filter = document.getElementById('saveFilters').value;
	
	window.location=rooter + '/simpleSearch.do?AI001=' + field001 + '&doSearch=1&saveFilters=' + filter + '&saveParameters=false&mainFilterArea=true';
}

function searchRubrikas(field001, rtype)
{
	var rooter = document.getElementById('root').value;
	var filter = document.getElementById('saveFilters').value;
	
	window.location=rooter + '/aiSearchForm.do?AI001=' + field001 + '&doSearch=1&saveFilters=' + filter + '&saveParameters=false&rtype=' + rtype + '&paieskosTipas=2&mainFilterArea=true';
}

function searchByCreatorText(text)
{
	var rooter = document.getElementById('root').value;
	var filter = document.getElementById('saveFilters').value;
	
	//text = URLencode(text);
	//text = encodeURI(text);
	
	window.location=rooter + '/simpleSearch.do?@creator=' + text + '&@creator.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&saveFilters=' + filter + '&saveParameters=false&mainFilterArea=true';
}

function searchTTT(text, id, textsTofind)
{
	var rooter = document.getElementById('root').value;
	var searchType = document.getElementById("paieskosSarTipas").options[document.getElementById("paieskosSarTipas").selectedIndex].value;
	
	if (textsTofind == 'nebenaudojamasParametras')
	{
		text = getObjInnerText(document.getElementById(id));
		//text = URLencode(text);
		//text = encodeURI(text);
		textsTofind = text;
	}
	
	if (id < 0)
	{
		id = "";
		textsTofind = "";
	}
	
	if (searchType == 0)
	{
		window.location=rooter + '/simpleSearch.do?@creator=' + text + '&@creator.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&idsDidFind-0=' + id + '&textsToFind-0=' + textsTofind+'&mainFilterArea=true';
	}
	
	if (searchType == 1)
	{
		window.location=rooter + '/simpleSearch.do?@authorsGroup=' + text + '&@authorsGroup.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&idsDidFind-1=' + id + '&textsToFind-1=' + textsTofind+'&mainFilterArea=true';
	}
	
	if (searchType == 2)
	{
		window.location=rooter + '/simpleSearch.do?@title=' + text + '&@title.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&idsDidFind=&mainFilterArea=true';
	}
	
	if (searchType == 3)
	{
		window.location=rooter + '/simpleSearch.do?@series=' + text + '&@series.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&idsDidFind=&mainFilterArea=true';
	}
	
	if (searchType == 4)
	{
		window.location=rooter + '/simpleSearch.do?@publisher=' + text + '&@publisher.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&idsDidFind=&mainFilterArea=true';
	}
	
	if (searchType == 5)
	{
		window.location=rooter + '/simpleSearch.do?@udk=' + text + '&doSearch=1&doCheck=1&searchSource=1&idsDidFind=&mainFilterArea=true';
	}
	
	if ((searchType == 6) || (searchType == 7) || (searchType == 8) || (searchType == 9) || (searchType == 10))
	{
		window.location=rooter + '/simpleSearch.do?@subject=' + text + '&@subject.relation=RELATION_EXACT&doSearch=1&doCheck=1&searchSource=1&idsDidFind-10='+ id + '&textsToFind-10=' + textsTofind+'&mainFilterArea=true';
	}
}

function showSelected(type)
{
	var rooter = document.getElementById('root').value;
	var size = document.getElementById('defaultPageSize').value;
	
	if (document.getElementById('selectedIds').value != "," || document.getElementById('selectedIds').value != "")
	{
		if (type == 0)
			window.location=rooter + '/simpleSearch.do?showSelected=selected&gotoPage=1&pageSize=' + size;
	
		if (type == 1)
			window.location=rooter + '/simpleSearch.do?showSelected=all&gotoPage=1&pageSize=' + size;
	}
}

function setSortBy(catalog)
{
	var selectValue = document.getElementById('sortBySelect').value;
	var submitButton = document.getElementById('submitButton');
	var sort = document.getElementsByName('sortBy');
	var search = document.getElementsByName('doSearch');
	for (j = 0; j < sort.length; j++)
	{
		sort[j].value = selectValue;
	}
	for (k = 0; k < search.length; k++)
	{
		search[k].value = '1';
	}
	if ('false' == catalog)
	{
		if (null == submitButton)
			document.getElementById('simpleSearch').submit();
		else 
			submitButton.click();
	}
	else
	{
		var x = document.getElementsByName('sortMyList');
		for (i = 0; i < x.length; i++)
		{
			if (x[i].value == 'false')
			{
				x[i].value = 'true';
			}
		}
	 	document.getElementById('detailsForm').submit();
	 }
}

// Jei atidarytas pilnas aprašas ar unimarc, patikrinam, ar pažymėtų id sąraše yra atidaryto įrašo id. Jei ne, įdeda.
// Tam, kad nebūtų būtina pažymėti tą atidarytą įrašą.
function checkSelectedIds(id)
{
	if (id != "list")
	{
		var idai = document.getElementById('selectedIds').value;
		
		if (idai.indexOf(id) == -1)
		{
			idai = idai + id + ",";
			checkItems(idai);
		}
	}
	
	//document.getElementById('selectedIdsOrder').value = document.getElementById('selectedIds').value;
}

function ShowRecs()
{
	//alert('<bean:message key="searchRezRelated.jsp.NoSelected" />');
    $("#dialog").dialog("open");
    $("#dialogContent").html(message1);
}	
function removeFilters()
{
	if (document.getElementById('removeFltr').checked)
	{
		document.getElementById('saveFilters').value = 'true';
	}
	else
	{
		document.getElementById('saveFilters').value = 'true';
		document.getElementById('removeFltr').disabled = 'false';
	}
}

// <Popup>
// Pilno apraso nuorodos "Tema" atsidarantis meniu langelis.

// Langelio uzdarymas
// Koordinaciu gaudymas, tikrinimas, kada pele iseis is langelio ir pagrindines nuorodos ribu.
//gk=window.Event?1:0; // navigateurs Gecko ou IE
//encours = '';


function hidePopup(mouseover, obj)
{
	if (0 == mouseover)
	{
		obj.style.display = "none";
	}
	else 
		obj.style.display = "block";
}

// Langelio atidarymas paspaudus ant nuorodos
function displayPopup(theme_popup, popup)
{
	var sObject = document.getElementById(theme_popup);
	var popUpTable = document.getElementById("hidDiv_" + theme_popup);
	
	var poshoriz = sObject.offsetLeft;
	var posvert = sObject.offsetTop;
	var pObject = sObject;
	
	while (pObject = pObject .offsetParent){
		poshoriz += pObject .offsetLeft;
		posvert += pObject .offsetTop;
	}

	document.getElementById(popup).innerHTML = popUpTable.innerHTML;
	document.getElementById(popup).style.top = posvert + 15 + "px";
	document.getElementById(popup).style.left = poshoriz +"px";
	document.getElementById(popup).style.display = "block";
}

// </Popup>

function getAllParts()
{
	var rooter = document.getElementById('root').value;
	var tevo_id = document.getElementById('fullRecId').value;
	
	window.location=rooter + '/simpleSearch.do?tevo_id=' + tevo_id + '&doSearch=1&clearAllFilters=1';
}

function myPrint(printObj){
	var cover = document.getElementById("cover");	// salina virseli
	if (null != cover)
		printObj.removeChild(cover);
	
	var html = printObj.innerHTML;
	var head = document.getElementsByTagName("head")[0].innerHTML;
	OpenWindow=window.open("", "Print", "height=auto, width=auto,toolbar=no,scrollbars=yes,menubar=yes");
	OpenWindow.document.write("<html>");
	OpenWindow.document.write("<head>");
	OpenWindow.document.write(head);
	OpenWindow.document.write("</head>");
	OpenWindow.document.write("<body>");
	OpenWindow.document.write(html);
	OpenWindow.document.write("</body>");
	OpenWindow.document.write("</html>");
	OpenWindow.document.close();
	OpenWindow.print();
}

function searchArticles(leidinyje, sourceId)
{
	var rooter = document.getElementById('root').value;
	window.location=rooter + '/simpleSearch.do?@sourceId='+leidinyje+'&source_id='+sourceId+'&doCheck=1&doSearch=1&searchSource=1&filter.area.c9=1';
}


function groupBy(catalog) {
	var selectedValue = document.getElementById('grupavimas').value;
	var rooter = document.getElementById('root').value;
	if (catalog == 'true') 
		window.location=rooter + '/searchRezGroupBy.do?groupBy=' + selectedValue+'&catalog='+catalog+'&doNotHistory=1';
	else window.location=rooter + '/searchRezGroupBy.do?groupBy=' + selectedValue+'&catalog='+catalog+'&doSearch=1';
}

function selectGroup(group) {
	var root = document.getElementById('root').value;
	var cql = document.getElementById('mainCqlQuery').value;
	cql = URLencode(cql);
	window.location=root + '/selectGroup.do?group=' + group + '&mainCqlQuery='+cql;
}
