function getBaseHref() {
	
	return document.getElementsByTagName ("BASE")[0].href;
}

function sendContact(){
	
	document.getElementById('error_nombre_contacto').style.display="none";
	document.getElementById('error_surname_contacto').style.display="none";
	document.getElementById('error_consulta_contacto').style.display="none";
	document.getElementById('error_mail_contacto').style.display="none";
	document.getElementById('error_format_mail_contacto').style.display="none";
	document.getElementById('error_envio_contacto').style.display="none";
	document.getElementById('error_datos_contacto').style.display="none";
	document.getElementById('envio_ok_contacto').style.display="none";
	document.getElementById('error').style.display="none";
	document.getElementById('error2').style.display="none";
	
	var ok=true;
	var _name = document.getElementById('name').value;
	var _surname = document.getElementById('surname').value;
	var _surname2 = document.getElementById('surname2').value;
	var _mail = document.getElementById('mail').value;
	var _phone = document.getElementById('phone').value;
	var _comment = document.getElementById('comment').value;
	var _town = document.getElementById('town').value;
	var datos = document.getElementById('datos');
	
	if(_comment.length == 0){
		document.getElementById('error_consulta_contacto').style.display="block";
		document.getElementById('comment').focus();
		document.getElementById('error').style.display="table-row";
		document.getElementById('error2').style.display="table-row";
		ok=false;
	}
	
	if(_mail.length == 0){
		document.getElementById('error_mail_contacto').style.display="block";
		document.getElementById('mail').focus();
		document.getElementById('error').style.display="table-row";
		document.getElementById('error2').style.display="table-row";
		ok=false;
	}
	
	if(!cw_es_email(document.getElementById('mail'))){
		document.getElementById('error_format_mail_contacto').style.display="block";
		document.getElementById('mail').focus();
		document.getElementById('error').style.display="table-row";
		document.getElementById('error2').style.display="table-row";
		ok=false;
	}
	
	if(_surname.length == 0){
		document.getElementById('error_surname_contacto').style.display="block";
		document.getElementById('surname').focus();
		document.getElementById('error').style.display="table-row";
		document.getElementById('error2').style.display="table-row";
		ok=false;
	}
	
	if(_name.length == 0){
		document.getElementById('error_nombre_contacto').style.display="block";
		document.getElementById('name').focus();
		document.getElementById('error').style.display="table-row";
		document.getElementById('error2').style.display="table-row";
		ok=false;
	}
	
	
	if(!datos.checked){
		document.getElementById('error_datos_contacto').style.display="block";
		document.getElementById('error').style.display="table-row";
		document.getElementById('error2').style.display="table-row";
		ok=false;
	}
	
	if(ok){
		$.ajax({
	           url: "sendContact.php",
	           type: "POST",
	           dataType: "text",
	           data: {
	           		name: _name,
					surname: _surname,
					surname2: _surname2,
					mail: _mail,
					town: _town,
					phone: _phone,
					comment : _comment
	           },
	           error: function(req, err, obj) {
	           	alert('error');
	            alert(err);
	           },
	           success: function(txt) {
	       		if (txt == "OK"){
	       			document.getElementById('envio_ok_contacto').style.display="table-row";
	 
	       			document.getElementById('name').setAttribute("disabled","true");
					document.getElementById('surname').setAttribute("disabled","true");
					document.getElementById('surname2').setAttribute("disabled","true");
					document.getElementById('mail').setAttribute("disabled","true");
					document.getElementById('phone').setAttribute("disabled","true");
					document.getElementById('town').setAttribute("disabled","true");
					document.getElementById('comment').setAttribute("disabled","true");
					document.getElementById('datos').setAttribute("disabled","true");
					document.getElementById('send').setAttribute("onclick","return false;");
	       			document.getElementById('error2').style.display="table-row";
	       		}else{
	       			document.getElementById('error_envio_contacto').style.display="block";
	       			document.getElementById('error2').style.display="table-row";
	       			return false;
	       		}
	       		
	           }
	        });	
	}
        
      return false;
}



function sendInscription(){

	//alert("Entro");
	
	document.getElementById('error_nombre_insc').style.display="none";
	
	document.getElementById('error_surnames_insc').style.display="none";
	
	document.getElementById('error_mail_insc').style.display="none";
	
	document.getElementById('error_format_mail_insc').style.display="none";
	document.getElementById('error_phone_insc').style.display="none";
	document.getElementById('error_dni_insc').style.display="none";
	document.getElementById('error_dni_format_insc').style.display="none";
	
	document.getElementById('error_envio_insc').style.display="none";
	document.getElementById('error_datos_insc').style.display="none";
	//document.getElementById('error_datos_envio').style.display="none";
	document.getElementById('error_envio_insc').style.display="none";
	
	//document.getElementById('error_cliente_insc').style.display="none";
	document.getElementById('error_existe_insc').style.display="none";
	document.getElementById('error').style.display="none";
	document.getElementById('error2').style.display="none";
	

	var ok = true;
	var _origin = document.getElementById('origin').value;
	var _cod_profim = document.getElementById('cod_profim').value;
	var _name = document.getElementById('name').value;
	var _surnames = document.getElementById('surnames').value;
	var _mail = document.getElementById('mail').value;
	var _phone = document.getElementById('phone').value;
	var _dni = document.getElementById('dni').value;
	//var _cliente = document.getElementById('cliente').value;
	var _fk_event = document.getElementById('fk_event').value;
	var _show_eafi = document.getElementById('show_eafi').value;
	var datos = document.getElementById('datos');
	var _status = document.getElementById('status').value;
	
	/* variables addpepper */
	//cid=111111&sid=22222&wid=33333&swid=abc123
	var _cid = document.getElementById('cid').value;
	var _sid = document.getElementById('sid').value;
	var _wid = document.getElementById('wid').value;
	var _swid = document.getElementById('swid').value;
	
	//var radio = document.inscriptionForm.cliente;
	//var _cliente;
	//alert("entra");	
	/*
	for (var i=0; i < radio.length; i++){
	   if (radio[i].checked)
	      	_cliente = radio[i].value;
	}
	*/
	if(!datos.checked){
		document.getElementById('error_datos_insc').style.display="block";
		document.getElementById('error').style.display="";
		ok=false;
	}
	
	/*
	if(_cliente!= '0' && _cliente!="1"){
		document.getElementById('error_cliente_insc').style.display="block";
		document.getElementById('error').style.display="table-row";
		ok=false;
	}*/
	if(_mail.length == 0){
		document.getElementById('error_mail_insc').style.display="block";
		document.getElementById('error').style.display="";
		document.getElementById('mail').focus();
		ok=false;
	}else{
	
		if(!cw_es_email(document.getElementById('mail'))){
			document.getElementById('error_format_mail_insc').style.display="block";
			document.getElementById('mail').focus();
			ok=false;
		}
	}
	
	if(_phone.length == 0){
		document.getElementById('error_phone_insc').style.display="block";
		document.getElementById('phone').focus();
		ok=false;
	}
	
	if(_dni.length == 0){
		document.getElementById('error_dni_insc').style.display="block";
		document.getElementById('dni').focus();
		ok=false;
	}else{
	
		if(!nif(_dni)){
			document.getElementById('error_dni_format_insc').style.display="block";
			document.getElementById('dni').focus();
			ok=false;
		}
	}
	
	if(_surnames.length == 0){
		document.getElementById('error_surnames_insc').style.display="block";
		document.getElementById('surnames').focus();
		ok=false;
	}
	
	if(_name.length == 0){
		document.getElementById('error_nombre_insc').style.display="block";
		document.getElementById('name').focus();
		ok=false;
	}
	
	
	

	if(ok){
	
	
	$.ajax({
           url: getBaseHref()+"sendInscription.php",
           type: "POST",
           dataType: "text",
           data: {
           		name: _name,
				surnames: _surnames,
				phone: _phone,
				mail: _mail,
				dni: _dni, 
				/*cliente: _cliente,*/
				status: _status,
				show_eafi: _show_eafi,
				fk_event : _fk_event
           },
           error: function(req, err, obj) {
           	alert('error');
            alert(err);
           },
           success: function(txt) {
       		if (txt == "OK"){
       			if (_cod_profim != ''){
       				profimCheck(_fk_event,_cod_profim,_dni,_origin);
       				
       				// Pixel de control tag Centrocom
       				if (_origin=='ctc'){
       					var dni_mod = _dni.substring(_dni.length - 5);
       					parent.frame_cpc.location.href = "http://www.netfilia.com/advertiser/Conceptdata/savedata/?conceptid=2640&m=1&source="+dni_mod;
       					//document.getElementById('datos').setAttribute("disabled","true");
       				}
       				
					// Pixel de control tag Adpepper
       				if (_cid.length>0){
       					
       					parent.apm.location.href = "http://ilead.itrack.it/clients/ext.aspx?targetpage=signup&openpopup=0&cid="+_cid+"&sid="+_sid+"&wid="+_wid+"&swid="+_swid;
       				}
       			}
       				
       			document.getElementById('envio_ok_insc').style.display="block";
       			document.getElementById('name').setAttribute("disabled","true");
				document.getElementById('surnames').setAttribute("disabled","true");
				document.getElementById('mail').setAttribute("disabled","true");
				document.getElementById('phone').setAttribute("disabled","true");
				document.getElementById('dni').setAttribute("disabled","true");
				/*for (var i=0; i < radio.length; i++){
				   radio[i].setAttribute("disabled","true");
				}*/
				document.getElementById('send').setAttribute("onclick","return false;");
				document.getElementById('datos').setAttribute("disabled","true");
				document.getElementById('error2').style.display="";
       		}if (txt == "OK_RESERVA"){
       			/* PROFIM */
       			if (_cod_profim != ''){
       				// Pixel de control tag Centrocom
       				       				
       				profimCheck(_fk_event,_cod_profim,_dni,_origin);
       				
       				if (_origin=='ctc'){
       					var dni_mod = _dni.substring(_dni.length - 5);
       					parent.frame_cpc.location.href = "http://www.netfilia.com/advertiser/Conceptdata/savedata/?conceptid=2640&m=1&source="+dni_mod;
       				}
       				if (_cid.length>0){
       					
       					parent.apm.location.href = "http://ilead.itrack.it/clients/ext.aspx?targetpage=signup&openpopup=0&cid="+_cid+"&sid="+_sid+"&wid="+_wid+"&swid="+_swid;
       				}
       			}
       			
       			
       			document.getElementById('envio_ok_insc_reserva').style.display="block";
       			document.getElementById('name').setAttribute("disabled","true");
				document.getElementById('surnames').setAttribute("disabled","true");
				document.getElementById('mail').setAttribute("disabled","true");
				document.getElementById('phone').setAttribute("disabled","true");
				document.getElementById('dni').setAttribute("disabled","true");
				/*for (var i=0; i < radio.length; i++){
				   radio[i].setAttribute("disabled","true");
				}*/
				document.getElementById('send').setAttribute("onclick","return false;");
				document.getElementById('datos').setAttribute("disabled","true");
				document.getElementById('error2').style.display="";
       		}else if(txt == "ERROR_EXISTE"){
       			document.getElementById('error').style.display="";
       			document.getElementById('error_existe_insc').style.display="block";
       		}else{
       			document.getElementById('error_envio_insc').style.display="block";
       			document.getElementById('error2').style.display="";
       			return false;
       		}
           }
        });	
	}else{
		document.getElementById('error').style.display="";
		return false;
	}
	
        
      return false;
}

function profimCheck (id,code,dni,origin){
	
	var id_insc = "";
	if (origin != '')
		id_insc = origin+"_";

	var dni_mod = dni.substring(dni.length - 5);
		
	id_insc = id_insc + id + "_" +dni_mod;
	//alert (id_insc);
	WBO_AMOUNT="0.0"; /* <== set here the command amount */
	WBO_CLIENT="";     /* <== set here your client id */
	WBO_INVOICE="";     /* <== set here your bill id */
	WBO_NUM_ITEMS=1; /* <== set here the number of item */
	WBO_POST_VALIDATION=0; /* <== set to 1 if need backoffice validation */
	
	if (webo_performance) {
		transfo = new performanceTransfo(SITE=454585, WBO_ID_TRANSFO=code);
		transfo.setAmount(WBO_AMOUNT);
		transfo.setClient(WBO_CLIENT);
		transfo.setId(WBO_INVOICE);
		transfo.setQuantity(WBO_NUM_ITEMS);
		transfo.setPostValidation(WBO_POST_VALIDATION);
		transfo.setHost("profimes.solution.weborama.fr");
		
		/* == Optional parameters == */
		transfo.addParam("CodigodelaCreatividad", id_insc);
		/* == Type parameters == */
		/* == Free parameters == */
		transfo.execute();
	}

}

// COMPROBACI�N DE MAIL (COPIA DE validateForm.js)

 function cw_esta_vacio(variable) { 
        if((variable == null) || (variable.length == 0) || (variable.value == "")) { 
          return true; 
        }
        return false;
    }
    
   function cw_tiene_espacios (variable) {   
       var espacio = " \n\t\r";
       var i;
       for (i = 0; i < variable.value.length; i++) {   
           var car = variable.value.substring (i, i+1);
           if (espacio.indexOf(car) != -1) return true;
       }
       return false;
   }

   function cw_es_email (address) {   

       if(cw_esta_vacio(address)) return false; 
       if(cw_tiene_espacios(address)) return false;
       
       var i = 1;
       var addressLength = address.value.length;
       var fuera = false;
       var car = "";

       // Mirando por la @
       while (!fuera) {
         if(i < addressLength) {
         car =  address.value.substring (i, i+1);
             if(car == "@") fuera=true;
         } else { fuera = true }
         i++;
       } 

       if (i >= addressLength) { return false; } 
       if (car != "@") { return false; } 
       i++;

       // Mirando por el .
       fuera = false;
       car = "";
       while (!fuera) {
         if(i < addressLength) {
             car =  address.value.substring (i, i+1);
             if(car == ".") fuera=true;
         } else { fuera = true }
         i++;
       } 
       if (i >= addressLength) { return false; } 
       if (car != ".") { return false; } 
       return true;
   }
   
   
   function nif(dni){
	  var numero = dni.substr(0,dni.length-1);
	  var let = dni.substr(dni.length-1,1);
	  numero = numero % 23;
	  var letra='TRWAGMYFPDXBNJZSQVHLCKET';
	  var letra2='trwagmyfpdxbnjzsqvhlcket';
	  letra=letra.substring(numero,numero+1);
	  letra2=letra2.substring(numero,numero+1);
	  if (letra!=let && letra2!=let) {
	    return false;
	  }
	  return true;
   }

function checkfecha(date){
	document.getElementById('error_date_2').style.display="none";
	var start_date=document.getElementById(date).value;
	var ok=true;
	//alert(start_date);
	if(!valiDate(start_date)){
		//alert("fecha invalida");
		document.getElementById('error_date_2').style.display="block";
		document.getElementById(date).focus();
		ok=false;
	}	
	return ok;
}
function teclaPresionada(e){
	if(window.event){
	    keynum = e.keyCode
	}else if(e.which){
	    keynum = e.which
	}
	return keynum;
}

function checkSearchDate(){
	document.getElementById('error_date_1').style.display="none";
	var start_date=document.getElementById('initdate').value;
	var end_date=document.getElementById('search_fecha_fin').value;
	
	var ok=true;
	if((start_date=='' || start_date=='dd/mm/aaaa') && (end_date=='' || end_date=='dd/mm/aaaa')){
		document.getElementById('error_date_1').style.display="block";
		ok=false;
	}	
	if(ok){
		//alert("entra:"+document.filtro_fecha.search_fecha_inicio.value+" FIN: "+end_date);			
		document.getElementById('filtro_fecha').submit();
	}
	else{	
	}
}

function valiDate(Cadena){  
    var Fecha= new String(Cadena)   //Crea un string  
    var RealFecha= new Date()   //Para sacar la fecha de hoy  
    // Cadena A�o  
    var Ano= new String(Fecha.substring(Fecha.lastIndexOf("/")+1,Fecha.length))  
    // Cadena Mes  
    var Mes= new String(Fecha.substring(Fecha.indexOf("/")+1,Fecha.lastIndexOf("/")))  
    // Cadena D�a  
    var Dia= new String(Fecha.substring(0,Fecha.indexOf("/")))  
    var ok= true;
    // Valido el a�o  
    if (isNaN(Ano) || Ano.length<4 || parseFloat(Ano)<1900){  
        ok=false;
    }  
    // Valido el Mes  
    if (isNaN(Mes) || parseFloat(Mes)<1 || parseFloat(Mes)>12){  
        ok=false;  
    }  
    // Valido el Dia  
    if (isNaN(Dia) || parseInt(Dia, 10)<1 || parseInt(Dia, 10)>31){  
        ok=false;
    }
    if (Mes==4 || Mes==6 || Mes==9 || Mes==11 || Mes==2) {  
        if (Mes==2 && Dia > 28 || Dia>30) {  
        	ok=false;
        }  
    }  
	return ok;
}  

function showFilters(_action){
        if(_action == '1'){
        $("span#minimize").css({display:"block"});
        $("span#expand").css({display:"none"});

        $("div#FILTROS").css({display:"block"});
        }
        else{
        $("span#minimize").css({display:"none"});
        $("span#expand").css({display:"block"});
        $("div#FILTROS").css({display:"none"});
        }
    return false;

}


function getCalendar(_cyear,_cmonth){
	var ok = true;
	/*var _cyear = document.getElementById('cyear').value;
	var _cmonth = document.getElementById('cmonth').value;	*/
    var data = {cmonth: _cmonth,
                cyear: _cyear};		
	$.ajax({
           url: "getCalendar.php",
           type: "POST",
           dataType: "xml",
           //dataType: "text",
           data: data,
           error: function(req, err, obj) {
            alert("ERROR: "+err+" REQ: "+req+" OBJ: "+obj);
           },
           success: function(xml) {
                //alert(xml); return;
                var res = $("res",xml).text();
                var elems = $("elems",xml).text();
                var num_elems = $("num_elems",xml).text();
                var year_actual = $("year_actual",xml).text();
                var previous_year = $("previous_year",xml).text();
                var next_year = $("next_year",xml).text();
                var mounth_actual_text = $("mounth_actual_text",xml).text();
                var mounth_actual = $("mounth_actual",xml).text();
                var previous_month_value_mounth = $("previous_month_value_mounth",xml).text();
                var previous_month_value_year = $("previous_month_value_year",xml).text();
                var next_month_value_mounth = $("next_month_value_mounth",xml).text();
                var next_month_value_year = $("next_month_value_year",xml).text();
                
                    if (res=="OK"){
                        var num;
                        var color;
                        var link;
                        var primero;
                        var ultimo;
                        var Calendar='';
                        //alert("entra");
                        $("#calendar_this").html('');
                        Calendar=Calendar+'<table class="tab_calendar" id="test" cellpadding="0" cellspacing="0" align="center" \>'+
                        '<tr valign="middle" id="tab_calendar_year">'+
			            '    <td colspan="7" height="30"><a href=\"\" onclick=\"getCalendar(\''+previous_year+'\',\''+mounth_actual+'\');return false;\">'+previous_year+'</a> \<\<  <strong>'+year_actual+'</strong> \>\> <a href=\"\" onclick=\"getCalendar(\''+next_year+'\',\''+mounth_actual+'\');return false;\">'+next_year+'</a>'+
			            '    </td>'+
			            '</tr>'+
			            '<tr valign="middle" id="tab_calendar_month">'+
			            '    <td colspan="7">'+
			            '	 <a onclick="getCalendar(\''+previous_month_value_year+'\',\''+previous_month_value_mounth+'\');return false;" href="#">\<\<</a>'+
			            '	 <strong>'+mounth_actual_text+'</strong>  '+
			            '    <a onclick="getCalendar(\''+next_month_value_year+'\',\''+next_month_value_mounth+'\');return false;" href="#">\>\></a></td>'+
			            '</tr>'+
			            '<tr valign="middle" id="tab_calendar_day">'+
			            '    <td>L</td>'+
			            '    <td>M</td>'+
			            '    <td>Mi</td>'+
			            '    <td>J</td>'+
			            '    <td>V</td>'+
			            '    <td>S</td>'+
			            '    <td>D</td>'+
			            '</tr>';         
                        $("elems", xml).each(function() {
                            num = $("num",this).text();
                            color = $("color",this).text();
                            link = $("link",this).text();                       	
                            primero = $("primero",this).text();                       	
                            ultimo = $("ultimo",this).text();
                        	//alert("NUM :"+num+" . COLOR: "+color+" LINK: "+link+" PRIMERO: "+primero+" ultimo:"+ultimo);                   
                            if(primero == '1'){
                            	Calendar=Calendar+'\<tr valign="middle"\>';	
                            }
                        	
                        	if(color != ''){
                        		Calendar=Calendar+'\<td class="'+color+'" \>';
                        	}else{
                        		Calendar=Calendar+'\<td\>';	
                        	}
                        	if(link != ''){
                        		Calendar=Calendar+'\<a href=\"'+link+'\" \>';
                        	}   
                        	Calendar=Calendar+num;
                         	if(link != ''){
                        		Calendar=Calendar+'\<\/a\>';
                        	}  
                       		Calendar=Calendar+'\<\/td\>';                        	
                         	if(ultimo != ''){
                        		Calendar=Calendar+'\<\/tr\>';
 									
                        	} 

                        	//alert($("#calendar_this").text());                    	   
                        /*
		            <tr valign="middle" id="tab_calendar_month">
		                <td colspan="7"><a href="{previous_month_link}">{txt_previous_month}</a> 
		                	&nbsp; <strong>{txt_current_month}</strong> &nbsp; 
		                	<a href="{next_month_link}">{txt_next_month}</a></td>
		            </tr>                        
		            <!-- IF calendario.PRIMERO == '1' --><!-- ENDIF -->
		            	<td <!-- IF calendario.COLOR != '' -->class="{calendario.COLOR}"<!-- ENDIF --><!-- IF calendario.TITLE != '' --> title="{calendario.TITLE}"<!-- ENDIF -->><!-- IF calendario.LINK != '' --><a href='{calendario.LINK}'><!-- ENDIF -->{calendario.NUM}<!-- IF calendario.LINK != '' --></a><!-- ENDIF --></td>
		            <!-- IF calendario.ULTIMO == '1' --></tr><!-- ENDIF -->                        
                        */	
                        })
						//alert(Calendar);  
                        $("#calendar_this").html(Calendar);
                           //alert('ok');
                           //self.location.href="curriculum_publicaciones.html";
                           //self.location.reload(true);
                           return true;
                       }else{
                           //alert("error");
                           alert("erro:"+res);
                           return false;
                       }
                //self.location.href="index.html";
           }
        });
	
        
      return false;
}
