/*

Copyright (C) 2005-2008 BetaCONCEPT LP.

		  This file is part of BetaCMS.
		 
		  BetaCMS is free software: you can redistribute it and/or modify
		  it under the terms of the GNU General Public License as published by
		  the Free Software Foundation, either version 3 of the License, or
		  (at your option) any later version.
		 
		  BetaCMS is distributed in the hope that it will be useful,
		  but WITHOUT ANY WARRANTY; without even the implied warranty of
		  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		  GNU General Public License for more details.
		 
		  You should have received a copy of the GNU General Public License
		  along with BetaCMS.  If not, see <http://www.gnu.org/licenses/>.
		 
Authors 
		  Gregory Chomatas (gchomatas@betaconcept.com)
		  Savvas Triantafyllou (striantafyllou@betaconcept.com)
	
*/
function parseParametersForTextSearchAndSubmit(pageNumber){
						
	var textSearched=document.getElementById('pageForm:textSearchURL').value;
						
	if (textSearched != null){
		//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;
		}
		
		textSearched = Url.encode(textSearched);				
		location.href='/portal/resource/contentObject/textSearched/'+textSearched+'/pageNumber/'+pageNumber;
		return true;
							
	}
	else {
		alert('Η λέξη αναζήτησης πρέπει να έχει τουλάχιστον 3 χαρακτήρες');
		return false;
	}						
						
}


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){
			parseParametersForTextSearchAndSubmit(1);
		}	
	
    }
}

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){
		//Trim value
		textSearched = textSearched.replace(/^\s*/, "").replace(/\s*$/, "");

		//Check if lenght is less than 3 characters if there is a value
		if (textSearched.length < 3 && textSearched.length >= 1){
			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;
		}
							
		if (textSearched == '') {
			textSearched = 'noTextSearch';
		}
	}
	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);
}

function showExpirationMessageWithRichModalPanel(){
	Richfaces.showModalPanel('expirationMessagePanel');
}
function showFormSubmissionMessage(messagePanel){
	if (document.getElementById('pageForm:successfulFormSubmission').value == 'true') {
		messagePanel.show()
	};
}
/*
 * 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;
  }
}
//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;
	}
} 