
function joinGroup(groupId, lang) {
	membership(groupId, lang, "join");
}


function quitGroup(groupId, lang) {
	membership(groupId, lang, "quit");
}

function quitGroupAjax(groupId, lang) {
	$("#quit-"+groupId).hide();
	$("#loader-quit-"+groupId).show();
	$.ajax({
		   type: "POST",
		   url: 'index.php?eID=ikdgfmm_ajax',
		   asynchronous: true,
		   data: 'groupId=' + groupId + '&action=quit_with_other_message&lang=' + lang,
		   dataType: 'json',
		   success: function(data){
	     		$("#loader-quit-"+groupId).hide();
	     		
	     		countSpan = getMemberCountSpan(groupId);
	     		
				if (countSpan && typeof data.members_count != 'undefined' ) {
					countSpan.innerHTML = data.members_count;
				}
	     		
	     		$("#quit-"+groupId).get(0).innerHTML = data.message;
	     		$("#quit-"+groupId).show();	     		
			}
		 });
}

function getMemberCountSpan(groupId) {
	if ( $("#members_count-" + groupId).length ) {
		return $("#members_count-" + groupId).get(0);
	}
	
	if ( $("#members_count").length == 1 ) {
		return $("#members_count").get(0);
	}
	
	return null;	
	
}

function membership(groupId, lang, action) {
	$.ajax({
		   type: "POST",		   
		   url: ajaxurl,
		   asynchronous: true,
		   data: 'groupId=' + groupId + '&pi4mode=apprformrequest&action='+action+'&L='+lang,
		   dataType: 'json',
		   success: function(data){
			
				data.code == 200 ?
				$("#dialog-message").dialog({
					modal: true,
					title: dialog_title,
					//width: 200,
					buttons: {
						Ok: function() {
							$(this).dialog('close');
						}
					}
				}) :
				$("#dialog-message").dialog({
					modal: true,
					title: dialog_title,
					resizable:false,
					//width: 600,
					//height:250,
					buttons: {
						Ok: function() {
							$(this).dialog('close');
						}
					}
				});					
	     		countSpan = getMemberCountSpan(groupId);
	     		
				if (countSpan && typeof data.members_count != 'undefined' ) {
					countSpan.innerHTML = data.members_count;
				}
				
				$("#dialog-message").get(0).innerHTML = data.message;
				$("#dialog-message").dialog('open');
				
				if ( data.code == 200 ) {
					$("#" + action + "_link").hide();
					$("#" + opposite(action) + "_link").show();					
				}
				
			}
		 });
}

function opposite(action) {
	if ( action == "join" ) return "quit";
	return "join";
}
function confirmappl(){
	suppltext = escape(document.getElementById('tx_ikdgfmm_suppl').value);
	document.getElementById('ajaxim').style.display='';
	document.getElementById('confirmappl_base').style.display='NONE';
		
	$.ajax({
		   type: "POST",		   
		   url: ajaxurl,
		   asynchronous: true,
		   data: 'groupId=' + confirmappl.arguments[1] + '&pi4mode=apprformrequest&action=confirm&suppltext='+suppltext+'&L='+confirmappl.arguments[2],
		   dataType: 'json',
		   success: function(data){
			document.getElementById('confirmappl').style.display='';	
			document.getElementById('ajaxim').style.display='NONE';			
			}
		 });
	
	}
