//CONTACTO
function validar_contacto() {
	document.getElementById('contacto_advertencia').innerHTML='';
	document.getElementById('contacto_nombre').innerHTML='';
	document.getElementById('contacto_email').innerHTML='';
	document.getElementById('contacto_info').innerHTML='';
	var valido=1;
	var mensaje = "*"
   	var formulario = document.getElementById('formularioContacto');
    if(document.formularioContacto.contacto_nombre.value.length <= 0) {
		document.getElementById('contacto_nombre').innerHTML = mensaje;
        valido=0;
    }
    if(!mail(document.formularioContacto.contacto_email.value)) {
       document.getElementById('contacto_email').innerHTML = mensaje;
        valido=0;
    }
    if(document.formularioContacto.contacto_telefono.value.length <= 0) {
       document.getElementById('contacto_telefono').innerHTML = mensaje;
        valido=0;
    }
    if(document.formularioContacto.contacto_info.value.length <= 0) {
        document.getElementById('contacto_info').innerHTML = mensaje;
        valido=0;
    }
	if(valido == 1) {
    	enviarContacto(formulario);
		//document.formularioContacto.submit();
	} else
		document.getElementById('contacto_advertencia').innerHTML='(*) Información Incompleta';
}


//OFERTAS
function validar_oferta() {
	document.getElementById('oferta_advertencia_check').innerHTML='';
	document.getElementById('oferta_advertencia').innerHTML='';
	document.getElementById('oferta_nombre').innerHTML='';
	document.getElementById('oferta_email').innerHTML='';
	document.getElementById('oferta_fijo').innerHTML='';
	document.getElementById('oferta_cel').innerHTML='';
	var valido=1;
	var mensaje = "*"
   	var formulario = document.getElementById('formularioOferta');
	if (!validar_checks(formulario)) {
		document.getElementById('oferta_advertencia_check').innerHTML = "Debes elegir al menos un destino";
        valido=0;
	}
   if(document.formularioOferta.oferta_nombre.value.length <= 0) {
		document.getElementById('oferta_nombre').innerHTML = mensaje;
        valido=0;
    }
    if(!mail(document.formularioOferta.oferta_email.value)) {
       document.getElementById('oferta_email').innerHTML = mensaje;
        valido=0;
    }
    if(document.formularioOferta.oferta_fijo.value.length <= 0) {
        document.getElementById('oferta_fijo').innerHTML = mensaje;
        valido=0;
    }
	 if(document.formularioOferta.oferta_cel.value.length <= 0) {
        document.getElementById('oferta_cel').innerHTML = mensaje;
        valido=0;
    }
	if(valido == 1) {
		enviarOferta(formulario);
	} else
		document.getElementById('oferta_advertencia').innerHTML='(*) Información Incompleta';
}

//CARNET
function validar_carnet() {
	document.getElementById('carnet_advertencia').innerHTML='';
	document.getElementById('carnet_nombre').innerHTML='';
	document.getElementById('carnet_cedula').innerHTML='';
	document.getElementById('carnet_pasaporte').innerHTML='';
	document.getElementById('carnet_email').innerHTML='';
	document.getElementById('carnet_telefono').innerHTML='';
	var valido=1;
	var mensaje = "*"
   	var formulario = document.getElementById('formularioCarnet');
	
	if(document.formularioCarnet.carnet_nombre.value.length <= 0) {
		document.getElementById('carnet_nombre').innerHTML = mensaje;
        valido=0;
    }
    if(document.formularioCarnet.carnet_cedula.value.length <= 0) {
        document.getElementById('carnet_cedula').innerHTML = mensaje;
        valido=0;
    }
	if(document.formularioCarnet.carnet_pasaporte.value.length <= 0) {
        document.getElementById('carnet_pasaporte').innerHTML = mensaje;
        valido=0;
    }
	if(!mail(document.formularioCarnet.carnet_email.value)) {
       document.getElementById('carnet_email').innerHTML = mensaje;
        valido=0;
    }
    if(document.formularioCarnet.carnet_telefono.value.length <= 0) {
        document.getElementById('carnet_telefono').innerHTML = mensaje;
        valido=0;
    }
	if(valido == 1) {
		enviarCarnet(formulario);
	} else
		document.getElementById('carnet_advertencia').innerHTML='(*) Información Incompleta';
}

//PROMOTORES
function validar_promotores() {
	document.getElementById('promotores_advertencia').innerHTML='';
	document.getElementById('promotores_nombre').innerHTML='';
	document.getElementById('promotores_email').innerHTML='';
	document.getElementById('promotores_telefono').innerHTML='';
	document.getElementById('promotores_info').innerHTML='';
	
	var valido=1;
	var mensaje = "*"
   	var formulario = document.getElementById('formularioPromotores');
	
	if(document.formularioPromotores.promotores_nombre.value.length <= 0) {
		document.getElementById('promotores_nombre').innerHTML = mensaje;
        valido=0;
    }
    if(!mail(document.formularioPromotores.promotores_email.value)) {
       document.getElementById('promotores_email').innerHTML = mensaje;
        valido=0;
    }
	if(document.formularioPromotores.promotores_telefono.value.length <= 0) {
        document.getElementById('promotores_telefono').innerHTML = mensaje;
        valido=0;
    }
	if(document.formularioPromotores.promotores_info.value.length <= 0) {
        document.getElementById('promotores_info').innerHTML = mensaje;
        valido=0;
    }
	if(valido == 1) {
		enviarPromotores(formulario);
	} else
		document.getElementById('promotores_advertencia').innerHTML='(*) Información Incompleta';
}

//COTIZA
function validar_cotiza() {
	document.getElementById('cotiza_advertencia').innerHTML='';
	document.getElementById('cotiza_nombre').innerHTML='';
	document.getElementById('cotiza_email').innerHTML='';
	document.getElementById('cotiza_telefono').innerHTML='';
	document.getElementById('cotiza_info').innerHTML='';
	
	var valido=1;
	var mensaje = "*"
   	var formulario = document.getElementById('formularioCotiza');
	
	if(document.formularioCotiza.cotiza_nombre.value.length <= 0) {
		document.getElementById('cotiza_nombre').innerHTML = mensaje;
        valido=0;
    }
    if(!mail(document.formularioCotiza.cotiza_email.value)) {
       document.getElementById('cotiza_email').innerHTML = mensaje;
        valido=0;
    }
	if(document.formularioCotiza.cotiza_telefono.value.length <= 0) {
        document.getElementById('cotiza_telefono').innerHTML = mensaje;
        valido=0;
    }
	if(document.formularioCotiza.cotiza_info.value.length <= 0) {
        document.getElementById('cotiza_info').innerHTML = mensaje;
        valido=0;
    }
	if(valido == 1) {
		enviarCotiza(formulario);
	} else
		document.getElementById('cotiza_advertencia').innerHTML='(*) Información Incompleta';
}


/********************LIMPIAR***********/
function limpiar(form) {
	document.getElementById(form).reset();
}

/***************************************VALIDACIONES*****************/
//validar grupo de checks
function validar_checks(formulario) {
	var cont = 0;
	for(i = 0; cnt = formulario.elements[i]; i++) {
		if (cnt.type=='checkbox') {
			if (cnt.checked) {
				cont++;
				break;
			}
		}
	}
	if (cont == 0)
		return false;
	else
		return true;
}

//Email
function mail(texto){

    var mailres = true;            
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
    
    var arroba = texto.indexOf("@",0);
    if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
    
    var punto = texto.lastIndexOf(".");
                
     for (var contador = 0 ; contador < texto.length ; contador++){
        if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
            mailres = false;
            break;
     }
    }

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
     mailres = true;
    else
     mailres = false;
                
    return mailres;
} 

