﻿/*
Libreria de Codigos JavaScript para AJAX
Autor		:	Diego Lerma Gomez
Fecha	:	08-01-2008
Cliente	:	Peru Virtual [ www.ienti.com ]
*/

/*
Esta funcion crea un nuevo objeto XMLHTTP
*/
function dlgAJAX(){

	var objeto = false;	// Creando un objeto AJAX
	try {
		// Se intenta crear un objeto ActiveX  Msxml2.XMLHTTP
		objeto = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		// Si no lo consigue intenta crear otro objeto ActiveX
		try {
			// Pero esta vez Microsoft.XMLHTTP
			objeto = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			// y si tampoco lo logra devuelve un valor falso
			//alert( "No crea AJAX " + E.toString() );
			objeto = false;
		}
	}

	if (!objeto && typeof XMLHttpRequest!='undefined') {
		objeto = new XMLHttpRequest();
	}
	return objeto;
}
/*
Envia un Pedido en AJAX
*/
function enviaDAJAX(destino,cadena){
	var ajax;
	ajax = dlgAJAX();
	ajax.open("POST", destino, true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(cadena);
	return ajax;
}
function enviaAJAX(destino,contenido,cadena){
	var ajax;
	ajax = enviaDAJAX(destino,cadena);
	ajax.onreadystatechange = function() {
		if ( ajax.readyState == 4 ) {
			contenido.innerHTML = ajax.responseText;
		} else {
			contenido.innerHTML = "<p align=\"center\"><img src=\"http://www.ienti.com/images/icon_cargando.gif\" width=\"16\" height=\"16\" alt=\"Procesando...\" /></p>";
		}
	}
}
var dondeAviso = "RPTA";
function setAviso(s,n){
	document.getElementById(dondeAviso).innerHTML = "";
	document.getElementById(dondeAviso).style.display = "block";
	document.getElementById(dondeAviso).innerHTML = "<p class=\""+n+"\">" + s + "</p>";
	setTimeout( "document.getElementById(dondeAviso).style.display = 'none'; ", 6531 );
}
function validaTodo(formu){
//Recorremos el formulario
	for(i = 1; i<formu.length;i++){
		var obj = formu.elements[i];
		if( obj.value == "" ){
			setAviso("Por favor complete el formulario.","mal");
			obj.focus();
			return false;
		}
	}
return true;
}
function levantaDatos(formu){
	var cad = "";
	for ( i=0; i < formu.length ; i++ ){
		var obj = formu.elements[i];
		if ( obj.type == "checkbox" || obj.type == "radio" ){
			if ( obj.checked ){
				cad += "&" + obj.name + "=" + obj.value;
			}
		} else {
			cad += "&" + obj.name + "=" + encodeURI(obj.value);
		}
	}
	return cad;
}
/* Diego Lerma Gomez */