/////////////////////////////////////////////////////////////////////////////////
///////																	  ///////
///////                      FUNCIONES PROPIAS                            ///////
///////                                                                   ///////
/////////////////////////////////////////////////////////////////////////////////


function calendario(input,boton){
	
	
	 					Calendar.setup({
        										inputField     :    input,
        										ifFormat       :    "%d/%m/%Y",
        										button         :    boton,
											 	showsTime      :    true,
        										timeFormat     :    "24"
    					});
    												
    												


}


function validar(){
	
	var s = document.getElementById('suscribir').value;
	
	//var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	
	var filter = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	
	//if (s.length == 0 ) return true;
		
	if (filter.test(s)){
			
			//return true;
			document.getElementById('suscribirnewsletter').submit();
					
	}else{
			alert("Ingrese una dirección de correo válida");
			document.getElementById('suscribir').focus();
			
			return false;
	}

}


function validaradhesion(){
	
	
	
	//if(document.getElementById('ref').value == ""){
		
	//		alert("Ingrese una Referencia");
	//		document.getElementById('ref').focus();
	//		return false;
	
	//}
	
	if(document.getElementById('nombre').value == ""){
		
			alert("Ingrese un nombre de la empresa");
			document.getElementById('nombre').focus();
			return false;
	
	}
	
	
	if(document.getElementById('foto1').value == ""){
		
			alert("Debes insertar un logo de la empresa");
			document.getElementById('foto1').focus();
			return false;
	
	}
	
	//foto1
	
	if(document.getElementById('direccion').value == ""){
		
			alert("Ingrese una dirección");
			document.getElementById('direccion').focus();
			return false;
	
	}
	
	
	if(document.getElementById('cifnif').value == ""){
		
			alert("Ingrese un cif de la empresa");
			document.getElementById('cifnif').focus();
			return false;
	
	}
	
	
	if(document.getElementById('poblacion').value == ""){
		
			alert("Ingresa una poblacion");
			document.getElementById('poblacion').focus();
			return false;
	
	}
	
	
	var cp =  document.getElementById('cpostal').value;

	var filter2 = /^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$/;
	
	if(!filter2(cp) || cp ==""){
		
			alert("Ingrese un código postal válido");
			document.getElementById('cpostal').focus();
			return false;
	
	}
	
	
	if(document.getElementById('telefono').value == ""){
		
			alert("Ingrese un teléfono");
			document.getElementById('telefono').focus();
			return false;
	
	}
		
	
	if(document.getElementById('web').value == ""){
		
			alert("Ingresa la web de la entidad");
			document.getElementById('web').focus();
			return false;
	
	}
	
	
	
	if(!document.getElementById('ofertados1').checked && !document.getElementById('ofertados2').checked && !document.getElementById('ofertados3').checked && !document.getElementById('ofertados4').checked && !document.getElementById('ofertados5').checked){
		
			alert("Debe seleccionar al menos un servicio ofertado");
			document.getElementById('ofertados1').focus();
			return false;
	
	}
	
	
	if(document.getElementById('personacontacto').value == ""){
		
			alert("Ingrese una persona de contacto de la empresa");
			document.getElementById('personacontacto').focus();
			return false;
	
	}
	
	if(document.getElementById('cargo').value == ""){
		
			alert("Ingrese un cargo de la persona de contacto");
			document.getElementById('cargo').focus();
			return false;
	
	}
	
	
	if(document.getElementById('telefonocontacto').value == ""){
		
			alert("Ingrese un teléfono de la persona de contacto");
			document.getElementById('telefonocontacto').focus();
			return false;
	
	}
	
	
	var email = document.getElementById('email').value;
		
	if(email == ""){
		
			alert("Ingrese un email");
			document.getElementById('email').focus();
			return false;
	
	}
	
	
	//var Formato = /^([\w-\.])+@([\w-]+\.)+([a-z]){2,4}$/;
	var Formato = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	
	var Comparacion = Formato.test(email);
     
	if(Comparacion == false){
          
	  alert("Ingrese un mail válido");
      document.getElementById('email').focus();
      return false;
          
    }
		
	
	if(!document.getElementById('heleido').checked){

		alert("Debes aceptar las condiciones legales");
		//document.getElementById('heleido').focus();
		return false;
		
	}
	
	//document.getElementById('adhesion').submit();
}


function iniciarsesion(){
	
	var obj = document.getElementById("iniciosesion");
  	
	if(obj.style.display == "block") obj.style.display = "none"
  	else obj.style.display = "block"

	
}


function validarmiperfil(){
	
	if(document.getElementById('nick').value == ""){
		
			alert("Ingrese un nombre de usuario.");
			document.getElementById('nick').focus();
			return false;
	
	}
	
	if(document.getElementById('apellidos').value == ""){
		
			alert("Ingrese un apellido de usuario.");
			document.getElementById('apellidos').focus();
			return false;
	
	}
	
	var usuario = document.getElementById('usuario').value;
	
	//var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	var filter = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	
	//if (s.length == 0 ) return true;
		
	if (!filter.test(usuario) || usuario == ""){
			
			alert("Ingrese una dirección de correo válida");
			document.getElementById('email').focus();	
			return false;
			
	}
	
	if(document.getElementById('password').value == "" || document.getElementById('password2').value == ""){
		
			if(document.getElementById('password').value != document.getElementById('password2').value){
			alert("Las contraseñas deben ser iguales.");
			document.getElementById('password').focus();
			return false;
			}
	}
	
	//document.getElementById('editarmiperfil').submit();
	
}


function validarmiempresa(){
	
	if(document.getElementById('nombre').value == ""){
		
			alert("Ponga un nombre a la Empresa");
			document.getElementById('nombre').focus();
			return false;
	
	}
	
	
	var s = document.getElementById('email').value;
	
	//var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	var filter = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	
	//if (s.length == 0 ) return true;
		
	/*if (!filter.test(s)){
		
			alert("Ingrese un mail corporativo válido");
			document.getElementById('email').focus();
			return false;
			
	}*/
	
	//var Formato = /^([\w-\.])+@([\w-]+\.)+([a-z]){2,4}$/;
	var Formato = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	
	var Comparacion = Formato.test(s);
     
	if(Comparacion == false){
          
	  alert("Ingrese un email corporativo valido");
      document.getElementById('email').focus();
      return false;
          
    }
	
	
	document.getElementById('miempresa').submit();
	
}

function ofertasdemandas(){
	
	//alert("Entra");
	if(document.getElementById('ofertademanda').value == "2"){

		document.getElementById('solodemandas').style.display = "block";
		
	}else{

		document.getElementById('solodemandas').style.display = "none";
		
	}
	
}


function validarresponderofertasydemandas(){
	
	if(document.getElementById('nombre').value == ""){
		
			alert("Ponga un nombre a la respuesta");
			document.getElementById('nombre').focus();
			return false;
	
	}
	
	if(document.getElementById('empresa').value == ""){
		
			alert("Ponga un nombre a la empresa");
			document.getElementById('empresa').focus();
			return false;
	
	}
	
	if(document.getElementById('respuesta').value == ""){
		
			alert("Escriba una respuesta");
			document.getElementById('respuesta').focus();
			return false;
	
	}
	
}


function mostrarfckeditor (tipotextarea, nombretextarea, valortextarea, ancho, alto,url) 
{
    var oFCKeditor = new FCKeditor(nombretextarea);
    oFCKeditor.BasePath = url;
    if (alto != "")    oFCKeditor.Height    = alto;       
    if (ancho != "")    oFCKeditor.Width    = ancho;                                       
    oFCKeditor.ToolbarSet    = tipotextarea;
    oFCKeditor.Value    = valortextarea;
    oFCKeditor.Create();
    /*
    Para insertar el valor del fckeditor en la base de datos es conveniente hacer lo siguiente:
    str_replace("\r\n", "", $_POST['textopublicidad'])
    Para mostrar un fckeditor, basta con añadir las siguientes líneas en el lugar deseado:
    <script type="text/javascript">                       
    var valortextarea = "<? echo $campo['texto_publicidad']; ?>";   
    mostrarfckeditor("Default", "textopublicidad", valortextarea, 500, 400);   
    </script>   
    */
}



function validarofertasydemandas(){

	if(document.getElementById('titulo').value == ""){
		
			alert("Ingrese un Titulo de la Oferta/Demanda.");
			document.getElementById('titulo').focus();
			return false;
	
	}
	
	if(document.getElementById('fechalimite').value == ""){
		
			alert("Ingrese una fecha límite");
			document.getElementById('fechalimite').focus();
			return false;
	
	}
	
	var correo = document.getElementById('correo').value;
	
	//var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	var filter = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	
	//if (s.length == 0 ) return true;
		
	if (!filter.test(correo)){
		
			alert("Ingrese un correo válido");
			document.getElementById('correo').focus();
			return false;
			
	}
	
	//document.getElementById('crearofertasdemandas').submit();

}

//////////////////////////////////////////////////////////////
// AVISO DE ELIMINAR UN PROYECTO, DOCUMENTO, USUARIOS...
//////////////////////////////////////////////////////////////		
	
function aviso(ideliminar, url, propiedadeliminar){
	
	if (!confirm("¡¡ALERTA!! se va a eliminar "+propiedadeliminar)) 
	{
		return false;
	}
	else 
	{
		document.location = url+'/'+ideliminar+'/';
		return true;
	}

}


function validarusuarios(){
		
	
	if(document.getElementById('nick').value == ""){
		
			alert("Ingrese un Nombre");
			document.getElementById('nick').focus();
			return false;
	
	}
	
	
	if(document.getElementById('apellidos').value == ""){
		
			alert("Ingrese un Apellido");
			document.getElementById('apellidos').focus();
			return false;
	
	}
	
	
	


	
	var s = document.getElementById('usuario').value;
	
	//var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	
	var filter = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	
	//if (s.length == 0 ) return true;
		
	if (!filter.test(s)){
		
			alert("Ingrese una dirección de correo válida");
			document.getElementById('usuario').focus();
			return false;
			
	}
		
	
	
	if(document.getElementById('id').value==""){
	
	if(document.getElementById('password').value == "" || document.getElementById('password2').value == ""){

			alert("Ingrese las contraseñas");
			document.getElementById('password').focus();
			return false;
			
	}
	
	
	if(document.getElementById('password').value!=document.getElementById('password2').value){
			
			alert("Las contraseñas no son iguales");
			document.getElementById('password2').focus();
			return false;
	}
	
	}else{
		
		if(document.getElementById('password').value!=document.getElementById('password2').value){
			
			alert("Las contraseñas no son iguales");
			document.getElementById('password2').focus();
			return false;
		}
		
		
	}
	
	return true;
}



//////////////////////////////////////////////////////////////
// FUNCION PARA AÑADIR SERVICIOS
//////////////////////////////////////////////////////////////

function anadirservicios(numeroservicio)
{           
	
    if (numeroservicio == "editardos")
	{
		//alert("editardos");
		cadena = document.getElementById("valoresatribdos").value;	
		cadena = cadena.split(";");		
		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			// introduzco el valor en el div para mostrarlos			
			j=i+1;			
			
			if(j!="1"){
            		
            	espacio = "<br />";
					            		
            }else{
            		
            	espacio = "";
            		
            }
            	
			/*if (j<=5)
			{*/				
				valoratributo = espacio+"<strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvalorservicio('"+j+"','dos')\" title=\"Eliminar\">(x)</a>";
				document.getElementById("valoresatributodos").innerHTML = document.getElementById("valoresatributodos").innerHTML+valoratributo+"\n";				
				// oculto el enlade de añadir para que no pueda introducir más valores
				/*if (j==5)
				{
					document.getElementById("anadirdos").style.visibility  = "hidden";
				}
			}*/		
		}				
	}
    
	
	if (numeroservicio == "editartres")
	{
		//alert("editartres");
		cadena = document.getElementById("valoresatribtres").value;	
		cadena = cadena.split(";");		
		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			// introduzco el valor en el div para mostrarlos			
			j=i+1;			
			
			if(j!="1"){
            		
            	espacio = "<br />";
					            		
            }else{
            		
            	espacio = "";
            		
            }
            	
			/*if (j<=5)
			{*/				
				valoratributo = espacio+"<strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvalorservicio('"+j+"','tres')\" title=\"Eliminar\">(x)</a>";
				document.getElementById("valoresatributotres").innerHTML = document.getElementById("valoresatributotres").innerHTML+valoratributo+"\n";				
				// oculto el enlade de añadir para que no pueda introducir más valores
				/*if (j==5)
				{
					document.getElementById("anadirdos").style.visibility  = "hidden";
				}
			}*/		
		}				
	}
    
	
	if (numeroservicio == "editarcuatro")
	{
		//alert("editarcuatro");
		cadena = document.getElementById("valoresatribcuatro").value;	
		cadena = cadena.split(";");		
		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			// introduzco el valor en el div para mostrarlos			
			j=i+1;			
			
			if(j!="1"){
            		
            	espacio = "<br />";
					            		
            }else{
            		
            	espacio = "";
            		
            }
            	
			/*if (j<=5)
			{*/				
				valoratributo = espacio+"<strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvalorservicio('"+j+"','cuatro')\" title=\"Eliminar\">(x)</a>";
				document.getElementById("valoresatributocuatro").innerHTML = document.getElementById("valoresatributocuatro").innerHTML+valoratributo+"\n";				
				// oculto el enlade de añadir para que no pueda introducir más valores
				/*if (j==5)
				{
					document.getElementById("anadirdos").style.visibility  = "hidden";
				}
			}*/		
		}				
	}
	
    
	if (numeroservicio == "editarcinco")
	{
		//alert("editarcinco");
		cadena = document.getElementById("valoresatribcinco").value;	
		cadena = cadena.split(";");		
		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			// introduzco el valor en el div para mostrarlos			
			j=i+1;			
			
			if(j!="1"){
            		
            	espacio = "<br />";
					            		
            }else{
            		
            	espacio = "";
            		
            }
            	
			/*if (j<=5)
			{*/				
				valoratributo = espacio+"<strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvalorservicio('"+j+"','cinco')\" title=\"Eliminar\">(x)</a>";
				document.getElementById("valoresatributocinco").innerHTML = document.getElementById("valoresatributocinco").innerHTML+valoratributo+"\n";				
				// oculto el enlade de añadir para que no pueda introducir más valores
				/*if (j==5)
				{
					document.getElementById("anadirdos").style.visibility  = "hidden";
				}
			}*/		
		}				
	}
	
	
	
	if (numeroservicio == "editarseis")
	{
		//alert("editarseis");
		cadena = document.getElementById("valoresatribseis").value;	
		cadena = cadena.split(";");		
		// recorro el array
		for (var i=0;i<cadena.length-1;i++)
		{
			// introduzco el valor en el div para mostrarlos			
			j=i+1;			
			
			if(j!="1"){
            		
            	espacio = "<br />";
					            		
            }else{
            		
            	espacio = "";
            		
            }
            	
			/*if (j<=5)
			{*/				
				valoratributo = espacio+"<strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvalorservicio('"+j+"','seis')\" title=\"Eliminar\">(x)</a>";
				document.getElementById("valoresatributoseis").innerHTML = document.getElementById("valoresatributoseis").innerHTML+valoratributo+"\n";				
				// oculto el enlade de añadir para que no pueda introducir más valores
				/*if (j==5)
				{
					document.getElementById("anadirdos").style.visibility  = "hidden";
				}
			}*/		
		}				
	}
	

    if (document.getElementById("valoratributo"+numeroservicio) && document.getElementById("valoratributo"+numeroservicio).value != "")   
    {
        // inicializo las variables
        var j = "";
        var repetido = "no";       
        
        //document.getElementById("valoresatributo"+numeroservicio).innerHTML = "";   
        // asigno el valor introducido a una variable
        var valoratributo = document.getElementById("valoratributo"+numeroservicio).value;   
        // introduzco el valor en el input para luego guardarlos
        
        //alert("num atrib: " + numeroatributo);
        
        document.getElementById("valoresatrib"+numeroservicio).value = document.getElementById("valoresatrib"+numeroservicio).value+valoratributo+";";
        // convierto la cadena en un array
        var cadena = document.getElementById("valoresatrib"+numeroservicio).value;
        cadena = cadena.split(';');           
       
        // miro si el nuevo valor ya está en el array       
        for(ind=0; ind<cadena.length-2; ind++)
        {                                   
            if (document.getElementById("valoratributo"+numeroservicio).value == cadena[ind])
            {                             
                repetido = "si";                                                      
            }                  
        }
        // si ya estaba en el array lo borro del input para que luego no lo pinte
        
        
        if (repetido == "si")
        {
            cadena = document.getElementById("valoresatrib"+numeroservicio).value; 
            cadena = cadena.split(";");
            document.getElementById("valoresatrib"+numeroservicio).value = "";
            
            for(var i=0;i<cadena.length-2;i++){
            	
            	//alert ("Cadena i: " + cadena[i]);
            	document.getElementById("valoresatrib"+numeroservicio).value = document.getElementById("valoresatrib"+numeroservicio).value + cadena[i] + ";";
            	
            }
            
            //alert(document.getElementById("valoresatrib"+numeroservicio).value);
         
        }
		
        
        
        // recorro el array
       	if(repetido != "si"){
       		
        for (var i=cadena.length-2;i<cadena.length-1;i++)
        {
            // introduzco el valor en el div para mostrarlos        
            j=i+1;                       

            	if(j!="1"){
            		
            		espacio = "<br />";
					            		
            	}else{
            		
            		espacio = "";
            		
            	}
            	
            	
            	//var indice = document.getElementById("valoratributo"+numeroservicio).selectedIndex;  
            	//var indice = document.formu.valoratributodos.selectedIndex;
            	
            	//alert("Indice: " + indice);   	
            	//var valor = document.getElementById("valoratributo"+numeroservicio).options[indice].text;
            	//alert("Valor índice: " + valor);
            	
            	//alert("Nombre i: " + nombres[i]);   	
            	        
                valoratributo = espacio+"<strong>"+ cadena[i] +"</strong> <a href=\"javascript:eliminarvalorservicio('"+j+"','"+numeroservicio+"')\" title=\"Eliminar\">(x)</a>";
                //valoratributo = espacio+"<strong>"+ valor +" ("+ cadena[i] +")</strong> <a href=\"javascript:eliminarvalorservicio('"+j+"','"+numeroservicio+"')\" title=\"Eliminar\">(x)</a>";
                
                document.getElementById("valoresatributo"+numeroservicio).innerHTML = document.getElementById("valoresatributo"+numeroservicio).innerHTML+valoratributo+"\n";
               
                // situo el cursor en el input para introducir un nuevo valor para este atributo
                document.getElementById("valoratributo"+numeroservicio).focus();
        
        }
          
       	}
        
        
    }
   
}




//////////////////////////////////////////////////////////////
// FUNCION PARA BORRAR SERVICIOS
//////////////////////////////////////////////////////////////

function eliminarvalorservicio(valor, numeroatributo)
{   

	// convierto la cadena en un array
    var cadena = document.getElementById("valoresatrib"+numeroatributo).value;
    cadena = cadena.split(";");
    
    
    // inicializo todo
    document.getElementById("valoresatributo"+numeroatributo).innerHTML = "";

    document.getElementById("valoresatrib"+numeroatributo).value = "";   
    
    var k = 0;
    
    
    // recorro el array
    for (var i=0;i<cadena.length-1;i++)
    {
        
    	// si el campo es distinto al que he pulsado para eliminar, lo añado
        j=i+1;    
            	   
        if (j != valor )
        {
        	
        	//var indice = document.getElementById("valoratributo"+numeroatributo).selectedIndex;
           	//var indice = document.formu.valoratributodos.selectedIndex;
          	//alert("Indice: " + indice);   	
          	//var valor = document.getElementById("valoratributo"+numeroatributo).options[cadena[i]].text;
        	
          	//alert("Valor: " + valor);
          	
        	k = k+1;                       
            document.getElementById("valoresatrib"+numeroatributo).value = document.getElementById("valoresatrib"+numeroatributo).value+cadena[i]+";";
            
            
            if(j!="1"){
            		
            	espacio = "<br />";
					            		
            }else{
            		
            	espacio = "";
            		
            }
            
            //alert("j: " + j);
            //alert("k: " + k);
            
            valoratributo = espacio+"<strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvalorservicio('"+k+"','"+numeroatributo+"')\" title=\"Eliminar\">(x)</a>";
         
            //valoratributo = "<strong>"+cadena[i]+"</strong> <a href=\"javascript:eliminarvaloratributo('"+k+"','"+numeroatributo+"')\" title=\"Eliminar\">(x)</a><br />";
            document.getElementById("valoresatributo"+numeroatributo).innerHTML = document.getElementById("valoresatributo"+numeroatributo).innerHTML+valoratributo+"\n";           
            
            // muestro el enlade de añadir para que pueda introducir más valores
            //document.getElementById("anadir"+numeroatributo).style.visibility = "visible";               
        
        }
    
    
    }
       
    // situo el cursor en el input para introducir un nuevo valor para este atributo
    document.getElementById("valoratributo"+numeroatributo).focus();

}
