// Communities js file for Profile Editing/Message Center


function profile_edit(field) {
	$('div.profile .' + field).hide();
	$('div.profile div.edit_' + field).show();
}

function profile_save(field) {
	var data = {};
	
	if (field == 'contact') {
		data.email = $('div.profile #email_body').val();
		data.officephone = $('div.profile #officephone_body').val();
		data.homephone = $('div.profile #homephone_body').val();
		data.fax = $('div.profile #fax_body').val();
		data.website = $('div.profile #website_body').val();
		$.post(profile_edit_url,data,function(response){ 
			$('div.profile span.email').text(data.email);
			$('div.profile span.officephone').text(data.officephone);
			$('div.profile span.homephone').text(data.homephone);
			$('div.profile span.fax').text(data.fax);
			$('div.profile span.website').text(data.website);
		});
	}

	if (field == 'summary') {
		var value = $('div.profile #summary_body').val();
		data.summary = value;
		$.post(profile_edit_url,data,function(response){ $('div.profile div.summary-body').html(value); });
	}

	if (field == 'specialties') {
		var specialty_1_title = $('div.profile #specialty_1_title').val();
		var specialty_1_body = $('div.profile #specialty_1_body').val();
		var specialty_2_title = $('div.profile #specialty_2_title').val();
		var specialty_2_body = $('div.profile #specialty_2_body').val();
		var value = '';
		if (specialty_1_title && specialty_1_body) {
			value += '<strong>' + specialty_1_title + '</strong><p>' + specialty_1_body + '</p>';
		}
		if (specialty_2_title && specialty_2_body) {
			value += '<strong>' + specialty_2_title + '</strong><p>' + specialty_2_body + '</p>';
		}
		data.specialties = value;
		$.post(profile_edit_url,data,function(response){ $('div.profile div.specialties-body').html(value); });
	}

	if (field == 'discipline') {
		var discipline_title = $('div.profile #discipline_title').val();
		var discipline_body = $('div.profile #discipline_body').val();
		var value = '';
		if (discipline_title && discipline_body) {
			value += '<strong>' + discipline_title + '</strong><p>' + discipline_body + '</p>';
		}
		data.primarydiscipline = value;
		$.post(profile_edit_url,data,function(response){ $('div.profile div.discipline-body').html(value); });
	}

	if (field == 'background') {
		data.undergradcollege = $('div.profile #ug_college_body').val();
		data.undergraddegree = $('div.profile #ug_degree_body').val();
		data.undergradyear = $('div.profile #ug_year_body').val();
		data.postgradcollege = $('div.profile #pg_college_body').val();
		data.postgraddegree = $('div.profile #pg_degree_body').val();
		data.postgradyear = $('div.profile #pg_year_body').val();
		data.hobbies = $('div.profile #hobbies_body').val();
		$.post(profile_edit_url,data,function(response){ 
			$('div.profile div.ug_college').text(data.undergradcollege);
			$('div.profile div.ug_degree').text(data.undergraddegree);
			$('div.profile div.ug_year').text(data.undergradyear);
			$('div.profile div.pg_college').text(data.postgradcollege);
			$('div.profile div.pg_degree').text(data.postgraddegree);
			$('div.profile div.pg_year').text(data.postgradyear);
			$('div.profile div.hobbies').text(data.hobbies);
		});
	}

	if (field == 'languages') {
		var vals = [];
		var markup = [];
		var checked = $('input.lang-cb:checked');
		for (var i = 0; i < checked.length; i++) {
			if (checked[i].value == 'Other' && $('#other_language').val()) {
				var val = $('#other_language').val();
				vals.push(val);
				markup.push('<li>' + val + '</li>');
			} else {
				vals.push(checked[i].value);
				markup.push('<li>' + checked[i].value + '</li>');
			}
		}
		data.languages = vals.join(",");
		$.post(profile_edit_url,data,function(response){ $('div.profile ul.languages-body').html(markup.join('')); });
	}

	profile_cancel(field);
}

function profile_cancel(field) {
	$('div.profile div.edit_' + field).hide();
	$('div.profile .' + field).show();
}


// Message Center
function select_all() {
	var cbs = $('td.checkbox input').attr('checked',$('th.checkbox input').attr('checked'));
	for (var i = 0; i < cbs.length; i++) {
		highlight(cbs[i]);
	}
}

function highlight(cb) {
	if ($(cb).attr('checked')) {
		$(cb).parents('tr').addClass('active');
	} else {
		$(cb).parents('tr').removeClass('active');
	}
}

function mark_as_read(msg_id) {
	var row_elem = document.getElementById("messagerow_"+msg_id);
	var marked;
	$.get('/communities/mark_as_read', {msg_id: msg_id}, function(data){marked = data; if(marked){removeClass(row_elem, 'new');}});
}

//Forums

function add_forum_attachment() {
	var random = new Date().valueOf();
	var att = $('#forum_post .attachments');
	att.append('<div id="att_holder_' + random +'"><input type="file" name="attachment_' + random + '" /> <a href="javascript:void(0)" onclick="delete_forum_attachment(\'' + random + '\')">Remove</a></div>');
}

function delete_forum_attachment(id) {
	$('#att_holder_' + id).remove();
}

function submit_post() {
	if ($('#subject').val() == '') {
		$('p.error').text('Please enter a Subject').show();
		return;
	}
	if ($('#message').val() == '') {
		$('p.error').text('Please enter a Message').show();
		return;
	}
	$('#forum_post').submit()
}

function remove_doc() {
	var message = "Are you sure you want to remove this document?";
	if (confirm(message)) {
		$('#delete_doc').submit();
	}
}

function clearField(inp, act){
	var defaultVal = 'Search Forums';
	if(act == 1){ //onfocus
		if(inp.value == defaultVal){
			inp.value = '';
		};
	} else if(act == 0){ //onblur
		var tmp = inp.value.replace(/[\s]/g,'');
		if(tmp == ''){
			inp.value = defaultVal;
		};
	};
};
