//The function getObjNN4(obj,name) returns the object for "name". It starts the search in "obj"
//This function is needed to find nested elements in a page.
function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

// The function returns the style object
function getStyleObject(objectId) {
	if(document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		return getObjNN4(document,objectId);
	} else {
		return false;
	}
} 

// The function hides or shows a page element.
// show an element
// changeObjectVisibility('myObjectId', 'block');
// hide an element
// changeObjectVisibility('myObjectId', 'none');
function changeObjectVisibility(objectId, newVisibility) {
    var styleObject = getStyleObject(objectId, document);
    if(styleObject) {
		styleObject.display = newVisibility;
		return true;
    } 
    else {
		return false;
    }
}

function validateTextSearched(textSearched) {
	
		//Trim value
		textSearched = textSearched.replace(/^\s*/, "").replace(/\s*$/, "");

		//Check if lenght is less than 3 characters if there is a value
		//Must escape less than
		if (textSearched.length < 3){
			alert('Η λέξη αναζήτησης πρέπει να έχει τουλάχιστον 3 χαρακτήρες');
			return false;
		}
												
		//Search for invalid characters
		if (textSearched.indexOf('[') != -1 ||
			textSearched.indexOf(']') != -1 ||
			textSearched.indexOf('\\') != -1 ||
			textSearched.indexOf('/') != -1 ||
			textSearched.indexOf('^') != -1 ||
			textSearched.indexOf('$') != -1 ||
			textSearched.indexOf('.') != -1 ||
			textSearched.indexOf('|') != -1 ||
			textSearched.indexOf('?') != -1 ||
			textSearched.indexOf('*') != -1 ||
			textSearched.indexOf('+') != -1 ||
			textSearched.indexOf(')') != -1 ||
			textSearched.indexOf('(') != -1 ||
			textSearched.indexOf('{') != -1 ||
			textSearched.indexOf('}') != -1 ||
			textSearched.indexOf('&amp;') != -1) {
				alert('Η λέξη αναζήτησης περιέχει μη επιτρεπτούς χαρακτήρες:[,],{,},\\,/,^,$,.,|,?,*,+,(,),&amp;');
				return false;
		}
		
		return true;
}


function submitTextSearch(){
						
	var textSearched=document.getElementById('searchbox').value;
	
	if (textSearched != null) {
		if (validateTextSearched(textSearched)) {					
			textSearched = Url.encode(textSearched);			
			location.href='/portal/resource/contentObject/textSearched/'+textSearched;
		}
	}
	else {
		alert('Η λέξη αναζήτησης πρέπει να έχει τουλάχιστον 3 χαρακτήρες');
	}					
}


function loadResourceListPage(resourceRequestURL, pageNumber) {
	resourceRequestURL = resourceRequestURL.replace(/pageNumber\/[0-9]+/, "pageNumber/" + pageNumber);
	location.href='/portal' + resourceRequestURL;
	return true;
}

function submitTextSearchOnEnterKey(e) {
	var evt=(e)?e:(window.event)?window.event:null;
 	if(evt){
        var key=(evt.charCode)?evt.charCode:((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));
       // alert("key pressed" + key);
        if(key==13){
			submitTextSearch();
		}	
	
    }
}

function callFunctionOnEnterKey(e, func, arg) {
	var evt=(e)?e:(window.event)?window.event:null;
 	if(evt){
        var key=(evt.charCode)?evt.charCode:((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));
       // alert("key pressed" + key);
        if(key==13){
			func(arg);
		}	
	
    }
}

function getValueFromSelectOneMenu(menuId){

	var selectOneMenu  = document.getElementById(menuId);
	if (selectOneMenu != null){
		var selectedValue = selectOneMenu.value;
								
		if (selectedValue == 'org.jboss.seam.ui.NoSelectionConverter.noSelectionValue'){
				selectedValue = '0';
		}
	}
	else{
		selectedValue = '0';
	}
	
	return selectedValue;
}
				
function getValueFromDateField(dateComponentId){
	var dateField = document.getElementById(dateComponentId);
	var date  = document.getElementById(dateComponentId).value;
	if (date == '' || date == null){
		date = '1900-01-01';
	}
	
	return date;
}
				
function parseParametersForQueryRefinementAndSubmit(pageNumber){
						
	var textSearched=document.getElementById('pageForm:textSearchedInQueryRefinement').value;
						
	if (textSearched != null){
		if (validateTextSearched(textSearched)) {
			textSearched = Url.encode(textSearched);
		}
		else {
			return false;
		}	
	}
	else {
		textSearched = 'noTextSearch';
	}						
	
	var selectedContentObjectType = getValueFromSelectOneMenu('pageForm:contentObjectTypesMenu');  
	var selectedTopicFirstLevel  = getValueFromSelectOneMenu('pageForm:subjectTaxonomyFirstLevelSelectOneMenu');
	var selectedTopicSecondLevel  = getValueFromSelectOneMenu('pageForm:subjectTaxonomySecondLevelSelectOneMenu');
							
	var fromDate  = getValueFromDateField('pageForm:fromDateString');
	var toDate  = getValueFromDateField('pageForm:toDateString');
							
	if ( ( textSearched == ''  || textSearched == 'noTextSearch')
		&& selectedContentObjectType == '0'
		&& selectedTopicFirstLevel == '0' 
		&& selectedTopicSecondLevel == '0'
		&& fromDate  == '1900-01-01'
		&& toDate  == '1900-01-01'){
		
		alert('Παρακαλώ εισάγετε τουλάχιστον ένα κριτήριο για να γίνει η αναζήτηση');
		 return false;
	}
						
	var resourceRequestURL = '/portal/resource/contentObject';
	if (selectedContentObjectType != 0) {
		resourceRequestURL = resourceRequestURL + '/contentTypes/' + selectedContentObjectType;
	}
	
	if (selectedTopicSecondLevel != 0) {
		resourceRequestURL = resourceRequestURL + '/topicNames/' + selectedTopicSecondLevel + '/searchInSubtopics/true';
	}
	else if (selectedTopicFirstLevel != 0) {
		resourceRequestURL = resourceRequestURL + '/topicNames/' + selectedTopicFirstLevel + '/searchInSubtopics/true';
	}

	if (fromDate != '1900-01-01') {
		resourceRequestURL = resourceRequestURL + '/fromDateInclusive/' + fromDate;
	}
	
	if (toDate != '1900-01-01') {
		resourceRequestURL = resourceRequestURL + '/toDateInclusive/' + toDate;
	}
	
	if (textSearched != 'noTextSearch') {
		resourceRequestURL = resourceRequestURL + '/textSearched/' + textSearched;
	}
	
	resourceRequestURL = resourceRequestURL + '/pageNumber/' + pageNumber;
	
	location.href = resourceRequestURL;
	return true;
}

function processFirstLevelTopicSelection(secondLevelMenuId) {
	var secondLevelMenu = document.getElementById(secondLevelMenuId);
	if (secondLevelMenu != null) {
		secondLevelMenu.value = 'org.jboss.seam.ui.NoSelectionConverter.noSelectionValue';
	}
	
	parseParametersForQueryRefinementAndSubmit(1);
}
;