////////////////////////////////////////////////////////////////////////////////
// Modelització d'una estació
////////////////////////////////////////////////////////////////////////////////
function estacio(sNomEstacio, iIdentificador, sNomMunicipi) {
	
	var o = new Object();
	// propietats privades
	o.sNomEstacio = sNomEstacio;
	o.iIdentificador = iIdentificador;
	o.sNomMunicipi = sNomMunicipi;
	
	// mètodes públics
	o.getNom = estacio_getNom;
	o.getIdentificador = estacio_getIdentificador;
	o.getNomMunicipi = estacio_getNomMunicipi;
	return o;
}
function estacio_getNom () {
	return this.sNomEstacio;
}
function estacio_getIdentificador() {
	return this.iIdentificador;
}
function estacio_getNomMunicipi() {
	return this.sNomMunicipi;
}

////////////////////////////////////////////////////////////////////////////////
// Modelització d'una comarca
////////////////////////////////////////////////////////////////////////////////
function comarca (sNomComarca, iIdentificador) {
	var o = new Object();
	// propietats privades
	o.sNomComarca = sNomComarca;
	o.iIdentificador = iIdentificador;
	o.iNumEstacions = 0;
	o.aEstacions = new Array();
	
	//mètodes públics
	o.afegirEstacio = comarca_afegirEstacio;
	o.getEstacions = comarca_getEstacions;
	o.getNom = comarca_getNom;
	o.getIdentificador = comarca_getIdentificador;
	o.getLlistaIdentificadorsEstacions = comarca_getLlistaIdentificadorsEstacions;
	
	// Inicialitzar objecte
	for (var i = 2; i < comarca.arguments.length; i++) {
		o.afegirEstacio(comarca.arguments[i]);
	}
	return o;
}

function comarca_afegirEstacio(eEstacio) {
	this.aEstacions[this.iNumEstacions] = eEstacio;
	this.iNumEstacions++;
	return true;
}

function comarca_getEstacions() {
	return this.aEstacions;
}

function comarca_getNom() {
	return this.sNomComarca;
}
function comarca_getIdentificador() {
	return this.iIdentificador;
}
function comarca_getLlistaIdentificadorsEstacions() {
	var llista = "";
	var estacions = this.aEstacions;
	for (var j=0; j<estacions.length-1; j++) {
		llista = llista +estacions[j].getIdentificador() + ":";
	}
	llista = llista + estacions[estacions.length-1].getIdentificador()
	return llista
}

////////////////////////////////////////////////////////////////////////////////
// Representa el conjunt de totes les comarques
////////////////////////////////////////////////////////////////////////////////
function comarques() {
	var o = new Object();
	// propietats privades
	o.aComarques = new Array();
	o.iNumComarques = 0;
	
	//mètodes públics
	o.afegirComarca = comarques_afegirComarca;
	o.getComarques = comarques_getComarques;
	o.getComarca = comarques_getComarca;
	// Inicialitzar objecte
	for (var i = 0; i < comarques.arguments.length; i++) {
		o.afegirComarca(comarques.arguments[i]);
	}
	return o;
}
function comarques_afegirComarca (cComarca) {
	return this.aComarques[parseInt(cComarca.getIdentificador(),10)] = cComarca;
}
function comarques_getComarques () {
	return this.aComarques;
}
function comarques_getComarca (iIdentificador) {
	return this.aComarques[iIdentificador];
}

////////////////////////////////////////////////////////////////////////////////
// Donat el codi comarca pinta el nom de comarca i el nom i 
//poblacio de les estacions en dues sortides diferents
////////////////////////////////////////////////////////////////////////////////
function mostraEstacions(iCodiComarca, sAppenderComarca, sAppenderEstacions, sNomDiv) {
	// Fem visible el div
	document.getElementById(sNomDiv).style.visibility ='visible';
	// Obtenim l'objecte on escriure la comarca
	var DOMViewerObj = document.getElementById(sAppenderComarca);
	var sLlista = "";
	var mLlista = "";
	com = (catalunya.getComarques())[iCodiComarca];
	DOMViewerObj.innerHTML=com.getNom();
	// Obtenim l'objecte on escriure les estacions
	DOMViewerObj = document.getElementById(sAppenderEstacions);
	DOMViewerObjMunicipi = document.getElementById("MunicipiDiv");
	if (com != null) {
		var estacions = com.getEstacions();
		for (var j=0; j<estacions.length; j++) {
			sLlista = sLlista + estacions[j].getNom() + " ("+ estacions[j].getIdentificador() + ")" + "<br>";
			mLlista = mLlista + estacions[j].getNomMunicipi() + "<br>";
		}
		DOMViewerObj.innerHTML=sLlista;
		DOMViewerObjMunicipi.innerHTML=mLlista;
		return true;
	}
	return false;
}

function ocultaEstacions(sNomDiv) {
	var DOMViewerObj=document.getElementById(sNomDiv);
	DOMViewerObj.style.visibility = 'hidden';
	return true;
}

function envia(sCodiComarca, sNom,noCarga) {
	var DOMComboObj = document.getElementById(sNom);
	DOMComboObj.value = sCodiComarca;
	var DOMFormObj=DOMComboObj.form;
	if (noCarga == null){
		if (enviado() == true) {
			return DOMFormObj.submit();
		}
	}else{	//que se salte el cargant pagina
		return DOMFormObj.submit();
	}
	return DOMFormObj.submit();
}


//Hay que tener el div waitDiv
function enviado() {
	if (cuenta == 0)
	{
		ap_showWaitMessage('waitDiv', 3);
		document.body.style.cursor="wait";
//		document.body.style.cursor="progress";	
		cuenta++;
		return true;
	}
	else
	{
		alert("Petició realitzada. Siusplau esperi uns instants");
		return false;
	}
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Tractament de les imatges
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function mostra(imatge) {
	var str = imatge.src;
	var newSrc = str.substr(0, str.search("Off."))
	newSrc = newSrc.concat("On.");
	newSrc = newSrc.concat(str.substr(str.search("Off.")+4, str.length));
	imatge.src=newSrc;
	return true;
}

function oculta(imatge) {
	var str = imatge.src;
	var newSrc = str.substr(0, str.search("On."))
	newSrc = newSrc.concat("Off.");
	newSrc = newSrc.concat(str.substr(str.search("On.")+3, str.length));
	imatge.src=newSrc;
	return true;
}

////////////////////////////////////////////////////////////////////////////////
// Fechas
//////////////////////////////////////////////////////////////////////////////
function yearNow(){
	var now = new Date();
	var YearNow = now.getFullYear();
	return YearNow;
}
function yearOneBefore(){
	var now = new Date();
	var YearNow = now.getFullYear();
	return YearNow - 1;
}
function enviaFechas(sCodiComarca, sNom,nomEstacio,any) {

	var DOMViewObj = document.getElementById("comarca");
	var DOMComboObj = document.getElementById(sNom);
	DOMViewObj.value = nomEstacio;
	DOMComboObj.value=sCodiComarca;
//	alert(any);
	var DOMAnclaObj = document.getElementById("ancla");
	DOMAnclaObj.value = any;
	
	var DOMFormObj=DOMComboObj.form;
	return DOMFormObj.submit();
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Per al tractament del click
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
var cuenta = 0;

function ap_showWaitMessage(div,flag) {
	var x = document.getElementById(div);
	if (flag == 0) {
		x.style.visibility ='hidden';
	}
	if (flag == 3) {

		x.style.visibility ='visible';
		x.style.left=280/2; 
	}

	return true;
} 


//Substituye todos los caractes de una string por otro caracter
function subtituirCaracteres(palabra,find,replace){
	var str = palabra;
	while (str.indexOf(find) > -1) {
		str = str.replace(find, replace);
	}	
	return str;
}

//Acceso Mapa
function accederMapa(){
	window.open('DadesDiaPerMapa.do','Mapa','width=1040,height=730,top=0,left=0,resizable=YES,location=no,menubar=no,direcotries=no,scrollbars=no');
}

