
function logout() {
	window.location =webroot +  '/login.php?action=logout';
}
function logout_m() {
	window.location =webroot +  '/login.mb.php?action=logout';
}
function showUserList() {
	window.location = webroot +'/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 = webroot+'/vigilancia/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 = webroot+'/include/detallAlertaEMA.php?data=' + escape(splitId[0]) + '&estacio=' + escape(splitId[1]) + 
		'&variable=' + escape(splitId[2])+ '&prevNext=' + prevNext + 
		'&alerts=' + escape(listaAlerts);
	var dataInici = top.getDateInicial(); 
	if (dataInici) {
		url+="&dataInici="+escape(formatDate(dataInici));
	}
	var dataFi = top.getDateFinal();
	if (dataFi) {
		url+="&dataFi="+escape(formatDate(dataFi));
	}
	//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";
		document.getElementById(idFinestra).style.height = document.getElementById("barra-"+idFinestra).scrollHeight + 'px';
	}else{
		finestra.style.display="";
		var height = document.getElementById("barra-"+idFinestra).scrollHeight + 
			document.getElementById("content-"+idFinestra).scrollHeight + 10;
		document.getElementById(idFinestra).style.height = height + 'px';
	}
	
}

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="";
		top.dojo.publish('/onUpdateVisibility', [idFinestra]);
	}else{
		finestra.style.display="none";
		dojo.publish("/closeFinestra", [idFinestra]);
	}
}

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 = webroot +'/vigilancia/validacio.php';
}

function showVisor() {
	window.location = webroot +'/vigilancia/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 formatDate_mb(date) {
	return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear(); 
	}

function formatTitleDate(date, hideHour) 
{
	var result = '' + parsearDosDigitos(date.getDate()) + '/' + parsearDosDigitos(date.getMonth() + 1) + '/' + date.getFullYear();
	if (!hideHour)
	{
		result += ' a les ' + parsearDosDigitos(date.getHours()) + ':' + parsearDosDigitos(date.getMinutes()) + '';
	}
	return result;
}

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 getDateInicial() {
	var dateFinal = document.getElementById('fechaInicial');
	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) {
	// comprobamos que aun esté en pantalla
	if (top.document.getElementById(idFinestra)) {
		if (idClick == idFinestra) {
			top.document.getElementById(idFinestra).style.zIndex = '100';
		} else {
			top.document.getElementById(idFinestra).style.zIndex = '99';
		}
	}
}	

function exportGoogleEarth(inicio, fin, comarca) {
	url = webroot + '/vigilancia/visor/export.php?inici=' + escape(inicio) + "&fi=" + escape(fin);
	if (comarca) {
		url += "&comarcaId=" + comarca;
	}
	window.open(url,"_blank");
}

function isToday(date) {
	var now = new Date();
	return (date.getFullYear() == now.getFullYear()) && (date.getMonth() == now.getMonth()) &&
		(date.getDate() == now.getDate());
}

function getAbsoluteElementPosition(element) {
	if(typeof element=="string")
		element=document.getElementById(element);
	if(!element)return{top:0,left:0};
	var y=0;
	var x=0;
	while(element.offsetParent) {
		x+=element.offsetLeft;
		y+=element.offsetTop;
		element=element.offsetParent;
	}
	return{top:y,left:x};
}

function showFitxa(id, data, lista) {
	showFinestra('fitxaDia', true, webroot+'/panels/vacia.html');
	var url = webroot+'/observacio/include/fitxa.php?estacio=' + escape(id) + '&data=' + escape(formatDate(data)) + '&noCache=' + new Date().getTime();
	if (lista) {
		url += "&lista=" + escape(lista);
	}
	showFinestra('fitxaDia', true, url);
}


function showFitxa2(id, data, lista) {
	showFinestra('fitxaDia', true, webroot+'/panels/vacia.html');
	var url = webroot+'/observacio/include/fitxa2.php?estacio=' + escape(id) + '&data=' + escape(formatDate(data)) + '&noCache=' + new Date().getTime();
	if (lista) {
		url += "&lista=" + escape(lista);
	}
	showFinestra('fitxaDia', true, url);
}

function baixaEstacio(id)
{
	if (confirm('Si esborra aquesta estació es donaran de baixa les seves metadades.\nVol continuar?')) 
	{
		document.location.href = webroot + "/observacio/variables.php?action=delete&idEstacio=" + id;
	}
}

function makeLink(content, url, link)
{
    if (link)
    {
        var a = document.createElement('a');
        a.href = url;
        a.appendChild(content);
        return a;
    }
    else
    {
        return content;
    }
}

