// AJAX - funciones para evitar recarga
var xmlHttp;
var div_destino;
	
function crearXMLHttpRequest(){
	if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	}
}
	
function getQueryBuscarGoogle(){
	
	var argumento = escape(document.getElementById("argumento").value);

	var query = "argumento=" + argumento;
	
	return query;
}	

function getQuerySitioWebCategoria(){

	var accion_tipo = document.getElementById("accion_tipo").value;
	var codigo_actual = document.getElementById("codigo_actual").value;
	var nombre = escape(document.getElementById("nombre").value);

	var query = "accion_tipo=" + accion_tipo;
	query =  query + "&codigo_actual=" + codigo_actual;
	query = query + "&nombre=" + nombre;
	
	return query;
}	

function getQueryResultadoGoogle(){
	var accion_tipo = escape(document.getElementById("accion_tipo").value);
	var codigo_actual = document.getElementById("codigo_actual").value;
	var categoria = document.getElementById("categoria").value;
	var titulo = escape(document.getElementById("titulo").value);
	var direccion = escape(document.getElementById("direccion").value);
	var descripcion = escape(document.getElementById("descripcion").value);

	var query = "accion_tipo=" + accion_tipo;
	query = query + "&codigo_actual=" + codigo_actual;
	query = query + "&categoria=" + categoria;
	query = query + "&titulo=" + titulo;
	query = query + "&direccion=" + direccion;
	query = query + "&descripcion=" + descripcion;
	
	return query;
}

function getQueryResultadoGoogleB(forma){
	//var accion_tipo = eval(document.forms[forma].accion_tipo.value);
	var accion_tipo = "1";
	var codigo_actual = eval(document.forms[forma].codigo_actual.value);
	var categoria = eval(document.forms[forma].categoria.value);
	var titulo = escape(document.forms[forma].titulo.value);
	var direccion = escape(document.forms[forma].direccion.value);
	var descripcion = escape(document.forms[forma].descripcion.value);

	var query = "accion_tipo=" + accion_tipo;
	query = query + "&codigo_actual=" + codigo_actual;
	query = query + "&categoria=" + categoria;
	query = query + "&titulo=" + titulo;
	query = query + "&direccion=" + direccion;
	query = query + "&descripcion=" + descripcion;
	
	return query;
}
	
function buscar_en_google(div, url){
	div_destino = div;
	crearXMLHttpRequest();
	var queryString = getQueryBuscarGoogle();
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = manejadorRespuesta;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(queryString); 
}

function cargarResultado(div, url, forma){
	div_destino = div;
	crearXMLHttpRequest();
	var queryString = getQueryResultadoGoogleB(forma);
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = manejadorRespuesta;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(queryString); 
}

function enviarFormaSitioWeb(div, forma){
	div_destino = div;
	crearXMLHttpRequest();
	var query = "";
	
	if(forma == "forma_sitio_web_categoria"){
		query = getQuerySitioWebCategoria();
	}
	if(forma == "forma_sitio_web"){
		query = getQueryResultadoGoogle();
	}
	
	var url = document.forms[forma].action;
	
	xmlHttp.open("POST", url, true);
	xmlHttp.onreadystatechange = manejarRespuestaForma;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(query);
}

	
function manejadorRespuesta(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			document.getElementById(div_destino).innerHTML = xmlHttp.responseText;
		}
	}
}
