/*
 * utils.js
 *
 * Version: $Revision: 1866 $
 *
 * Date: $Date: 2007-04-23 17:20:41 +0300 (Mon, 23 Apr 2007) $
 *
 * Copyright (c) 2004, Hewlett-Packard Company and Massachusetts
 * Institute of Technology.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the Hewlett-Packard Company nor the name of the
 * Massachusetts Institute of Technology nor the names of their
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

/*
 * Utility Javascript methods for DSpace
 */

// Popup window - here so it can be referred to by several methods
var popupWindow;

// =========================================================
//  Methods for e-person popup window
// =========================================================

// Add to list of e-people on this page -- invoked by eperson popup window
function addEPerson(id, email, name)
{
    var newplace = window.document.epersongroup.eperson_id.options.length;

    if (newplace > 0 && window.document.epersongroup.eperson_id.options[0].value == "")
    {
        newplace = 0;
    }

    // First we check to see if e-person is already there
    for (var i = 0; i < window.document.epersongroup.eperson_id.options.length; i++)
    {
        if (window.document.epersongroup.eperson_id.options[i].value == id)
        {
            newplace = -1;
        }
    }

    if (newplace > -1)
    {
        window.document.epersongroup.eperson_id.options[newplace] = new Option(name + " (" + email + ")", id);
    }
}

// Add to list of groups on this page -- invoked by eperson popup window
function addGroup(id, name)
{
    var newplace = window.document.epersongroup.group_ids.options.length;

	if (newplace > 0 && window.document.epersongroup.group_ids.options[0].value == "")
    {
        newplace = 0;
    }

    // First we check to see if group is already there
    for (var i = 0; i < window.document.epersongroup.group_ids.options.length; i++)
    {
        // is it in the list already
        if (window.document.epersongroup.group_ids.options[i].value == id)
        {
            newplace = -1;
        }

        // are we trying to add the new group to the new group on an Edit Group page (recursive)
        if (window.document.epersongroup.group_id)
        {
            if (window.document.epersongroup.group_id.value == id)
            {
                newplace = -1;
            }
        }
    }

    if (newplace > -1)
    {
        window.document.epersongroup.group_ids.options[newplace] = new Option(name + " (" + id + ")", id);
    }
}

// This needs to be invoked in the 'onClick' javascript event for buttons
// on pages with a dspace:selecteperson element in them
function finishEPerson()
{
    selectAll(window.document.epersongroup.eperson_id);

	if (popupWindow != null)
	{
		popupWindow.close();
	}
}

// This needs to be invoked in the 'onClick' javascript event for buttons
// on pages with a dspace:selecteperson element in them
function finishGroups()
{
    selectAll(window.document.epersongroup.group_ids);

    if (popupWindow != null)
    {
		popupWindow.close();
    }
}

// =========================================================
//  Miscellaneous utility methods
// =========================================================

// Open a popup window (or bring to front if already open)
function popup_window(winURL, winName)
{
    var props = 'scrollBars=yes,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width=640,height=480';
    popupWindow = window.open(winURL, winName, props);
    popupWindow.focus();
}


// Select all options in a <SELECT> list
function selectAll(sourceList)
{
    for(var i = 0; i < sourceList.options.length; i++)
    {
        if ((sourceList.options[i] != null) && (sourceList.options[i].value != ""))
            sourceList.options[i].selected = true;
    }
    return true;
}

// Deletes the selected options from supplied <SELECT> list
function removeSelected(sourceList)
{
    var maxCnt = sourceList.options.length;
    for(var i = maxCnt - 1; i >= 0; i--)
    {
        if ((sourceList.options[i] != null) && (sourceList.options[i].selected == true))
        {
            sourceList.options[i] = null;
        }
    }
}






//******************************************************
// Functions used by controlled vocabulary add-on
// There might be overlaping with existing functions
//******************************************************

function expandCollapse(node, contextPath) {
	node = node.parentNode;
	var childNode  = (node.getElementsByTagName("ul"))[0];

	if(!childNode) return false;

	var image = node.getElementsByTagName("img")[0];
	
	if(childNode.style.display != "block") {
		childNode.style.display  = "block";
		image.src = contextPath + "/image/controlledvocabulary/m.gif";
		image.alt = "Collapse search term category";
	} else {
		childNode.style.display  = "none";
		image.src = contextPath + "/image/controlledvocabulary/p.gif";
		image.alt = "Expand search term category";
	}
	
	return false;
}


function getAnchorText(ahref) {
 	if(isMicrosoft()) return ahref.childNodes.item(0).nodeValue;
	else return ahref.text;
}

function getTextValue(node) {
 	if(node.nodeName == "A") {
 		return getAnchorText(node);
 	} else {
 		return "";
 	}
 	
}


function getParentTextNode(node) {
	var parentNode = node.parentNode.parentNode.parentNode;
	var children = parentNode.childNodes;
	var textNode;
	for(var i=0; i< children.length; i++) {
		var child = children.item(i);
		if(child.className == "value") {
			return child;
		}
	}
	return null;
}

function ec(node, contextPath) {
	expandCollapse(node, contextPath);
	return false;
}


function i(node) {
	return sendBackToParentWindow(node);
}


function getChildrenByTagName(rootNode, tagName) {
	var children = rootNode.childNodes;
	var result = new Array(0);
	if(children == null) return result;
	for(var i=0; i<children.length; i++) {
		if(children[i].tagName == tagName) {
			var elementArray = new Array(1);
			elementArray[0] = children[i];
			result = result.concat(elementArray);
		}
	}
	return result;
}

function popUp(URL) {
	var page;
	page = window.open(URL, 'controlledvocabulary', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=650,height=450');
}


function isNetscape(v) {
		  return isBrowser("Netscape", v);
}
	
function isMicrosoft(v) {
		  return isBrowser("Microsoft", v);
}

function isMicrosoft() {
		  return isBrowser("Microsoft", 0);
}


function isBrowser(b,v) {
		  browserOk = false;
		  versionOk = false;

		  browserOk = (navigator.appName.indexOf(b) != -1);
		  if (v == 0) versionOk = true;
		  else  versionOk = (v <= parseInt(navigator.appVersion));
		  return browserOk && versionOk;
}


////////////////////////////////////////////////////////////////////////////////////////////


//FUNCTIONS FOR THE ADVANCED SERACH EFFECTS////////////////////////////////

function onFieldChange(){
	var selectedIndex = document.getElementById('tfield1').selectedIndex;
	if (selectedIndex == 7 || selectedIndex == 13){
		document.getElementById('tfield4_column').style.display = "block";
		document.getElementById('tfield4_label').style.display = "block";
		document.getElementById('tfield4').style.display = "block";	
	}
	else{
		document.getElementById('tfield4_column').style.display = "none";
		document.getElementById('tfield4_label').style.display = "none";
		document.getElementById('tfield4').style.display = "none";
		//document.getElementById('tquery1_label').innerHTML = "Search for:";
		document.getElementById('tquery4_label').style.display = "none";
		document.getElementById('tquery4').style.display = "none";
		document.getElementById('date_row').setAttribute('height','25');
		document.getElementById('tfield4').selectedIndex = 0;
		document.getElementById('tfield4').options[0].selected = true;
		document.getElementById('tquery4').value = "";
	}	
}

function onFieldChange2(){
	var selectedIndex = document.getElementById('tfield2').selectedIndex;
	if (selectedIndex == 7 || selectedIndex == 13){
		document.getElementById('tfield5_column').style.display = "block";
		document.getElementById('tfield5_label').style.display = "none";
		document.getElementById('tfield5').style.display = "block";
	}
	else{
		document.getElementById('tfield5_column').style.display = "none";
		document.getElementById('tfield5_label').style.display = "none";
		document.getElementById('tfield5').style.display = "none";
		document.getElementById('tquery2_label').style.display = "none";
		document.getElementById('tquery5_label').style.display = "none";
		document.getElementById('tquery5').style.display = "none";
		document.getElementById('date_row2').setAttribute('height','25');
		document.getElementById('tfield5').selectedIndex = 0;
		document.getElementById('tfield5').options[0].selected = true;
		document.getElementById('tquery5').value = "";
	}	
}

function onFieldChange3(){
	var selectedIndex = document.getElementById('tfield3').selectedIndex;
	if (selectedIndex == 7 || selectedIndex == 13){
		document.getElementById('tfield6_column').style.display = "block";
		document.getElementById('tfield6_label').style.display = "none";
		document.getElementById('tfield6').style.display = "block";
	}
	else{
		document.getElementById('tfield6_column').style.display = "none";
		document.getElementById('tfield6_label').style.display = "none";
		document.getElementById('tfield6').style.display = "none";
		document.getElementById('tquery3_label').style.display = "none";
		document.getElementById('tquery6_label').style.display = "none";
		document.getElementById('tquery6').style.display = "none";
		document.getElementById('date_row3').setAttribute('height','25');
		document.getElementById('tfield6').selectedIndex = 0;
		document.getElementById('tfield6').options[0].selected = true;
		document.getElementById('tquery6').value = "";
	}	
}

function onDateSelection()
{
	document.getElementById('tfield4_column').style.display = "block";
	document.getElementById('tfield4_label').style.display = "block";
	document.getElementById('tfield4').style.display = "block";
}

function onOtherTypeSelection()
{
	document.getElementById('tfield4_column').style.display = "none";
	document.getElementById('tfield4_label').style.display = "none";
	document.getElementById('tfield4').style.display = "none";
	//document.getElementById('tquery1_label').innerHTML = "Search for:";
	document.getElementById('tquery4_label').style.display = "none";
	document.getElementById('tquery4').style.display = "none";
	document.getElementById('date_row').setAttribute('height','25');
	document.getElementById('tfield4').selectedIndex = 0;
	document.getElementById('tfield4').options[0].selected = true;
	document.getElementById('tquery4').value = "";
}

function onDateSelection2()
{
	document.getElementById('tfield5_column').style.display = "block";
	document.getElementById('tfield5_label').style.display = "none";
	document.getElementById('tfield5').style.display = "block";
}

function onOtherTypeSelection2()
{
	document.getElementById('tfield5_column').style.display = "none";
	document.getElementById('tfield5_label').style.display = "none";
	document.getElementById('tfield5').style.display = "none";
	document.getElementById('tquery2_label').style.display = "none";
	document.getElementById('tquery5_label').style.display = "none";
	document.getElementById('tquery5').style.display = "none";
	document.getElementById('date_row2').setAttribute('height','25');
	document.getElementById('tfield5').selectedIndex = 0;
	document.getElementById('tfield5').options[0].selected = true;
	document.getElementById('tquery5').value = "";
}

function onDateSelection3()
{
	document.getElementById('tfield6_column').style.display = "block";
	document.getElementById('tfield6_label').style.display = "none";
	document.getElementById('tfield6').style.display = "block";
}

function onOtherTypeSelection3()
{
	document.getElementById('tfield6_column').style.display = "none";
	document.getElementById('tfield6_label').style.display = "none";
	document.getElementById('tfield6').style.display = "none";
	document.getElementById('tquery3_label').style.display = "none";
	document.getElementById('tquery6_label').style.display = "none";
	document.getElementById('tquery6').style.display = "none";
	document.getElementById('date_row3').setAttribute('height','25');
	document.getElementById('tfield6').selectedIndex = 0;
	document.getElementById('tfield6').options[0].selected = true;
	document.getElementById('tquery6').value = "";
} 

function onOptionChange(fromLabel, searchLabel){
	var selectedIndex = document.getElementById('tfield4').selectedIndex;
	if (selectedIndex == 3){
		//document.getElementById('tquery1_label').innerHTML = "\u0391\u03C0\u03CC";
		document.getElementById('tquery1_label').innerHTML = fromLabel;
		document.getElementById('tquery4_label').style.display = "block";
		document.getElementById('tquery4').style.display = "block";
		document.getElementById('date_row').setAttribute('height','50');
	}
	else{
		//document.getElementById('tquery1_label').innerHTML = "Search for:";
		document.getElementById('tquery1_label').innerHTML = searchLabel;
		document.getElementById('tquery4_label').style.display = "none";
		document.getElementById('tquery4').style.display = "none";
		document.getElementById('date_row').setAttribute('height','25');
		document.getElementById('tquery4').value = "";
	}	
}

function onOptionChange2(fromLabel, searchLabel){
	var selectedIndex = document.getElementById('tfield5').selectedIndex;
	if (selectedIndex == 3){
		//document.getElementById('tquery2_label').innerHTML = "\u0391\u03C0\u03CC";
		document.getElementById('tquery2_label').innerHTML = fromLabel;
		document.getElementById('tquery2_label').style.display = "block";
		document.getElementById('tquery5_label').style.display = "block";
		document.getElementById('tquery5').style.display = "block";
		document.getElementById('date_row2').setAttribute('height','50');
	}
	else{
		document.getElementById('tquery2_label').style.display = "none";
		document.getElementById('tquery5_label').style.display = "none";
		document.getElementById('tquery5').style.display = "none";
		document.getElementById('date_row2').setAttribute('height','25');
		document.getElementById('tquery5').value = "";
	}	
}

function onOptionChange3(fromLabel, searchLabel){
	var selectedIndex = document.getElementById('tfield6').selectedIndex;
	if (selectedIndex == 3){
		//document.getElementById('tquery3_label').innerHTML = "\u0391\u03C0\u03CC";
		document.getElementById('tquery3_label').innerHTML = fromLabel;
		document.getElementById('tquery3_label').style.display = "block";
		document.getElementById('tquery6_label').style.display = "block";
		document.getElementById('tquery6').style.display = "block";
		document.getElementById('date_row3').setAttribute('height','50');
	}
	else{
		document.getElementById('tquery3_label').style.display = "none";
		document.getElementById('tquery6_label').style.display = "none";
		document.getElementById('tquery6').style.display = "none";
		document.getElementById('date_row3').setAttribute('height','25');
		document.getElementById('tquery6').value = "";
	}	
}

function onBetweenSelection()
{
	document.getElementById('tquery1_label').innerHTML = '<fmt:message key="jsp.search.advanced.option.to"/>';
	document.getElementById('tquery4_label').style.display = "block";
	document.getElementById('tquery4').style.display = "block";
	document.getElementById('date_row').setAttribute('height','50');
}

function onBetweenSelection2()
{
	document.getElementById('tquery2_label').innerHTML = "\u0391\u03C0\u03CC";
	document.getElementById('tquery2_label').style.display = "block";
	document.getElementById('tquery5_label').style.display = "block";
	document.getElementById('tquery5').style.display = "block";
	document.getElementById('date_row2').setAttribute('height','50');
}

function onBetweenSelection3()
{
	document.getElementById('tquery3_label').innerHTML = "\u0391\u03C0\u03CC";
	document.getElementById('tquery3_label').style.display = "block";
	document.getElementById('tquery6_label').style.display = "block";
	document.getElementById('tquery6').style.display = "block";
	document.getElementById('date_row3').setAttribute('height','50');
}

function onOtherOptionSelection()
{
	//document.getElementById('tquery1_label').innerHTML = "Search for:";
	document.getElementById('tquery4_label').style.display = "none";
	document.getElementById('tquery4').style.display = "none";
	document.getElementById('date_row').setAttribute('height','25');
	document.getElementById('tquery4').value = "";
}

function onOtherOptionSelection2()
{
	document.getElementById('tquery2_label').style.display = "none";
	document.getElementById('tquery5_label').style.display = "none";
	document.getElementById('tquery5').style.display = "none";
	document.getElementById('date_row2').setAttribute('height','25');
	document.getElementById('tquery5').value = "";
}

function onOtherOptionSelection3()
{
	document.getElementById('tquery3_label').style.display = "none";
	document.getElementById('tquery6_label').style.display = "none";
	document.getElementById('tquery6').style.display = "none";
	document.getElementById('date_row3').setAttribute('height','25');
	document.getElementById('tquery6').value = "";
}

function onResetClick(){
	onOtherTypeSelection();
	onOtherTypeSelection2();
	onOtherTypeSelection3();
}

function validateDateNumber(check){
	if (document.getElementById('tfield1').selectedIndex == 7 || document.getElementById('tfield1').selectedIndex == 13){
		var date1 = document.getElementById('tquery1').value;
		var date2 = document.getElementById('tquery4').value;
		if (!isNaN(date1) && !isNaN(date2)){
			document.getElementById('error_log').style.display = "none";
			document.getElementById('adv_search_submit').disabled = false;
			if (check){
				validateDateNumber2(false);
				validateDateNumber3(false);	
			}
		}
		else{
			document.getElementById('error_log').innerHTML = "Please, insert a valid year!";
			document.getElementById('error_log').style.display = "block";
			document.getElementById('adv_search_submit').disabled = true;
		}
	}
}

function validateDateNumber2(check){
	if (document.getElementById('tfield2').selectedIndex == 7 || document.getElementById('tfield2').selectedIndex == 13){
		var date1 = document.getElementById('tquery2').value;
		var date2 = document.getElementById('tquery5').value;
		if (!isNaN(date1) && !isNaN(date2)){
			document.getElementById('error_log').style.display = "none";
			document.getElementById('adv_search_submit').disabled = false;
			if (check){
				validateDateNumber(false);
				validateDateNumber3(false);
			}
		}
		else{
			document.getElementById('error_log').innerHTML = "Please, insert a valid year!";
			document.getElementById('error_log').style.display = "block";
			document.getElementById('adv_search_submit').disabled = true;
		}
	}	
}

function validateDateNumber3(check){
	if (document.getElementById('tfield3').selectedIndex == 7 || document.getElementById('tfield3').selectedIndex == 13){
		var date1 = document.getElementById('tquery3').value;
		var date2 = document.getElementById('tquery6').value;
		if (!isNaN(date1) && !isNaN(date2)){
			document.getElementById('error_log').style.display = "none";
			document.getElementById('adv_search_submit').disabled = false;
			if (check){
				validateDateNumber(false);
				validateDateNumber2(false);
			}
		}
		else{
			document.getElementById('error_log').innerHTML = "Please, insert a valid year!";
			document.getElementById('error_log').style.display = "block";
			document.getElementById('adv_search_submit').disabled = true;
		}
	}	
}


//////////////////////////////////////////////////////////////////////////


