
var AjaxEnginePage;
var ClientInfoPage;
var XMLHTTP;
var XMLHTTP_Implicado;
var XMLHTTP_Direccion;
var idObjeto = "dataContents";
var idObjetoTmp = "dataContentsTmp";
var idVitrina1 = "idVitrina1";
var idVitrina2 = "idVitrina2";
var idVitrina3 = "idVitrina3";
var fotoTienda1 = "fotoTienda1";
var fotoTienda2 = "fotoTienda2";
var fotoTienda3 = "fotoTienda3";
var infoTienda1 = "infoTienda1";
var infoTienda2 = "infoTienda2";
var infoTienda3 = "infoTienda3";
var idVitrinaGrande = "idVitrinaGrande";

var resultadoGeneral = "";
var totalImagenes = "0";
var numImagenActual = "0";
 
AjaxEnginePage = "AjaxEngine.aspx";
ClientInfoPage="getClientInfo.aspx";

function ExtraerXML(cadenaXML,elementoXML)
{
    var elemento = "";
	var endpos = 0;
	
	//elementoXML=elementoXML.toUpperCase();
	var ini_label = "<" + elementoXML;
	var end_label = "</" + elementoXML;
 
 	var startpos = cadenaXML.indexOf(ini_label);
	if (startpos != -1) {
	    startpos += elementoXML.length;
		startpos += 2;
		var endpos = cadenaXML.indexOf(end_label);
		elemento = cadenaXML.substring(startpos,endpos);
	}
	return elemento;
}

function CreateXMLHTTP()
{
	try
	{
		XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XMLHTTP = null;
		}
	}
	//Creating object in Mozilla and Safari 
	if(!XMLHTTP && typeof XMLHttpRequest != "undefined") 
	{
		XMLHTTP = new XMLHttpRequest();
	}
}


function btnAjax_OnClick() 
{
	//var Id_Estado = encodeURIComponent(document.getElementById("txtID_Estado").value);
	//var Nombre_Estado = encodeURIComponent(document.getElementById("txtNombre_Estado").value);
    //var Descripcion = encodeURIComponent(document.getElementById("txtDescripcion").value);

	// construct the URL 
	//var requestUrl ="../"+ AjaxEnginePage  + "?Action=MostrarVitrinaDefault&Id_Estado="+ Id_Estado
	var requestUrl =  AjaxEnginePage  + "?Action=MostrarVitrinaDefault";//&Id_Estado="+ Id_Estado
	//+"&Nombre_Estado="+ Nombre_Estado
	//+"&Descripcion="+ Descripcion;
	
	CreateXMLHTTP();
	
	// If browser supports XMLHTTPRequest object
	if(XMLHTTP)
	{
		//Setting the event handler for the response
		XMLHTTP.onreadystatechange = MostrarVitrinaDefault;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XMLHTTP.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XMLHTTP.send(null);		
	}
	
}

function MostrarVitrinaDefault()
{
	// To make sure receiving response data from server is completed
	document.getElementById(idObjeto).innerHTML="<img src='../images/ajaxRed.gif'> Leyendo Informacion...";
    resultadoGeneral = XMLHTTP.responseText;
    //alert(ExtraerXML(XMLHTTP.responseText,"cantidad"));
	if(XMLHTTP.readyState == 4)
	{
		//Valid Response is received
		if(XMLHTTP.status == 200)
		{		
			//EWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV4
				        if(XMLHTTP.responseText.indexOf('WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV4')!=-1)
								{
								document.getElementById(idObjeto).innerHTML= ExtraerXML(XMLHTTP.responseText,"WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV4");
						        if(XMLHTTP.responseText.indexOf('WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV5')!=-1)
								    {
								        GenerarVentanaExcel(ExtraerXML(XMLHTTP.responseText,"WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV5"));
								    }
								if(XMLHTTP.responseText.indexOf('WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV1')!=-1)
								    {
								        document.getElementById("TiempoCarga").innerHTML="Generado en " + ExtraerXML(XMLHTTP.responseText,"WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV1") + " Segundos.";
								    }    
								}
								else
								{
                                    resultadoGeneral = XMLHTTP.responseText;
                                    //alert(ExtraerXML(XMLHTTP.responseText,"cantidad"));
                                    /*int cc;
                                    cc = parseInt(ExtraerXML(XMLHTTP.responseText,"cantidad"));
                                    alert(cc);
                                    cc++;
                                    alert(cc);
                                    //numImagenActual = XMLHTTP.responseText;
                                    */
	 		                        document.getElementById(idObjeto).innerHTML=XMLHTTP.responseText;
	 		                        document.getElementById(idObjetoTmp).innerHTML=XMLHTTP.responseText;
                                    //var randomnumber=Math.floor(Math.random()*6);
                                    //alert(randomnumber);
                                    document.getElementById(idVitrinaGrande).innerHTML=document.getElementById(idVitrina1).innerHTML=ExtraerXML(XMLHTTP.responseText,"1")+"ImgProducto0.png";
                                    document.getElementById(idVitrina2).innerHTML=ExtraerXML(XMLHTTP.responseText,"3")+"ImgProducto0.png";
                                    document.getElementById(idVitrina3).innerHTML=ExtraerXML(XMLHTTP.responseText,"4")+"ImgProducto0.png";
                                    //document.getElementById(idVitrinaGrande).innerHTML=ExtraerXML(XMLHTTP.responseText,"1")+"ImgProducto0.png";
                                    
	 		                    }
			//document.getElementById(idObjeto).innerHTML="Listo!...";
	 
		}
		else //something is wrong 
		{
			document.getElementById(idObjeto).innerHTML="Error Status = " + XMLHTTP.status ;
		}	
	}
	else
	{
		document.getElementById(idObjeto).innerHTML="Cargando...<img src='../images/ajaxRed.gif'>";
	}
}


/*


//CON ESTE JS SE HACE LA PETICION DE RESPUESTA CON AJAX AL SERVER

function btnActualizar_Estados_OnClick() 
{
	//
	var Id_Estado = encodeURIComponent(document.getElementById("txtID_Estado").value);
	var Nombre_Estado = encodeURIComponent(document.getElementById("txtNombre_Estado").value);
    var Descripcion = encodeURIComponent(document.getElementById("txtDescripcion").value);

	// construct the URL 
	var submitOK="true";
       if (Nombre_Estado.length<2)
         {
         alert("Debe ingresar Nombre del Estado");
         submitOK="false";
         }
   if   (submitOK=="true")
   {
	var requestUrl ="../"+ AjaxEnginePage  + "?Action=Actualizar_Estados&Id_Estado="+ Id_Estado
	+"&Nombre_Estado="+ Nombre_Estado
	+"&Descripcion="+ Descripcion;
	
	CreateXMLHTTP();
	
	// If browser supports XMLHTTPRequest object
	if(XMLHTTP)
	{
		//Setting the event handler for the response
		XMLHTTP.onreadystatechange = ShowSuccessMsg;
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		XMLHTTP.open("GET", requestUrl,  true);
		
		//Sends the request to server
		XMLHTTP.send(null);		
	}
	}
}



//EJEMPLOS DE COMO SE RECIBE LOS DATOS DEVUELTOS EN AJAX POR EL SERVIDOR EL AJAX EL CODIGO BEHIND ESTA MAS ABAJO

function MostraLista()
{
	// To make sure receiving response data from server is completed
	document.getElementById(idObjeto).innerHTML="<img src='../images/ajaxRed.gif'> Leyendo Informacion...";

	if(XMLHTTP.readyState == 4)
	{
		//Valid Response is received
		if(XMLHTTP.status == 200)
		{		
			//EWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV4
				        if(XMLHTTP.responseText.indexOf('WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV4')!=-1)
								{
								document.getElementById(idObjeto).innerHTML= ExtraerXML(XMLHTTP.responseText,"WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV4");
						        if(XMLHTTP.responseText.indexOf('WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV5')!=-1)
								    {
								        GenerarVentanaExcel(ExtraerXML(XMLHTTP.responseText,"WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV5"));
								    }
								if(XMLHTTP.responseText.indexOf('WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV1')!=-1)
								    {
								        document.getElementById("TiempoCarga").innerHTML="Generado en " + ExtraerXML(XMLHTTP.responseText,"WEWKWLA8IKBAGKVY47LBWKETNFJDQKU8TNLCWKP46YDDALV1") + " Segundos.";
								    }    
								}
								else
								{
	 		                        document.getElementById(idObjeto).innerHTML=XMLHTTP.responseText;
	 		                    }
			//document.getElementById(idObjeto).innerHTML="Listo!...";
	 
		}
		else //something is wrong 
		{
			document.getElementById(idObjeto).innerHTML="Error Status = " + XMLHTTP.status ;
		}	
	}
	else
	{
		document.getElementById(idObjeto).innerHTML="Cargando...<img src='../images/ajaxRed.gif'>";
	}
}
function MostraLista_Usos()
{
	// To make sure receiving response data from server is completed
	if(document.getElementById("formato_Respuesta").value=="tabla")
	    document.getElementById(idObjeto).innerHTML="<img src='../images/ajaxRed.gif'> Leyendo Informacion...";

	if(XMLHTTP.readyState == 4)
	{
		//Valid Response is received
		if(XMLHTTP.status == 200)
		{		
			//alert(XMLHTTP.responseText);
			if(document.getElementById("formato_Respuesta").value=="tabla")
			{
			    document.getElementById(idObjeto).innerHTML="Listo!...";
			    document.getElementById(idObjeto).innerHTML=XMLHTTP.responseText;
			    document.getElementById("tools_Bar").innerHTML="<a href='#' id='btnPrint'><img src='../images/print.gif' /></a>";
			    document.getElementById("btnPrint").href="javascript:window.open('frm_Busca_Usos_PDF.aspx" + "?ano=" + document.getElementById("anoUso").value + "&"+ document.getElementById("OpcionesCampos").value + "=" + document.getElementById("Campo").value + "','Resultado','toolbar=no,location=no,menubar=no,resizable=yes,width=600,height=440');window.location='frm_Busca_Usos.aspx'";
			}
			else
			{
			    document.getElementById('formato_Respuesta').value='tabla'
			    window.open('http://localhost/Gobierno_Extras/RPT_Usos_PDF.asp?cadenaXML=' + XMLHTTP.responseText,'Formato_Usos','menubar=0, location=0, status=1, scrollbars=1, resize=1, width=800, height=600');
			}
		}
		else //something is wrong 
		{
			document.getElementById(idObjeto).innerHTML="Error Status = " + XMLHTTP.status ;
		}	
	}
	else if(document.getElementById("formato_Respuesta").value=="tabla")
	{
		document.getElementById(idObjeto).innerHTML="Cargando...<img src='../images/ajaxRed.gif'>";
	}
}



//AQUI ESTA EL CODIGO QUE IRIA EN EL BEHIND osea en el archivo .cs que corre en el server

if (Request["Action"] != null && Request["Action"].Trim() != "")
            {
                if (Request["Action"] == "IdentificarAlc" && (Request["X"] != null && Request["Y"].Trim() != ""))
                    IdentificarAlc(Convert.ToDouble(Request["X"]), Convert.ToDouble(Request["Y"]));
                else if (Request["Action"] == "Login")
                    Login(Request["Login"], Request["Clave"]);
                else if (Request["Action"] == "CambiarClave")
                    CambiarClave(Request["a"], Request["b"], Request["c"]);
                else if (Request["Action"] == "BuscarDireccion" && (Request["Direccion"] != null))
                    BuscarDireccion(Convert.ToString(Request["Direccion"]));
                else if (Request["Action"] == "LongitudxDiametro")
                    Estadistica();
                else if (Request["Action"] == "ActualizarImplicados")
                {
                    if (Request["Id_Implicado"] != null && Request["Id_Implicado"].Trim() != "")
                    {
                        ActualizarImplicados(Convert.ToInt32(Request["Id_Solicitud"]), Convert.ToInt32(Request["Id_Implicado"]), Convert.ToInt32(Request["Identificacion"]), Convert.ToInt32(Request["Id_Tipo_Identificacion"]), Request["Nombre"], Request["Apellidos"], Convert.ToInt32(Request["Genero"]), Convert.ToDateTime(Request["Fecha_Nac"], myCI.DateTimeFormat), Convert.ToInt32(Request["Id_Lugar_Nac"]), Request["Direccion"], Request["Telefono"], Request["Email"], Convert.ToInt32(Request["Id_Lugar_Procedencia"]), Convert.ToDateTime(Request["Fecha_Registro"]), Convert.ToInt32(Request["Id_Usuario_Registro"]), Request["CampoIdImplicado"], Request["CampoNombre"]);
                    }
                    else if (Request["Id_Atiende_Visita"] != null && Request["Id_Atiende_Visita"].Trim() != "")
                    {
                        ActualizarImplicados(((Request["Id_Solicitud"] != null && Request["Id_Solicitud"].Trim() != "") ? Convert.ToInt32(Request["Id_Solicitud"]) : 0), Convert.ToInt32(Request["Id_Atiende_Visita"]), Convert.ToInt32(Request["Identificacion"]), Convert.ToInt32(Request["Id_Tipo_Identificacion"]), Request["Nombre"], Request["Apellidos"], Convert.ToInt32(Request["Genero"]), Convert.ToDateTime(Request["Fecha_Nac"]), Convert.ToInt32(Request["Id_Lugar_Nac"]), Request["Direccion"], Request["Telefono"], Request["Email"], Convert.ToInt32(Request["Id_Lugar_Procedencia"]), Convert.ToDateTime(Request["Fecha_Registro"]), Convert.ToInt32(Request["Id_Usuario_Registro"]), Request["CampoIdImplicado"], Request["CampoNombre"]);
                    }
                    else
                    {
                        //((Request["Identificacion"] != null && Request["Identificacion"].Trim() != "" && Int32.TryParse (Request["Identificacion"]) ) ? Convert.ToInt32(Request["Id_Implicado"]) : 0)
                        ActualizarImplicados(((Request["Id_Solicitud"] != null && Request["Id_Solicitud"].Trim() != "") ? Convert.ToInt32(Request["Id_Solicitud"]) : 0), ((Request["Id_Implicado"] != null && Request["Id_Implicado"].Trim() != "") ? Convert.ToInt32(Request["Id_Implicado"]) : 0), ((Request["Identificacion"] != null && Request["Identificacion"].Trim() != "") ? Convert.ToInt32(Request["Identificacion"]) : 0), Convert.ToInt32(Request["Id_Tipo_Identificacion"]), Request["Nombre"], Request["Apellidos"], Convert.ToInt32(Request["Genero"]), Convert.ToDateTime(Request["Fecha_Nac"], myCI.DateTimeFormat), Convert.ToInt32(Request["Id_Lugar_Nac"]), Request["Direccion"], Request["Telefono"], Request["Email"], Convert.ToInt32(Request["Id_Lugar_Procedencia"]), Convert.ToDateTime(Request["Fecha_Registro"]), Convert.ToInt32(Request["Id_Usuario_Registro"]), Request["CampoIdImplicado"], Request["CampoNombre"]);
                    }
                }

}


        public void Login(String Login, String Clave)
        {
            Global_Renamed objGloblal = new Global_Renamed();
            objGloblal.CadenaDeConexion = "Persist Security Info = true;server=localhost; user id=root; password=nts; database=Conv_Seg_Ciu; pooling=false;";
            System.Text.StringBuilder sb = new System.Text.StringBuilder("");
            //sb.Append("<script language=JavaScript>");
            if (objGloblal.Conectar(Login, Clave))
            {
                Session["Login"] = objGloblal.UsuarioActivo.Login;
                Session["LoginId"] = objGloblal.UsuarioActivo.Id_Usuario;
                Session["user"] = objGloblal.UsuarioActivo.Nombre;
                HttpCookie cookie = Request.Cookies["Preferencias"];
                if (cookie == null)
                {
                    cookie = new HttpCookie("Preferencias");
                }

                cookie["Nombre"] = Login;
                cookie.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(cookie);

                //sb.Append("<script language=JavaScript>");
                sb.Append("var pagina = 'gobierno/inicial.aspx';");
                sb.Append("document.location.href = pagina;");
                //sb.Append("alert('OK');");
                //sb.Append("</script>");
            }
            else
            {
                //sb.Append("<script language=JavaScript>");
                sb.Append("document.getElementById('MensageLogin').innerHTML='");
                sb.Append("<table><tr>");
                sb.Append("<td valign=middle><image src=images/error.png></td>");
                sb.Append("<td valign=middle><font color=red><B>" + objGloblal.Detalles_Ultima_Accion + "</B></font></td>");
                sb.Append("");
                sb.Append("</tr></table>");
                sb.Append("';");

                sb.Append("document.getElementById('Login').value='';");
                sb.Append("document.getElementById('clave').value='';");
                sb.Append("document.getElementById('Login').focus();");
                //sb.Append("</script>");
            }
            //sb.Append("</script>");
            //ClientScript.RegisterStartupScript(Page.GetType(), "Login", sb.ToString());
            Response.AddHeader("pragma", "no-cache");
            Response.CacheControl = "Private";
            Response.Expires = 0;
            //Response.ContentType = "text/html";
            Response.Write(sb.ToString());

        }



*/




