//////////////////////////////////////////////////////////////////////////////////
// Ramon Navarro
// Codi javascript amb tecnologia XMLHttpRequest (no estandar w3c)
//////////////////////////////////////////////////////////////////////////////////

var xmlhttp;
var idactivo;

function loadChange(idobj, url){
	try{
		//alert("a: "+idobj);
		idactivo = idobj;
		if(window.XMLHttpRequest){
			// code for Mozilla, etc.
			xmlhttp = new XMLHttpRequest();
			xmlhttp.onreadystatechange = xmlhttpAccion;
			xmlhttp.open("POST", url, true);
			xmlhttp.send(null);
		}
		else{
			if(window.ActiveXObject){
				// code for IE
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				if(xmlhttp){
					xmlhttp.onreadystatechange = xmlhttpAccion;
					xmlhttp.open("POST", url, true);
					xmlhttp.send();
				}
			}
		}
	}
	catch(e){
		alert("ERROR loadChange:"+e)
	}
}

function loadChangeIH(idobj, url){
	try{
		//alert("a: "+idobj);
		idactivo = idobj;
		if(window.XMLHttpRequest){
			// code for Mozilla, etc.
			xmlhttp = new XMLHttpRequest();
			xmlhttp.onreadystatechange = xmlhttpAccionIH;
			xmlhttp.open("POST", url, true);
			xmlhttp.send(null);
		}
		else{
			if(window.ActiveXObject){
				// code for IE
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				if(xmlhttp){
					xmlhttp.onreadystatechange = xmlhttpAccionIH;
					xmlhttp.open("POST", url, true);
					xmlhttp.send();
				}
			}
		}
	}
	catch(e){
		alert("ERROR loadChangeIH:"+e)
	}
}

function xmlhttpAccion(){
	try{
		// alert("idactivo:"+idactivo);
		// alert("xmlhttp:"+xmlhttp);
		// if xmlhttp shows "loaded"
		if(xmlhttp.readyState == 4){
			// alert("4");
			// if "OK"
			if(xmlhttp.status == 200){
				// alert("200");
				//alert(document.getElementById(idactivo).background);
				//document.getElementById(idactivo).background = xmlhttp.responseText;
				//alert(document.getElementById(idactivo).style.backgroundImage);
				//alert(xmlhttp.responseText);
				// document.getElementById(idactivo).style.backgroundImage = xmlhttp.responseText;
				// alert("idactivo:"+idactivo+", document.getElementById(idactivo):"+document.getElementById(idactivo));
				// alert(document.getElementById(idactivo).style.backgroundImage);
				// alert(xmlhttp.responseText);
				document.getElementById(idactivo).style.backgroundImage = "url('"+xmlhttp.responseText+"')";
				//alert(document.getElementById(idactivo).style.backgroundImage);
			}
			else{
				document.getElementById(idactivo).appendChild(document.createComment("Error en la recepción de los datos."));
				// document.getElementById(idactivo).innerHTML = "Error en la recepción de los datos";
				// <!-- traducir -->
			}
		}
	}
	catch(e){
		alert("ERROR xmlhttpAccion:"+e)
	}
}

function xmlhttpAccionIH(){
	try{
		// if xmlhttp shows "loaded"
		if (xmlhttp.readyState == 4){
			// if "OK"
			if(xmlhttp.status == 200){
				//alert(document.getElementById(idactivo).background);
				//document.getElementById(idactivo).background = xmlhttp.responseText;
				//alert(document.getElementById(idactivo).style.backgroundImage);
				//alert(xmlhttp.responseText);
				document.getElementById(idactivo).innerHTML = xmlhttp.responseText;
				//alert(document.getElementById(idactivo).style.backgroundImage);
			}
			else{
				document.getElementById(idactivo).innerHTML = "Error en la recepción de los datos"; // <!-- traducir -->
			}
		}
	}
	catch(e){
		alert("ERROR xmlhttpAccionIH:"+e)
	}
}

function seleccionarPorValor(combo, valor){
	try{
		for(var i = 0; combo.options.length; i++){
			if(combo.options[i].value == valor){
				combo.options[i].selected = true;
				return(true);
			}
		}
		return(false);
	}
	catch(e){
		alert("ERROR seleccionarPorValor: "+e);
	}
}

function accModColor2(idobj, idmodelo, idcolor){
	try{
		var combo = document.getElementById("colorModelo");
		seleccionarPorValor(combo, idcolor);
		accModColor(idobj, idmodelo, idcolor);
	}
	catch(e){
		alert("ERROR accModColor2: "+e);
	}
}

function accModColor(idobj, idmodelo, idcolor){
	try{
		var url = "miniconsultas.php?consulta=datosmodelos&idcolor="+idcolor+"&idmodelo="+idmodelo+"&tipoaccion=imgmodcolor";
		loadChange(idobj, url);
	}
	catch(e){
		alert("ERROR accModColor: "+e);
	}
} // fin función


function accPresupuestar(idobj, tipopre, objform){
	try{
		//alert("b: "+idobj);
		//var url = "miniconsultas/datosmodelos.php?idcolor="+idcolor+"&tipoaccion=imgmodcolor";
		var url = "miniconsultas.php?consulta=presupuesto";
		url += "&msuperficie="+objform.msuperficie.value;
		url += "&ctraxdia="+objform.ctraxdia.value;
		url += "&cgenxdia="+objform.cgenxdia.value;
		url += "&cmatxm="+objform.cmatxm.value;
		url += "&pbeneficio="+objform.pbeneficio.value;
		url += "&cdiexdia="+objform.cdiexdia.value;
		switch(tipopre){
			case 'A':
				url += "&numtraa="+objform.numtraa.value;
				url += "&dejecucion="+objform.dejecucion.value;
				url += "&tipoaccion=calculara";
			break;
			case 'B':
				url += "&numtrab="+objform.numtrab.value;
				url += "&mxdia="+objform.mxdia.value;
				url += "&tipoaccion=calcularb";
			break;
		}
		loadChangeIH(idobj, url);
	}
	catch(e){
		alert("ERROR accPresupuestar:"+e)
	}
} // fin función

