﻿var xmlHttpContacto; // global instance of XMLHttpRequest
var valido;
var segmento = "N";

function createXmlHttpRequestRegistro() {
    if (window.ActiveXObject) {
        xmlHttpContacto = new ActiveXObject("Microsoft.XMLHTTP");
    }

    else if (window.XMLHttpRequest) {
        xmlHttpContacto = new XMLHttpRequest();
    }

}

function validaContacto() {
    var nombreM = document.getElementById('nombreM');
    var emailM = document.getElementById('emailM');
    var emailI = document.getElementById('emailI');
    var comentarioM = document.getElementById('comentarioM');

    nombreM.style.display = 'none';
    emailM.style.display = 'none';
    emailI.style.display = 'none';
    comentarioM.style.display = 'none';
    

    var nombre = document.forma.nombre.value;
    var email = document.forma.email.value;
    var comentario = document.forma.comentario.value;
    var telefono = document.forma.telefono.value;
    var seccion = document.forma.seccion.value;
    var ocupacion = document.forma.ocupacion.value;
    var razonSocial = document.forma.razonSocial.value;
    var giro = document.forma.giro.value;
    var telNegocio = document.forma.telNegocio.value;

    var CalleNum = document.forma.CalleNum.value;
    var Colonia = document.forma.Colonia.value;
    var CP = document.forma.CP.value;
    var Delegacion = document.forma.Delegacion.value;
    var Estado = document.forma.Estado.value;
    var Ciudad = document.forma.Ciudad.value;

    var Direccion = "Calle y Número: " + CalleNum + " Colonia: " + Colonia + " C.P: " + CP +  " Delegación o Municipio" + Delegacion + " Ciudad: " + Ciudad + " Estado: " + Estado;
    var valido = true;

    if (nombre == "") {
        valido = false;
        nombreM.style.display = 'block';
    }

    if (email == "") {
        valido = false;
        emailM.style.display = 'block';
    }
    else {
        var filter = /^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
        if (!filter.test(email)) {
            valido = false;
            emailI.style.display = 'block';
        }
    }


	
		
  	if (comentario==""){
        valido = false;
        comentarioM.style.display = 'block';
    }						
		 
    if (valido) {
        document.getElementById("mensajeContacto").innerHTML = 'Enviando datos...';
        Scribe.services.Contacto.envia(nombre, email, telefono, comentario, seccion, ocupacion, razonSocial, giro, telNegocio, Direccion, OnCompleteContacto, OnTimeOutContacto, OnErrorContacto);
    }
}

function OnCompleteContacto(args) {
    document.getElementById("mensajeContacto").innerHTML = '';
    if (args == "OK") {
        document.forma.comentario.value = '';
        document.getElementById("mensajeContacto").innerHTML = 'Tu comentario se ha enviado con éxito';
    } else {
        document.getElementById("mensajeContacto").innerHTML = 'Ha ocurrido un error - ' + args;
    }
}

function OnTimeOutContacto(args) {
    document.getElementById("mensajeContacto").innerHTML = "Se ha agotado el tiempo, por favor inténtalo de nuevo más tarde.";
}

function OnErrorContacto(args) {
    document.getElementById("mensajeContacto").innerHTML = "Ha ocurrido un error, Inténtalo más tarde";
}

function activa(objeto) {
    myDiv = document.getElementById("distribuidor");
    if (objeto.value == "Ventas y Productos") {
        myDiv.style.display = 'block';
    } else {
        myDiv.style.display = 'none';
    }

}

function actualizaListado() {
    myText = document.getElementById("TextCP");
//    myHidden = document.getElementById("cantidad");
    myFrame = document.getElementById("listado");
    myFrame.src = "AtencionDistribuidor.aspx?codigo=" + myText.value + "&segmento=" + segmento;
}

function setCuantos(letra) {
    //myHidden = document.getElementById("cantidad");
    //    myHidden.value = letra;
    segmento = letra;
    if (letra == "D") {
        document.getElementById("cantidadB").innerHTML= "Más de una caja";
    } else {
        document.getElementById("cantidadB").innerHTML = "Menos de una caja";
    }
}

function cambiaOcupacion(objeto) {
    myDiv = document.getElementById("ocupacionNPDiv");
    if (objeto.value == "NegocioPropio") {
        myDiv.style.display = 'block';
    } else {
        myDiv.style.display = 'none';
    }
}


function validaContactoCorto(seccion) {
    var nombre = document.forma.nombre.value;
    var email = document.forma.email.value;
    var comentario = document.forma.comentario.value;
    var seccion = seccion;

    var filter = /^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;

    if ((nombre == "")||(nombre == "Nombre")) {
        document.getElementById("msgCC").innerHTML = "Escribe tu nombre";
    } else if ((email == "")||(nombre == "Email")) {
        document.getElementById("msgCC").innerHTML =  "Escribe tu Email";
    } else if (!filter.test(email)) {
        document.getElementById("msgCC").innerHTML = "Escribe un Emil válido";
    } else if ((comentario=="")||(nombre == "Comentarios")){
        document.getElementById("msgCC").innerHTML = "Escribe lo que quieres decirnos";
    } else {		 
        document.getElementById("msgCC").innerHTML = 'Enviando datos...';
        Scribe.services.Contacto.enviaCorto(nombre, email, comentario, seccion, OnCompleteContactoCorto, OnTimeOutContactoCorto, OnErrorContactoCorto);
    }
}

function OnCompleteContactoCorto(args) {
    document.getElementById("msgCC").innerHTML = '';
    if (args == "OK") {
        document.forma.comentario.value = '';
        document.getElementById("msgCC").innerHTML = 'Tu comentario se ha enviado con éxito';
    } else {
        document.getElementById("msgCC").innerHTML = 'Ha ocurrido un error - ' + args;
    }
}

function OnTimeOutContactoCorto(args) {
    document.getElementById("msgCC").innerHTML = "Se ha agotado el tiempo, por favor inténtalo de nuevo más tarde.";
}

function OnErrorContactoCorto(args) {
    document.getElementById("msgCC").innerHTML = "Ha ocurrido un error, Inténtalo más tarde";
}

