function EnllacaIdioma(intOrigen,bIdioma1,bIdioma2){

//catala=1,castellà=2,angles=3
var Idiomes = new Array()
Idiomes[0] = "cat"   	//directori
Idiomes[1] = "cas"
Idiomes[2] = "eng"


	if(document.getElementById){
	
		var strURL = new String(window.location)
		
		/*
		CSC 18/07/2006 
		Ho he hagut de comentar perquè donava un error javascript
		Aquests camps no existeixen a cap pàgina.
		
		var idioma1 = document.getElementById("idioma1_link")
		var idioma2 = document.getElementById("idioma2_link")
		
		
		idioma1.href = strURL.replace("/"+Idiomes[intOrigen-1]+"/","/"+Idiomes[bIdioma1-1]+"/")
		idioma2.href = strURL.replace("/"+Idiomes[intOrigen-1]+"/","/"+Idiomes[bIdioma2-1]+"/")
		
		*/

	}
	
}

function addEvent(obj, evType, fn){ 

	if(obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	}else if (obj.attachEvent){ 
		return obj.attachEvent("on" + evType, fn); 
	}else{ 
		return false; 
	} 
 
}

function SelectIdioma(){
var testIdioma = ""

	try{testIdioma=idioma}
	catch(e){}

	if(testIdioma!=""){
		switch(testIdioma){
			case "cat":
				EnllacaIdioma(1,2,3) 
				break
			case "cas":
				EnllacaIdioma(2,1,3) 
				break
			case "eng":
				EnllacaIdioma(3,1,2) 
				break
		}
	}
}


function TreureTextInput(){

	if(document.getElementById){
		//cercador
		if(document.getElementById("cerca")){
			document.getElementById("cerca").blur()
			addEvent(document.getElementById("cerca"), 'focus', function(){
																		 if(document.getElementById("cerca").value=="cercar" || 
																			document.getElementById("cerca").value=="buscar" ||
																			document.getElementById("cerca").value=="search" 
																			) 
																		 document.getElementById("cerca").value=""
																}
			);
		}

		//qui és qui
		if(document.getElementById("query")){
			document.getElementById("query").blur()
			addEvent(document.getElementById("query"), 'focus', function(){
																		 if(document.getElementById("query").value=="Cerca de persones i organismes" ) 
																		 document.getElementById("query").value=""
																}
			);
		}
	}
	
}

//Retorna true si value es nul , "" o "   "
function esBuit(value){

	if (!value) return true;

	if (value==null) return true;

	if (value=="") return true;

	if (trim(value)=="") return true;

	return false;
}

function buidar(camp){
	
	if (!camp) return;
	
	var input = document.getElementById(camp);
	
	if (!input) return;
	
	if (!input.value) return;
	
	input.value='';

}

function afegirOpcioSelleccionadaDunSelectAUnAltre(selectOrigen, selectDesti){
	//Si el select Origen o Desti son nulls ( no existeixen ) sortim.
	if (!selectOrigen) return;
	if (!selectDesti) return;
	
	//Si el select origen no té cap valor selleccionat, sortim.
	if (esBuit(getSelectedValue(selectOrigen))) return;
	
	//Si el sellect Desti ja conté una opció amb el valor de l'opcio que volem afegir sortim
	if (selectConte(selectDesti,getSelectedValue(selectOrigen))) return;
	
	selectDesti.options [selectDesti.options.length] = new Option (getSelectedValueText(selectOrigen),getSelectedValue(selectOrigen));
}

function selectConte(select,valor){
	//Si el select es null ( no existeix ) sortim.
	if (!select) return;
	
	//Si alguna de les opcions del sellect te el valor que busquem retornem true
	for (i = 0; i< select.options.length; i++){
		if (select.options[i].value == valor) return true;
	}
	//Si no es aixi retornem false
	return false;
}
		

function esborrarOpcioSelleccionada(select){

	index = select.selectedIndex;

	//Si no hi ha cap element seleccionat sortim
	if (index < 0 ) return;
	if (index > select.options.length) return;

	for (i = index; i < select.options.length - 1; i++){
		if ( ( i < select.options.length - 1) && ( i > -1 ) ){
			select.options[i] = new Option(select.options[(i+1)].text, select.options[(i+1)].value);
		}
	}
	select.options[select.options.length - 1] = null;			
}

function ValidaParaulaCerca(){
	
		if(!document.getElementById) return;
		if(!document.getElementById("cercador")) return;
		
		var objFORM = document.getElementById("cercador");

		if(objFORM.nodeName.toLowerCase()!="form"){
			objFORM = objFORM.getElementsByTagName("form");
			
			if(objFORM.length>0){
				objFORM = objFORM[0];
			}else{
				return;
			}
		
		}
		
		objFORM.onsubmit = function(){

			var ca = "Si us plau, introduïu les paraules a cercar.";
			var es = "Por favor, introduzca las palabras a buscar.";
			var en = "Type your search please.";
			var continuar = true;
			var idioma = "";
			
			if(document.documentElement.getAttribute && (document.documentElement.getAttribute("lang")!="" || document.documentElement.getAttribute("xml:lang")!="" )){
				idioma = (document.documentElement.getAttribute("lang")?document.documentElement.getAttribute("lang"):(document.documentElement.getAttribute("xml:lang")?document.documentElement.getAttribute("xml:lang"):""));
			}

			switch(document.getElementById("cerca").value){
				
				case "":
					alert(eval(idioma));
					continuar = false;
					break;
				case "cercar":
					alert(idioma!=""?eval(idioma):ca);
					continuar = false;
					break;
				case "buscar":
					alert(idioma!=""?eval(idioma):es);
					continuar = false;
					break;
				case "search":
					alert(idioma!=""?eval(idioma):en);
					continuar = false;
					break;
			}
		
			if(continuar)
				return true;
			else{
				document.getElementById("cerca").focus();
				return false;
			}
			
		}
		
}

function ObjectsIE(){

	if(navigator.userAgent.toLowerCase().indexOf("msie")>-1 && document.getElementsByTagName && document.createElement){
		script = document.createElement("script");
		script.setAttribute("type","text/javascript");
		script.setAttribute("src","http://www.gencat.cat/js/objects_ie.js");
		document.getElementsByTagName('head')[0].appendChild(script);		
	}
	
}


if(navigator.userAgent.toLowerCase().indexOf("minimo")>-1){
	var blocEstil = document.createElement("style");
	blocEstil.innerHTML = "body{width:100%!important;}"
	blocEstil.innerHTML += "div{width:100%!important;float:none!important;clear:both!important;	margin:0!important;	padding:0!important;}"
	blocEstil.innerHTML += ".border_left,.contingut_pastilla_bg,.contingut_pastilla_bg_nofloat{background-image:none!important;}"
	blocEstil.innerHTML += "div#marc div.menu_superior, div#marc div.menu_superior ul, div#capcalera div#menu_cap ul{float:left !important;}"
	blocEstil.innerHTML += "div#capcalera div#menu_cap ul li{display:inline; float:left; margin-left:0; background-color:#818181; color: #fff;}"
	blocEstil.innerHTML += "div.titulomenu{background-image:none!important;}"
	blocEstil.innerHTML += "div#impacte, div#marc div.imatge_impacte, div#marc div.imatge_impacte_petita,h1, div#bloc_dades_img{display:none;}"
	blocEstil.innerHTML += "div.ocult{position: inherit; height:auto; width:auto; overflow: visible ; margin: .2em auto 0 auto !important; font-size:.7em;}"
	document.getElementsByTagName('head')[0].appendChild(blocEstil);		
}

addEvent(window, 'load', ObjectsIE);
addEvent(window, 'load', SelectIdioma);
addEvent(window, 'load', TreureTextInput);
addEvent(window, 'load', ValidaParaulaCerca);


function getSelectedValue (obj) {

	if (!obj) return;

	var selectedItem = obj.selectedIndex;

	
	if (isNaN(selectedItem)) return;
	if (selectedItem < 0) return;
	var selectedValue = obj.options[selectedItem].value;

	return selectedValue;
}

function getSelectedValueText (obj) {

	var selectedItem = obj.selectedIndex;
	var selectedText = obj.options[selectedItem].text;
	return selectedText;
}

function seleccionarOpcioPerValor (select,valor){

	if (! select) 			return;

	if (! select.options) 	return;

	if (! valor ) 			return;

	for (j=0; j<select.options.length;j++){					
		if (valor == select.options[j].value){
			select.selectedIndex=j;
		}
	}	
}

function desactivarGrup(id, bDisabled) {
     var radioBtns = document.getElementById(id);     
     if (radioBtns != null){     	 
	     for (i = 0; i < radioBtns.length; i++){
	          radioBtns[i].disabled = bDisabled;
	     }
	 }
}

function desactivarTotesSelects(bDisabled) {
	var objs = document.getElementsByTagName('select');
	for (j=0; j<objs.length;j++){					
		desactivarSelect(objs[j].id, bDisabled);
	}
}

function desactivarTotsInputs(bDisabled) {
	var objs = document.getElementsByTagName('input');
	for (j=0; j<objs.length;j++){
		if (objs[j].type == 'radio'){					
			objs[j].disabled = bDisabled;
		}
	}
}

function desactivarSelect(id, bDisabled) {
	var objSelect = document.getElementById(id);
	if (objSelect != null){
		objSelect.disabled=bDisabled;		
	}
}

function desactivarTot(bDisabled){
	desactivarTotesSelects(bDisabled);
	desactivarTotsInputs(bDisabled);
}

function trim(value){
value = lTrim(value);
return rTrim(value);
}

function rTrim(value){
while(value.charAt((value.length -1))==" "){
value = value.substring(0,value.length-1);
}
return value;
}


function lTrim(value){
while(value.charAt(0)==" "){
value = value.replace(value.charAt(0),"");
}
return value;
} 

