// JavaScript Document
//// AJAX FUNCTIONS 
var estat_ajax = 'OK';

function nuevoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function updateTinyint(taula_activa,camp_actiu,id_actiu){
	var valor_actiu = document.getElementById(camp_actiu+"_"+id_actiu).value;
	var element = "contenedor_"+camp_actiu+"_"+id_actiu;
	contenedor = document.getElementById(element);
	ajax=nuevoAjax();
	//contenedor.innerHTML = '<img src="../iconos/icon_orange.gif" alt="Guardant" width="10" height="10">';
	ajax.open("POST", "../update_tinyint.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==0) {} //el 0 vol dir que el retorn es estat INICIALIZAR 
		if (ajax.readyState==1) {} //el 1 vol dir que el retorn es estat CARGANDO 
		if (ajax.readyState==2) {} //el 2 vol dir que el retorn es estat CARGADO 
		if (ajax.readyState==3) {} //el 3 vol dir que el retorn es estat INTERACTUANDO 

		if (ajax.readyState==4) { //el 4 vol dir que el retorn es estat COMPLETADO 
			contenedor.innerHTML = ajax.responseText;
			if(valor_actiu=='1'){
				document.getElementById(camp_actiu+"_"+id_actiu).value=0;
			} else{
				document.getElementById(camp_actiu+"_"+id_actiu).value=1;
			}
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("taula_activa="+taula_activa+"&camp_actiu="+camp_actiu+"&id_actiu="+id_actiu+"&valor_actiu="+valor_actiu)
}

function ConfirmUpdateTinyint(taula_activa,camp_actiu,id_actiu){
	agree = confirm("Segur que vol canviar d'estat aquest registre?");
	if (agree) {
		var valor_actiu = document.getElementById(camp_actiu+"_"+id_actiu).value;
		var element = "contenedor_"+camp_actiu+"_"+id_actiu;
		contenedor = document.getElementById(element);
		ajax=nuevoAjax();
		//contenedor.innerHTML = '<img src="../iconos/icon_orange.gif" alt="Guardant" width="10" height="10">';
		ajax.open("POST", "../update_tinyint.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==0) {} //el 0 vol dir que el retorn es estat INICIALIZAR 
			if (ajax.readyState==1) {} //el 1 vol dir que el retorn es estat CARGANDO 
			if (ajax.readyState==2) {} //el 2 vol dir que el retorn es estat CARGADO 
			if (ajax.readyState==3) {} //el 3 vol dir que el retorn es estat INTERACTUANDO 
	
			if (ajax.readyState==4) { //el 4 vol dir que el retorn es estat COMPLETADO 
				contenedor.innerHTML = ajax.responseText;
				if(valor_actiu=='1'){
					document.getElementById(camp_actiu+"_"+id_actiu).value=0;
				} else{
					document.getElementById(camp_actiu+"_"+id_actiu).value=1;
				}
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("taula_activa="+taula_activa+"&camp_actiu="+camp_actiu+"&id_actiu="+id_actiu+"&valor_actiu="+valor_actiu)
	}
}

function updateCamp(id_actiu,taula_activa,camp_actiu){
	var elemento = camp_actiu+"_"+id_actiu;
	if(estat_ajax == 'OK'){
		estat_ajax = 'KO';
		var reg_id = id_actiu;
		var contenedor = document.getElementById('contenedor_'+camp_actiu+'_'+id_actiu);
		var valor_ajax = document.getElementById(elemento).value;
		//alert(valor_ajax);
		ajax=nuevoAjax();
		ajax.open("POST", "../ajax/update_valor_ajax.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==0) {	contenedor.innerHTML = '<img src="../icons/icon_orange.gif" alt="-" width="5" height="20">';} //INTERACTUANDO
			if (ajax.readyState==1) {	contenedor.innerHTML = '<img src="../icons/icon_orange.gif" alt="-" width="5" height="20">';} //INTERACTUANDO
			if (ajax.readyState==2) {	contenedor.innerHTML = '<img src="../icons/icon_orange.gif" alt="-" width="5" height="20">';} //INTERACTUANDO
			if (ajax.readyState==3) {	contenedor.innerHTML = '<img src="../icons/icon_orange.gif" alt="-" width="5" height="20">';} //INTERACTUANDO
			if (ajax.readyState==4) { //COMPLETADO
				contenedor.innerHTML = ajax.responseText;
				estat_ajax = 'OK';
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		//ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
		var resultat = "taula_activa="+taula_activa+"&camp_actiu="+camp_actiu+"&id_actiu="+reg_id+"&valor_ajax="+valor_ajax;
		ajax.send(resultat);
	} else {
		document.getElementById(elemento).focus();
	}
}

function updateHistJuri(id_exp_juri_actiu){
	var id_actiu = id_exp_juri_actiu;
		var contenedor = document.getElementById('contenedor_expjuriact_'+id_actiu);
		var form_update = document.getElementById('form_update_historial_'+id_actiu);
		var valor_ajax = id_exp_juri_actiu;
		var url_ajax = "http://DespatxPalleja.no-ip.org/update_exp_juri.php";//?reg_id_actiu="+id_exp_juri_actiu;
		//alert(valor_ajax);
		ajax=nuevoAjax();
		ajax.open("POST", url_ajax ,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==0) {	contenedor.innerHTML = '<img src="../icons/icon_loading.gif" alt="-" width="16" height="16">';} //INTERACTUANDO
			if (ajax.readyState==1) {	contenedor.innerHTML = '<img src="../icons/icon_loading.gif" alt="-" width="16" height="16">';} //INTERACTUANDO
			if (ajax.readyState==2) {	contenedor.innerHTML = '<img src="../icons/icon_loading.gif" alt="-" width="16" height="16">';} //INTERACTUANDO
			if (ajax.readyState==3) {	contenedor.innerHTML = '<img src="../icons/icon_loading.gif" alt="-" width="16" height="16">';} //INTERACTUANDO
			if (ajax.readyState==4) { //COMPLETADO
				contenedor.innerHTML = "Datos Actualizados";//ajax.responseText;
				form_update.submit();
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		//ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
		var resultat = "reg_id_actiu="+valor_ajax;
		ajax.send(resultat);
}

function PromptUpdateCamp(id_actiu,taula_activa,camp_actiu){
	var elemento = camp_actiu+"_"+id_actiu;
	if(estat_ajax == 'OK'){
		estat_ajax = 'KO';
		var reg_id = id_actiu;
		var contenedor = document.getElementById('contenedor_'+camp_actiu+'_'+id_actiu);
		var valor_ajax = prompt('Introduzca el nuevo valor','');
		if(valor_ajax!=null){
			//alert(valor_ajax);
			ajax=nuevoAjax();
			ajax.open("POST", "../ajax/update_valor_ajax.php",true);
			ajax.onreadystatechange=function() {
				if (ajax.readyState==0) {	contenedor.innerHTML = '<img src="../icons/icon_orange.gif" alt="-" width="5" height="20">';} //INTERACTUANDO
				if (ajax.readyState==1) {	contenedor.innerHTML = '<img src="../icons/icon_orange.gif" alt="-" width="5" height="20">';} //INTERACTUANDO
				if (ajax.readyState==2) {	contenedor.innerHTML = '<img src="../icons/icon_orange.gif" alt="-" width="5" height="20">';} //INTERACTUANDO
				if (ajax.readyState==3) {	contenedor.innerHTML = '<img src="../icons/icon_orange.gif" alt="-" width="5" height="20">';} //INTERACTUANDO
				if (ajax.readyState==4) { //COMPLETADO
					contenedor.innerHTML = ajax.responseText;
					estat_ajax = 'OK';
					document.getElementById(elemento).value = valor_ajax;
				}
			}
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			//ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
			var resultat = "taula_activa="+taula_activa+"&camp_actiu="+camp_actiu+"&id_actiu="+reg_id+"&valor_ajax="+valor_ajax;
			ajax.send(resultat);
		} else {
			estat_ajax = 'OK';
		}
	} else {
		document.getElementById(elemento).focus();
	}
}
