﻿
var AddEvent = function(el){
	addEvento(el,'click', function(){
		return clickWT(el);
	});
};

var addEvento = function(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 WTGencat(){

    metaTags["DCS.dcssip"] = "www.meteo.cat";

	// marcatge grups i subgrups
	var strURI = window.location.pathname;
	strURI = strURI.toLowerCase();
	strURI = strURI.replace("mediamb_xemec/servmet/","");
	strURI = strURI.replace("mediamb_xemec/","");
	metaTags["DCS.dcsuri"] = strURI;


	var grup = obteNomGrup(strURI);
	var sGrup = obteNomSGrup(strURI);
	
	//títol sense accents
	metaTags["WT.ti"] = sense_accents(document.title.toLowerCase());

	if(grup!=""){		
		metaTags["WT.cg_n"] = grup;
			if(sGrup!=""){		
				metaTags["WT.cg_s"] = sGrup;	
			}
	}

	if(!document.getElementsByTagName) return;
	var ancores = document.getElementsByTagName("a");
	for(var i=0, z=ancores.length; i<z; i++){
		/*if(ancores[i].onclick==null){			//20091221: idescat
			ancores[i].onclick = function(){ 
				return clickWT(this); 
			} 
		}*/
		AddEvent(ancores[i]);
		

	}

	//urls invàlides
	if (document.domain.substr(0,3)=='pre'){	
		return false; 
	} else if (!(document.domain.match('meteocat.')) && !(document.domain.match('meteo.cat'))) { 	
		return false; 	
	}

}

function clickWT(ancora){

	var href = ancora.href;
	var extensio = "";
	var protocol = ""; var strURI = "";
	var domini = "";
	if(href.lastIndexOf(".")>-1){
		extensio =
		href.slice(href.lastIndexOf(".")+1).toLowerCase();
		if(extensio=="pdf" || extensio=="doc" || extensio=="xls"){
			domini = href.slice(href.indexOf("/")+2);
			if(domini.indexOf("/")>-1){
				domini = domini.slice(0,domini.indexOf("/"));
			}
			if(href.indexOf("//")>-1){
				protocol = href.slice(0, href.indexOf("//")+2);
			}
			strURI = href.replace(protocol + domini, "");
			strURI = strURI.toLowerCase();
			strURI = strURI.replace("mediamb_xemec/servmet/","");
			//dcsMultiTrack('DCS.dcssip', domini,'DCS.dcsuri', strURI, 'WT.ti', ancora.innerHTML);
			dcsMultiTrack('DCS.dcssip', "www.meteo.cat",'DCS.dcsuri', strURI, 'WT.ti', ancora.innerHTML);
		}
	}
}

function obteNomGrup(strURI){
	//alert(strURI);

	if(strURI.indexOf("/pagines/")==-1){
		if((strURI.indexOf("/previsio/mitja_termini/")>-1)||(strURI.indexOf("/modelitzacio/")>-1) || (strURI.indexOf("/previsio/municipal")>-1)){
			return "Prediccio automatica";
			
		}else if ((strURI.indexOf("/previsio/previsio")>-1) || (strURI.indexOf("/paicat")>-1)|| (strURI.indexOf("/pedra")>-1)|| (strURI.indexOf("/comarques/")>-1) || (strURI.indexOf("/mar/")>-1) || (strURI.indexOf("/pirineu/")>-1) || (strURI.indexOf("/uvi/")>-1) || (strURI.indexOf("/smr/")>-1)){
			return "Prediccio manual";
			
		}else if (strURI.indexOf("/previsio/resum/")>-1){
			return "Butlleti i resums climatics";
			
		}else if((strURI.indexOf("/radar/")>-1) || (strURI.indexOf("/llamps/")>-1) || (strURI.indexOf("/satelit/")>-1)){
			return "Teledeteccio";		
		
		}else if((strURI.indexOf("/castellano/")>-1) || (strURI.indexOf("/english/")>-1)){
			return "Idiomes";		

		}else if(strURI.indexOf("/xom/")>-1){
			return "Dades XOM";		
			
		}else if(strURI.indexOf("/dades/")>-1){
			return "Climatologia";	

		}else if(strURI.indexOf("/appjava/")>-1){
			return "Dades EMA";	
			
		}else if(strURI.indexOf("/radioson/")>-1){
			return "RAM";	
					
		}else if ((strURI=='/') || (strURI=='/index.html')){ //pagina inicial
			return "Pagina inicial";
			
		}else{
			return "";
		}
	}else{ //dins pagines/
		if (strURI.indexOf("/allaus/")>-1){
			return "Prediccio manual";

		}else if(strURI.indexOf("/xom/")>-1){
			return "Dades XOM";		
		
		}else if((strURI.indexOf("/climescatalunya")>-1) ||(strURI.indexOf("butlleti")>-1)){
			return "Climatologia";		

		}else if((strURI.indexOf("/atlesclim")>-1) || (strURI.indexOf("6190.htm")>-1)){
			return "Atles climatic";		
			
		}else if(strURI.indexOf("/anuaris/")>-1){
			return "Anuaris";		
			
		}else{ //altres dins pagines
			return "Informacions generals";
		}
	}
}

function obteNomSGrup(strURI){
	if(strURI.indexOf("/pagines/")==-1){
		
		if((strURI.indexOf("/previsio/mitja_termini/")>-1) || (strURI.indexOf("/previsio/municipal")>-1)){
			return "Prediccio mitja termini";
			
		}else if (strURI.indexOf("/modelitzacio/")>-1){
			return "Models numerics";
			
		}else if (strURI.indexOf("/previsio/previsio")>-1){
			return "Prediccio general";
			
		}else if (strURI.indexOf("/comarques/")>-1){
			return "Prediccio comarcal";
			
		}else if (strURI.indexOf("/previsio/pedra")>-1){
			return "Prediccio calamarsa";
			
		}else if (strURI.indexOf("/mar/")>-1){
			return "Prediccio maritima";
			
		}else if (strURI.indexOf("/pirineu/")>-1){
			return "Prediccio Pirineu";
					
		}else if (strURI.indexOf("/previsio/paicat")>-1){
			return "Prediccio Paisos Catalans";
			
		}else if(strURI.indexOf("/uvi/")>-1){
			return "Prediccio UVI";		
	
		}else if(strURI.indexOf("/smr/")>-1){
			return "Avisos SMR";		

		}else if(strURI.indexOf("/previsio/resum/")>-1){
			return "Resum diari";		
			
		}else if(strURI.indexOf("/radar/")>-1){
			return "Radar";		

		}else if(strURI.indexOf("/llamps/")>-1){
			return "Llamps";		

		}else if(strURI.indexOf("/satelit/")>-1){
			return "Satel.lit";	
			
		}else if(strURI.indexOf("/castellano/")>-1){
			return "Pagines en castella";			
			
		}else if(strURI.indexOf("/english/")>-1){
			return "Pagines en angles";			
		
		}else if(strURI.indexOf("/xom/vigilancia/")>-1){
			return "Vigilancia";
		
		}else if(strURI.indexOf("/xom/observacio/")>-1){
			return "Observacio";

		}else if(strURI.indexOf("/dades/")>-1){
			return "Petició de dades";		
			
		}else if(strURI.indexOf("dadesdiapermapa")>-1){
			return "Darreres 24 hores. Visualitzacio geografica";	
		
		}else if(strURI.indexOf("selecciopercomarca")>-1){
			return "Seleccio per comarca";	
//
		}else if(strURI.indexOf("selecciopermunicipi")>-1){
			return "Seleccio per municipi";	
			
		}else if(strURI.indexOf("seleccioperestacio")>-1){
			return "Seleccio per EMA";	
			
		}else if(strURI.indexOf("selecciototesEstacions")>-1){
			return "Seleccio totes les EMA";	
			
		}else if(strURI.indexOf("seleccioaltamuntanya")>-1){
			return "Seleccio alta muntanya";		
			
		}else if(strURI.indexOf("resumdiari")>-1){
			return "Resum diari comarca";	
			
		}else if(strURI.indexOf("detall24estacio")>-1){
			return "Dades diaries estacio";			

		}else if(strURI.indexOf("grafiquesestacio")>-1){
			return "Grafiques estacio";	
			
		}else if(strURI.indexOf("fitxaestacio")>-1){
			return "Fitxa Estacio";	
		
		}else if(strURI.indexOf("mapper.do")>-1){
			return "Mapper.do: últimes dades/selecció data";	

		}else if(strURI.indexOf("seleccioboia")>-1){
			return "Mapa seleccio boia";	

		}else if(strURI.indexOf("fitxaboia")>-1){
			return "Fitxa boia";	

		}else if(strURI.indexOf("detall24boia")>-1){
			return "Dades diaries boia";	

		}else if(strURI.indexOf("grafiquesboia")>-1){
			return "Grafiques boia";	
			
		}else if(strURI.indexOf("dadesactualsboia")>-1){
			return "Ultimes dades boia";	

		}else if(strURI.indexOf("/radioson/")>-1){
			return "Radiosondatge";	
			
		}else if ((strURI=='/') || (strURI=='/index.html')){
			return "Pagina inicial (home catala)";		

		}else{
			return "";
		}
		
	}else{   //dins pagines/
		if (strURI.indexOf("/allaus/")>-1){
			return "Prediccio perill allaus";

		}else if (strURI.indexOf("/xom/")>-1){
			return "Descripcio XOM";

		}else if (strURI.indexOf("/inf_productes/")>-1){
			return "Informacio productes";

		}else if (strURI.indexOf("/notes.html")>-1){
			return "Notes informatives";
		
		}else if(strURI.indexOf("/climescatalunya")>-1){
			return "Clima de Catalunya";		

		}else if(strURI.indexOf("/precipitacio6190.htm")>-1){
			return "61-90: Precipitacio";		
			
		}else if(strURI.indexOf("/temperatura6190.htm")>-1){
			return "61-90: Temperatura";		

		}else if(strURI.indexOf("/atlesclim.html")>-1){
			return "Atles Climatic de Catalunya (ACC)";			

		}else if(strURI.indexOf("/atlesclimdigi.html")>-1){
			return "Atles Climatic Digital de Catalunya (ACDC)";						

		}else if(strURI.indexOf("/butlleticlim.html")>-1){
			return "Butlletins climatics";		

		}else if(strURI.indexOf("/anuaris/anuaris.htm")>-1){
			return "Pagina entrada";		
			
		}else if(strURI.indexOf("/anuaris/2007")>-1){
			return "Anuari 2007";		
		
		}else if(strURI.indexOf("/anuaris/2003")>-1){
			return "Anuari 2003";		
			
		}else if(strURI.indexOf("/anuaris/2002")>-1){
			return "Anuari 2002";		
			
		}else if(strURI.indexOf("/anuaris/2001")>-1){
			return "Anuari 2001";		
			
		}else if(strURI.indexOf("/anuaris/2000")>-1){
			return "Anuari 2000";		
			
		}else if(strURI.indexOf("/anuaris/1999")>-1){
			return "Anuari 1999";		
			
		}else if(strURI.indexOf("/anuaris/1998")>-1){
			return "Anuari 1998";		
			
		}else if(strURI.indexOf("/anuaris/1997")>-1){
			return "Anuari 1997";		
			
		}else{
			return "Altres";
		}
	}
}

function sense_accents(s){
    var vocals;
    var res, c, v;
    vocals = new Array("aàáâä", "eèéëê", "iíìïî", "oóòöô", "uúùüû");

    var i, j;
    res = s + '';
    for(i=0;i<vocals.length;i++){
        v = vocals[i]; // TRACTAMENT MINÚSCULES
        c = v.slice(0,1);
        for (j = 1 ; j<vocals[i].length;j++){
            res = res.replace(v.slice(j, j+1), c);
		}
	}

	/* for(i=0;i<vocals.length;i++){
        v = vocals[i].toUpperCase(); // TRACTAMENT MAJÚSCULES
        c = v.slice(0,1);
        for (j = 1 ; j<vocals[i].length;j++){
            res = res.replace(v.slice(j, j+1), c);
		}
	}*/
    return res;
}


	// codifica un string en utf8
	function encode_utf8( string )
	{
		if (encodeURIComponent) {
			stringAux = encodeURIComponent(string);
		} else {
			// versions antigues de navegadors IE i Nestcape, no es traduira a utf8
			stringAux = escape(string);
		}
	
		return unescape(stringAux);
	}

	// codifica tots els camps del formulari en encoding utf8
    function codifica_form_UTF8(theForm) {
	
	   for(i=0; i<theForm.elements.length; i++) {
		   var alertText = "";
	   
		   var elm_obj =  theForm.elements[i];  
		   var elm_name = elm_obj.name;
		   var elm_type = elm_obj.type;
		   var elm_value = elm_obj.value;
		   
		  if(elm_type != "submit" && (!(elm_type == "textarea" && elm_name != "meta_inf")) && elm_type != "button" && elm_type != "reset" && elm_type != "file" && elm_type != "image" && elm_type != "password") {
			   alertText += "Element Name: " + elm_name + "\n"; 
			   alertText += "Element Type: " + elm_type + "\n"; 
				
			  if(elm_type == "text" || (elm_type == "textarea" && elm_name != "meta_inf") || elm_type == "hidden"){
	  		      elm_obj.value = encode_utf8(elm_value);
  				  alertText += "Element Value: " + elm_value + "\n";
			  }
			  else if(elm_type == "checkbox"){
				  elm_obj.value = encode_utf8(elm_value);
				  alertText += "Element Value: " + theForm.elements[i].value + "\n";
			  }
			  else if(elm_type == "radio"){
 				  elm_obj.value = encode_utf8(elm_value); 	
  				  alertText += "Element Value: " + elm_obj.value + "\n";
			  }
			  else if(elm_type == "select-one"){
				  elm_obj.options[elm_obj.selectedIndex].text = encode_utf8(elm_obj.options[elm_obj.selectedIndex].text);
  				  alertText += "Element Value: " + elm_obj.options[elm_obj.selectedIndex].text + "\n";
			  }
			  else if(elm_type == "select-multiple"){

				  var opt_selected = new Array();
    	          var index = 0;
        		  for (var k=0; k < elm_obj.options.length;k++)
	              {
					  if (elm_obj.options[k].selected)
					  {
						 elm_obj.options[k].text = encode_utf8(elm_obj.options[k].text);
 						 opt_selected[index] = elm_obj.options[k].text;
						 index++;
					  }
	              }
				  alertText += "Element Value: " + opt_selected + "\n";
			  }
	  	      //alert(alertText);
		  }
	   }

	}


