//preview our post
function previewPost(el)
{
	//alert('test');
	
	var form = getElementForm(el);
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'post';

	ajaxConfig.onSuccess = function(t) {
		$('postPreview').innerHTML = t.responseText;
		document.location = '#postPreview';
		//if(scrollCheck) scroll(0,0);
	};
	
	//get form data
	ajaxConfig.parameters = formSerialize(el);

	new Ajax.Request(form.action,ajaxConfig);
}

//take the thread preview and post it
function threadPost(el)
{
	//alert('test');
	//return 0;
	var form = $(el);
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'post';

	ajaxConfig.onSuccess = function(t) {
		//alert('test stuff2');
		var threadPostResponse = t.responseText;
		var groupID = ajaxConfig.parameters.groupID;
		var threadID = ajaxConfig.parameters.threadID;
		if(threadID==null || threadID == '')
			threadID = t.responseText;
		//alert('threadID: '+threadID);
		
		
		if(threadPostResponse==0)
			showPopWin('/_lib/popupLib/?popup=requireApproval&groupID='+groupID+'&threadID='+threadID+'&title=Requires Approval', 420, 300, null);
		else
			window.location = '?view=thread&threadID='+threadID+'&groupID='+groupID;
	
	};
	
	//get form data
	ajaxConfig.parameters = formSerialize(el);

	//alert('test stuff1');
	//alert(Object.toQueryString(ajaxConfig.parameters));
	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//take the thread preview and post it
function rate(el,threadID)
{
	$('rating').remove();
	
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	ajaxConfig.parameters.threadID=threadID;
	ajaxConfig.parameters.ajaxAction='rateThread';
	ajaxConfig.method = 'get';
	//$('threadRating').innerHTML++;
	ajaxConfig.onSuccess = function(t) {
		$('threadRating').innerHTML++;
		
	};

	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//forward the thread
function forwardThread(threadID)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	ajaxConfig.parameters.threadID=threadID;
	ajaxConfig.parameters.groupID=groupID;
	ajaxConfig.parameters.modName='forwardThread';
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		$('forwardThread').innerHTML=t.responseText;
		Element.show('forwardThread');
		formsInit('',$('forwardThread'));
		groupsButtonMgrInit('#forwardThread');
		//groupsButtonMgrInit($('forwardThread'))
		document.location = '#forwardThread';
	};
	
	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}

//admin delete thread
function deleteThread(threadID)
{
	if(!confirm('Are you sure you wish to delete this discussion thread?'))
		return 0;
	//return 0;
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		document.location = '/_groups/?groupID='+groupID;
	};
	
	//get form data
	ajaxConfig.parameters.threadID = threadID;
	ajaxConfig.parameters.groupID = groupID;
	ajaxConfig.parameters.ajaxAction = 'deleteThread';

	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//report the thread
function reportThread(threadID, threadDisplayNum)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	ajaxConfig.parameters.threadID=threadID;
	ajaxConfig.parameters.groupID=groupID;
	ajaxConfig.parameters.threadDisplayNum=threadDisplayNum;
	ajaxConfig.parameters.modName='reportThread';
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		$('report_'+threadDisplayNum).innerHTML=t.responseText;
		Element.show('report_'+threadDisplayNum);
		formsInit('',$('report_'+threadDisplayNum));
		groupsButtonMgrInit('#report_'+threadDisplayNum);
		//groupsButtonMgrInit($('forwardThread'))
		document.location = '#report_'+threadDisplayNum;
	};
	
	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}

//manage page->admin thread submission
function adminThread(threadID, value)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	ajaxConfig.parameters.threadID=threadID;
	ajaxConfig.parameters.groupID=groupID;
	ajaxConfig.parameters.value=value;
	ajaxConfig.parameters.ajaxAction='adminThread';
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		$('contentReq').innerHTML--;
		Element.remove('thread_'+threadID);
	};
	
	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//manage page->admin file upload
function adminFile(fileID, value)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	ajaxConfig.parameters.fileID=fileID;
	ajaxConfig.parameters.groupID=groupID;
	ajaxConfig.parameters.value=value;
	ajaxConfig.parameters.ajaxAction='adminFile';
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		$('contentReq').innerHTML--;
		Element.remove('file_'+fileID);
	};
	
	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//manage page->admin file upload
function adminBookmark(siteID, value, url, site, description)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	ajaxConfig.parameters.siteID=siteID;
	ajaxConfig.parameters.groupID=groupID;
	ajaxConfig.parameters.url=url;
	ajaxConfig.parameters.site=site;
	ajaxConfig.parameters.description=description;
	ajaxConfig.parameters.value=value;
	ajaxConfig.parameters.ajaxAction='adminBookmark';
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		$('contentReq').innerHTML--;
		Element.remove('site_'+siteID);
	};
	
	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//make thread sticky
function stickyThread(threadID, stick)
{
	//setup our ajax stuff
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		reloadPage();
	};
	
	ajaxConfig.parameters.threadID = threadID;
	ajaxConfig.parameters.stick = stick;
	ajaxConfig.parameters.ajaxAction = 'stickyThread';
	
	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//censor thread
function censorThread(threadID)
{
	//setup our ajax stuff
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		reloadPage();
	};
	
	ajaxConfig.parameters.threadID = threadID;
	ajaxConfig.parameters.ajaxAction = 'censorThread';

	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

function addFriends(el)
{
	var friends = formSerialize(el);
	$('friendsListField').value = '';
	Object.keys(friends).each( function(thing) { $('friendsListField').value =  $('friendsListField').value + thing+';'} );
	Element.hide('addRecipients');
	Element.show('forwardMessage');
	//friends.each()
	//alert(friends);
}

//handle our module paging data loads
function pagingFunctions(el)
{
	//setup our ajax stuff
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		//alert(Object.keys(ajaxConfig.parameters));
		//alert(modName);
		$(modName).innerHTML = t.responseText; 
		
		groupsButtonMgrInit('#'+modName);
	};
	
	if(this.getAttribute('goToPage') != null)
		ajaxConfig.parameters.goToPage = this.getAttribute('goToPage');
		
	if(this.getAttribute('setItems') != null)
		ajaxConfig.parameters.setItems = this.getAttribute('setItems');

	if(this.getAttribute('numSetItems') != null)
		ajaxConfig.parameters.numSetItems = this.getAttribute('numSetItems');

	if(this.getAttribute('itemsPer') != null)
		ajaxConfig.parameters.itemsPer = this.getAttribute('itemsPer');

	if(this.getAttribute('groupID') != null)
		ajaxConfig.parameters.groupID = this.getAttribute('groupID');
		
	if(this.getAttribute('catID') != null)
		ajaxConfig.parameters.catID = this.getAttribute('catID');

	if(this.getAttribute('currPage') != null)
		ajaxConfig.parameters.currPage = this.getAttribute('currPage');

	if(this.getAttribute('totalItems') != null)
		ajaxConfig.parameters.totalItems = this.getAttribute('totalItems');
	
	if(this.getAttribute('threadID') != null)
		ajaxConfig.parameters.threadID = this.getAttribute('threadID');
		
	if(this.getAttribute('folderID') != null)
		ajaxConfig.parameters.folderID = this.getAttribute('folderID');

	if(this.getAttribute('s') != null)
		ajaxConfig.parameters.s = this.getAttribute('s');

	if(this.getAttribute('modName') != null)
	{
		var modName = this.getAttribute('modName');
		ajaxConfig.parameters.modName = modName;
	}

	
	ajaxConfig.parameters.appName = appName;

	//send user id
	//ajaxConfig.parameters.id = userID;
	
	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}

//
function membersList(el)
{
	//setup our ajax stuff
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		//alert(Object.keys(ajaxConfig.parameters));
		//alert(modName);
		$(modName).innerHTML = t.responseText; 
		
		groupsButtonMgrInit('#'+modName);
	};
	
	if(this.getAttribute('sortby') != null)
		ajaxConfig.parameters.sortby = this.getAttribute('sortby');
		
	if(this.getAttribute('limit') != null)
		ajaxConfig.parameters.limit = this.getAttribute('limit');

	if(this.getAttribute('groupID') != null)
		ajaxConfig.parameters.groupID = this.getAttribute('groupID');

	if(this.getAttribute('modName') != null)
	{
		var modName = this.getAttribute('modName');
		ajaxConfig.parameters.modName = modName;
	}
	
	ajaxConfig.parameters.appName = appName;
	
	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}

//browse group categories behavior for newest/largest
function browseGroupCat(el)
{
	//alert('test');
	//setup our ajax stuff
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		//alert(Object.keys(ajaxConfig.parameters));
		//alert(modName);
		$(modName).innerHTML = t.responseText; 
		
		groupsButtonMgrInit('#'+modName);
	};
	
	if(this.getAttribute('sortby') != null)
		ajaxConfig.parameters.sortby = this.getAttribute('sortby');
		
	if(this.getAttribute('itemsPer') != null)
		ajaxConfig.parameters.itemsPer = this.getAttribute('itemsPer');

	if(this.getAttribute('goToPage') != null)
		ajaxConfig.parameters.goToPage = this.getAttribute('goToPage');

	if(this.getAttribute('catID') != null)
		ajaxConfig.parameters.catID = this.getAttribute('catID');

	if(this.getAttribute('modName') != null)
	{
		var modName = this.getAttribute('modName');
		ajaxConfig.parameters.modName = modName;
	}
	
	ajaxConfig.parameters.appName = appName;
	
	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}

//
function folderFiles(el)
{
	//setup our ajax stuff
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		//alert(Object.keys(ajaxConfig.parameters));
		//alert(modName);
		$(modName).innerHTML = t.responseText; 
		
		groupsButtonMgrInit('#'+modName);
	};
	
	if(this.getAttribute('sortBy') != null)
		ajaxConfig.parameters.sortBy = this.getAttribute('sortBy');
		
	if(this.getAttribute('limit') != null)
		ajaxConfig.parameters.limit = this.getAttribute('limit');

	if(this.getAttribute('groupID') != null)
		ajaxConfig.parameters.groupID = this.getAttribute('groupID');
		
	if(this.getAttribute('folderID') != null)
		ajaxConfig.parameters.folderID = this.getAttribute('folderID');
		
	//if(this.getAttribute('goToPage') != null)
		ajaxConfig.parameters.goToPage = 1;

	if(this.getAttribute('modName') != null)
	{
		var modName = this.getAttribute('modName');
		ajaxConfig.parameters.modName = modName;
	}
	
	ajaxConfig.parameters.appName = appName;
	
	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}

//trigger our button after a file is uploaded
function groupFiles(trigger, val)
{
	groupsButtonTrigger(trigger);	
}

function deleteFavSite(currElement,siteID)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';
	ajaxConfig.onSuccess = function(t) { 
		groupsButtonTrigger(currElement);
		};
	ajaxConfig.parameters.app = appName;
	ajaxConfig.parameters.ajaxAction = 'favSiteUpdate';
	ajaxConfig.parameters.siteID = siteID;
	ajaxConfig.parameters.user = id;
	ajaxConfig.parameters.siteDelete = 1;
	ajaxConfig.parameters.groupID = groupID;
	
	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

function saveFavSites(currElement)
{
	//alert($('groupFavSites_Edit'));
	var obj = $('groupFavSites_Editform').serialize(true);

	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';
	ajaxConfig.onSuccess = function(t) { groupsButtonTrigger(currElement); };

	ajaxConfig.parameters.ajaxAction = 'favSiteUpdate';
	ajaxConfig.parameters.user = id;
	ajaxConfig.parameters.groupID = groupID;
	ajaxConfig.parameters.update = 1;
	ajaxConfig.parameters.favSites = Object.toJSON(obj);
	//alert(ajaxConfig.parameters.favSites);
	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//your groups page
//subscribe/unsubscribe to group digest
function setSubscription(groupID,enable)
{
	//alert(groupID)
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';
	ajaxConfig.onSuccess = function(t) { 
										if(enable)
										{
											Element.show('unsubscribeT_'+groupID);
											Element.hide('subscribeT_'+groupID);
											
											Element.show('unsubscribe_'+groupID);
											Element.hide('subscribe_'+groupID);
										}
										else
										{
											
											Element.show('subscribeT_'+groupID);
											Element.hide('unsubscribeT_'+groupID);
											
											Element.show('subscribe_'+groupID);
											Element.hide('unsubscribe_'+groupID);
										}
								};

	ajaxConfig.parameters.ajaxAction = 'subscriptionSettings';
	ajaxConfig.parameters.user = id;
	ajaxConfig.parameters.groupID = groupID;
	ajaxConfig.parameters.enable = enable;
	//alert(ajaxConfig.parameters.favSites);
	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//your groups page
//set top group
function setTopGroup(groupID)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';
	ajaxConfig.onSuccess = function(t) { 

								};

	ajaxConfig.parameters.ajaxAction = 'setTopGroup';
	ajaxConfig.parameters.user = id;
	ajaxConfig.parameters.groupID = groupID;
	//alert(ajaxConfig.parameters.favSites);
	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}


function leaveGroup()
{
	document.location='/_groups/';
}

//use this behavior for the random/newest/largest groups box
function browseGroupsList(el)
{

	//setup our ajax stuff
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		//alert(Object.keys(ajaxConfig.parameters));
		//alert(modName);
		$(modName).innerHTML = t.responseText; 
		
		groupsButtonMgrInit('#'+modName);
	};
	
	if(this.getAttribute('sortby') != null)
		ajaxConfig.parameters.sortby = this.getAttribute('sortby');
	
	//alert(ajaxConfig.parameters.sortby);
	
	if(this.getAttribute('limit') != null)
		ajaxConfig.parameters.limit = this.getAttribute('limit');

	if(this.getAttribute('modName') != null)
	{
		var modName = this.getAttribute('modName');
		ajaxConfig.parameters.modName = modName;
	}
	
	ajaxConfig.parameters.appName = appName;
	
	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}

//manage groups add admin function
function groupAdminInit()
{
	var memberAC = new dmsAutoComplete('newAdmin','newAdminDiv',interactDir+'memberXMLList.php');
	
	memberAC.chooseFunc = function(id,label){
		$('memberName').value = id;
	}

	//dont clear field when enter is pressed
	memberAC.clearField = false;
}

function removeAdmin(username)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';
	ajaxConfig.onSuccess = function(t) { $(username+'_admin').remove(); };

	ajaxConfig.parameters.ajaxAction = 'updateAdmin';
	ajaxConfig.parameters.user = id;
	ajaxConfig.parameters.groupID = groupID;
	ajaxConfig.parameters.admin = username;
	ajaxConfig.parameters.enable = 0;

	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//admin reject/approve join requests
function joinRequestAdmin(username, allow)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';
	ajaxConfig.onSuccess = function(t) {
									//alert(t.responseText);
									$(username+'_join').remove(); 
									$('numJoinReq').innerHTML--
									};

	ajaxConfig.parameters.ajaxAction = 'joinRequestAdmin';
	ajaxConfig.parameters.user = id;
	ajaxConfig.parameters.groupID = groupID;
	ajaxConfig.parameters.member = username;
	ajaxConfig.parameters.allow = allow;

	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//user join request
function joinRequest()
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';
	ajaxConfig.onSuccess = function(t) { 
										document.location = '/_groups/?groupID='+groupID;
									};

	ajaxConfig.parameters.ajaxAction = 'joinRequest';
	ajaxConfig.parameters.groupID = groupID;

	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

/*
moved to form in closeGroup popup
function closeGroup()
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';
	ajaxConfig.onSuccess = function(t) { document.location='/_groups'; };

	ajaxConfig.parameters.ajaxAction = 'closeGroup';
	ajaxConfig.parameters.user = id;
	ajaxConfig.parameters.groupID = groupID;

	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}
*/

function reloadPage()
{
	window.location.reload();
}

//ajax behavior to reload your groups members
function yourGroupsMembers()
{
	//setup our ajax stuff
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		$('groupMembers').innerHTML = t.responseText; 
		groupsButtonMgrInit('#groupMembers');
	};
	
	if(this.getAttribute('sortby') != null)
		ajaxConfig.parameters.sortby = this.getAttribute('sortby');
	
	if(this.getAttribute('limit') != null)
		ajaxConfig.parameters.limit = this.getAttribute('limit');

	if(this.getAttribute('modName') != null)
	{
		var modName = this.getAttribute('modName');
		ajaxConfig.parameters.modName = modName;
	}

	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}

//select invites to group function
function inviteSelect()
{

	var compose = $('selectForm').serialize(true);
	var recipients = Object.keys(compose);
	//alert(recipients);
	
	if(recipients!='')
	{
		Element.show('invite1');
		Element.hide('invite2');

		$('recipientsInput').value = '';

		recipients.each(function(recipient) {
			$('recipientsInput').value += recipient+';';
		});
	}
	else
	{
		Element.hide('invite1');
		Element.show('invite2');
	}
}

//reset group invite forms
function resetInvite()
{
	Element.hide('inviteFriends');
	Form.reset('groupInviteForm');
	Form.reset('selectForm');
	$('inviteMessage').setAttribute('focusClear',1);
	$('recipientsInput').disable();
}

//do group invitation
function groupInvite()
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';
	ajaxConfig.onSuccess = function(t) {
			resetInvite();
		};

	$('recipientsInput').enable();
	ajaxConfig.parameters.ajaxAction = 'groupInvite';
	ajaxConfig.parameters.recipients = $('recipientsInput').value;
	ajaxConfig.parameters.message = $('inviteMessage').value;
	ajaxConfig.parameters.groupID = groupID;
	
	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}

//check response from a file upload
function fileUploadResponse(el,vars)
{
	//check our file upload response
	var jsonstuff = json_decode(vars);
	
	if(jsonstuff.statusCode==-7 || jsonstuff.statusCode==-9)
		showPopWin('/_lib/popupLib/?popup=badFileUpload&title=Bad File', 420, 300, null);
	else
		reloadPage();
}

//group admin remove member
function removeMember()
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';
	ajaxConfig.onSuccess = function(t) { 
							$F('removeMember').each(function(member) { $(member).remove(); });
					 };			 
	ajaxConfig.parameters.ajaxAction = 'removeMember';
	ajaxConfig.parameters.memberList = $F('removeMember').toJSON();
	ajaxConfig.parameters.groupID = groupID;
	//ajaxConfig.parameters.admin = username;
	//ajaxConfig.parameters.enable = 0;

	new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
}


//group admin delete file
function deleteFile(fileID) {
	if(confirm('Are you sure you want to delete this file?'))
	{
		var ajaxConfig = new Object();
		ajaxConfig.parameters = new Object();
		
		ajaxConfig.method = 'get';
		ajaxConfig.onSuccess = function(t) { 
								reloadPage();
						 };			 
		ajaxConfig.parameters.ajaxAction = 'deleteFile';
		ajaxConfig.parameters.fileID = fileID;
		new Ajax.Request(interactDir+'groupsActions.php',ajaxConfig);
	}
}