
function logout() {
	window.location = 'login.php?action=logout';
}

function showPerfil(userId,flowto) {
	 url= 'perfil.php?userid=' + userId;
	if(flowto){
		url+='&flow='+flowto;
	}
	window.location=url;
}

function showUserList() {
	window.location = 'usuaris.php';
}

// Recupera un valor d'un camp de formulari de qualsevol tipus
function getValue(fld)
{
	var tipo = new String(fld.type);
	if ((fld.length > 1) && fld[0].type)
	{
		tipo = new String(fld[0].type);
	}

	if (tipo.indexOf("select") == 0)
	{
		return fld.options[fld.selectedIndex].value;
	}
	else if (tipo == "radio")
	{
		for (var i=0; i<fld.length; i++)
		{
			if (fld[i].checked)
			{
				return fld[i].value;
			}
		}
		return null;
	}
	else
	{
		return fld.value;
	}
}

function getNumber(fld, min, max)
{
	var str = new String(getValue(fld)).replace(',', '.');
	if ((trim(str)=="") || isNaN(str))
	{
		return null;
	}
	else
	{
		var result = Number(str);
		if (
			((typeof(min) != "undefined") && (result < min)) || 
			((typeof(max) != "undefined") && (result > max))
		)
		{
			return null;
		}
		else
		{
			return result;
		}
	}
}

// Modifica el valor d'un camp de formulari de qualsevol tipus
function setValue(fld, val)
{
	var tipo = new String(fld.type);
	if (tipo.indexOf("select") == 0)
	{
		for (var i=0; i<fld.options.length; i++)
		{
			if (fld.options[i].value == val)
			{
				fld.selectedIndex = i;
				return;
			}
		}
	}
	else
	{
		fld.value = val;
	}
}

//function showDetallObservacio(id, prevNext, fechaIni, fechaFin) {
function showDetallObservacio(id, prevNext, listaObs) {
	var url = 'include/detallObservacio.php?observacioid=' + escape(id) + '&prevNext=' + prevNext + 
		//'&fechaIni=' + fechaIni + '&fechaFin=' + fechaFin;
		'&observacions=' + escape(listaObs);
	//alert(url);
	showFinestra('observacio', true, url);
}

function showDetallAlertaEMA(idEMA, prevNext, listaAlerts) {
	var splitId = idEMA.split('_');
	var url = 'include/detallAlertaEMA.php?data=' + escape(splitId[0]) + '&estacio=' + escape(splitId[1]) + 
		'&variable=' + escape(splitId[2])+ '&prevNext=' + prevNext + 
		'&alerts=' + escape(listaAlerts);
	//alert(url);
	showFinestra('observacio', true, url);
}

function trim(txt)
{
	var str = new String(txt);
	return str.replace(/(^\s*)|(\s*$)/g, "");
}


function minimitzaFinestra(idFinestra){	
	var finestra=document.getElementById("content-"+idFinestra);
	if(finestra.style.display==""){
		finestra.style.display="none";
	}else{
		finestra.style.display="";
	}
	
}

function showFinestra(idFinestra, visible, url){
	if (url != null) {
		var iframe = document.getElementById(idFinestra + '-frame');
		if (iframe != null) {
			iframe.src = url;
		}
	}
	var finestra=document.getElementById(idFinestra);
	if(visible){		
		finestra.style.display="";
	}else{		
		finestra.style.display="none";
	}
}

function resizeFrame(id){
	// Variable para Height del iFrame
	var hFrame;
	//var wFrame;
	if(document.frames){
		// ES IE
		// Miramos el contenido del iframe para IE
		hFrame = document.frames(id + "-frame").document.body.scrollHeight;
		//wFrame = document.frames(id + "-frame").document.body.scrollWidth;
	} else {
		// ES OTRO
		// Miramos el contenido del iframe para GEKO/MOZILLA/...
		hFrame = frames[id + "-frame"].document.body.scrollHeight;
		//wFrame = frames[id + "-frame"].document.body.scrollWidth;
	}
	//alert(hFrame);
	document.getElementById(id + "-frame").height = hFrame;
	document.getElementById("content-" + id).style.height = hFrame + "px";
	document.getElementById(id).style.height = (hFrame + 37) + "px";
	//document.getElementById(id + "-frame").width = wFrame;
	//document.getElementById("content-" + id).style.width = wFrame + "px";
	//document.getElementById(id).style.width = (wFrame + 20) + "px";
}

function showFotografia(url) {
	window.open(webroot + '/panels/imatge.php?imageURL=' + url,"_blank");
}

function showValidacio() {
	window.location = 'validacio.php';
}

function showVisor() {
	window.location = 'visor.php';
}

function parseDate(sDate) {
	if (sDate != null) {
		var fechaHora = sDate.split(" ");
		var fecha = fechaHora[0].split("-");
		var hora = fechaHora[1].split(":");
		return new Date(fecha[0], fecha[1]-1, fecha[2], hora[0], hora[1], hora[2], 0);
	} else {
		return null;
	}
}

function formatDate(date) {
	return '' + date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + 
		date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds() + ''; 
}

function formatTitleDate(date) {
	return '' + parsearDosDigitos(date.getDate()) + '/' + parsearDosDigitos(date.getMonth() + 1) + '/' + date.getFullYear() + 
		' a les ' + parsearDosDigitos(date.getHours()) + ':' + parsearDosDigitos(date.getMinutes()) + '';
}

function searchItem(lista, valor){
	var ind, pos;
	for(ind=0; ind<lista.length; ind++) {
    if (lista[ind] == valor)
    	break;
    }
    pos = (ind < lista.length)? ind : -1;
    return (pos);
}

function parsearDosDigitos(num) {
	if (Math.floor(num/10) > 0) {
		return '' + num  + '';
	} else {
		return '0' + num;
	}
}

function updateTime(element, actualText) {
	var now = new Date();
	element.innerHTML = '' + parsearDosDigitos(now.getDate()) + '/' + parsearDosDigitos(now.getMonth() + 1) + '/' + now.getFullYear() + ' a les ' + 
		parsearDosDigitos(now.getHours()) + ':' + parsearDosDigitos(now.getMinutes()) + ' ' + actualText + '';
}

function refreshTime(element, actualText) {
	window.setInterval(function () {updateTime(element, actualText);}, 1000);
}

function getDateFinal() {
	var dateFinal = document.getElementById('fechaFinal');
	if (dateFinal != null) {
		var sDate = dateFinal.innerHTML;
		var fechaHora = sDate.split(" ");
		var fecha = fechaHora[0].split("/");
		var hora = fechaHora[3].split(":");
		return new Date(fecha[2], fecha[1]-1, fecha[0], hora[0], hora[1], 0, 0);
	}
}

function updateVisibility(idClick, idFinestra) {
}	

function exportGoogleEarth(inicio, fin, comarca) {
	url = webroot + '/visor/export.php?inici=' + escape(inicio) + "&fi=" + escape(fin);
	if (comarca) {
		url += "&comarcaId=" + comarca;
	}
	window.open(url,"_blank");
}


function dojoParse(elemName){	
	dojo.require("dojo.parser");
	dojo.require("dijit.layout.TabContainer");
	dojo.require("dijit.layout.ContentPane");
	
	//dojo.addOnLoad(function() {dojo.parser.parse();});
	dojo.parser.parse(dojo.byId(elemName));	
}

function dojoDestroy(elementId){
	dojo.forEach(
  		dojo.query('[widgetId]', document.getElementById(elementId)),
    		function(widget) {      
      			var widgetId=widget.id.substring(7);
      			if (dijit.byId(widgetId)) {      			
        			dijit.byId(widgetId).destroy();
      			}
    		}
	);
	

}

function showHelp(idioma) {
	//alert("Enllaç no configurat");
	window.open("http://www.meteocat.com/mediamb_xemec/servmet/marcs/marc_xemec.html");
}

