/*******************************************
 *       Calculadores  2        *
*******************************************/
function validarFormulari2() {
	var form = document.forms['calcForm'];
	var sistRefI = form.sistRefOrig.options[form.sistRefOrig.selectedIndex].value;
	var sistRefO = form.sistRefDesti.options[form.sistRefDesti.selectedIndex].value;
	var sistCoordIn = form.sistCoordOrig.options[form.sistCoordOrig.selectedIndex].value;
	var sistCoordOut = form.sistCoordDesti.options[form.sistCoordDesti.selectedIndex].value;
	var hIn = form.alturaOrig.options[form.alturaOrig.selectedIndex].value;
	var hOut = form.alturaDesti.options[form.alturaDesti.selectedIndex].value;
	var geoidIn = form.geoideOrig.options[form.geoideOrig.selectedIndex].value;	
	var geoidOut = form.geoideDesti.options[form.geoideDesti.selectedIndex].value;
	
	if (trim(form.source.value) == "") {
		alert(TXT_COORDIN);
		return false;
	}
	
	//si coordin=geo i coordout=geo  i sistRef== --> no enviar. nomes javascript i no hi ha altures o mateixa altura (no cal transformar)
	if (sistRefI == sistRefO && ((sistCoordIn =="3" && sistCoordOut =="1") ||  (sistCoordIn =="1" && sistCoordOut =="3")) && hIn ==hOut){
		if (geoidIn !="-1"){
			alert(TXT_ERROR_8871); //no es pot seleccionar geoide entrada
			var obj = document.getElementById('geoideOrig');
			obj.selectedIndex=0;
			return false;
		}
		if (geoidOut!="-1") {
			alert(TXT_ERROR_8860); //no es pot seleccionar geoide sortida
			var obj = document.getElementById('geoideDesti');
			obj.selectedIndex=0;
			return false;
		}
	}
	/*
	if (sistCoordIn == "2" && sistCoordOut =="2") {
		//geocentriques orig i desti
		//mirar si hi ha seleccionat els 2 geoides
		if (geoidIn == "-1") {
			alert(TXT_WARN_GEOIDE_ORIG);
			return false;
		}
		if (geoidOut == "-1") {
			alert(TXT_WARN_GEOIDE_DESTI);
			return false;
		}
		
	} else {
		//si geocentriques orig --> altura elip o ortometrica
		if (sistCoordIn == "2" && hOut == "-1") {
			alert(TXT_WARN_ALTURA_DESTI);
			return false;
		}
		//si geocentriques desti --> altura elip o ortometrica orig
		if (sistCoordOut == "2" && hIn == "-1") {
			alert(TXT_WARN_ALTURA_ORIG);
			return false;
		}
	}
	
	//check si altura orig != sense altures si sistcoordin = 2 geocentrica
	if (sistCoordIn == "2" && hIn != "-1") {
		alert(TXT_WARN_ALTURA_NO);
		return false;
	}
	//check si altura desti != sense altures si sistcoordout = 2 geocentrica
	if (sistCoordOut == "2" && hOut != "-1") {
		alert(TXT_WARN_ALTURA_NO);
		return false;
	}
	/*
	//si altura ortometrica--> geoide
	if (hIn== "1" && geoidIn == "-1") {
		alert(TXT_WARN_GEOIDE_ORIG);
		return false;
	}
	if (hOut== "1"&& geoidOut == "-1") {
		alert(TXT_WARN_GEOIDE_DESTI);
		return false;
	}
	
	//Geoides
	if (sistRefI == sistRefO) {
		//si sist ref == 
		if (geoidOut == "-1" && (hIn == "0" || sistCoordIn == "2") && (hOut == "1")) {
			//si altura eh o geocentriques ORIGEN i altura ortometrica desti --> alert(cal geoide desti)
			alert(TXT_WARN_GEOIDE_DESTI);
			return false;
		} else if (geoidOut == "-1" && hIn=="1" && (hOut=="0" || sistCoordOut=="2")) {
			//si altura ortometrica geo/utm ORIGEN i altura elip o geocentriques DESTI --> alert(cal geoide desti)
			alert(TXT_WARN_GEOIDE_DESTI);
			return false;
		} else if (sistCoordIn != "2" && hIn=="0" && sistCoordOut == "2" && geoidOut !="-1") {
			//si origen es utm/geo altura eh i desti geocentrica amb geoid --> no cal geoide desti
			alert(TXT_WARN_GEOID_DESTI_NO);
			return false;
		} else if (sistCoordOut != "2" && hOut=="0" && sistCoordIn == "2" && geoidIn !="-1") {
			//si origen es geocentrica amb geoide i desti utm/geo altura eh  --> no cal geoide origen
			alert(TXT_WARN_GEOID_ORIG_NO);
			return false;
		}
		
	} else {
		//si sist ref !=
		//si altura eh o geocentriques ORIGEN i altura ortometrica desti --> alert(cal geoide ORIGEN)
		//si altura eh o geocentriques ORIGEN i altura elip o geocentriques DESTI --> alert(cal geoide ORIGEN i DESTI)
		//SI altura ortometrica ORIGEN i altura elip o geocentriques DESTI -->alert(cal geoide desti)
		if (geoidIn == "-1" && (hIn == "0" || sistCoordIn == "2") && (hOut == "1")) {
			alert(TXT_WARN_GEOIDE_ORIG);
			return false;
		} else if (geoidIn == "-1" && (hIn == "0" || sistCoordIn == "2") && (hOut == "0" || sistCoordOut =="2")) {
			alert(TXT_WARN_GEOIDE_ORIG);
			return false;
		} else if (geoidOut == "-1" && (hIn == "0" || sistCoordIn == "2") && (hOut == "0" || sistCoordOut =="2")) {
			alert(TXT_WARN_GEOIDE_DESTI);
			return false;
		} else if (geoidOut == "-1" && (hIn=="1") && (hOut=="0" || sistCoordOut=="2")) {
			alert(TXT_WARN_GEOIDE_DESTI);
			return false;
		}
	}
		*/
	return true;
}

function processarDades2(){
	var form = document.forms['calcForm'];
	var sistRefI = form.sistRefOrig.options[form.sistRefOrig.selectedIndex].value;
	var sistRefO = form.sistRefDesti.options[form.sistRefDesti.selectedIndex].value;
	var sistCoordIn = form.sistCoordOrig.options[form.sistCoordOrig.selectedIndex].value;
	var sistCoordOut = form.sistCoordDesti.options[form.sistCoordDesti.selectedIndex].value;
	var hIn = form.alturaOrig.options[form.alturaOrig.selectedIndex].value;
	var hOut = form.alturaDesti.options[form.alturaDesti.selectedIndex].value;
	
		
	//validar formulari: source no estigui vuit, i totes les opcions marcades.
	if (validarFormulari2()) {
	
		var entrada = form.source.value;
		var lineas = entrada.split('\n');
		var x_lon, y_lat, z_h; //x/Lon, y/Lat, z/h per coordenades (x,y) (lon, lat) (x,y,z) i (lon, lat, h)
		var tmp_result; //vble transf de coord GG MM SS a decimals
		var coordenades2;
		
		for (i=0;i<lineas.length;i++) {
			coordenades = lineas[i].split(',');
			if (coordenades == "")
				break;
			if(i == 0){
				if (sistCoordIn == "3") {
					//convertir coordenades en graus minuts i segons a coord decimals
					coordenades2 = convertDmsToDeg(coordenades);
					if (coordenades2) {
						x_lon = coordenades2[0];
						y_lat = coordenades2[1];
						//check lon (-180, 180) && lat (-90,90)
						if (x_lon < -180 || x_lon > 180) {
							alert(TXT_ERROR_5002);
							return false;
						} else if (y_lat < -90 || y_lat > 90) {
							alert(TXT_ERROR_5003);
							return false;
						}
						tmp_result = coordenades2[0] +","+coordenades2[1];
						//copiar altura
						if (hIn != "-1") {
							if (coordenades.length > 2)
								tmp_result += ","+coordenades[2];
						}
					} else 
						return false;
				} else if (sistRefI == sistRefO && sistCoordIn == "1" && sistCoordOut =="3") {
					//convertir coordenades decimals a coord en graus minuts i segons
					x_lon = coordenades[0];
					y_lat = coordenades[1];
					//check lon (-180, 180) && lat (-90,90)
					if (x_lon < -180 || x_lon > 180) {
						alert(TXT_ERROR_5002);
						return false;
					} else if (y_lat < -90 || y_lat > 90) {
						alert(TXT_ERROR_5003);
						return false;
					}
					tmp_result = convertDegToDMS(coordenades[0]) +","+convertDegToDMS(coordenades[1]);
					//copiar altura
					if (hIn != "-1") {
						if (coordenades.length > 2)
							tmp_result += ","+coordenades[2];
					}
				} else {
					x_lon = coordenades[0];
					y_lat = coordenades[1];
					
				}
				if (hIn != "-1") {
					if (coordenades.length > 2)
						z_h = coordenades[2];
					else {
						alert(TXT_ALTURA_DATA_NO);
						return false;
					} 
				} else {
					if (coordenades.length > 2)
						z_h = coordenades[2];
					else
						z_h = 0; //sense altures --> h=0
				}
				
			}else if(i > 0){
				if (sistCoordIn == "3") {
					//convertir coordenades en graus minuts i segons a coord decimals
					coordenades2 = convertDmsToDeg(coordenades);
					if (coordenades2) {
						x_lon += ";"+coordenades2[0];
						y_lat += ";"+coordenades2[1];
						if (coordenades[0] < -180 || coordenades[0] > 180) {
							alert(TXT_ERROR_5002);
							return false;
						} else if (coordenades[1] < -90 || coordenades[1] > 90) {
							alert(TXT_ERROR_5003);
							return false;
						}
						tmp_result += '\n'+coordenades2[0] +","+coordenades2[1];
						//copiar altura
						if (hIn != "-1") {
							if (coordenades.length > 2)
								tmp_result += ","+coordenades[2];
						}
					} else
						return false;
				} else if (sistRefI == sistRefO && sistCoordIn == "1" && sistCoordOut =="3") {
					//convertir coordenades decimals a coord en graus minuts i segons
					
					x_lon += ";"+coordenades[0];
					y_lat += ";"+coordenades[1];
					if (coordenades[0] < -180 || coordenades[0] > 180) {
						alert(TXT_ERROR_5002);
						return false;
					} else if (coordenades[1] < -90 || coordenades[1] > 90) {
						alert(TXT_ERROR_5003);
						return false;
					}
					tmp_result += '\n'+convertDegToDMS(coordenades[0]) +","+convertDegToDMS(coordenades[1]);
					//copiar altura
					if (hIn != "-1") {
						if (coordenades.length > 2)
							tmp_result += ","+coordenades[2];
					}
				} else {
					x_lon += ";"+coordenades[0];
					y_lat += ";"+coordenades[1];
				}
				if (hIn != "-1") {
					if (coordenades.length > 2)
						z_h += ";" +coordenades[2];
					else {
						alert(TXT_ALTURA_DATA_NO);
						return false;
					} 
				} else {
					if (coordenades.length > 2)
						z_h += ";" +coordenades[2];
					else
						z_h += ";0"; //sense altures --> h=0
				}
			}
		}
		//si coordin=geo i coordout=geo  i sistRef== --> no enviar. nomes javascript i no hi ha altures
		if (sistRefI == sistRefO && ((sistCoordIn =="3" && sistCoordOut =="1") ||  (sistCoordIn =="1" && sistCoordOut =="3")) && hIn ==hOut){
			//no cridar a les calculadores i mostrar resultats directament
			form.result.value = tmp_result;
		} else {
			enviar2(x_lon, y_lat, z_h);
		}
	}
}

function enviar2(x_lon, y_lat, z_h)
{
	var form = document.forms['calcForm'];
	var sistRefI = form.sistRefOrig.options[form.sistRefOrig.selectedIndex].value;
	var sistRefO = form.sistRefDesti.options[form.sistRefDesti.selectedIndex].value;
	var sistCoordIn = form.sistCoordOrig.options[form.sistCoordOrig.selectedIndex].value;
	var sistCoordOut = form.sistCoordDesti.options[form.sistCoordDesti.selectedIndex].value;
	var hIn = form.alturaOrig.options[form.alturaOrig.selectedIndex].value;
	var hOut = form.alturaDesti.options[form.alturaDesti.selectedIndex].value;
	var geoidIn = form.geoideOrig.options[form.geoideOrig.selectedIndex].value;	
	var geoidOut = form.geoideDesti.options[form.geoideDesti.selectedIndex].value;
	var urlServlet;
	
	//urlServlet = "http://localhost:8080/geocalc2/geocalc2.icc?"+"ref1="+sistRefI+"&ref2="+sistRefO+"&coord1="+sistCoordIn+"&coord2="+sistCoordOut+"&h1="+hIn+"&h2="+hOut+"&g1="+geoidIn+"&g2="+geoidOut;
	urlServlet = "http://www.icc.cat/geocalc2/geocalc2.icc?"+"ref1="+sistRefI+"&ref2="+sistRefO+"&coord1="+sistCoordIn+"&coord2="+sistCoordOut+"&h1="+hIn+"&h2="+hOut+"&g1="+geoidIn+"&g2="+geoidOut;
	Initialize();
	form.elements['result'].value = "";
	
	var parametres = "&x_lon="+ x_lon + "&y_lat="+ y_lat + "&z_h="+ z_h;
	
	document.getElementById('loading').style.visibility = "visible";

	if(req != null && typeof urlServlet != "undefined"){

		req.onreadystatechange = function() {processReqTransformation2();};
		req.open("POST", urlServlet + parametres, true);
		//Necessari per enviar formulari via post
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		//req.setRequestHeader("Content-length", parametres.length);
		req.setRequestHeader("Connection", "close");
		req.send();
	
	}
}


//handle Transformation
function processReqTransformation2() {
	var form = document.forms['calcForm'];
	var sistRefI = form.sistRefOrig.options[form.sistRefOrig.selectedIndex].value;
	var sistRefO = form.sistRefDesti.options[form.sistRefDesti.selectedIndex].value;
	var sistCoordIn = form.sistCoordOrig.options[form.sistCoordOrig.selectedIndex].value;
	var sistCoordOut = form.sistCoordDesti.options[form.sistCoordDesti.selectedIndex].value;
	var hOut = form.alturaDesti.options[form.alturaDesti.selectedIndex].value;
	var strError = "TXT_ERROR_";
	// only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {

		var result = JSON.parse(req.responseText);		
		
		//mirar si hi ha un error result.error
		if (result.error) {
			alert(TXT_ERROR);
		} else {
			var resultat;
			var isize =1; //longitud de l'array de resultats
			var codiError =codiError2= coordError ="";
			if (result.conversio.length > 0) {
				//mirar si hi ha codi d'error != 0
				if (result.conversio[0].codierr != 0) {
					codiError2 =result.conversio[0].codierr +'';
					codiError = strError + codiError2.replace(/-/g,'');
					if (codiError2 == "-8873" ||codiError2 == "-8862" ||codiError2 == "-8852" ||codiError2 == "-8843" ||codiError2 == "-8837" || codiError2 == "-8834" || codiError2 == "-8823" ||codiError2 == "-8813" || codiError2 == "-8804" || codiError2 == "-8200"|| codiError2 == "-8894" || codiError2 == "-8896") {
						//no es pot seleccionar altura de sortida 
						var obj = document.getElementById('alturaDesti');
						obj.selectedIndex=0;
					} else if (codiError2 == "-8860" ||codiError2 == "-8850" ||codiError2 == "-8821" ||codiError2 == "-8802") {
						//no es pot seleccionar geoide sortida
						var obj = document.getElementById('geoideDesti');
						obj.selectedIndex=0;
					}else if (codiError2 == "-8895" ||codiError2 == "-8897") {
						//no es pot seleccionar altura d'entrada
						var obj = document.getElementById('alturaOrig');
						obj.selectedIndex=0;
					}else if (codiError2 == "-8871" ||codiError2 == "-8861" ||codiError2 == "-8851"||codiError2 == "-8841"||codiError2 == "-8811"||codiError2 == "-8801") {
						//no es pot seleccionar geoide entrada
						var obj = document.getElementById('geoideOrig');
						obj.selectedIndex=0;
					}else if (codiError2 == "-5556" ||codiError2 == "-5555" ||codiError2 == "-5023" ||codiError2 == "-5503" ||codiError2 == "-5502") {
						//coordenada geografica fora de rang--> display coordenades amb error
						if (result.conversio[0].strErr !="") {
							if (sistCoordIn == "0") {
								coordError = " (X, Y) = ";
							} else if (sistCoordIn == "2"){
								coordError = " (X, Y, Z) = ";
							}else {
								coordError = " (lon, lat) = ";
							}
							var tmpCoord = ltrim(result.conversio[0].strErr, ' '); //treure primer espai
							tmpCoord = tmpCoord.substr(0,tmpCoord.length-1); //treure ultima coma
							var tmpCoord2 = tmpCoord.split(', ');
							for (i=0;i<tmpCoord2.length;i++) {
								coordError += "("+result.conversio[0].xLonIn[tmpCoord2[i]] + "," + result.conversio[0].yLatIn[tmpCoord2[i]];
								if (sistCoordIn == "2"){
									coordError += "," + result.conversio[0].zHIn[tmpCoord2[i]];
								}
								coordError +=") ";
							}
						}
					}else if (codiError2 == "-5001") {
						//coordenada UTM y negativa--> display coordenades amb error
						if (result.conversio[0].strErr !="") {
							if (sistCoordIn == "0") {
								coordError = " (X, Y) = ";
							} else {
								coordError = " (lon, lat) = ";
							}
							var tmpCoord = ltrim(result.conversio[0].strErr, ' ');
							tmpCoord = tmpCoord.substr(0,tmpCoord.length-1); //treure ultima coma
							var tmpCoord2 = tmpCoord.split(', ');
							for (i=0;i<tmpCoord2.length;i++) {
								coordError += "("+result.conversio[0].xLonIn[tmpCoord2[i]] + "," + result.conversio[0].yLatIn[tmpCoord2[i]]+") ";
							}
						}
					} else if (codiError2 == "-8898") {
						//no es pot seleccionar altura d'entrada ni de sortida
						var obj = document.getElementById('alturaOrig');
						obj.selectedIndex=0;
						var obj2 = document.getElementById('alturaDesti');
						obj2.selectedIndex=0;
					}
					if (coordError != "")
						alert(eval(codiError) + coordError);
					else
						alert(eval(codiError));
				} else {
					//resultats
					var boolCM = false;
					if (sistCoordOut == "0" && document.getElementById('convMeridians').checked) { 
						//conv meridians nomes s'ha de mostrar si sistCoorDesti es utm i esta seleccionat
						boolCM = true;
					}
					isize = result.conversio[0].isize;
					for (i=0;i<isize;i++) {
						var res = result.conversio[0].xLonOut[i] + "," + result.conversio[0].yLatOut[i];
						if (sistCoordOut == "2"){ //cota elip/orto o z (geocentriques)
							res += "," + result.conversio[0].zHOut[i];
						} else if (sistCoordOut == "3") {
							//convertir coord en graus minuts i segons
							res = convertDegToDMS(result.conversio[0].xLonOut[i]) + "," + convertDegToDMS(result.conversio[0].yLatOut[i]);
							if (sistCoordIn == "2" || hOut != "-1") {
								res += "," + result.conversio[0].zHOut[i];
							}
						} else if (boolCM) {
							//si =sist ref i =sist coord=utm --> resultat es coordenades d'entrada + convergencia meridians
							if (sistRefI == sistRefO && sistCoordIn == "0") {
								//mostrar coordenades xLonIn xLatOut
								res = result.conversio[0].xLonIn[i] + "," + result.conversio[0].yLatIn[i];
								
								if (hOut != "-1") {
									res += "," + result.conversio[0].zHOut[i];
								}
							} else	if (sistCoordIn == "2" || hOut != "-1") {
								res += "," + result.conversio[0].zHOut[i];
							}
							//convergencia de meridians			
							res += "," +result.conversio[0].convergenciaG[i]+" "+result.conversio[0].convergenciaM[i]+" "+result.conversio[0].convergenciaS[i];
							//escala
							res += "," +result.conversio[0].escala[i];
							
						} else {
							//geoDec, utm sense CM
							if (sistCoordIn == "2" || hOut != "-1") {
								res += "," + result.conversio[0].zHOut[i];
							}
						}
						form.elements['result'].value += res;
												
						if (i < isize-1){
							form.elements['result'].value += '\n';
						}
					}
				}
			}
		}
		        	
        } else {
        	if (req.status == 414 || req.status == 413)
        		alert(TXT_ERROR_VOLUM);
    		else 
    			alert(TXT_ERROR);
    	}
    }
    document.getElementById('loading').style.visibility = "hidden";
}

function displayTooltip2(divId, sistCoord) {
	var obj =document.getElementById(divId);
	var txt, txtEg;
	txtEg = "<br/>" + TXT_EXEMPLE + " ";
	var boolCM = false; //convergencia de meridians
	var boolCota = false; //cota el.lipsoidal/ortometrica
	if (divId == "sourceTooltip" && document.getElementById('alturaOrig').options[document.getElementById('alturaOrig').selectedIndex].value != "-1") {
		boolCota = true;
	} else if (divId == "resultTooltip") {
		if (document.getElementById('convMeridians').checked)
			boolCM = true;
		if (document.getElementById('alturaDesti').options[document.getElementById('alturaDesti').selectedIndex].value != "-1")
			boolCota = true;
	}
	//alert(divId + " elip " + boolCotaElip + " cm " + boolCM + " y sistCoord " + sistCoord);
	switch (sistCoord) {
	case "0":
		txt = "Easting(X),Northing(Y)";
		txtEg +="432648.873,4624697.432";
		if (boolCota) {
			txt +=",h";
			txtEg +=",24.89";
		}
		if (boolCM) 
			txt += ",CM,K";
		break;
	case "1":
		txt = TXT_LONLAT_TOOLTIP;
		txtEg +="2.18969857,41.77052639";
		if (boolCota) {
			txt +=",h";
			txtEg +=",24.89";
		}
		break;
	case "3":
		txt = TXT_LONLAT_TOOLTIP;
		txtEg +="2 11 22.9164,41 46 13.893";
		if (boolCota) {
			txt +=",h";
			txtEg +=",24.89";
		}
		break;
	case "2":
		txt =  "X,Y,Z";
		txtEg +="4839482.173,15265.210,4142713.260";
		break;
	case "":
		txt ="";
		txtEg ="";
		break
	}
	if (divId == "sourceTooltip") 
		obj.innerHTML = "<b>" +txt + "</b>" +txtEg;
	else 
		obj.innerHTML = "<b>" +txt+ "</b>";
	
}


//onchange sist coord origen o sist ref desti
function selectSistCoord2() {
	var form = document.forms['calcForm'];
	var sistRefI = form.sistRefOrig.options[form.sistRefOrig.selectedIndex].value;
	var sistRefO = form.sistRefDesti.options[form.sistRefDesti.selectedIndex].value;
	var sistCoordIn = form.sistCoordOrig.options[form.sistCoordOrig.selectedIndex].value;

	displayTooltip2('sourceTooltip', sistCoordIn); //display del tooltip amb sist coord
	/*
	if (sistRefI == sistRefO) {
		//mateix sistema de referencia
		document.getElementById('sistCoordDesti').length = 0; //esborrar totes les opcions sist coord desti
		if (sistCoordIn == "1") {
			form.sistCoordDesti.options[0]=new Option(TXT_UTM_SELECT,"0");
			form.sistCoordDesti.options[1]=new Option(TXT_GEO_SELECT, "3");
			form.sistCoordDesti.options[2]=new Option(TXT_GC_SELECT,"2");
		} else if (sistCoordIn == "0") {
			form.sistCoordDesti.options[0]=new Option(TXT_UTM_SELECT,"0");
			form.sistCoordDesti.options[1]=new Option(TXT_GEODEC_SELECT,"1");
			form.sistCoordDesti.options[2]=new Option(TXT_GEO_SELECT, "3");
			form.sistCoordDesti.options[3]=new Option(TXT_GC_SELECT,"2");
		} else if (sistCoordIn == "3") {
			form.sistCoordDesti.options[0]=new Option(TXT_UTM_SELECT,"0");
			form.sistCoordDesti.options[1]=new Option(TXT_GEODEC_SELECT, "1");
			form.sistCoordDesti.options[2]=new Option(TXT_GC_SELECT,"2");
		} else if (sistCoordIn == "2") {
			form.sistCoordDesti.options[0]=new Option(TXT_UTM_SELECT,"0");
			form.sistCoordDesti.options[1]=new Option(TXT_GEODEC_SELECT,"1");
			form.sistCoordDesti.options[2]=new Option(TXT_GEO_SELECT, "3");
		}
	} else {
		//sistRefI != sistRefO
		//(sistCoordIn == "geoDec" || sistCoordIn == "geo" || sistCoordIn == "utm" || sistCoordIn="gc")
		//esborrar i crear sist coord desti
		document.getElementById('sistCoordDesti').length = 0; //esborrar totes les opcions sist coord desti
		form.sistCoordDesti.options[0]=new Option(TXT_UTM_SELECT,"0");
		form.sistCoordDesti.options[1]=new Option(TXT_GEODEC_SELECT,"1");
		form.sistCoordDesti.options[2]=new Option(TXT_GEO_SELECT, "3");
		form.sistCoordDesti.options[3]=new Option(TXT_GC_SELECT,"2");	
		
	}
	//display del tooltip amb sist coord utm
	displayTooltip2('resultTooltip', '0');
	//activar conv meridians. primera opcion de coordenades desti es utm
	document.getElementById('convMeridiansDiv').style.visibility='visible';
	*/
	if (sistCoordIn == "2") { //geocentriques
		//desactivar altura orig
		obj = document.getElementById('alturaOrig');
		obj.selectedIndex=0;
	} 
	//activar geoides de sortida
	selectGeoide('geoideDesti',sistRefO);
}

//onChange sist coord desti
function selectAlturesGc2() {
	var form = document.forms['calcForm'];
	var sistCoordIn = form.sistCoordOrig.options[form.sistCoordOrig.selectedIndex].value;
	var sistCoordOut = form.sistCoordDesti.options[form.sistCoordDesti.selectedIndex].value;
		
	if (sistCoordOut == "2") { //geocentriques
		//desactivar altura desti
		obj = document.getElementById('alturaDesti');
		obj.selectedIndex=0;
		document.getElementById('convMeridiansDiv').style.visibility='hidden';
			
	} else if (sistCoordOut =="1" || sistCoordOut =="3" || sistCoordOut =="0") {
		if (sistCoordOut == "0") 
			document.getElementById('convMeridiansDiv').style.visibility='visible';
		else 
			document.getElementById('convMeridiansDiv').style.visibility='hidden';
	}
	displayTooltip2('resultTooltip', sistCoordOut); //display del tooltip amb sist coord 
}

function resetForm2() {
	var form = document.forms['calcForm'];
	form.result.value = "";
	form.source.value = "";
	obj = document.getElementById('sistRefOrig');
	obj.selectedIndex=0;
	obj = document.getElementById('sistRefDesti');
	obj.selectedIndex=0;
	obj = document.getElementById('sistCoordOrig');
	obj.selectedIndex=0;
	obj = document.getElementById('sistCoordDesti');
	obj.selectedIndex=1;
	obj = document.getElementById('alturaOrig');
	obj.selectedIndex=0;
	obj = document.getElementById('alturaDesti');
	obj.selectedIndex=0;
	obj = document.getElementById('geoideOrig');
	obj.selectedIndex=0;
	obj = document.getElementById('geoideDesti');
	obj.selectedIndex=0;
	document.getElementById('convMeridiansDiv').style.visibility='hidden';
}

function initTooltips2() {
	displayTooltip2('sourceTooltip', "0"); //display del tooltip amb sist coord
	displayTooltip2('resultTooltip', "1"); //display del tooltip amb sist coord
}
function selectGeoide(divId, sistRef) {
	var form = document.forms['calcForm'];
	var obj =document.getElementById(divId);

	obj.length=0;
	obj.options[0]=new Option(TXT_GEOIDE_NO,"-1");
	if (sistRef == "100") {
		obj.options[1]=new Option(TXT_GEOIDE_ANIV,"1");
	} else {
		obj.options[1]=new Option(TXT_GEOIDE_EGM,"0");
		obj.options[2]=new Option(TXT_GEOIDE_ANIV,"1");
	}
}
function displayCM2() {
	var form = document.forms['calcForm'];
	var sistRefI = form.sistRefOrig.options[form.sistRefOrig.selectedIndex].value;
	var sistRefO = form.sistRefDesti.options[form.sistRefDesti.selectedIndex].value;
	var sistCoordIn = form.sistCoordOrig.options[form.sistCoordOrig.selectedIndex].value;
	var sistCoordOut = form.sistCoordDesti.options[form.sistCoordDesti.selectedIndex].value;
	
	//mirar si sist ref orig =sit ref desti i utm = utm --> no es pot desactivar el checkbox
	if (!document.getElementById('convMeridians').checked && sistRefI == sistRefO && sistCoordIn == "0" && sistCoordOut == "0") {
		document.getElementById('convMeridians').checked = true;
	}
	displayTooltip2('resultTooltip', '0');
}

