
//Funcion que crea el objeto XML 
function getxmlhttp (){
//Create a boolean variable to check for a valid Microsoft active x instance.
var xmlhttp = false;
//Check if we are using internet explorer.
try {
//If the javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
//If not, then use the older active x object.
try {
//If we are using internet explorer.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
//Else we must be using a non-internet explorer browser.
xmlhttp = false;
}
}
// If not using IE, create a
// JavaScript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
//Function to process an XMLHttpRequest.

/* HASTA AQUI HEMOS CREADO EL OBJETO AJAX */
/* ******************************* */
var aok;
//Funcion que recorre todos los elementos del formulario
function getformvalues (fobj){
var str = "";


//Recorre con un bucle el formulario y sus elementos
for(var i = 0; i < fobj.elements.length; i++){

str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
}
//devuelve una variable con todos los values, en forma de GET .

return str;
}
/************************/


//funcion para POST
function submitform (theform, serverPage, objID){
var file = serverPage;
var str = getformvalues(theform);

obj = document.getElementById(objID);
processajax (serverPage, obj, "post", str);// llama a preccessajax(pasa datos)

}

// funcion que procesa los objetos que le envia el formulario
/* ------------------------ */
function processajax (serverPage, obj, getOrPost, str){
//Get an XMLHttpRequest object for use.
xmlhttp = getxmlhttp ();

xmlhttp.open("POST", serverPage, true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText + "<br><span style='color:green;'> estado de la conexión asincrona: <b>"+xmlhttp.statusText+ "</b> </span>";

}else{
		obj.innerHTML = "<span style='background-color:#ddd;'>realizando petición... </span>";	
		}
}
xmlhttp.send(str);
}

function processajaxGET (serverPage1, obj1){

//Get an XMLHttpRequest object for use.
xmlhttp3 = getxmlhttp ();

xmlhttp3.open("GET", serverPage1, true);
xmlhttp3.onreadystatechange = function() {
if (xmlhttp3.readyState == 4 && xmlhttp3.status == 200) {
obj1.innerHTML = xmlhttp3.responseText;

}else{
		obj.innerHTML = "<span style='background-color:#ddd;'>realizando petición... </span>";	
		}
}
xmlhttp3.send(null);
}