//////////////////////////////////////////////////////
// 	Archivo RCR_ajax.js 	 		  		 		//
// 	para la manipulacion de las peticiones AJAX y   //
// 	la carga de contenido de paginas                //
//	Autor: por Ronal Cruz Romero					//
//	Creado: 25-04-2007								//
//////////////////////////////////////////////////////

var http_request = false;
var xmlhttp;

// Funcion para llamar un URL y devolver mensaje en un ALERT
function makeRequest(url) 
{
	http_request = false;
	
	if (window.XMLHttpRequest) 
	{ // Mozilla...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType("text/xml");
		}
	} 
	else if (window.ActiveXObject) 
	{ // IE
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
			}
		}
	}
	
	if (!http_request) 
	{
		alert("ERROR :( No es posible crear una instancia XMLHTTP");
		return false;
	}
	http_request.onreadystatechange = alertContents; // Que funcion llamar al recibir respuesta del servidor
	http_request.open("GET", url, true);
	http_request.send(null);
}

function alertContents() 
{
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			alert(http_request.responseText);
		} 
		else 
		{
			alert("Hubo problemas con la petición.");
		}
	}
}

function XHConn()
{
	var bComplete = false;
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch (e) 
	{
		try 
		{ 
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
		catch (e) 
		{ 
			try 
			{ 
				xmlhttp = new XMLHttpRequest(); 
			}
			catch (e) 
			{ 
				xmlhttp = false; 
			}
		}
	}
	if (!xmlhttp) 
		return null;

	this.connect = function(sURL, sMethod, sVars, fnDone)
	{
		if (!xmlhttp) 
			return false;
		bComplete = false;
		sMethod = sMethod.toUpperCase();
		try 
		{
			if (sMethod == "GET")
			{
				xmlhttp.open(sMethod, sURL+"?"+sVars, true);
				sVars = "";
			}
			else
			{
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState == 4 && !bComplete)
				{
					bComplete = true;
					fnDone(xmlhttp);
				}
			};
			xmlhttp.send(sVars);
		}
		catch(z) 
		{ 
			return false; 
		}
		return true;
	};
	return this;
}

function Load_and_Run(url, sVars, fnDone)
{
	var myConn = new XHConn();
	if (!myConn) 
		alert("XMLHTTP no esta disponible. Inténtalo con un navegador más actual.");
	myConn.connect(url, "GET", sVars, fnDone);
}	


// Funcion para llamar un URL y devolver contenido en un contenedor(target)
function Load_Content(target, url, sVars, msg)
{
	document.getElementById(target).innerHTML = msg;
	var myConn = new XHConn();
	if (!myConn) 
		alert("XMLHTTP no esta disponible. Inténtalo con un navegador más actual.");
	var peticion = function (oXML) 
	{ 
		document.getElementById(target).innerHTML = oXML.responseText; 
	};
	myConn.connect(url, "GET", sVars, peticion);
}	

function Load_cross_domain(target, url, msg) 
{
	document.getElementById(target).innerHTML = msg;
	var myConn = new XHConn();
	if (!myConn) 
		alert("XMLHTTP no esta disponible. Inténtalo con un navegador más actual.");
	var peticion = function (oXML) 
	{ 
		document.getElementById(target).innerHTML = oXML.responseText; 
	};
	myConn.connect('load_url.php', "GET", 'url='+url, peticion);
}	

function crearFrame(url, sVars, _width, _height) 
{
	var myFrame = document.createElement("IFRAME");
	myFrame.id = "myFrame";
	if (sVars !="")
		myFrame.src = url+"?"+sVars;
	else
		myFrame.src = url;
	myFrame.width = _width;
	myFrame.height = _height;
	var control = document.getElementById("myFrame")
	if (control==null) 
	{
		document.body.appendChild(myFrame);
	}
}

function crearDiv(_id, _content, _top, _left, _width, _height, _visible) 
{
	var myDiv = document.createElement("DIV");
	myDiv.id = _id;
	myDiv.width = _width;
	myDiv.height = _height;
	var control = document.getElementById(_id)
	if (control!=null) 
	{
		document.body.removeChild(control);
	}
	document.body.appendChild(myDiv);
	if (!_visible)
		document.getElementById(_id).style.visibility="hidden";
	document.getElementById(_id).innerHTML = _content; 
}

function hide_win(obj)
{
	obj.parentNode.parentNode.parentNode.parentNode.style.visibility='hidden';
}

function make_win(_id, _title, _imgpath, _width, _height, _visible, innercontent)
{
	var myDiv = document.createElement("DIV");
	var content = "";
	myDiv.id = _id;
	myDiv.width = _width;
	myDiv.height = _height;
	var control = document.getElementById(_id)
	if (control!=null) 
	{
		document.body.removeChild(control);
	}
	document.body.appendChild(myDiv);
	if (!_visible)
		document.getElementById(_id).style.visibility="hidden";
	document.getElementById(_id).style.position="absolute";
	document.getElementById(_id).style.top="0";
	document.getElementById(_id).style.left="0";
	//Alinear al centro content = "<div style = 'display: table; margin-left: auto; margin-right: auto; width: "+(_width-20)+"px;'>";
	content = "<div style = 'display: table; width: "+(_width-20)+"px;'>";
    content += "<div style = 'float: left; height: 62px; width: 38px; background:url("+_imgpath+"borde_sup_iz.png)'></div>";
    content += "<div style = 'float: left; height: 62px; width: "+ (_width-150)+"px; background:url("+_imgpath+"borde_sup.png)'><div style = 'float: left; height: 62px; padding-top:35; padding-left:10;'>"+_title+"</div></div>";
    content += "<div style = 'float: left; height: 62px; width: 92px; background:url("+_imgpath+"borde_sup_der.png)'><div style = 'float: left; height: 62px; padding-top:35; padding-left:10;'><img src='"+_imgpath+"btminus.png' width='15' height='15'><img src='"+_imgpath+"separador.png' width='4'><img src='"+_imgpath+"btplus.png' width='15' height='15'><img src='"+_imgpath+"separador.png' width='4'><img src='"+_imgpath+"btclose.png' width='15' height='15' onClick='hide_win(this)'></div></div>";
	
    content += "<div style = 'float: left; height: "+_height+"px; width: 38px; background:url("+_imgpath+"borde_iz.png)'></div>";
    content += "<div id='"+_id+"_child' style = 'overflow:auto; float: left; height: "+_height+"px; width: "+ (_width-84)+"px; background:url("+_imgpath+"centro.png)'>"+innercontent+"</div>";
    content += "<div style = 'float: left; height: "+_height+"px; width: 26px; background:url("+_imgpath+"borde_der_fino.png)'></div>";
	
	content += "<div style = 'float: left; height: 47px; width: 38px; background:url("+_imgpath+"borde_inf_iz.png)'></div>";
    content += "<div style = 'float: left; height: 47px; width: "+ (_width-96)+"px; background:url("+_imgpath+"borde_inf.png)'></div>";
    content += "<div style = 'float: left; height: 47px; width: 38px; background:url("+_imgpath+"borde_inf_der.png)'></div>";
	content += "</div>";
	document.getElementById(_id).innerHTML = content; 
}

function make_popup(_id, _title, _imgpath, _width, _height, _visible, innercontent)
{
	var myDiv = document.createElement("DIV");
	var content = "";
	myDiv.id = _id;
	myDiv.width = _width;
	myDiv.height = _height;
	var control = document.getElementById(_id)
	if (control!=null) 
	{
		document.body.removeChild(control);
	}
	document.body.appendChild(myDiv);
	if (!_visible)
		document.getElementById(_id).style.visibility="hidden";
	document.getElementById(_id).style.position="absolute";
	document.getElementById(_id).style.top="0";
	document.getElementById(_id).style.left="0";
	//Alinear al centro content = "<div style = 'display: table; margin-left: auto; margin-right: auto; width: "+(_width-20)+"px;'>";
	content = "<div style = 'display: table; width: "+(_width)+"px;'>";
    content += "<div style = 'float: left; height: 19px; width: 19px; background:url("+_imgpath+"1.png)'></div>";
    content += "<div style = 'float: left; height: 19px; width: "+(_width-45)+"px; background:url("+_imgpath+"2.png)'><div style = 'float: left; height: 19px;'></div>"+_title+"</div>";
    content += "<div style = 'float: left; height: 19px; width: 26px; background:url("+_imgpath+"3.png)'></div>";
	
    content += "<div style = 'float: left; height: "+_height+"px; width: 19px; background:url("+_imgpath+"8.png)'></div>";
    content += "<div id='"+_id+"_child' style = 'overflow:auto; float: left; height: "+_height+"px; width: "+(_width-31)+"px; background:url("+_imgpath+"0.png)'>"+innercontent+"</div>";
    content += "<div style = 'float: left; height: "+_height+"px; width: 11px; background:url("+_imgpath+"4.png)'></div>";
	
	content += "<div style = 'float: left; height: 27px; width: 19px; background:url("+_imgpath+"7.png)'></div>";
    content += "<div style = 'float: left; height: 27px; width: "+ (_width-45)+"px; background:url("+_imgpath+"6.png)'></div>";
    content += "<div style = 'float: left; height: 27px; width: 26px; background:url("+_imgpath+"5.png)'></div>";
	content += "</div>";
	document.getElementById(_id).innerHTML = content; 
}
