/*
 * utils.js
 *
 * Version: $Revision: 1799 $
 *
 * Date: $Date: 2007-04-17 13:15:58 +0100 (Tue, 17 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;
}

//FUNCTION TO EXTEND THE DOCUMENT (+/-)
function showBranch(branch){
 var objBranch = document.getElementById(branch).style;
 if(objBranch.display=="block")
 objBranch.display="none";
 else
 objBranch.display="block";
 objBranch.visibility="visible";
}

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

//FUNCTION FOR THE FIRST PAGE IMAGE CHANG? EFFECT
function changeimage(towhat,url){
if (document.images){
myimage=new Image()
myimage.src=towhat
document.images.targetimage.src=myimage.src
gotolink=url
}
}
function changeimageUK(towhat,url){
if (document.images){
myimageUK=new Image()
myimageUK.src=towhat
document.images.targetimage.src=myimageUK.src
gotolink=url
}
/*}
function changeimage2(towhat,url){
if (document.images){
document.images.targetimage2.src=towhat.src
gotolink=url
}*/

}
function warp(){
window.location=gotolink
}
/*function warp2(){
window.location=gotolink
}*/


var myimages=new Array()
var myimagesUK=new Array()
var gotolink="#"

function createImage(source){

}
function preloadimages(){
for (i=0;i<preloadimages.arguments.length;i++){
//myimages[i]=new Image()
myimages[i]=preloadimages.arguments[i]
}
}

function preloadimagesUK(){
for (i=0;i<preloadimagesUK.arguments.length;i++){
//myimages[i]=new Image()
myimagesUK[i]=preloadimagesUK.arguments[i]
}
}

preloadimages("icons/main/pandekths-fonto-3.jpg","icons/main/INE-PROSOPOGRAFIES.jpg","icons/main/INA-PEGIOIGITES.jpg","icons/main/INE-XARTES.jpg","icons/main/INA-METONOMASIES.jpg","icons/main/INA-ERALDIKA.jpg","icons/main/INE-ZWGRAFOI.jpg","icons/main/INE-EKPAIDEYTIKOI.jpg","icons/main/INE-TYPOS.jpg","icons/main/INE-BIOMHXANIA.jpg","icons/main/IBE-BYZANTINOI.jpg","icons/main/IERA-EPIGRAFES.jpg")

preloadimagesUK("icons/main/pandekths-fonto-3-engl.jpg","icons/main/INE-PROSOPOGRAFIES-engl.jpg","icons/main/INA-PEGIOIGITES-engl.jpg","icons/main/INE-XARTES-engl.jpg","icons/main/INA-METONOMASIES-engl.jpg","icons/main/INA-ERALDIKA-engl.jpg","icons/main/INE-ZWGRAFOI-engl.jpg","icons/main/INE-EKPAIDEYTIKOI-engl.jpg","icons/main/INE-TYPOS-engl.jpg","icons/main/INE-BIOMHXANIA-engl.jpg","icons/main/IBE-BYZANTINOI-engl.jpg","icons/main/IERA-EPIGRAFES-engl.jpg")

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


function onLocationChange(contextPath,counter){
	var selectedValue = document.getElementById('tlocation').options[document.getElementById('tlocation').selectedIndex].value;
	var forms = document.forms;
	//alert("counter"+counter);
	for(var i=0; i< forms.length; i++){
	
		//alert("in for i="+ i);
		if( forms[i].name.indexOf('_') == 12){
			//alert("browseButton recognized");
			forms[i].action = contextPath + selectedValue + '/browse';
		}
	}
	
	//alert("selectedValue = " + selectedValue);
	//alert("contextPath = " + contextPath);
	//alert(contextPath + selectedValue + '/browse');
	
}


//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 = "\u0391\u03C0\u03CC";
	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;
		}
	}	
}


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


var browserLanguage = "el";

function setBrowserLanguage(lang){
	browserLanguage = lang;
}

function getBrowserLanguage(){
	return browserLanguage;
}

/////////////////// Epigraphs //////////////////////
function textLinkPressed(){
	document.getElementById('text_div').style.display = "block";
	document.getElementById('xml_div').style.display = "none";
	document.getElementById('text_link').style.backgroundColor = "Beige";
	document.getElementById('xml_link').style.backgroundColor = "#ECECEC";
}

function xmlLinkPressed(){
	document.getElementById('text_div').style.display = "none";
	document.getElementById('xml_div').style.display = "block";
	document.getElementById('xml_link').style.backgroundColor = "Beige";
	document.getElementById('text_link').style.backgroundColor = "#ECECEC";
}

function textLinkPressedGR(){
	document.getElementById('text_div_gr').style.display = "block";
	document.getElementById('xml_div_gr').style.display = "none";
	document.getElementById('text_link_gr').style.backgroundColor = "Beige";
	document.getElementById('xml_link_gr').style.backgroundColor = "#ECECEC";
}

function xmlLinkPressedGR(){
	document.getElementById('text_div_gr').style.display = "none";
	document.getElementById('xml_div_gr').style.display = "block";
	document.getElementById('xml_link_gr').style.backgroundColor = "Beige";
	document.getElementById('text_link_gr').style.backgroundColor = "#ECECEC";
}

function test(locale){

			var s = document.location.toString(); 

			if(s.indexOf("?") == -1 ){

				toLoad = document.location + '?locale='+locale;
			}
			else{

				if( s.indexOf("locale") == -1) {

					toLoad = document.location + '&locale='+locale;

				}
				else if (s.indexOf("?locale") == -1){

					toLoad = s.substring(0,s.indexOf("&locale")) + s.substring(s.indexOf("&locale")+10) + '&locale='+locale;
				}
				else {

					toLoad = s.substring(0,s.indexOf("?")) + '?locale='+locale +s.substring(s.indexOf("?")+10);
					
				}

			}
//s = document.location + '?locale='+locale;
	//		alert(toLoad);
		//	alert(locale);
		window.location.assign(toLoad);
			window.reload(true);
		}

function changeLocaleShort(locale,shortId){

			var s = document.location.toString(); 

			if(s.indexOf("?") == -1 ){

				toLoad = document.location + '?locale='+locale;
			}
			else{

				if( s.indexOf("locale") == -1) {

					toLoad = document.location + '&locale='+locale;

				}
				else if (s.indexOf("?locale") == -1){

					toLoad = s.substring(0,s.indexOf("&locale")) + s.substring(s.indexOf("&locale")+10) + '&locale='+locale;
				}
				else {

					toLoad = s.substring(0,s.indexOf("?")) + '?locale='+locale +s.substring(s.indexOf("?")+10);
					
				}

			}
		
		if(s.indexOf("type=title") > 0 ){	
			if(s.indexOf("sort_by") == -1 ){
				toLoad = toLoad + '&sort_by=' + shortId;
			}
			else{
				toLoad = toLoad.substring(0,toLoad.indexOf("&sort_by")) + '&sort_by=' + shortId + toLoad.substring(toLoad.indexOf("&sort_by")+10);
			}
		}

		if(s.indexOf("type=author") > 0 ){
			if(s.indexOf("value=") > 0 ){
				if(s.indexOf("sort_by") == -1 ){
					toLoad = toLoad + '&sort_by=' + shortId;
				}
				else{
					toLoad = toLoad.substring(0,toLoad.indexOf("&sort_by")) + '&sort_by=' + shortId + toLoad.substring(toLoad.indexOf("&sort_by")+10);
				}
			}
			else{
				if(locale == "en"){
					toLoad = toLoad.substring(0,toLoad.indexOf("type=author")) + 'type=authorEng' + toLoad.substring(toLoad.indexOf("&",toLoad.indexOf("type=author")));
				}
				else{
					toLoad = toLoad.substring(0,toLoad.indexOf("type=author")) + 'type=authorGr' + toLoad.substring(toLoad.indexOf("&",toLoad.indexOf("type=author")));
				}
			}
		}
		
		if(s.indexOf("type=subject") > 0 ){
			if(s.indexOf("value=") > 0 ){
				if(s.indexOf("sort_by") == -1 ){
					toLoad = toLoad + '&sort_by=' + shortId;
				}
				else{
					toLoad = toLoad.substring(0,toLoad.indexOf("&sort_by")) + '&sort_by=' + shortId + toLoad.substring(toLoad.indexOf("&sort_by")+10);
				}
			}
			else{
				if(locale == "en"){
					toLoad = toLoad.substring(0,toLoad.indexOf("type=subject")) + 'type=subjectEng' + toLoad.substring(toLoad.indexOf("&",toLoad.indexOf("type=subject")));
				}
				else{
					toLoad = toLoad.substring(0,toLoad.indexOf("type=subject")) + 'type=subjectGr' + toLoad.substring(toLoad.indexOf("&",toLoad.indexOf("type=subject")));
				}
			}
		}

		
		
		window.location.assign(toLoad);
			window.reload(true);
		}