function login(usern,pass,type){
	  $.ajax({
          type: "POST",
          crossDomain: true,
          url: "http://m2s.es/app/api/connect/login.php",
          data: {"user": usern, "passmd5": pass},
          cache:false,
          dataType: 'json',
          beforeSend: function() {
          console.log('Connecting...');
          if(type == 'login'){
            $('#formsd').css('display','none');
            var sending = '<div id="sending-load"><img src="images/load-login.gif"></div>';
            $('.form').append(sending);
          }
          },
          success: function(result) {
            if(result.mensaje == 'OK'){
              if(type == 'login'){
                if (localStorage) {
                  localStorage.setItem("user", usern);  
                  localStorage.setItem("passwd", pass); 
                  keyuser = result.key;
				  keyuser = keyuser.replace('==','');
				  localStorage.setItem("keyuser", keyuser); 
                  if(result.data.imagepr){
                    localStorage.setItem("imguse", result.data.imagepr); 
                  }
                  localStorage.setItem("emailMD5", md5(result.data.email));  
                  registerntf('login');
                }else{
	              alert('This app needs localstorage!')
                };
              }
              console.log('Completed session');
              }
            if(result.mensaje == 'e2'){
              errormod(Language.datanotenter);
              if(type == 'login'){
              $('#formsd').css('display','block');
              $('#sending-load').remove();
              }
              console.log("Data not entered");
            }
            if(result.mensaje == 'e3'){
             if(type == 'login'){
	            errormod(Language.userpassincorrect);
              $('#formsd').css('display','block');
              $('#sending-load').remove();
              $('input[name="user"]').val('');
              $('input[name="pass"]').val('');
              }else{
	              localStorage.removeItem('user');
	              localStorage.removeItem('passwd');
	              window.location.href="login.html"
              }
              console.log("The username or password are incorrect.");
            }
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.error(textStatus+' '+XMLHttpRequest.status); 
            if(type == 'login'){
              errormod(Language.errorpetitionm2s);
              $('#formsd').css('display','block');
              $('#sending-load').remove();
              $('input[name="user"]').val('');
              $('input[name="pass"]').val('');
            }else{
	          var divno = document.createElement('div');
              divno.innerHTML='<div id="list-notifications" style="text-align:center;cursor:pointer"></div>';
              divno.className='background-dark';
              var modsv = document.getElementById('modsv');
              modsv.appendChild(divno);
              $('#list-notifications').append(Language.errorconection);
              $('#list-notifications').click(function(){
                window.location.reload(); 
              })
            }  
          }
     })
	}