var ajaxForm
var inscripcionHecha = false
function cambioProvincia(provincia)
{
    var url = './traerPoblaciones.aspx?id=' + provincia.options[provincia.selectedIndex].value;
    ajax.url = url;
    ajax.post = ''
    ajax.f = "provinciasDescargadas()";
    ajax.error = "error()"
    ajax.run();
}

function provinciasDescargadas()                                                      
{
    var s = ajax.xmlDoc.responseText.trim();
    var a = s.split('@')
    var a2;
    var i = 0;
    document.forms['fDirector'].poblacion.length = 0;
    document.forms['fDirector'].poblacion.options[0] = new Option('Selecciona una población','000000');
    for (i = 0; i < a.length; i++)
    {
        a2 = a[i].split('|')
        document.forms['fDirector'].poblacion.options[i + 1] = new Option(a2[1],a2[0]);
    }
    document.forms['fDirector'].poblacion.selectedIndex = 0
}

function comprobarLongitud(nombre, id, l)
{
}

function comprobarLongitudActividad(nombre, id, l)
{
}

function inscribirse(form)
{
    if (!inscripcionHecha)
    {
        // Comprobamos que est&eacute;n todos los datos
        var mensajes = new Array();
        var mensaje = '';
        var i = 0;
        var chequeados = 0

        if (form.nombre_empresa.value == '')
            mensajes[mensajes.length] = 'Es obligatorio poner el nombre de la empresa.'

        if ((form.direccion_empresa.value == '') || (form.direccion_empresa.value.length < 5))
            mensajes[mensajes.length] = 'Es obligatorio poner la direcci&oacute;n de la empresa.'

        if (
        (form.cp.value.length < 5) || 
        (isNaN(form.cp.value))
        )
            mensajes[mensajes.length] = 'Es obligatorio poner el CP.'

        i = form.poblacion.selectedIndex	
        if (form.poblacion.options[i].value == '000000')
            mensajes[mensajes.length] = 'Es obligatorio seleccionar una poblaci&oacute;n.'

        if (
        (form.telefono.value.length < 9) ||
        (isNaN(form.telefono.value))
        )
            mensajes[mensajes.length] = 'Es obligatorio el n&uacute;mero de tel&eacute;fono.'

        if ((form.cif.value == '') || (form.cif.value.length < 5))
            mensajes[mensajes.length] = 'Es obligatorio poner el CIF de la empresa.'

        if (form.actividad.value.length == 0)
            mensajes[mensajes.length] = 'Es obligatorio describir la actividad de la empresa.'

        chequeados = 0
        for(i = 1; i < arrayProductos.length; i++)
        {
            if (eval('form.producto_' + arrayProductos[i] + '.checked'))
        {
            chequeados++
            break
            }
        }

        if (
        (chequeados == 0) && (form.productos_otros.value.length == 0)
        )
            mensajes[mensajes.length] = 'Hay que elegir un producto en el que tengas inter&eacute;s.'

        var participante1 = (form.nombre_participante_1.value.length > 3) && 
                (!isNaN(form.movil_participante_1.value)) &&
                (form.apellidos_participante_1.value.length > 3) &&
                (!isNaN(form.telefono_participante_1.value)) &&
                (form.cargo_participante_1.value.length > 3) &&
                ($CEmail(form.email_participante_1.value))
            
        var participante2 = (form.nombre_participante_2.value.length > 3) && 
                (!isNaN(form.movil_participante_2.value)) &&
                (form.apellidos_participante_2.value.length > 3) &&
                (!isNaN(form.telefono_participante_2.value)) &&
                (form.cargo_participante_2.value.length > 3) &&
                ($CEmail(form.email_participante_2.value))
                
        if (!participante1)
            mensajes[mensajes.length] = 'Se tienen que rellenar todos los datos del participante 1.'

        if (
        (form.nombre_participante_2.value.length > 3) || (form.apellidos_participante_2.value.length > 3)
        )
        {
            if (!participante2)
            mensajes[mensajes.length] = 'Se tienen que rellenar todos los datos del participante 2.'
        }

        for(i = 0; i < form.necesita_habitacion.length; i++)
        {
            if (eval('form.necesita_habitacion[' + i + '].checked'))
            {
                chequeados++
                break;
            }
        }	
        if (chequeados == 0)
            mensajes[mensajes.length] = 'Hay que elegir si necesita habitaci&oacute;n o no.'

        var obj = eval('form.necesita_habitacion[0]') 
        if (obj.checked)
        {
            // Necesita una habitacion. Vamos a ver si la ha seleccionado
            chequeados = 0
            for(i = 1; i < arrayDiasParticipacion.length; i++)
            {
                obj = eval('form.alojamiento_' + arrayDiasParticipacion[i])
                if (!isNaN(obj.value) && (obj.value.length > 0))
                {
                    chequeados++
                    break
                }
            }
            if (chequeados == 0)
                mensajes[mensajes.length] = 'Has puesto que necesitas habitaci&oacute;n y no has puesto el n&uacute;mero de ellas.'
        }
        
        /*if (
            (form.nombre_sustituto.value == '') || 
            (!$CEmail(form.email_sustituto.value)) || 
            (form.cargo_sustituto.value == '')            
            )
            mensajes[mensajes.length] = 'Hay que rellenar el nombre, email y cargo del sustituto.'*/
            
        if (!form.acepto_condiciones.checked)
            mensajes[mensajes.length] = 'Es imprescindible aceptar las condiciones del evento.'	

        if (mensajes.length > 0)	
        {
            for(i = 0; i < mensajes.length ; i++)
                mensaje += '<div style="float:left; margin: 5px 0 0 0;width: 100%">- ' + mensajes[i] + '</div>'
            var lMensajes = new Clayer();
            lMensajes.setAncho(400)
            lMensajes.setAlto((mensajes.length + 4) * 22)
            lMensajes.setMensaje(mensaje)
            lMensajes.open()
        }
        else
        {
			var url = serializeForm(form);
			$.ajax({
			   type: "POST",
			   url: "./inscripcionDirectores.aspx",
			   data: url,
			   success: function(msg){
				 inscripcionRealizada(msg);
			   },
			   error : function()
			   {
				error();
				}
			 });
        }
    }
    else $aviso("Lo sentimos. Ya has realizado tu inscripci&oacute;n")
}

function comprobarDatos(form)
{
    var cabeceras = new Array()
    var valores = new Array();
    var mensaje = ""
	var chequeados = 0
	var s = '';
	var ancho1 = 0;
	var ancho2 = 0;
  
  	cabeceras[cabeceras.length] = "1. DATOS DE LA EMPRESA"
    valores[valores.length] = '';
  
    cabeceras[cabeceras.length] = "Empresa: "
    valores[valores.length] = (form.nombre_empresa.value == '')?'No especificada':form.nombre_empresa.value;
    
    cabeceras[cabeceras.length] = "Direcci&oacute;n: "
    valores[valores.length] = (form.direccion_empresa.value == '')?'No especificada':form.direccion_empresa.value;
    
    cabeceras[cabeceras.length] = "CP: "
    valores[valores.length] = (form.cp.value == '')?'No especificado':form.cp.value;
    
    cabeceras[cabeceras.length] = "Provincia: "
    valores[valores.length] = (form.provincia[form.provincia.selectedIndex].value == "00")?'No seleccionada':form.provincia.options[form.provincia.selectedIndex].text;
    
    cabeceras[cabeceras.length] = "Poblaci&oacute;n: "
    valores[valores.length] = (form.poblacion[form.poblacion.selectedIndex].value == "00000")?'No seleccionada':form.poblacion.options[form.poblacion.selectedIndex].text;
	
	cabeceras[cabeceras.length] = "Tel&eacute;fono: "
    valores[valores.length] = (form.telefono.value == '')?'No especificado':form.telefono.value;
	
	cabeceras[cabeceras.length] = "Fax: "
    valores[valores.length] = (form.fax.value == '')?'No especificado':form.fax.value;
    
    cabeceras[cabeceras.length] = "Cif: "
    valores[valores.length] = (form.cif.value == '')?'No especificado':form.cif.value;
    
    cabeceras[cabeceras.length] = "Grupo: "
    valores[valores.length] = (form.grupo.value == '')?'No especificado':form.grupo.value;
    
    cabeceras[cabeceras.length] = "Web: "
    valores[valores.length] = (form.web.value == '')?'No especificado':form.web.value;
    
    cabeceras[cabeceras.length] = "Email: "
    valores[valores.length] = (form.email.value == '')?'No especificado':form.email.value;
	
	cabeceras[cabeceras.length] = "2. DESCRIPCI&Oacute;N DE LA ACTIVIDAD"
    valores[valores.length] = '';
	
	cabeceras[cabeceras.length] = "Actividad: "
    valores[valores.length] = (form.actividad.value == '')?'No especificada':form.actividad.value;
	
	
	cabeceras[cabeceras.length] = "3. CIFRAS"
    valores[valores.length] = '';
	
	chequeados = -1
	for(i = 0; i < form.id_facturacion.length; i++)
    {
        obj = form.id_facturacion[i]
        if (obj.checked)
        {
            chequeados = obj.value
            break;
        }
    }
	
	cabeceras[cabeceras.length] = "Facturaci&oacute;n: "
    valores[valores.length] = (chequeados == -1)?'No proporcionada':$('#facturacion_' + chequeados).html()
	
	chequeados = -1
	for(i = 0; i < form.id_trabajadores.length; i++)
    {
        if (eval('form.id_trabajadores[' + i + '].checked'))
        {
            chequeados = eval('form.id_trabajadores[' + i + '].value')
            break;
        }
    }
	
	cabeceras[cabeceras.length] = "Nº de trabajadores: "
    valores[valores.length] = (chequeados == -1)?'No proporcionado':$('#trabajadores_' + chequeados).html()
	
	cabeceras[cabeceras.length] = "4. ACCIONES PREVISTAS"
    valores[valores.length] = '';
	
	cabeceras[cabeceras.length] = "Acci&oacute;n 1: "
    valores[valores.length] = (form.accion1.value == '')?'No especificada':form.accion1.value;
	
	cabeceras[cabeceras.length] = "Acci&oacute;n 2: "
    valores[valores.length] = (form.accion2.value == '')?'No especificada':form.accion2.value;
	
	cabeceras[cabeceras.length] = "Acci&oacute;n 3: "
    valores[valores.length] = (form.accion3.value == '')?'No especificada':form.accion3.value;
	
	cabeceras[cabeceras.length] = "Acci&oacute;n 4: "
    valores[valores.length] = (form.accion4.value == '')?'No especificada':form.accion4.value;
	
	cabeceras[cabeceras.length] = "5. PODUCTOS/SERVICIOS QUE DEMANDA"
    valores[valores.length] = '';
	
	s = ''
	chequeados = 0
	for(i = 1; i < arrayProductos.length; i++)
	{
		if (eval('form.producto_' + arrayProductos[i] + '.checked'))
		{
			//s += ((s == '')?'':', ') + $('#desc_producto_' + arrayProductos[i]).html()
			chequeados++
			cabeceras[cabeceras.length] = $('#desc_producto_' + arrayProductos[i]).html() + ':'
            valores[valores.length] = eval("form.descripcion_producto_" + arrayProductos[i]).value
		}
	}
	
	//s += ((s == '')?'':', ') + form.productos_otros.value
	if (form.productos_otros.value != '')
	{
	    cabeceras[cabeceras.length] = 'Otros productos: '
        valores[valores.length] = form.productos_otros.value
        chequeados++
	}
	if (chequeados == 0)
	{	
	    cabeceras[cabeceras.length] = ""
        valores[valores.length] = 'No hay seleccionado ning&uacute;n producto'
    }
	
	cabeceras[cabeceras.length] = "6. PARTICIPANTES"
    valores[valores.length] = '';
	
	cabeceras[cabeceras.length] = "Nombre participante 1:"
    valores[valores.length] = (form.nombre_participante_1.value == '')?'No especificado':form.nombre_participante_1.value;
	
	cabeceras[cabeceras.length] = "M&oacute;vil participante 1:"
    valores[valores.length] = (form.movil_participante_1.value == '')?'No especificado':form.movil_participante_1.value;
	
	cabeceras[cabeceras.length] = "Apellidos participante 1:"
    valores[valores.length] = (form.apellidos_participante_1.value == '')?'No especificado':form.apellidos_participante_1.value;
	
	cabeceras[cabeceras.length] = "Tel6eacute;fono participante 1:"
    valores[valores.length] = (form.telefono_participante_1.value == '')?'No especificado':form.telefono_participante_1.value;
	
	cabeceras[cabeceras.length] = "Cargo participante 1:"
    valores[valores.length] = (form.cargo_participante_1.value == '')?'No especificado':form.cargo_participante_1.value;
	
	cabeceras[cabeceras.length] = "Email participante 1:"
    valores[valores.length] = (form.email_participante_1.value == '')?'No especificado':form.email_participante_1.value;
	
	cabeceras[cabeceras.length] = "Nombre participante 2:"
    valores[valores.length] = (form.nombre_participante_2.value == '')?'No especificado':form.nombre_participante_2.value;
	
	cabeceras[cabeceras.length] = "M&oacute;vil participante 2:"
    valores[valores.length] = (form.movil_participante_2.value == '')?'No especificado':form.movil_participante_2.value;
	
	cabeceras[cabeceras.length] = "Apellidos participante 2:"
    valores[valores.length] = (form.apellidos_participante_2.value == '')?'No especificado':form.apellidos_participante_2.value;
	
	cabeceras[cabeceras.length] = "Tel6eacute;fono participante 2:"
    valores[valores.length] = (form.telefono_participante_2.value == '')?'No especificado':form.telefono_participante_2.value;
	
	cabeceras[cabeceras.length] = "Cargo participante 2:"
    valores[valores.length] = (form.cargo_participante_2.value == '')?'No especificado':form.cargo_participante_2.value;
	
	cabeceras[cabeceras.length] = "Email participante 2:"
    valores[valores.length] = (form.email_participante_2.value == '')?'No especificado':form.email_participante_2.value;
    
    cabeceras[cabeceras.length] = "7. D&Iacute;AS DE PARTICIPACI&Oacute;N"
    valores[valores.length] = '';
    
    chequeados = -1
	for(i = 0; i < form.dias_participacion.length; i++)
    {
        if (eval('form.dias_participacion[' + i + '].checked'))
        {
            chequeados = eval('form.dias_participacion[' + i + '].value')
            break;
        }
    }
    
    cabeceras[cabeceras.length] = "Participaci&oacute;n: "
    valores[valores.length] = $('#participacion_' + chequeados).html();
    
    cabeceras[cabeceras.length] = "8. SU PARTICIPACI&Oacute;N COMO INVITADO INCLUYE"
    valores[valores.length] = '';
    
    s = ''
    if (form.necesita_habitacion[0].checked)
        s = 'Si'    
    else if (form.necesita_habitacion[1].checked)
        s ='No'
    else s = 'No especificado'
        
    cabeceras[cabeceras.length] = "Necesita habitaci&oacute;n: "
    valores[valores.length] = s;
    
    cabeceras[cabeceras.length] = "9. OPCIONES ADICIONALES: ALOJAMIENTO"
    valores[valores.length] = '';
    
    s = ''
    chequeados = 0
    for(i = 1; i < arrayDiasParticipacion.length; i++)
    {
        obj = eval('form.alojamiento_' + arrayDiasParticipacion[i])
        if (!isNaN(obj.value) && (obj.value.length > 0))
        {
            chequeados++
	        cabeceras[cabeceras.length] = $('#texto_alojamiento_' + arrayDiasParticipacion[i]).html() + ': '
            valores[valores.length] = obj.value
         }
    }
    if (chequeados == 0)
    {
        cabeceras[cabeceras.length] = 'Necesita alojamiento: '
        valores[valores.length] = 'No'
    }
    
    cabeceras[cabeceras.length] = "10. SUSTITUTO, NOMBRE Y FECHA"
    valores[valores.length] = '';
    
    cabeceras[cabeceras.length] = "Nombre:"
    valores[valores.length] = (form.nombre_sustituto.value == '')?'No especificado':form.nombre_sustituto.value;
    
    cabeceras[cabeceras.length] = "Cargo:"
    valores[valores.length] = (form.cargo_sustituto.value == '')?'No especificado':form.cargo_sustituto.value;
    
    cabeceras[cabeceras.length] = "Email:"
    valores[valores.length] = (form.email_sustituto.value == '')?'No especificado':form.email_sustituto.value;
    
    cabeceras[cabeceras.length] = "DNI:"
    valores[valores.length] = (form.dni_sustituto.value == '')?'No especificado':form.dni_sustituto.value;
    
    cabeceras[cabeceras.length] = "Lugar y fecha:"
    valores[valores.length] = (form.lugaryfecha_sustituto.value == '')?'No especificado':form.lugaryfecha_sustituto.value;
    
    cabeceras[cabeceras.length] = "Aceptación de condiciones:"
    valores[valores.length] = (form.acepto_condiciones.checked)?'Has aceptado las condiciones':'No has aceptato las condiciones';
    
    for(i = 0; i < cabeceras.length; i++)
    {
		if (valores[i] != '')
		{
			if (cabeceras[i] == '')
			{
				ancho1 = '0'
				ancho2 = '590'
			}
			else
			{
				ancho1 = '310'
				ancho2 = '295'
			}
			mensaje += '<div style="float:left; margin-bottom: 4px">'
        	mensaje += "<div style='float:left; width: " + ancho1 + "px'>" + cabeceras[i] + "</div>" + "<div style='float:left; width: " + ancho2 + "px'>" + valores[i] + "</div>"
        	mensaje +="</div>"
		}
		else
		{
			s = cabeceras[i];
			
			mensaje += "<div style='float:left; width: 590px; text-align: left; margin: 3px; 0px' class='font14 bold'>" + s + "</div>"
		}
    }
    
    var lMensajes = new Clayer();
    lMensajes.setAncho(720)
    lMensajes.setAlto(600)
    lMensajes.setTitulo("Comprobaci&oacute;n de datros introducidos")
    mensaje = "<div style='float:left; width: 700px; overflow:auto; height: 500px'>" + mensaje + "</div>"
    lMensajes.setMensaje(mensaje)
    lMensajes.open()
}

function serializeForm(form)
{
    //var i = 0;
    var s = '';
	
	s = $('#fDirector').serialize() + '&aProductos=' + arrayProductos
    
    return s
}

function error()
{
    $CloseLoading("Incribiendo...")
    $aviso("Ha habido un error realizando su inscripción.<br> Int&eacute;ntelo m&aacute;s tarde por favor")
}

function inscripcionRealizada(msg)
{
    var mensaje = '';
    $CloseLoading()
    if (msg.toLowerCase() == 'ok')
    {
        mensaje = 'Sus solicitud de inscripci&oacute;n a BusinessEvent&#8482; ha sido registrada.<br><br>'
        mensaje += 'Un asesor de eventos se pondr&aacute; en contacto con Ud. para confirmar su solicitud.<br><br>'
        mensaje += 'Puede ponerse en contacto con nosotros para resolver cualquier duda en el 902 93 18 18.<br><br>'
        mensaje += '&iexcl;Gracias por su confianza!'
        
        inscripcionHecha = true
        var c = new Clayer();
        c.setAlto(200)
        c.setAncho(450)
        c.setMensaje(mensaje);
        c.open()
    }
    else
    {
        mensaje = "Ha habido un error realizando su inscripci&oacute;n.<br> Int&eacute;ntelo m&aacute;s tarde por favor o póngase en contacto con el 902 93 18 18."
        $aviso(mensaje)
    }
}
String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g,'')}
