var ProjectManager = function(){
	var originalHTML;
	var tabBody;
	var needTypes;
	var project, newNeed;
	var bUseTinyMceForNeedsForNeeds = false;  
	
	var idMap = {
		title: 'project-name',
		description: 'project-desc',
		location: 'project-location',
		startdate: 'project-start',
		enddate: 'project-end',
		contact_name: 'project-contact-name',
		contact_email: 'project-contact-email',
		contact_phone: 'project-contact-tel',
		contact_organization: 'project-contact-company',
		privacy: 'project-privacy-setting'
	};
	
	var needIdMap = {
		description: 'need-desc',
		needId: 'needTypeSelection',
		location: 'need-location',
		enddate: 'need-end',
		startdate: 'need-start',
		needtypeSkills: 'other-skills-detail',
		needtypeMaterial: 'other-material-detail'
	};
	
	function showSkills(){
		$('#needTypeSkills').show();
		$('#needTypeMaterial').hide();
		$('#needTypeFinancial').hide();
		$('#skillButton').addClass('active');
		$('#financialButton').removeClass('active');
		$('#materialButton').removeClass('active');
	}
	
	function showMaterials(){
		$('#needTypeSkills').hide();
		$('#needTypeMaterial').show();
		$('#needTypeFinancial').hide();
		$('#skillButton').removeClass('active');
		$('#financialButton').removeClass('active');
		$('#materialButton').addClass('active');
	}
	
	function showFinancials(){
		$('#needTypeSkills').hide();
		$('#needTypeMaterial').hide();
		$('#needTypeFinancial').show();
		$('#skillButton').removeClass('active');
		$('#materialButton').removeClass('active');
		$('#financialButton').addClass('active');
	}
	
	function getNeedTypesHtml(){
		//needTypes
		var needEntry = '';
		needEntry += '<ul id="needTypeTabs">';
		needEntry += '<li>'
		needEntry += '<a href="#" id="skillButton" class="active" onclick="ProjectManager.showSkills();return false;">Skill and Labor</a>';
		needEntry += '</li>'
		needEntry += '<li>'
		needEntry += '<a href="#" id="materialButton" onclick="ProjectManager.showMaterials();return false;">Equipment and Materials</a>';
		needEntry += '</li>'
		needEntry += '<li>'
		needEntry += '<a href="#" id="financialButton" onclick="ProjectManager.showFinancials();return false;">Financial</a>';
		needEntry += '</li>'
		needEntry += '</ul>'
		needEntry += '<div class="clear"></div>';
		needEntry += '<div><div id="needTypeSelection" class="needTypeSelection">';
		needEntry += '<div id="needTypeSkills">';
		needEntry += '<div class="age-sex">';
		needEntry += '<ul>';
		needEntry += '<li>';
		needEntry += '<input type="checkbox" name="sex-male" id="male" />';
		needEntry += '<label for="male">Male</label>';
		needEntry += '</li>';
		needEntry += '<li>';
		needEntry += '<input type="checkbox" name="sex-female" id="female" />';
		needEntry += '<label for="female">Female</label>';
		needEntry += '</li>	';
		needEntry += '<li>';
		needEntry += 'Between the ages of <input type="text" size="2" maxlength="2" id="min-age" /> and <input type="text" size="2" maxlength="2" id="max-age" />';
		needEntry += '</li>';
		needEntry += '</ul>';
		needEntry += '</div>';

		needEntry += '<ul class="skills-labor-list">';
		
		for(var i = 0; i < needTypes.skills.length; i++){
			var t = needTypes.skills[i];
			needEntry += '<li><input type="radio" name="needs-select" id="skill' + t.id + '" value="' + t.id + '" />';
			needEntry += '<label for="skill' + t.id + '">' + t.name + '</label>';
			if(t.isOther){
				needEntry += '<input type="text" value="" id="other-skills-detail" maxlength="255" />';
			}
			needEntry += '</li>';
		}
		needEntry += '</ul></div>';
		
		needEntry += '<div id="needTypeFinancial" style="display:none;">';
		for(var i= 0; i < needTypes.financials.length; i++) {
			var t = needTypes.financials[i];
			needEntry += '<p>';
			needEntry += '<label for="finance' + t.id + '"><strong>' + t.name + '(required)</strong></label><br />';
			needEntry += '<input type="text" id="donation_url" maxlength="255" size="100" />';
			needEntry += '</p>';
		}
		needEntry += '</div>';
		
		needEntry += '<div id="needTypeMaterial" style="display:none;">';
		needEntry += '<ul class="equip-mats-list">';
		for(var i = 0; i < needTypes.materials.length; i++){
			var t = needTypes.materials[i];
			needEntry += '<li><input type="radio" name="needs-select" id="material' + t.id + '" value="' + t.id + '" />';
			needEntry += '<label for="material' + t.id + '">' + t.name + '</label>';
			if(t.isOther){
				needEntry += '<input type="text" value="" id="other-material-detail" maxlength="255" />';
			}
			needEntry += '</li>';
		}
		needEntry += '</ul></div><div class="clear"></div></div></div>';
		
		return needEntry;
	}
	
	function getRolledUpNeedHtml(need, i){
		var html = '';
		html += '<div class="need-wrapper need-rolled-up" id="need_div_' + need.id + '">';
		html += '<div class="need-summary">';
		html += '<div class="need-number">' + i + '</div>';
		html += '<h2>' + need.title + '</h2>';
		html += '<h3><strong>' + need.description + '</strong></h3>';
		html += '<p>';
		if(need.needSummary == '[other]'){
			html += need.needtype;
		} else {
			html += need.needSummary;
		}
		if(need.sex == 'M'){
			html += ' (male)';
		} else if(need.sex == 'F'){
			html += ' (female)';
		}
		
		if(need.agestart){
			html += ' between the ages of ' + need.agestart + ' and ' + need.ageend;
		}
		
		if(need.location){
			html += ' in ' + need.location;
		} else if(need.inheritLocation == '1'){
			html += ' at the opportunity\'s location';
		} else {
			html += ' at any location';
		}
		
		if(need.startdate){
			html += ' from ' + need.startdate + ' to ' + need.enddate;
		} else if(need.inheritDate == '1'){
			html += ' during the opportunity\'s time line';
		} else {
			html += ' at any time';
		}
		
		html += '.</p>';
		html += '<a href="/project_details.php?id=' + project.id +'&need='+ need.id +'" title="edit this need">edit this need</a>| ';
		html += '<a href="#" onclick="ProjectManager.deleteNeed(' + need.id + '); return false;">delete this need</a>';
		html += '</div></div>';
		
		return html;
	}
	
	function getNeedEntry(){
		var needEntry = '';
		needEntry += '<div id="need-outer"><div class="need-wrapper" id="need-entry-wrapper">';
		needEntry += '<h2>Tell us about your need</h2>';
		needEntry += '<div id="error-div"></div>'
		needEntry += '<p>';
		needEntry += '<label for="need-title"><strong>Need Title: (required)</strong></label><br />';
		needEntry += '<input type="text" id="need-title" maxlength="255" size="100" />';
		needEntry += '</p>';
		needEntry += '<p>';
		needEntry += '<label for="need-tags"><strong>Need Keywords: (required)</strong></label><br />';
		needEntry += '<input type="text" id="need-tags" maxlength="255" size="100" />';
		needEntry += '</p>';
		needEntry += '<p>';
		needEntry += '<label for="need-desc"><strong>Need Description: (required)</strong></label><br />';
		needEntry += '<textarea id="need-desc" maxlength="255" cols="100" rows="10"></textarea>';
		needEntry += '</p>';
		
		needEntry += getNeedTypesHtml();
		
		needEntry += '<div class="need-quantity">';
		needEntry += '<h3>Need Quantity</h3>';
		needEntry += '<label for="need-quantity"><strong>How many of this need do you need?</strong> <span class="hint">(if you need 500 pints of blood, type in 500 &mdash; <strong>numeric value only</strong>)</span></label>';
		needEntry += '<input type="text" name="need-quantity" id="need-quantity" maxlength="255" size="100" />';
		needEntry += '</div>';
		needEntry += '<div class="need-location">';
		needEntry += '<h3>Need Location</h3>';
		needEntry += '<ul>';
		needEntry += '<li>';
		needEntry += '<input type="radio" checked="checked" name="need-location" value="same" id="same-location" />';
		needEntry += '<label for="same-location"><strong>Yes,</strong> it\'s for the same location <span class="hint">(' + project.location + ')</span></label>';
		needEntry += '</li>';
		needEntry += '<li>';
		needEntry += '<input type="radio" name="need-location" id="different-location" value="different" />';
		needEntry += '<label for="different-location"><strong>No,</strong> it\'s in a different location:</label>';
		needEntry += '<div id="otherLocationDiv" style="display:none;"><input type="text" name="project-location" id="need-location" /></div>';
		needEntry += '</li>';
		needEntry += '<li>';
		needEntry += '<input type="radio" name="need-location" id="any-location" value="does not matter"/>';
		needEntry += '<label for="any-location">The location <strong>doesn\'t matter</strong>.</label>';
		needEntry += '</li>';
		needEntry += '</ul>';
		needEntry += '</div>';
		needEntry += '<div class="need-time">';
		needEntry += '<h3>Need Time Frame</h3>';
		needEntry += '<ul>';
		needEntry += '<li>';
		needEntry += '<input type="radio" checked="checked" name="need-time" id="same-time" />';
		needEntry += '<label for="same-time"><strong>Yes,</strong> it\'s in the same time frame as my opportunity <span class="hint">(' + project.startdate + ' - ' + project.enddate + ')</span></label>';
		needEntry += '</li>';
		needEntry += '<li>';
		needEntry += '<input type="radio" name="need-time" id="different-time" />';
		needEntry += '<label for="different"><strong>No,</strong> it\'s in a different time frame:</label>';
		needEntry += '<div id="otherTimeDiv" style="display:none;">';
		needEntry += '<p>';
		needEntry += '<label for="need-start">Start Date:</label>';
		needEntry += '<input type="text" name="need-start" id="need-start" /> yyyy-mm-dd';
		needEntry += '</p>';
		
		needEntry += '<p>';
		needEntry += '<label for="need-end">End Date:</label>';
		needEntry += '<input type="text" name="need-end" id="need-end" /> yyyy-mm-dd';
		needEntry += '</p>';
		needEntry += '</div>';
		needEntry += '</li>';
		needEntry += '<li>';
		needEntry += '<input type="radio" name="need-time" id="any-time" />';
		needEntry += '<label for="any-time">The time frame <strong>doesn\'t matter</strong>.</label>';
		needEntry += '</li>';
		needEntry += '</ul>';
		needEntry += '</div></div></div>';
		needEntry += '<button onclick="ProjectManager.addNeed(); return false;" class="add-new-need">Save this need</button> or <button onclick="ProjectManager.reset(); return false;" title="Cancel">Cancel</button><br>';
		return needEntry;
	}	
	
	function showProjectEntry(afterRender){
		var html = '';
		
		html += '<div id="post-need" style="opacity:0;">';
		html += '<div id="step1">';
		html += '<h1>First, tell us a little bit about your opportunity</h1>';
		html += '<input type="hidden" name="project-id" id="project-id" value="-1" />';
		html += '<div id="error-div"></div>'
		html += '<fieldset id="p-desc">';
		html += '<legend>Opportunity description</legend>';
		html += '<p>';
		html += '<label for="project-type">Opportunity Type: (required)</label>';
		html += '<select name="project-type" id="project-type">';
		html += '<option name="service" value="1">Service</option>';
		html += '<option name="management" value="2">Management</option>';
		html += '</select> <span id="type-info">Opportunities that have needs</span>';
		html += '</p>';		
		html += '<p>';
		html += '<label for="project-name">Opportunity Name: (required)</label>';
		html += '<input type="text" name="project-name" id="project-name"/>';
		html += '</p>';
		html += '<p>';
		html += '<label for="project-tags">Opportunity Keywords: (required)</label>';
		html += '<input type="text" name="project-tags" id="project-tags"/>';
		html += '</textarea>';
		html += '</p>';
		html += '<p>';
		html += '<label for="project-desc">Opportunity Description: (required)</label>';
		html += '<textarea name="project-desc" id="project-desc" rows="10" cols="50">';
		html += '</textarea>';
		html += '</p>';
				
		html += '<p>';
		html += '<label for="project-privacy-setting">Privacy: (required)</label>';
		html += '<select name="project-privacy-setting" id="project-privacy-setting">';
		html += '<option name="public" value="1">Public</option>';
		html += '<option name="private" value="2">Private</option>';
		html += '<option name="closed" value="3">Closed</option>';
		html += '</select> <span id="privacy-info">Anyone can join</span>';
		html += '</p>';
		
		html += '<p>';
		html += '<label for="project-group-id">Group: (required)</label>';
		html += '<select name="project-group-id" id="project-group-id">';
		html += groupsSelects;
		html += '</select>&nbsp;&nbsp;<button onclick="window.location=\'group_register.php\'; return false;">Create a Group</button>';
		html += '</p>';
		
		html += '<p>';
		html += '<label for="project-start">Start Date: (required)</label>';
		html += '<input type="text" name="project-start" id="project-start" /> yyyy-mm-dd';
		html += '</p>';
		
		html += '<p>';
		html += '<label for="project-end">End Date: (required)</label>';
		html += '<input type="text" name="project-end" id="project-end" /> yyyy-mm-dd';
		html += '</p>';
		
		html += '</fieldset>';
		html += '<fieldset id="p-location">';
		html += '<legend>Opportunity location</legend>';
		html += '<p>';
		html += '<label for="project-location">Address, city, or country: (required)</label>';
		html += '<input type="text" name="project-location" id="project-location" />';
		html += '<small class="caption">Where will volunteers need to go to help? Enter an address or approximate location.</small>';
		html += '</p>';
		html += '</fieldset>';
		html += '<fieldset id="p-contact">';
		html += '<legend>Contact Information</legend>';
		html += '<p>Please provide the name of the person or organization that can be contacted about this opportunity.</p>';
		html += '<p>';
		html += '<label for="project-contact-name">Name: (required)</label>';
		html += '<input type="text" name="project-contact-name" id="project-contact-name" value="" disabled/>';
		html += '</p>';
		html += '<p>';
		html += '<label for="project-contact-email">Email: (required)</label>';
		html += '<input type="text" name="project-contact-email" id="project-contact-email" value="" disabled/>';
		html += '</p>';
		html += '<p>';
		html += '<label for="project-contact-tel">Telephone:</label>';
		html += '<input type="text" name="project-contact-tel" id="project-contact-tel" />';
		html += '</p>';
		html += '<p>';
		html += '<label for="project-contact-company">Company</label>';
		html += '<input type="text" name="project-contact-company" id="project-contact-company" />';
		html += '</p>';
		html += '</fieldset>';
		html += '<div id="progress"><img src="img/loader.gif" alt="loading" /></div>';
		html += '<div id="save_continue">'
		html += '<button onclick="ProjectManager.processAddProject();return false;">Save and Continue</button>';
		html += ' or <a href="#" title="Cancel" onclick="ProjectManager.showBrowse();return false;">Cancel</a>';
		html += '</div>'
		html += '</div>';
		
		tabBody.children("div:first").fadeTo("slow", 0, function(){
			$('#tabBody').height('auto');
			tabBody.html(html); 
			tabBody.children("div:first").fadeTo("slow", 1);
			tinyMCE.init({
				mode : "textareas",
				theme : "simple"
			});
			
			$('#project-contact-name').val(username);
			$('#project-contact-email').val(useremail);
			
			$('#project-privacy-setting').change(function() {
				if( $('#project-privacy-setting').val() == 1 ) {
					$('#privacy-info').html('Anyone can join');
				} else if( $('#project-privacy-setting').val() == 2 ) {
					$('#privacy-info').html('Request to join or by Invitation');
				} else {
					$('#privacy-info').html('Invite only and does not display the group in the Gohub directory');
				}
			});
						
			$('#project-type').change(function() {
				if( $('#project-type').val() == 1 ) {
					$('#type-info').html('Opportunities that have needs');
				} else if( $('#project-type').val() == 2 ) {
					$.get('api/utils/getGroups.php', function(data, status){
						var r = eval('(' + data + ')');
						$('#man_group').html(r);
					});
					$('#type-info').html('Opportunities for internal management of your group or organization');
					$('#management_modal').modal();
				}
			});
									
			if(afterRender){
				afterRender();
			}
			
			$('#project-start').datePicker();
			$('#project-end').datePicker();
			$('#need-start').datePicker();
			$('#need-end').datePicker();		
		});
		
	}
	
	function management_modal() {
		$('#management_modal').modal();
	}
	
	function reset() {
		tabBody.children("div:first").fadeTo("slow");
		resetTabs();
	};
	
	function showNeedEntry(showMessage, displayForm){
		var html = '';
		html += '<div id="post-need" style="opacity:0;">';
		html += '';
		html += '<div id="step2">';
		html += '<h1>Your project</h1>';
		html += '<div id="project-carryover">';
		if(showMessage){
			html += '<div id="project-message">';
			html += '<h2>Opportunity Created!</h2>';
			html += '</div>';
		}
		html += '<div class="carryover-desc">';
		html += '<h3>' + project.title + '</h3>';
		html += '<p>' + project.description + '</p>';
		html += '<p>' + project.location + '</p>';
		html += '</div><div class="carryover-contact">';
		html += '<h3>Contact Information</h3>';
		html += '<ul>';
		html += '<li>' + project.contact_name + '</li>';
		html += '<li>' + project.contact_email + '</li>';
		if( project.contact_phone ) {
			html += '<li>' + project.contact_phone + '</li>';
		}
		if( project.contact_organization ) {
			html += '<li>' + project.contact_organization + '</li>';
		}
		html += '</ul>';
		html += '</div><div class="clear"></div>';
		html += '<a href="#" onclick="ProjectManager.editProjectDetails(); return false;">edit this project</a> | ';
		html += '<a href="#" onclick="ProjectManager.deleteProject(' + project.id + '); return false;">delete this project</a>';
		html += '</div>';
		html += '<h1>What (or who) do you need for this opportunity?</h1>';
		html += '<div id="rolledUpNeeds">';
		for(var i = 0; i < project.needs.length; i++){
			html += getRolledUpNeedHtml(project.needs[i], i+1);
		}
		html += '</div>';
		if(displayForm){
			html += getNeedEntry();			
		} else {
			html += '<div id="needLinks">';
			if(project.needs.length > 0){
				html += '<p><a href="#" onclick="ProjectManager.showNeedForm(); return false;" class="add-new-need">Add another need</a></p>';
			} else {
				html += '<p><a href="#" onclick="ProjectManager.showNeedForm(); return false;" class="add-new-need">Add first need</a></p>';
			}
			html += '<span id="progress"><img src="img/loader.gif" alt="loading" /></span>';
			html += '<a href="#" id="save_continue" onclick="ProjectManager.done();return false;" class="save-continue" title="Save and Continue">I\'m Done</a>';
			html += '</div>';
		}
		
		html += '</div>';
		html += '</div><!--/post-need-->';
		tabBody.children("div:first").fadeTo("slow", 0, function(){
			tabBody.html(html);
			tabBody.children("div:first").fadeTo("slow", 1);
			if(showMessage){
				setTimeout(function(){
					$('#project-message').fadeTo("slow", 0).slideUp("slow");
				}, 2000);
			}

			if(displayForm){
				bindNeedRadioHandlers();
			}
		});
	}
	
	function bindNeedRadioHandlers(){
		$("input[name='need-location']").bind('click', function(e){
			if(e.target.id == 'different-location'){
				$("#otherLocationDiv").show();
			} else {
				$("#otherLocationDiv").hide();
			}
		});

		$("input[name='need-time']").bind('click', function(e){
			if(e.target.id == 'different-time'){
				$("#otherTimeDiv").show();
			} else {
				$("#otherTimeDiv").hide();
			}
		});
	};
	
	function unbindNeedRadioHandlers(){
		$("input[name='need-location']").unbind();
		$("input[name='need-time']").unbind();
	}
	
	function addNeed(){
		var male = $('#male').get(0).checked;
		var female = $('#female').get(0).checked;
		var sex = 'B';
		if(male && !female){
			sex = 'M';
		} else if(!male && female){
			sex = 'F';
		}
		
		var needType = null;
		var needId = null;
		var needsSelection = $("input[name='needs-select']:checked");
		if(needsSelection.length > 0){
			needId = needsSelection.get(0).value;
			if(needTypes.otherIds[needId]){
				needType = $('#' + needTypes.otherIds[needId]).val();
			}
		}
		
		var financial= $('#donation_url').val();
		var extra= null;
		if( financial.length > 0 ) {
			extra= financial;
		}
		
		var location = '';
		var inheritLocation = false;
		var locationChecked = $("input[name='need-location']:checked");
		if(locationChecked.length > 0){
			location = locationChecked.get(0).id;
			if(location == 'different-location'){
				location = $('#need-location').val();
			} else if(location == 'same-location'){
				inheritLocation = true;
				location = null;
			} else {
				location = null;
			}
		}
		
		var timeFrameStart = '';
		var timeFrameEnd = '';
		var timeFrame;
		var inheritTime = false;
		var timeFrameChecked = $("input[name='need-time']:checked");
		if(timeFrameChecked.length > 0){
			timeFrame = timeFrameChecked.get(0).id;
			if(timeFrame == 'different-time'){
				timeFrameStart = $('#need-start').val();
				timeFrameEnd = $('#need-end').val();
			} else if(timeFrame == 'same-time'){
				timeFrameStart = null;
				timeFrameEnd = null;
				inheritTime = true;
			} else {
				timeFrameStart = null;
				timeFrameEnd = null;
			}
		}
		
		var title = $("#need-title").val();
		var desc = "";
		if ( this.bUseTinyMceForNeeds ){
			desc = tinyMCE.get("need-desc").getContent();
		}else{
			desc = $( "#need-desc" ).val();	
		}
		var category = $('#skillButton').hasClass('active') ? 0 : 1;
		var quantity = $('#need-quantity').val();
		
		var data = {
			title: title,
			description: desc,
			quantity: quantity,
			needId: needId,
			agestart: $("#min-age").val(),
			ageend: $('#max-age').val(),
			category: category,
			project: project.id,
			sex: sex,
			tags: $('#need-tags').val()
		};
		if(null != needType){
			data.needtype = needType;
		}
		
		if(null != extra) {
			data.extra= extra;
		}
		
		if(inheritTime){
			data['inheritDate'] = 1;
		} else {
			data['inheritDate'] = 0;
			if(null != timeFrameStart){
				data['startdate'] = timeFrameStart;
				data['enddate'] = timeFrameEnd;
			}
		}
		
		if(inheritLocation){
			data['inheritLocation'] = 1;
		} else {
			data['inheritLocation'] = 0;
			if(null != location){
				data['location'] = location;
			}
		}
		
		$.post('api/need/AddNeed.php', data, function(data, status){
			var r = eval('(' + data + ')');
			if(r.error){
				$('.error').removeClass('error');
				var errors = r.validationErrors;
				var html = '<div id="form-error"><h2>Oops, you forgot something</h2><ul>';
				for(var i = 0; i < errors.length; i++){
					var e = errors[i];
					html += '<li><a href="#' + needIdMap[e.field] + '">' + e.message + '</a></li>';
					$("#" + needIdMap[e.field]).parent().addClass('error');
				}
				html += '</ul></div>';
				$("#error-div").html(html);
			}else{
				project.needs.push(r.newNeed);
				unbindNeedRadioHandlers();
				$('#need-outer').height($('#need-entry-wrapper').height());
				$('.add-new-need').html('Add another need');
				$('#need-entry-wrapper').slideUp('normal', function(){
					showNeedEntry(false, false);
				});
			}
		});
		
	}
	
	function addProject(){
		// TinyMCE converts a double-carriage return to &nbsp;
		// This saves to the db as a non-printable, then displays in the UI
		// as garbage.  Convert any &nbsp to a <br/>
		//
		var rawDescription = tinyMCE.get("project-desc").getContent();
		var desc = rawDescription.replace(/&nbsp;/g, "<br/>");
		//alert( "Stripped: " + desc );
		var data = {
			title: $("#project-name").val(),
			description: desc, //tinyMCE.get("project-desc").getContent(),
			location: $("#project-location").val(),
			startdate: $("#project-start").val(),
			enddate: $("#project-end").val(),
			contact_name: $("#project-contact-name").val(),
			contact_email: $("#project-contact-email").val(),
			contact_phone: $("#project-contact-tel").val(),
			contact_organization: $("#project-contact-company").val(),
			id: $("#project-id").val(),
			privacy: $("#project-privacy-setting").val(),
			groupdID: $("#project-group-id").val(),
			tags: $('#project-tags').val()
		};
		var edit = false;
		var url = 'api/project/AddProject.php';
		if(parseInt(data.id) > 0){
			url = 'api/project/EditProject.php';
			edit = true;
		}
		if(edit){
			data.needs = project.needs;
		}
		project = data;
		$('#save_continue').hide();
		$('#progress').show();
		$.post(url, data, function(data, status){
			var r = eval('(' + data + ')');
			if(r.error){
				var errors = r.validationErrors;
				var html = '<div id="form-error"><h2>Oops, you forgot something</h2><ul>';
				for(var i = 0; i < errors.length; i++){
					var e = errors[i];
					html += '<li><a href="#' + idMap[e.field] + '">' + e.message + '</a></li>';
					$("#" + idMap[e.field]).parent().addClass('error');
				}
				html += '</ul></div>';
				$("#error-div").html(html);
				$('#save_continue').show();
				$('#progress').hide();
			}else{
				if(edit){
					showNeedEntry(false, false);
					$('#save_continue').show();
					$('#progress').hide();
				}else{
					project.id = r.projectId;
					project.needs = [];
					showNeedEntry(true, false);
					$('#progress').hide();
				}
			}
		});
	}
	
	function showBrowser() {
		tabBody.children("div:first").fadeTo("slow", 0, function(){
			tabBody.html('<div id="project-list">' + needsBrowserContent + '</div>');
			$('#tabBody').height('auto');
			tabBody.children("div:first").fadeTo("slow", 1);
		});
				
		$('#browse').addClass('active-tab');
		$('#search').removeClass('active-tab');
		$('#post').removeClass('active-tab');
	};
	
	function showSearch() {
		tabBody.children("div:first").fadeTo("slow", 0, function(){
			$('#tabBody').addClass('results');
			tabBody.html('<div id="searchWrapper">' + searchContent + '</div>');
			tabBody.children("div:first").fadeTo("slow", 1);
		});
		
		$('#browse').removeClass('active-tab');
		$('#search').addClass('active-tab');
		$('#post').removeClass('active-tab');
	};
	
	var that = {};
	
	that.init = function(username,email){		
		// Bind to the 'browse' anchor
		$('#browse').bind('click', function(e){
			e.preventDefault();
			e.stopPropagation();
			showBrowser();
		});
		
		// Bind to the 'search' anchor
		$('#search').bind('click', function(e){
			e.preventDefault();
			e.stopPropagation();
			showSearch();
		});
		
		// Bind to the 'cancelProject' anchor
		$('#cancelProject').bind('click', function(e){
			e.preventDefault();
			e.stopPropagation();
			showBrowser();
		});
		
		// Bind to the 'add a project' anchor
		$("#post-need-link").bind("click", function(e){
			e.preventDefault();
			e.stopPropagation();
			showProjectEntry();
		});
		
		tabBody = $("#tabBody");
		originalHTML = tabBody.html();
		
		$.get('api/need/GetNeedTypes.php', function(data, status){
			var r = eval('(' + data + ')');
			needTypes = r;
			needTypes.otherIds = {};
			for(var i = 0; i < needTypes.skills.length; i++){
				var t = needTypes.skills[i];
				t.isOther = t.isOther == "1";
				if(t.isOther){
					needTypes.otherIds[t.id] = 'other-skills-detail'
				}
			}
			
			for(var i = 0; i < needTypes.financials.length; i++){
				var t = needTypes.financials[i];
			}
			
			for(var i = 0; i < needTypes.materials.length; i++){
				var t = needTypes.materials[i];
				t.isOther = t.isOther == "1";
				if(t.isOther){
					needTypes.otherIds[t.id] = 'other-material-detail'
				}
			}
		});
		
		$.get('api/utils/getGroups.php', function(data, status){
			var r = eval('(' + data + ')');
			groupsSelects= r;
		});
	};
	
	that.new_project = function() {
		$.get('api/utils/getGroups.php', function(data, status){
			var r = eval('(' + data + ')');
			groupsSelects= r;
			showProjectEntry();
		});
	}
	
	that.init_search = function(){
		// Bind to the 'browse' anchor
		$('#browse').bind('click', function(e){
			e.preventDefault();
			e.stopPropagation();
			showBrowser();
		});
		
		// Bind to the 'search' anchor
		$('#search').bind('click', function(e){
			e.preventDefault();
			e.stopPropagation();
			showSearch();
		});
				
		tabBody = $("#tabBody");
		originalHTML = tabBody.html();
	};
	
	that.showSkills = function(){
		showSkills();
	};
	
	that.showMaterials = function(){
		showMaterials();
	};
	
	that.showFinancials = function(){
		showFinancials();
	};
	
	that.addNeed = function(){
		addNeed();
		if ( this.bUseTinyMceForNeeds ){
			tinyMCE.execCommand('mceFocus', false, 'need-desc');
			tinyMCE.execCommand('mceRemoveControl', false, 'need-desc');
		}
	};
	
	that.reset = function(){
		tabBody.html(originalHTML);
	};
	
	that.processAddProject = function(){
		addProject();
	};
	
	that.showBrowse= function() {
		showBrowser();
	};
	
	that.edit = function(projectId){
		$.get('api/project/GetProject.php?projectId=' + projectId, function(data, status){
			var r = eval('(' + data + ')');
			project = r;
			showNeedEntry(false, false);
		});
	};
	
	that.editProjectDetails = function(){
		showProjectEntry(function(){
			document.getElementById('project-id').value = project.id;
			document.getElementById('project-name').value = project.title;
			document.getElementById('project-desc').value = project.description;
			document.getElementById('project-start').value = project.startdate;
			document.getElementById('project-end').value = project.enddate;
			document.getElementById('project-location').value = project.location;
			document.getElementById('project-contact-name').value = project.contact_name;
			document.getElementById('project-contact-email').value = project.contact_email;
			document.getElementById('project-contact-tel').value = project.contact_phone;
			document.getElementById('project-contact-company').value = project.contact_organization;	
		});
	};
	
	that.showNeedForm = function(){
		var nl = $('#needLinks');
		nl.fadeOut("normal", function(){
			nl.css("height", "600px");	
			nl.html(getNeedEntry());
			nl.fadeIn();
			if ( this.bUseTinyMceForNeeds ){
				tinyMCE.init({
					mode : "none",
					theme : "simple"
				});
				tinyMCE.execCommand('mceAddControl', false, 'need-desc');
			}
			nl.height('');
			bindNeedRadioHandlers();
		});
	};
	
	that.done = function(){
		window.location.reload();
	};
	
	that.deleteNeed = function(needId){
		if(confirm('are you sure?')){
			$.post('api/need/DeleteNeed.php', {needId: needId});
			for(var i = 0; i < project.needs.length; i++){
				if(project.needs[i].id == needId){
				
				}
			}
			$('#need_div_' + needId).fadeOut('normal');
		}
	};
	
	that.deleteProject = function(projectId){
		if(confirm('Are you sure?')){
			$.post('api/project/DeleteProject.php', {projectId: projectId}, function(){
				ProjectManager.done();
			});
		}
	};
	
	return that;
}();