﻿// JavaScript Document
function nuevoAjax(){
	var xmlhttp=false;

	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	return xmlhttp;
}

//------------------------------------------------------------------------------
function validar(formulario){
    function validarcadena(campo,texto){
        if (campo.value.length < 1) {
            alert("Debe completar todos los campos");
            campo.focus();
            return (false);
        }
        var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú ";
        var checkStr = campo.value;
        var allValid = true;
        for (i = 0; i < checkStr.length; i++) {
            ch = checkStr.charAt(i);
            for (j = 0; j < checkOK.length; j++)
                if (ch == checkOK.charAt(j))
                    break;
            if (j == checkOK.length) {
                allValid = false;
                break;
            }
        }
        if (!allValid) {
            alert("Escriba sólo letras en el campo \""+texto+"\"");
            campo.focus();
            return (false);
        }
        return true;
    }

    function validarnumero(campo,texto){
        if (campo.value.length < 1) {
            alert("Debe completar todos los campos");
            campo.focus();
            return (false);
        }
        var checkOK = "0123456789.+()- ";
        var checkStr = campo.value;
        var allValid = true;
        for (i = 0; i < checkStr.length; i++) {
            ch = checkStr.charAt(i);
            for (j = 0; j < checkOK.length; j++)
                if (ch == checkOK.charAt(j))
                    break;
            if (j == checkOK.length) {
                allValid = false;
                break;
            }
        }
        if (!allValid) {
            alert("El "+texto+" no es válido");
            campo.focus();
            return (false);
        }
        return true;
    }

    function validaremail(campo){
        if ((campo.value.indexOf ('@', 0) == -1)||(campo.value.length < 5)) {
            alert("Escriba un email válido");
            return false;
          }
         return true;
    }

    if((validarcadena(formulario.nombre, "Nombre"))
        &&(validarcadena(formulario.apellido, "Apellido"))
           //&&(validarcadena(formulario.empresa, "Empresa"))
              &&(validaremail(formulario.email))
                    //&&(validarnumero(formulario.telefono, "Teléfono"))
           )
        return true;
    return false;
}


function enviar(resultado,url){
    if(validar(document.getElementById("formulario"))){
    	resul = document.getElementById(resultado);
	    ajax=nuevoAjax();
    	ajax.open("POST",url,true);
    	ajax.onreadystatechange=function() {
    		if (ajax.readyState==4) {
    			resul.innerHTML = ajax.responseText
    		}
    	}
	    ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        ajax.send("&nombre="+document.getElementById("nombre").value+"&apellido="+document.getElementById("apellido").value+"&empresa="+document.getElementById("empresa").value+"&email="+document.getElementById("email").value+"&phone="+document.getElementById("phone").value+"&location="+document.getElementById("location").value+"&state="+document.getElementById("state").value+"&cuit="+document.getElementById("cuit").value+"&fechaevento="+document.getElementById("fechaevento").value+"&comentario="+document.getElementById("comentario").value)
    }
}



