function pagefunctions(){
	var keyuser = localStorage.getItem('keyuser');
	
	$('#list-friends').append('<div id="loading-user" id="load-idsd"><img src="images/loading.gif" width="25px" height="25px"/> <span>'+Language.loading+'</span></div>');
	
	$.ajax({
    	type: "POST",
		crossDomain: true,
		url: "http://m2s.es/app/api/searchgroups.php",
		data: {"key": keyuser},
		cache: false,
		dataType: 'json',
		success: function(result) {
			$('#loading-user').remove();
			$('#list-friends').append('<div class="officials"></div>');
			for (var i = 0; i < result.officialgr.length; i++) {
				id = result.officialgr[i].id;
				namegroup = result.officialgr[i].namegroup;
				official = result.officialgr[i].official;
				imagepr = result.officialgr[i].imagepr;
				state = result.officialgr[i].state;
				grsearchitem = searchmount(id, namegroup, official, imagepr, state);
				$('#list-friends .officials').append(grsearchitem);
			}
			$('#list-friends').append('<div class="news"></div>');
			for (var i = 0; i < result.newgr.length; i++) {
				id = result.newgr[i].id;
				namegroup = result.newgr[i].namegroup;
				official = result.newgr[i].official;
				imagepr = result.newgr[i].imagepr;
				state = result.newgr[i].state;
				grsearchitem = searchmount(id, namegroup, official, imagepr, state);
				$('#list-friends .news').append(grsearchitem);
			}
			$('#list-friends').append('<div class="intgr"></div>');
			for (var i = 0; i < result.intgr.length; i++) {
				id = result.intgr[i].id;
				namegroup = result.intgr[i].namegroup;
				official = result.intgr[i].official;
				imagepr = result.intgr[i].imagepr;
				state = result.intgr[i].state;
				grsearchitem = searchmount(id, namegroup, official, imagepr, state);
				$('#list-friends .intgr').append(grsearchitem);
			}
			$('.officials').addClass(langdefault);
			$('.news').addClass(langdefault);
			$('.intgr').addClass(langdefault);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.error(textStatus + ' ' + XMLHttpRequest.status);
			errormod(Language.errorpetitionm2s);
			$('#loading-user').remove();
		}
	});
}

function submitsearch() {
	$('#list-friends').append('<div id="loading-user" id="load-idsd"><img src="images/loading.gif" width="25px" height="25px"/> <span>'+Language.loading+'</span></div>');
	$('#list-friends .officials').hide();
  	$('#list-friends .news').hide();
  	$('#list-friends .intgr').hide();
  	if (document.getElementById('search-items')) {
    	$('#search-items').remove();
  	}
  	$('#list-friends').append('<div id="search-items"></div>');
  	var valueinput = $('#search-useradd').val();
  	if (valueinput != '') {
    	$.ajax({
      		type: "POST",
      		crossDomain: true,
      		url: "http://m2s.es/app/api/searchgroups.php",
      		data: {"s": valueinput, "key": keyuser},
      		cache: false,
      		dataType: 'json',
      		success: function(data) {
        		if (data.search.length == 0) {
          			$('#list-friends #search-items').append("<p class='center'>"+Language.noresultsearch+"</p>");
        		}
        		for (var i = 0; i < data.search.length; i++) {
          			id = data.search[i].id;
          			namegroup = data.search[i].namegroup;
          			official = data.search[i].official;
          			imagepr = data.search[i].imagepr;
          			state = data.search[i].state;
          			grsearchitem = searchmount(id, namegroup, official, imagepr, state);
          			$('#list-friends #search-items').append(grsearchitem);
        		}
      		},
      		complete: function() {
        		if ($('#loading-user').length) {
          			$('#loading-user').remove()
        		}
      		},
      		error: function(XMLHttpRequest, textStatus, errorThrown) {
        		console.error(textStatus + ' ' + XMLHttpRequest.status);
        		errormod(Language.errorpetitionm2s);
        		$('#loading-user').remove();
      		}
    	});
  	}else{
    	$('#loading-user').remove();
    	$('.collection.withimages .officials').show();
    	$('.collection.withimages .news').show();
    	$('.collection.withimages .intgr').show();
  	}
}

  
function searchmount(id, namegroup, official, imagepr, state) {
  searchdiv = '';
  searchdiv += '<a href="profilegr.html?id=' + id + '">' 
  
  searchdiv += '<li class="li-chats">';
  if (imagepr != null) {
    searchdiv += '<img src="' + imagepr + '" class="img"/>';
  } else {
    var namesplit = namegroup.split(' ');
    abv1 = namesplit[0].charAt(0);
    if (namesplit[1]) {
        abv2 = namesplit[1].charAt(0);
        abv = abv1 + abv2;
    }else{
        abv = abv1;
    }
    searchdiv += '<div class="img" style="background-color:#777"><p unselectable="on" onselectstart="return false;" onmousedown="return false;">' + abv + '</p></div>';
  }
  searchdiv += '<div class="right-info">'
  if (official == 'yes') {
       searchdiv += '<span class="icon ok"></span>';
  }
  searchdiv += '<span class="icon chevron-right"></span></div><div class="right-p"><div><span class="name">';
  searchdiv += namegroup+'</span><span class="state">';
  if (state == '') {
    searchdiv += '--';
  } else {
    searchdiv += state;
  }
  searchdiv += '</span></div></div></li></a>';
  return searchdiv;
};