﻿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 envio_ajax(valores, fitxer, str_div, borra){
	ajax = nuevoAjax();
	var div = document.getElementById(str_div);

	ajax.open('POST', fitxer, true);

	ajax.onreadystatechange = function(){                
		if (ajax.readyState==1){
			//div.innerHTML = "<img src=\"img/loader.gif\">";
		}else if( ajax.readyState==4 ){
			switch ( ajax.status ) {
				case 200: 	var respuesta = ajax.responseText;
							if(borra==0){
								div.innerHTML = respuesta;
							}else{
								div.innerHTML += respuesta;  
								}
							break;
				case 404:
							div.innerHTML = "La direccion no existe :" + fitxer;
							break;
				case 500:	
							div.innerHTML = "Error en el codigo de la aplicacion\n enviando a " + str_div + "\nvalores= " + valores;
							break;
				default:	
							div.innerHTML = "Error["+ ajax.status +"]: ";
			}
		}
	}
	ajax.setRequestHeader('Charset', 'ISO-8859-1');
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajax.send(valores);
	return 1;
}



