var listingSet = "subscriptions";

function listings_draw_start(subSet){
	// clear content ...
	$("#Listings").html('<input type="button" class="btn" Value="Subscriptions" onclick="listings_get_draw(\'subscriptions\')" /> <input type="button" class="btn" value="Individual Submissions" onclick="listings_get_draw(\'individual\')" />');
	listingSet = subSet;
	if(subSet == 'subscriptions'){
		$("#Listings").append('<br /><br /><br /><table cellpadding="2" cellspacing="0" border="0" width="100%" id="listingsTable"><tr><td></td><td></td></tr></table>');
	}else{
		loadSubmissionsDrawResultsTable('Individual Submissions');
		$("#SubmissionResTable").append("<tr><td class=\"hdr\">Created</td><td class=\"hdr\">User</td><td class=\"hdr\">Type</td><td class=\"hdr\">About</td><td class=\"hdr\">Actions</td></tr>");
	}
}

function listingApplyCancel(event){
	event.stopPropogation = true;
	doneContext();
	return false;
}

function listingApplyDo(event){
	event.stopPropogation = true;
	id = $("#ApplyID").attr('value');
	updateTextArea("ApplyMesg");
	mesg = $("#ApplyMesg").attr('value');
	$.post('action.php', { section: 'submission' , action: 'apply' , id: id , mesg: mesg} , function(res){
		$('error',res).each(function(g){
			alert('An error occured: ' + $(this).text());
		});
		listings_get_draw(listingSet);
		doneContext();
	});
	
	return false;
}

function listingApply(id){
	newContext('ListingApply');
	$("#ListingApply").html('<form id="formListingApply" action=""></form>');
	$("#formListingApply").bind('submit', {} , listingApplyDo);
	$("#formListingApply").bind('reset', {} , listingApplyCancel);
	$("#formListingApply").append('<input type="hidden" name="id" id="ApplyID" value="' + id + '" />');
	$("#formListingApply").append('<b>Applying to #' + id + '</b><br /><br /><b>Expectations of member:</b>');
	$("#formListingApply").append('<div id="expectations"></div><br /><br />');
	$("#expectations").load('get.php', {section: 'submissiondetails' , id: id});
	$("#formListingApply").append('Explain to member how you plan to meet his/her expectations:');
	$("#formListingApply").append('<textarea name="ApplyMesg" id="ApplyMesg"></textarea><br />');
	generate_wysiwyg("ApplyMesg");
	$("#formListingApply").append('<b>Giving advice, making proposals or answering questions for free will result in your immediate expulsion as a mentor on RACsuccess-One-On-One service</b><br /><br />');
	$("#formListingApply").append('<input type="submit" class="btn" value="Apply" name="ApplyDo"/> ');
	$("#formListingApply").append('<input type="reset" class="btn" value="Cancel" name="ApplyCanel" />');
}

function listings_get_draw(subSet){
	listings_draw_start(subSet);
	if(!genStart()) return;
	
	$.post('get.php',{ type: 'xml' , section: 'submissions' , listings: 'true' , listingtype: subSet }, function(g){
		var i = 0;
		$('error',g).each(function(g){
			alert('An error occured: ' + $(this).text());
		});
		$('datarow',g).each(function(b){
			if(subSet == 'subscriptions'){
				// draw squares ...
				if((i % 2) == 0){
					$("#listingsTable").append('<tr id="listingRow' + i/2 + '"></tr>');
				}
				pageData = "";
				pageData += '<td align="center" width="350" height="300" valign="top">';
				pageData += '<table cellpadding="2" cellspacing="0" border="0" align="center" style="text-align:left; background-color:#EDF1F6; border:groove 2px #FFFFFF;" width="340" height="100%">';
				pageData += '<tr>';
				pageData += '<td style="border-bottom:solid 1px #CCCCCC;" height="44">';
				pageData += '<table cellpadding="0" cellspacing="0" border="0" width="100%">';
				pageData += '<tr>';
				pageData += '<td align="left">';
				pageData += '<div style="margin-bottom:5px;"><b>Created: </b>' + $(this).find('created').text() + '</div>';
				pageData += "<div style=\"margin-bottom:5px;\"><b>User:</b> <a href=\"#\" onclick=\"viewProfile('"+ $(this).find('user').find('mid').text() +"')\">" + $(this).find('user').find('name').text() + "</a> <img src=\"" + $(this).find('user').find('image').text() + "\"/></div>";
				pageData += '</td>';
				pageData += '<td align="right">';
				pageData += '<img src="'+ $(this).find('packageicon').text() + '"/> ' + $(this).find('user').find('package').text();
				pageData += '</td>';
				pageData += '</tr>';
				pageData += '</table>';
				pageData += '</td>';
				pageData += '</tr>';
				pageData += '<tr>';
				pageData += '<td><b>Expectations:</b><br /><br /><div style="height:180px; width: 330px;  overflow-x: scroll;" id="expectationsBox' + i + '"> </div>';
				pageData += '<div align="right"><input type="button" name="btnApply" value="Apply" class="btn" onClick="listingApply('+ $(this).find('id').text() + ')"></div></td>';
				pageData += '</tr>';
				pageData += '</table>';
				pageData += '</td>';
				$("#listingRow"+ (i-(i%2))/2).append(pageData);
				mID = $(this).find('id').text();
				$("#expectationsBox" + i).load('get.php', { section: 'submissiondetails', id: mID });
			}else{
				// draw rows ..
				row = $(this).attr('row');
				rowclass = $(this).attr('row') % 2 ? "evenrow" : "oddrow";
				
				$("#SubmissionResTable").append("<tr id=\"dataRow" + row + "\"></tr>");
				
				$("#dataRow" + row).append("<td class=" + rowclass + ">" + $(this).find('created').text() + "</td>");
				$("#dataRow" + row).append("<td class=" + rowclass + "><a href=\"#\" onclick=\"viewProfile('"+ $(this).find('user').find('mid').text() +"')\">" + $(this).find('user').find('name').text() + "</a> <img src=\"" + $(this).find('user').find('image').text() + "\"/></td>");
				$("#dataRow" + row).append("<td class=" + rowclass + ">" + $(this).find('type').text() + "</td>");
				$("#dataRow" + row).append("<td class=" + rowclass + ">" + $(this).find('subject').text() + "</td>");
				
				$("#dataRow" + row).append("<td class=" + rowclass + " id=\"actionsRow" + row + "\" style=\"white-space:nowrap;\"></td>");
				$("#actionsRow" + row).html("");
			
				$('button', this).each(function(g){
					$("#actionsRow" + row).append("<input type=\"button\" name=\"" + $(this).find("name").text() + "\" value=\"" + $(this).find("value").text() + "\" class=\"" + $(this).find("class").text() + "\" onclick=\"" + $(this).find("onclick").text() + "\" /> ");
				});
			}	
			i++;
		});
		if(subSet == 'individual'){
			$("#SubmissionResTable").append("<tr><td colspan=\"10\" class=\"hdr\">&nbsp;</td></tr>");
		}
		genStop();
	});
}

function listingsView(){
	newContext('Listings');
	listings_get_draw('subscriptions');
}
