
$(function() {
	var userId = 5;
	var userIdMap = {"userId":userId};
	
	var urlvars = getUrlVars();
	var activityId = parseInt(urlvars['id'], 10);
	var requestId = parseInt(urlvars['rid'], 10);
		
	
	$('div.modal-footer').on('click', 'button#submit', function(e) {
		
		var aiMap = {"activityId":activityId};
		
		aiMap['requestId'] = requestId;	//use the requestId to save/retrieve the comments
		aiMap['userId'] = userId;
		aiMap['topic'] = $('textarea#inputTopic').val();
		aiMap['text'] = $('textarea#inputMessage').val();
		
		// actually, I want this to be synchronous
		$.ajax({
			url:'../dart/saveNewComment.json',
			type:'POST',
			async:false,
			contentType:'application/json',
			dataType:'json',
			data:JSON.stringify(aiMap),
			success:function(data,textStatus,three) {
				if (textStatus === 'success') {
					try {
						
						var loc = window.opener.location.href.lastIndexOf('communication.html');
						
						if (loc != -1) {
							window.opener.update();							
						}						
					} catch (err) {
						//alert("Could not refresh the screen - error is: " + err.message);
					}
					window.close();
				}
			},
			error:function(data,textStatus,three) {
				alert("failure!!!!");
			}//end error
		});
	});
});

//Read a page's GET URL variables and return them as an associative array.
getUrlVars = function()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}