﻿// JScript File

var Cat;
var Respuestas;
var Consulta;

function LLenarCat_callback(response)
{
    var ds = response.value; 
    var optionObject;
    var optionRank;
    var List;
    
    if (response.error != null)
    {		
        alert("Se produjo un error al procesar la solicitud.");		
        return;
    }
    
    if (Cat == 0)
    {
        Vaciar(document.getElementById("lstCat2"));
        Vaciar(document.getElementById("lstCat"));
        Vaciar(document.getElementById("lstProductos"));
        
        List = document.getElementById("lstProductos");            
    }
    
    if (Cat == 1)
    {
        Vaciar(document.getElementById("lstCat2"));
        Vaciar(document.getElementById("lstCat"));
        
        List = document.getElementById("lstCat");
    }
    
    if (Cat == 2)
    {
        Vaciar(document.getElementById("lstCat2"));
        
        List = document.getElementById("lstCat2");
    }
        
    Respuestas = new Array(ds.Tables[0].Rows.length);        
    	
    for(var i = 0; i < ds.Tables[0].Rows.length; i++)
    {     
        optionObject = new Option(ds.Tables[0].Rows[i].Valor, ds.Tables[0].Rows[i].CodCategoria);
        optionRank = List.options.length;
        List.options[optionRank] = optionObject;
        Respuestas[i] = ds.Tables[0].Rows[i].Respuesta;
    }
    
    if (i == 0)
    {
        optionObject = new Option("Lista vacia.");
        optionRank = List.options.length;
        List.options[optionRank] = optionObject;   
    }
}

function Cargando()
{
    if (Cat == 0)
    {
        Vaciar(document.getElementById("lstCat2"));
        Vaciar(document.getElementById("lstCat"));
        Vaciar(document.getElementById("lstProductos"));
        
        List = document.getElementById("lstProductos");            
    }
    
    if (Cat == 1)
    {
        Vaciar(document.getElementById("lstCat2"));
        Vaciar(document.getElementById("lstCat"));
        
        List = document.getElementById("lstCat");
    }
    
    if (Cat == 2)
    {
        Vaciar(document.getElementById("lstCat2"));
        
        List = document.getElementById("lstCat2");
    }
        
    optionObject = new Option("Cargando ...");
    optionRank = List.options.length;
    List.options[optionRank] = optionObject;            
}

function Vaciar(List)
{
    for(var i = List.options.length; i >= 0; i--)
        List.remove(i);
        
    MostrarCuadro(false);
}

function MostrarCuadro(valor)
{
	var texto = "";
	var rta = "";
	var clase = "";		
	
    if(valor)
	{			
	    if (document.getElementById("lstCat2").selectedIndex != -1)
		{
			if (document.getElementById("lstCat2").selectedIndex != -1)
				rta = Respuestas[document.getElementById("lstCat2").selectedIndex];
    
			if (rta != "" )
			{
				texto = "<font class=\"etiquetaSinPadding\">Respuesta a su consulta:</font><br><br>";
				texto += rta;
				clase = "respuesta";
			}
			else
			{
				texto = "<font class=\"etiquetaSinPadding\">Su consulta:</font><br>";
				texto += "<textarea ID=\"txtMensaje\" style=\"width: 407px; height: 85px\"></textarea><br/>";
				texto += "<br/>";
				texto += "<button OnClick=\"Enviar();\">Enviar Consulta</button>";
				texto += "</span>";
            }
			
			document.getElementById("Texto").style.visibility = "visible";
            document.getElementById("txtTexto").innerHTML = texto;
			document.getElementById("Texto").className = clase;				
			
			if (rta == "" )
				document.getElementById("txtMensaje").focus();
		}
	}
	else
	{
		document.getElementById("txtTexto").innerHTML = "";
		document.getElementById("Texto").style.visibility = "hidden";
	}   
}

function Enviar()
{
    var Pais, Nivel1, Nivel2, Nivel3, Nom, Ape, Eml, Tel, Men, Ord, Dni, Mensaje;
    
    Pais   = document.getElementById("cmbPaises");
    Nivel1 = document.getElementById("lstProductos");
    Nivel2 = document.getElementById("lstCat");
    Nivel3 = document.getElementById("lstCat2");
    Nom    = document.getElementById("txtNombre");
    Ape    = document.getElementById("txtApellido");
    Eml    = document.getElementById("txtEmail");
    Tel    = document.getElementById("txtTelefono");
    Men    = document.getElementById("txtMensaje");   
    Ord    = document.getElementById("txtOrden"); 
    Dni    = document.getElementById("txtDni"); 
   
    Mensaje = Men.value;
  
    if (Nom.value == "" || Nom.value.length < 3)
    {
        alert("Por favor, ingrese un nombre valido.");
        Nom.focus();
        return;
    }
    
    if (Ape.value == "" || Ape.value.length < 3)
    {
        alert("Por favor, ingrese un apellido valido.");
        Ape.focus();
        return;
    }
	
	if ((Dni.value == "" || Dni.value.length < 7) && Pais.value != 3)
    {
        alert("Por favor, ingrese un D.N.I. valido.");
        Dni.focus();
        return;
    }
    
    if(!ValidaEmail(Eml.value))
    {
        alert("Por favor, ingrese una direccion de email valida.");
        Eml.focus();
        return;
    }
    
    if (Tel.value == "" || Tel.value.length < 6)
    {
        alert("Por favor, ingrese un telefono valido.");
        Tel.focus();
        return;
    }

    if (Mensaje.value == "")
    {
        alert("Por favor, ingrese un mensaje valido.");
        Men.focus();
        return;
    }            

	document.getElementById("Texto").style.visibility = "visible";
	document.getElementById("Texto").className = "resultado";
    document.getElementById("txtTexto").innerHTML = "Enviando su consulta ...";
		
     _Default.GuardarConsulta(Pais.value, Nivel1.value, Nivel2.value, Nivel3.value, Nom.value, Ape.value, Eml.value, Tel.value, Mensaje, Ord.value, Dni.value, GuardarConsulta_callback)
}

function GuardarConsulta_callback(response)
{
    var Resp = response.value; 
    
    if (response.error != null || Resp == false)
    {		
        alert("Se produjo un error al enviar su consulta, por favor intentelo en otro momento.");
		document.getElementById("txtTexto").innerHTML =  Consulta;
        return;	
    }

	document.getElementById("Texto").style.visibility = "visible";
    document.getElementById("txtTexto").innerHTML = "Su consulta ha sido enviada con &eacute;xito.";
	document.getElementById("Texto").className = "resultado";
}

function ValidaEmail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1)
	   return false;

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	   return false;

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	    return false;

	 if (str.indexOf(at,(lat+1))!=-1)
	    return false;

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	    return false;

	 if (str.indexOf(dot,(lat+2))==-1)
	    return false;
	
	 if (str.indexOf(" ")!=-1)
	    return false;

	 return true					
}

