
var SettingsCat = "";
var SettingsID = 0;

function loadSettings(sType){
	$.get('settings.php?category='+sType, {} , function(data){
		newContext('Settings'+sType);
		$("#Settings"+sType).append(data);
	});
}

function loadPackages(){
	$.get('managepackages.php', {} , function(data){
		newContext('Packages');
		$("#Packages").append(data);
	});
}

function viewProfile(uID){
	$.get('profile.php?userid='+uID, {} , function(data){
		newContext('Profile'+uID);
		$("#Profile"+uID).append(data);
		$("#Profile"+uID).append('<br /><br /><br /><br />');
		$.post('get.php', {type: 'xml' , section: 'profile' , uID: uID } , function(data){
			$('table', data).each(function(z){
				$('error', data).each(function(a){ alert('An error occured: ' + a); });
				loadSubmissionsDrawResultsTable("Profile"+uID, 'Comments and ratings left by members:');
				$('row', data).each(function(a){
					pID = $(this).find('id').text();
					$("#Profile"+uID + "ResTable").append("<tr id=\"row" + pID + "\"><td class=\"evenrow\"><b>Member:</b> "+ $(this).find('user').text() +"</td>" +
						"<td class=\"evenrow\">Type: " + $(this).find('type').text() + "</td>" +
						"<td class=\"evenrow\">Rating: "+ $(this).find('rating').find('value').text() +" <img src=\""+ $(this).find('rating').find('image').text() +"\" alt=\"Rating Name\"/></td></tr>" + 
					 	"<tr><td colspan=\"3\" class=\"oddrow\" id=\"Message"+pID+"\"></td>");
					$("#Message"+pID).load('get.php', { section: 'profilemesg', pID: pID});
				});
				//$("#SubmissionResTable").append("<tr><td colspan=\"2\" class=\"hdr\">&nbsp;</td></tr>");
			});
		});
	});
}


var ABperPg = "";
var ABpgNum = "";

function accountBalanceLoad(){
	args = new Object();
	if(ABperPg != ""){
		args.itemsperpage = ABperPg;
	}
	if(ABpgNum != ""){
		args.pg = ABpgNum;
	}
	$.post('accountbalance.php', args , function(data){
		$("#AccountBal").html(data);
	});	
}

function accountbalancePerPage(pPg){
	ABperPg = pPg;
	ABpgNum = "";
	accountBalanceLoad();
}

function accountbalancePage(pgN){
	ABpgNum = pgN;
	accountBalanceLoad();
}

function loadBalance(){
	newContext('AccountBal');
	ABpgNum = "";
	ABperPg = "";
	accountBalanceLoad();
}

function editProfile(){
	$.get('editprofile.php', {} , function(data){
		newContext('editProfile');
		$("#editProfile").append(data);
		generate_wysiwyg('backgroundText');
	});	
}

function ChangeUnPwDo(){
	pass = $("#ChgUPpass").attr('value');
	vpass = $("#ChgUPvpass").attr('value');
	$.post('changeunpw.php', {act: 'changeunpw' , password: pass , vpassword: vpass } , function(data){
		$("#ChangeUnPw").html(data);
	});
}

function changeAccount(){
	$.get('changeunpw.php', {} , function(data){
		newContext('ChangeUnPw');
		$("#ChangeUnPw").html(data);
	});	
}

function settingsEditPost(Type){
	formCat = SettingsCat;
	formID = SettingsID;
	if(Type == "wysiwyg"){
		updateTextArea("SettingsEditFormHTML");
		formEDIT = $("#SettingsEditFormHTML").attr('value');
	}else{
		formEDIT = $("#SettingsEditFormTEXT").attr('value');
	}
	$.post('settings.php', { act: 'save_setting', category: formCat , value: formEDIT , settingid: formID } , function(data) {
		doneContext();
	});
}

function settingsEdit(sType, sId){
	SettingsCat = sType;
	SettingsID = sId;
	$.post('settings.php', {category: sType , view: 'edit' , settingid: sId } , function(data){
		newContext('SettingEdit'+sId);
		$("#SettingEdit"+sId).html(data);
		if($("#SettingsEditFormType").attr("value") == "wysiwyg") {
			generate_wysiwyg("SettingsEditFormHTML");	
		}
	}); 
}


function pageLoader(){
	leftMenuLoad();
}

function mainContents(){
	newContext('Main');
	$("#Main").load('get.php', {type: 'html', section: 'mainpage'} );
}

function updateExpectations(){
	$("#mentorRequirementsStatus").html('<b>Saving....</b>');
	var expectations = $("#mentorRequirements").attr('value');
	
	$.post('action.php', {section: 'submission', action: 'expectations', text: expectations }, function(a){
		$('error', a).each(function(d){
			alert('An error occurred: ' + $(this).text());
		});
		$("#mentorRequirementsStatus").html('<b>Saved</b>');
	});
}

function loadAnnoucements(extra){	
	// need to get the announcements ...
	drawPrettyTable('AnnoucementsArea',"Annoucements",'announceTable');
	$('#announceTable').append('<tr id="hdr"><td class="hdr"><b>Posted</b></td><td class="hdr"><b>Message</b></td></tr>');
	if(extra == 'true') $("#hdr").append('<td class="hdr">For</td><td class="hdr">Options</td>');
	$.post('get.php', { type: 'xml', section: 'annoucements', extra: extra } , function(data){
		$('error', data).each(function(a){
			alert('An error occurred: ' + $(this).text());
		});
		i = 0;
		$('row', data).each(function(a){
			rowclass = ((i % 2) == 0) ? "evenrow" : "oddrow";
			rowContent = '<tr>' + 
					'<td class="' + rowclass + '">' + $(this).find('created').text() + '</td>' +
					'<td class="' + rowclass + '">' + $(this).find('message').text() + '</td>';
			if(extra == 'true'){
				rowContent += 
					'<td class="' + rowclass + '">' + $(this).find('for').text() + '</td>';	
				rowContent += 
					'<td class="' + rowclass + '"><input type="button" value="Delete" onclick="annoucementDel(\'' + $(this).find('id').text() + '\')" /></td>';
			}
			rowContent += '</tr>';
			$('#announceTable').append(rowContent);
			i++;
		});
	});
}

function annoucementDel(id){
	$.post('action.php', {section: 'annoucement' ,action: 'del' , id: id}, function(data){
		$('error', data).each(function(a){
			alert('An Error Occurred: ' + $(this).text());
		});
		$('success', data).each(function(a){
			$("#AnnoucementsArea").html('');
			loadAnnoucements('true');
		});
	});
}

function annoucementAdd(){
	mesg = $('#annoucementMessage').attr('value');
	For = $("#annoucementFor").val();
	
	$.post('action.php', {section: 'annoucement' ,action: 'add' , mesg: mesg, For: For}, function(data){
		$('error', data).each(function(a){
			alert('An Error Occurred: ' + $(this).text());
		});
		$('success', data).each(function(a){
			$("#annoucementMessage").attr('value','');
			$("#AnnoucementsArea").html('');
			loadAnnoucements('true');
		});
	});
}

function controlAnnouncements(){
	// load the annoucements control page ...
	newContext('CtrlAnnounce');
	$("#CtrlAnnounce").append('<br /><br /><div id="addAnnoucement"></div>');
	$("#CtrlAnnounce").append('<div id="AnnoucementsArea"></div>');
	loadAnnoucements('true');
	$("#addAnnoucement").append('Annoucement Message:<br />');
	$("#addAnnoucement").append('<textarea id="annoucementMessage" rows="5" cols="60"></textarea><br /><br />');
	$("#addAnnoucement").append('<select id="annoucementFor"><option value="mentor">Mentors</option><option value="member">Members</option></select><br /><br />');
	$("#addAnnoucement").append('<input type="button" class="btn" value="Add" onclick="annoucementAdd()" /><br /><br />');
	
}


function loadWorkingWith(cat){
	drawPrettyTable("my" + cat , cat + " who you are currently working with." , 'my'+cat+'Table');
	if(cat == "Members"){
		$("#my"+cat+"Table").append('<tr><td class="hdr">Member</td><td class="hdr">Subscribed</td><td class="hdr">Actions</td></tr>');
	}else{
		$("#my"+cat+"Table").append('<tr><td class="hdr">Mentor</td><td class="hdr">Actions</td></tr>');
	}
	$.post('get.php', {type: 'xml', section: 'workingwith'}, function(data){
		//$('error', data).each(function(a){ alert("An Error Occurred: " + $(this).data()); });
		i = 0;
		$('row', data).each(function(a){
			rowclass = ((i % 2) == 0) ? "evenrow" : "oddrow";
			rowContents = '<tr>';
			rowContents += '<td class="'+ rowclass +'"><a href="#" onclick="viewProfile(\'' + $(this).find('uidcode').text() + '\')" >' + $(this).find('name').text() + '</a></td>';
			if(cat == "Members"){
				rowContents += '<td class="' + rowclass + '">' + $(this).find('subscribed').text() + '</td>';
			}
			rowContents += '<td class="'+ rowclass + '"><input type="button" class="btn" value="Stop Working With This '+ ((cat == "Members") ? "Member" : "Mentor") +'" onclick="declineCont(\'0\',\'' + $(this).find('uid').text() + '\')"></td>';
			rowContents += '</tr>';
			$("#my"+cat+"Table").append(rowContents);
			i++;
		});
	});
}

function loadDeclines(){
	newContext('declines');
	$("#declines").load('userdecline.php', {});
}

// decline continuing to work with a specific member/mentor ...

function declineContDo(event){
	event.stopPropogation = true;
	$("#formArea").css('display','none');
	$("#BodyStuff").css('display','block');
	var otherid = $("#ReplyOtherID").attr('value');
	var id = $("#ReplyID").attr('value');
	updateTextArea("ReplyMesg");
	var mesg = $("#ReplyMesg").attr('value');
	
	$.post('action.php', { section: 'submission' , action: 'declinecont' , id: id , otherid: otherid, mesg: mesg} , function(res){
		$('error',res).each(function(g){
			alert('An error occured: ' + $(this).text());
		});
		doneContext();
	});
	
	return false;
}

function declineCont(id, otherid){
	newContext('declineCont');
	$("#declineCont").append('<form id="formdeclineCont" action=""></form>');
	$("#formdeclineCont").bind('submit', {} , declineContDo);
	$("#formdeclineCont").append('<input type="hidden" name="id" id="ReplyID" value="' + id + '" />');
	$("#formdeclineCont").append('<input type="hidden" name="otherid" id="ReplyOtherID" value="' + otherid + '" />');
	if(id == '0'){
		$("#formdeclineCont").append('<b>Decling to continue to work with mentor/member on All Submissions</b><br />');
	}else{
		$("#formdeclineCont").append('<b>Decling to continue to work with mentor/member on #' + id + '</b><br />');
	}
	$("#formdeclineCont").append('<textarea id="ReplyMesg"></textarea><br />');
	generate_wysiwyg("ReplyMesg");
	$("#formdeclineCont").append('<input type="submit" class="btn" value="Done" name="ReplyDo"/> ');
	$("#formdeclineCont").append('<input type="button" class="btn" value="Back" name="ReplyCanel" onclick="declineContCancel()" />');
}

function declineAccept(id)
{
	$("#declines").load('userdecline.php', {action: 'accept', id: id});
}

function declineReject(id)
{
	$("#declines").load('userdecline.php', {action: 'decline', id: id});
}
function loadEmailSettings()
{
	newContext('EmailSettings');
	$("#EmailSettings").load('emailsettings.php', {});
}

function saveEmailSettings()
{
	var args = new Object();
	args.action = 'save';
	args.remails = $("#remails").attr('value');
	args.userdecline = $("#userdecline").attr('value');
	args.mesg = $("#message").attr('value');
	args.newsubmission = $("#newsubmission").attr('value');
	args.approvedsubmission = $("#approvedsubmission").attr('value');
	args.acceptsubmission = $("#acceptsubmission").attr('value');
	args.replysubmission = $("#replysubmission").attr('value');
	args.completesubmission = $("#completesubmission").attr('value');
	
	$("#EmailSettings").load('emailsettings.php', args);
}
