// misc routines to support annotations and tags (and QAPhase3 comments - kkf)

function showDlg(dEle) {

	 $("#" + dEle).show() ;
}

function closeDialog(dEle) {

	$("#" + dEle).hide() ;
}
	

$().ready(function() {


    $('#LeftPane').bind('contextmenu', function(e) {

	var targ = (e.srcElement) ? e.srcElement : e.target ;
	var x = $(targ).attr("x") ;
	if (x) {
		var y = $(targ).attr("y") ;
		addAnnotation(x, y, $(targ)) ;
		return false ;
	}	
    });
});


var annotationX ;
var annotationY ;

function addAnnotation(x, y, targ) {

	annotationX = x ;
	annotationY = y ;

	$('#annoOf').html(targ.text()) ;
	$('#annoText').val('') ;
	$('#addAnno').show();
}

var editingAnnotationId ;

function editEAnno(seq, annotationId) {

	editingAnnotationId  = annotationId ;

	var annoContentsDiv = "#annoContent" + seq ;
	//alert("edit annotation=" + editingAnnotationId  + "\ncontents: " + $(annoContentsDiv).html()) ;

	$('#annoEditText').val($(annoContentsDiv).html()) ;
	$('#editAnno').show() ;
}


function saveAnnotation() {

	var annotatedContent = $('#annoText').val() ;
	var annoPublic = $('#annoPublic').attr('checked') ;
	var privateFlag = annoPublic ? 'n' : 'y';

	if (typeof annotationX == 'undefined') {
		annotationX = 0;
	}
	if (typeof annotationY == 'undefined') {
		annotationY = 0;
	}

	if (annotatedContent.length < 1) {
		alert ("you haven't entered any content") ;
		return ;
	}

	var postRes = "" ;

	$.ajax({
		type: "GET",
		url: "/ndp/del/annotation",
		async: false,
		data: "simulatedMethod=put&articleId=" + articleId + "&articlePartId=" + articlePartId + 
				"&xy=" + annotationX + "," + annotationY + "&content=" + annotatedContent +
				"&private=" + privateFlag,
		success: function(msg){
			postRes = msg ;
		//	alert(postRes) ;
			alert("Your note has been stored - refresh the page to view") ;
		},
		error: function(reqObj, msg, exceptionObj){
			postRes = "ERROR: " +  reqObj.statusText ;
		//	alert(postRes) ;
			alert("Your updates failed: " + postRes) ;
		}
	});

// NN
//	$('#addAnno').hide();
	$('#popup, #mask').remove();
	$('body').removeClass('popup');
}

function saveEditedAnnotation(annotationId) {

	var annotatedContent = $('#annoEditText').val() ;
	var annoPublic = $('#annoPublic').attr('checked') ;
	var privateFlag = annoPublic ? 'n' : 'y';

	if (annotatedContent.length < 1) {
		alert ("You haven't entered any content") ;
		return ;
	}

	var postRes = "" ;

		$.ajax({
			type: "POST",
			url: "/ndp/del/annotation",
			async: false,
//			data: "simulatedMethod=post&annotationId=" + editingAnnotationId + 
			data: "simulatedMethod=post&annotationId=" + annotationId + 
					"&content=" + annotatedContent + "&private=" + privateFlag,
			success: function(msg){
				postRes = msg ;
			//	alert(postRes) ;
				alert("Your note update has been stored - refresh the page to view") ;
			},
			error: function(reqObj, msg, exceptionObj){
				postRes = "ERROR: " +  reqObj.statusText ;
			//	alert(postRes) ;
				alert("Your updates failed: " + postRes) ;
			}
		});

//	$('#editAnno').hide();
	$('#popup, #mask').remove();
	$('body').removeClass('popup');
}

function editTags() {

	$('#ttagText').val(userTags) ; 
	$('#addTags').show();
}

function checkAndSaveTags() {
	var tagContent = $('#ttagText').val() ;
	var tags = tagContent.split(',');
	var errMsg = '';

	// No more than 50 tags
	if (tags.length > 50) {
		errMsg = 'You have exceeded the maximum number of tags for an article (50)';
	} else {
		// A tag should not exceed 60 characters
		for (var i = 0; i < tags.length; ++i) {
			var tag = tags[i].replace(/[^a-zA-Z0-9' \\-]/g, ' ').replace(/\s\s+/g, ' ').replace(/^\s+/, '').replace(/\s+$/, '');
			if (tag.length > 60) {
				errMsg = 'One of your tags exceeded 60 characters';
				break;
			}
		}
	}

	if (errMsg != '') {
		alert(errMsg);
	} else {
		saveTags();
	}
}

function saveTags() {

	var tagContent = $('#ttagText').val() ;
	var tagPublic = $('#ttagPublic').attr('checked') ;
	var privateFlag = tagPublic ? 'n' : 'y';

	var postRes = "" ;

	$.ajax({
		type: "GET",
		url: "/ndp/del/tag",
		async: false,
		data: "simulatedMethod=put&articleId=" + articleId  + "&name=" + tagContent + "&private=" + privateFlag,
		success: function(msg){
			postRes = msg ;
		//	alert(postRes) ;
			alert("Your updates have been applied - refresh the page to view") ;
		},
		error: function(reqObj, msg, exceptionObj){
			postRes = "ERROR: " +  reqObj.statusText ;
		//	alert(postRes) ;
			alert("Your updates failed: " + postRes) ;
		}
	});

// NN
//	$('#addTags').hide();
	$('#popup, #mask').remove();
	$('body').removeClass('popup');
}

function saveQAPhase3Note() {

	var notes = $('#QAPhase3Note').val() ; 
	var postRes = "" ;

	$.ajax({
		type: "POST",
		url: "/ndp/del/QAPhase3ExtraSampleArticleNote",
		async: false,
		data: "simulatedMethod=put&articleId=" + articleId  + "&notes=" + notes,
		success: function(msg){
			postRes = msg ;
		//	alert(postRes) ;
			alert("Your note has been saved") ;
		},
		error: function(reqObj, msg, exceptionObj){
			postRes = "ERROR: " +  reqObj.statusText ;
		//	alert(postRes) ;
			alert("Your update failed: " + postRes) ;
		}
	});
}
