
function cargaComboProvincias(){
	xmlProv=new ActiveXObject("Microsoft.XMLDOM")
	xmlProv.async=false
	xmlProv.load("provincias.xml")
	root=xmlProv.documentElement
	str="<select name='cmbProvincias' class='txt' onChange='cambioCaracteristicas()'>"
	str+="<option value=''> - Seleccionar - </option>"
	for (i=0;i<root.childNodes.length;i++){
		str+="<option value="+root.childNodes.item(i).childNodes.item(1).text+">"
		str+=root.childNodes.item(i).childNodes.item(0).text
		str+="</option>"
	}
	str+="</select>"
	tdProvincias.innerHTML=str
}
var bCambio=false
var bEdicion=false
function cambioCaracteristicas(){
	if (tdDatosGrid.innerHTML!=""){
		//alert("cambioCaracteristicas()")
		oRows=tdDatosGrid.getElementsByTagName("tr")
		//alert("Numero de filas "+oRows.length)
		for(i=0;i<oRows.length;i++){
			//alert(i)
			oTds=oRows[i].getElementsByTagName("td")
			var fachadas=""
			var sistema=""
			
			if (oTds[1].innerText=="Una"){
				fachadas="1"
			}else{
				fachadas="2"
			}
			
			if (oTds[2].innerText=="Primera planta"){
				planta=1
			}else if (oTds[2].innerText=="Entre plantas"){
				planta=0
			}else{
				planta=2
			}
			
			if (oTds[3].innerText=="Acumulador dinámico"){
				sistema="Acumulador dinámico"
			}else if (oTds[3].innerText=="Acumulador estático"){
				sistema="Acumulador estático"
			}else{
				sistema="Convector directo"
			}

			xmlNec= new ActiveXObject("Microsoft.XMLDOM")
			xmlNec.async=false
			xmlNec.load("tblNecesidadesCalorificas.xml")

			root=xmlNec.documentElement
			i2=0
			while (document.forms[0].hdnMultiplicador.value==""){
				if(root.childNodes.item(i2).getAttribute("aislada")==document.forms[0].cmbAislamiento.value && root.childNodes.item(i2).getAttribute("tipoCasa")==document.forms[0].cmbTipoVivienda.value && root.childNodes.item(i2).getAttribute("nFachadas")==fachadas && root.childNodes.item(i2).getAttribute("zonaClimatica")==document.forms[0].cmbProvincias.value && root.childNodes.item(i2).getAttribute("sistema")==sistema){
					//alert("root.childNodes.item(i).childNodes.item(planta).text-->"+root.childNodes.item(i).childNodes.item(planta).text)
					document.forms[0].hdnMultiplicador.value=root.childNodes.item(i2).childNodes.item(planta).text
					//alert("document.forms[0].hdnMultiplicador.value="+document.forms[0].hdnMultiplicador.value+"\nroot.childNodes.item(i2).childNodes.item(planta).text="+root.childNodes.item(i2).childNodes.item(planta).text)
				}
				i2++
			}
			//alert("Salimos del while")
			//document.forms[0].hdnPotencia.value=parseFloat(document.forms[0].hdnMultiplicador.value)*parseFLoat(document.forms[0].txtSuperficie.value)
			//alert("Superficie "+oTds[4].innerText+"\nMultiplicador "+document.forms[0].hdnMultiplicador.value)
			//alert("oTds[4].innerText="+oTds[4].innerText)
			oTds[5].innerText=document.forms[0].hdnMultiplicador.value*oTds[4].innerText
			oTds[6].innerText=modelo(oTds[3].innerText, oTds[5].innerText)
			//alert("llega")
			oTds[7].innerText=document.forms[0].hdnNAparatos.value
			document.forms[0].hdnMultiplicador.value=""
		}
	}else{
		//alert("Grid vacio")	
	}
}
function calcular(){
	//alert("calcular")
	if (validar()){
		var fachadas=""
		//var planta=""
		var sistema=""
		
		if (document.forms[0].cmbFachadas.value=="Una"){
			fachadas="1"
		}else{
			fachadas="2"
		}
		
		if (document.forms[0].cmbPlanta.value=="Primera planta"){
			//planta="PrimeraPlanta"
			planta=1
		}else if (document.forms[0].cmbPlanta.value=="Entre plantas"){
			//planta="entrePlantas"
			planta=0
		}else{
			//planta="ultimaPlanta"
			planta=2
		}
		
		if (document.forms[0].cmbSistema.value=="Acumulador dinámico"){
			sistema="Acumulador dinámico"
		}else if (document.forms[0].cmbSistema.value=="Acumulador estático"){
			sistema="Acumulador estático"
		}else{
			sistema="Convector directo"
		}

		xmlNec= new ActiveXObject("Microsoft.XMLDOM")
		xmlNec.async=false
		xmlNec.load("tblNecesidadesCalorificas.xml")

		root=xmlNec.documentElement
		i=0
		while (document.forms[0].hdnMultiplicador.value==""){
			if(root.childNodes.item(i).getAttribute("aislada")==document.forms[0].cmbAislamiento.value && root.childNodes.item(i).getAttribute("tipoCasa")==document.forms[0].cmbTipoVivienda.value && root.childNodes.item(i).getAttribute("nFachadas")==fachadas && root.childNodes.item(i).getAttribute("zonaClimatica")==document.forms[0].cmbProvincias.value && root.childNodes.item(i).getAttribute("sistema")==sistema){
				//alert(root.childNodes.item(i).childNodes.item(planta).text)
				document.forms[0].hdnMultiplicador.value=root.childNodes.item(i).childNodes.item(planta).text
			}
			i++
		}
		//document.forms[0].hdnPotencia.value=parseFloat(document.forms[0].hdnMultiplicador.value)*parseFLoat(document.forms[0].txtSuperficie.value)
		document.forms[0].hdnPotencia.value=document.forms[0].hdnMultiplicador.value*document.forms[0].txtSuperficie.value
		if (!bCambio){
			insertaFila()
		}else{
		}
		if (bEdicion){
			edicionDeFila()
			bEdicion=false
			bCambio=false
		}
		blanquea()
	}
}
function modelo(sSistema, sPotencia){
	//alert("modelo()")

	document.forms[0].hdnNAparatos.value=""

	xmlModelos= new ActiveXObject("Microsoft.XMLDOM")
	xmlModelos.async=false
	xmlModelos.load("aparatos.xml")

	root=xmlModelos.documentElement
	//alert(root.childNodes.length)
	bSeleccion=true
	sSeleccion=""
	var i=0
	var nAparatos=1
	if (sSistema==null){
		sSistema=document.forms[0].cmbSistema.value
	}
	if (sPotencia==null){
		sPotencia=document.forms[0].hdnPotencia.value
	}
	//alert(str+"\nPotencia="+document.forms[0].hdnPotencia.value)
	while (bSeleccion){
		//if(root.childNodes.item(i).childNodes.item(1).text==document.forms[0].cmbSistema.value){
		if(root.childNodes.item(i).childNodes.item(1).text==sSistema){
			potencia=parseInt(root.childNodes.item(i).childNodes.item(2).text)*nAparatos
			document.forms[0].hdnNAparatos.value=nAparatos
			if (sPotencia!=""){
				if (sPotencia>potencia){
//					alert("pasa por el if \nPotencia a cubrir "+document.forms[0].hdnPotencia.value+"\nPotencia "+potencia+"\nAparatos "+nAparatos)
//					sSeleccion=root.childNodes.item(i).childNodes.item(0).text
				}else{
					sSeleccion=root.childNodes.item(i).childNodes.item(0).text
					bSeleccion=false
				}
			}
		}
		if ((root.childNodes.length-1)==i){
			i=0
			nAparatos++
			//alert(nAparatos)
		}
		i++
		//alert("salimos del whlile")
	}
	//alert("salimos de modelo()")
	return sSeleccion
}
sCabeceraTabla="<table width='100%' border=1 cellpadding=0 cellspacing=0>"
sCuerpoTabla=""
sFinTabla="</table>"
function dibujaTabla(str){
	tdDatosGrid.innerHTML=sCabeceraTabla+sCuerpoTabla+sFinTabla
}
function insertaFila(){

	str="<tr onClick='fija(this); editar(this);' onMouseOver='cambio(this)' onMouseOut='reCambio(this)' class='tdLblI'>"
	str+="<td width='25%' class='lbl'>"+document.forms[0].txtDescripcionGrid.value+"</td>"
	str+="<td width='8%' class='lbl'>"+document.forms[0].cmbFachadas.value+"</td>"
	str+="<td width='8%' class='lbl'>"+document.forms[0].cmbPlanta.value+"</td>"
	str+="<td class='lbl'>"+document.forms[0].cmbSistema.value+"</td>"
	str+="<td width='9%' class='lbl'>"+document.forms[0].txtSuperficie.value+"</td>"
	str+="<td width='8%' class='lbl'>"+document.forms[0].hdnPotencia.value+"</td>"
	str+="<td width='8%' class='lbl'>"+modelo()+"</td>"
	str+="<td width='5%' class='lbl'>"+document.forms[0].hdnNAparatos.value+"</td>"
	str+="</tr>"

	sCuerpoTabla+=str
	dibujaTabla(sCuerpoTabla)

}
function editar(oRow){
	bCambio=true
	bEdicion=true
	oTds=oRow.getElementsByTagName("td")
	//alert("Numero de tds "+oTds.length)
	document.forms[0].txtDescripcionGrid.value=oTds[0].innerText
	document.forms[0].cmbFachadas.value=oTds[1].innerText
	document.forms[0].cmbPlanta.value=oTds[2].innerText
	document.forms[0].cmbSistema.value=oTds[3].innerText
	document.forms[0].txtSuperficie.value=oTds[4].innerText
	
}
function edicionDeFila(){
	bSalir=true
	i=0
	oRows=tdDatosGrid.getElementsByTagName("tr")
	while(bSalir){
		if (oRows[i].className=="tdLblIFija"){
			oTds=oRows[i].getElementsByTagName("td")
			oTds[0].innerText=document.forms[0].txtDescripcionGrid.value
			oTds[1].innerText=document.forms[0].cmbFachadas.value
			oTds[2].innerText=document.forms[0].cmbPlanta.value
			oTds[3].innerText=document.forms[0].cmbSistema.value
			oTds[4].innerText=document.forms[0].txtSuperficie.value
			oTds[5].innerText=document.forms[0].hdnPotencia.value
			oTds[6].innerText=modelo()
			oTds[7].innerText=document.forms[0].hdnNAparatos.value
			bSalir=false
		}
		i++
	}
}
function blanquea(){
	document.forms[0].txtDescripcionGrid.value=""
	document.forms[0].cmbFachadas.value=""
	document.forms[0].cmbPlanta.value=""
	document.forms[0].cmbSistema.value=""
	document.forms[0].txtSuperficie.value=""
	document.forms[0].hdnPotencia.value=""
	document.forms[0].hdnMultiplicador.value=""
}
function validar(){
	str=""
	if (document.forms[0].cmbProvincias.value==""){
		str+="Debe seleccionar una provincia.\n"
	}
	if (document.forms[0].cmbTipoVivienda.value==""){
		str+="Debe selleccionar un tipo de vivienda\n"
	}
	if (document.forms[0].cmbAislamiento.value==""){
		str+="Debe seleccionar un aislamiento\n"
	}
	if (document.forms[0].cmbFachadas.value==""){
		str+="Debe seleccionar un número de fachadas\n"
	}
	if (document.forms[0].cmbPlanta.value==""){
		str+="Debe seleccionar un tipo de planta\n"
	}
	if (document.forms[0].cmbSistema.value==""){
		str+="Debe seleccionar un tipo de sistema\n"
	}
	if (document.forms[0].txtSuperficie.value==""){
		str+="El campo superficie debe estar relleno\n"
	}else{
		if (isNaN(parseFloat(document.forms[0].txtSuperficie.value))){
			str+="El campo superficie debe ser numérico\n"
		}else{
			if (document.forms[0].txtSuperficie.value.indexOf(",",0)!=-1){
				str+="El campo superficie debe de tener los decimales separados por punto\n"
			}
		}
	}
	if (str==""){
		return true
	}else{
		alert(str)
		return false
	}
}
function imprimir(){
	var str=""
	str=mtoDatos.innerHTML
	mtoDatos.innerHTML=""
	window.print()
	mtoDatos.innerHTML=str
}
// Funciones de cambio de color de los "tr"

function cambio(obj){
	if(obj.className!="tdLblIFija"){
		obj.className="tdLblIPasa"
	}else{
		obj.className="tdLblIPasaSobreFijo"
	}
}
function reCambio(obj){
	if(obj.className!="tdLblIFija"){
		if(obj.className=="tdLblIPasaSobreFijo"){
			obj.className="tdLblIFija"
		}else{
			obj.className="tdLblI"
		}
	}
}
function fija(obj){
	var sTr=document.getElementsByTagName("tr")
	for (x=0;x<sTr.length ;x++ ){
		if(sTr[x].className=="tdLblIFija"){
			sTr[x].className="tdLblI"
		}
	}
	obj.className="tdLblIFija"
}
