function mx_AJAX(id){
	// CONST
	this.initial = 0;
	this.setup = 1;
	this.sent = 2;
	this.in_process = 3;
	this.completed = 4;

	
	// CONEXION
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		this.connection = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		// code for IE6, IE5
		this.connection = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
	  this.connection = null;
	}
	
	// functions
	this.connection.onreadystatechange=mx_AJAX_rsc;
	this.connection.my_parent = this;
	//this.connection.parent=this;
	this.finished=mx_AJAX_finished;
	this.send_form=mx_AJAX_send_form;
	// atributos
	this.element = document.getElementById(id);
	this.enviado = false;
	this.id=id;
}
function mx_AJAX_send_form(form){
	var o = new Object();
	var s = "";
	for(var e=0;e<form.elements.length;e++){
		//alert(form.elements[e].nodeName);
		if(form.elements[e].name.substr(0,4).toLowerCase()!="mxa_"){
			//o[form.elements[e].name]=form.elements[e].value;
			s +=form.elements[e].name+"="+escape(form.elements[e].value)+"&";
		}
	}
	
	this.connection.open(form.method,form.action,true);
	this.connection.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    this.connection.send(s);


	
}
function mx_AJAX_finished(txt){
	alert(txt);
	if(this.element!=null){
		this.element.innerHTML = txt;
	}
}
function mx_AJAX_rsc(){

	if(this.readyState==4){//finish
		switch(this.status){
			
			case 200:{
				//alert(this.responseText);
				//this.my_parent.finished(this.responseText);
				if(this.my_parent.element!=null){
					if(this.responseText=="<img src='images/done.gif' alt='Guardado' />"){
						
						this.my_parent.element.innerHTML = "<img src='images/done.gif' alt='Guardado' />";//this.responseText;
					}else{
						this.my_parent.element.innerHTML = "<a href='#' onclick='document.getElementById(\""+this.my_parent.id+"_error_msg\").style.display=\"\";return false;'><img src='images/error.gif' alt='Error "+this.status+"' /></a>";//this.responseText;
						this.my_parent.element.innerHTML+="<div id='"+this.my_parent.id+"_error_msg' style='background-color:#fff;display:none;position:absolute;top:0;border:1px solid'><h1>Error</h1><a href='#' onclick='document.getElementById(\""+this.my_parent.id+"_error_msg\").style.display=\"none\";return false;'>Cerrar mensaje</a><div style='overflow:auto;height:250px;width:400px'>"+window.location+"<br />"+this.responseText+"</div></div>";
						
					}
				}
			}break;
			default:{
				//this.my_parent.finished("ERROR 404");
				//this.my_parent.element.innerHTML = "<img src='images/error.gif' alt='Error "+this.status+"' />";//this.responseText;
				this.my_parent.element.innerHTML = "<a href='#' onclick='document.getElementById(\""+this.my_parent.id+"_error_msg\").style.display=\"\";return false;'><img src='images/error.gif' alt='Error "+this.status+"' /></a>";//this.responseText;
				this.my_parent.element.innerHTML+="<div id='"+this.my_parent.id+"_error_msg' style='background-color:#fff;display:none;position:absolute;top:0;border:1px solid'><h1>Error</h1><a href='#' onclick='document.getElementById(\""+this.my_parent.id+"_error_msg\").style.display=\"none\";return false;'>Cerrar mensaje</a><div style='overflow:auto;height:250px;width:400px'>" +
						window.location+"<br />"+
						"El sistema no se pudo conectar a internet.<br />" +
						"Por favor intente entrar a <a target='_blank' href='http://www.google.com'>www.google.com</a>.</br>" +
						"Si no se puede conectar, significa que su servicio de internet está abajo. Por favor póngase en contacto " +
						"con soporte técnico Telmex o Cablemas para reportar el problema." +
						"</div></div>";
			}break;
		}
		
	}

}
