$(function() {        	
  $(".datepicker").datepicker(datepickerLocalization);
  $("#analysisUploadForm").ajaxForm(formOptions({
    target: $("#analysisUploadForm > .formResults"),
    success: function() {
      setTimeout(function() {
        $("#analysisUploadForm > .formResults").fadeOut("slow", function() {
          $("#analysisUploadForm > .formResults").empty();
          $("#analysisUploadForm > .formResults").show('slow');
        });
      }, 10000);
    }
  }));
  $("#analysisSearchForm").ajaxForm(
		  formOptions({
			  beforeSubmit: function(data, form, options) {
				resetPaging();
		      },
			  target: $("#analysisSearchForm > .formResults"),
			  success: function() {
			  		updateTooltips();
		  		}
		  })
  );
  $("#analysisEditForm").ajaxForm(formOptions({
    target: $("#analysisEditForm > .formResults"),
    success: function() {
      setTimeout(function() {
        $("#analysisEditForm > .formResults").fadeOut("slow", function() {
          $("#analysisEditForm > .formResults").empty();
          $("#analysisEditForm > .formResults").show('slow');
        });
      }, 10000);
    }
  }));
	$("#analysisEditDialogButton").click(function() {
		var findingId = $("#analysisEditFindingSelect").val();
		var findingName = $("#analysisEditFindingSelect :selected").text();
		var quantity = parseFindingQuantity($("#analysisEditQuantity").val());
		var proportion = parseFindingProportion($("#analysisEditProportion").val());

		var textToInsert = [];
		var i = 0;
		textToInsert[i++] = '<tr>';
		textToInsert[i++] = '<td>';
		textToInsert[i++] = '<input type="hidden" name="findingIds" value="' + findingId + '" />';
		textToInsert[i++] = '<span>';
		textToInsert[i++] = findingName;
		textToInsert[i++] = '</span>';
		textToInsert[i++] = '</td>';
		textToInsert[i++] = '<td>';
		if (quantity != null) {
			textToInsert[i++] = '<input type="hidden" name="quantities" value="' + quantity + '" />';
		} else {
			textToInsert[i++] = '<input type="hidden" name="quantities" />';
		}
		textToInsert[i++] = '<span>';
		if (quantity != null) {
			for (var count = 0; count < quantity; count++) {
				textToInsert[i++] = '+';
			}
		} else {
			textToInsert[i++] = '-';
		}
		textToInsert[i++] = '</span>';
		textToInsert[i++] = '</td>';
		textToInsert[i++] = '<td>';
		if (proportion != null) {
			textToInsert[i++] = '<input type="hidden" name="proportions" value="' + proportion + '" />';
		} else {
			textToInsert[i++] = '<input type="hidden" name="proportions" />';
		}
		textToInsert[i++] = '<span>';
		if (proportion != null) {
			textToInsert[i++] = '>' + proportion + '%';
		} else if (quantity == null) {
			textToInsert[i++] = '-';
		}
		textToInsert[i++] = '</span>';
		textToInsert[i++] = '</td>';
		textToInsert[i++] = '<td>';
		textToInsert[i++] = '<a href="#" class="analysisEditFindingDelete" onclick="deleteAnalysisEditFinding(this)">Poista</a>';
		textToInsert[i++] = '</td>';
		textToInsert[i++] = '</tr>';
		$("#analysisEditFindings").append(textToInsert.join(''));
	});
/*	$(':text').each(function (){
		$(this).watermark(this.title);
	});*/
	resetPaging();
});
function deleteAnalysisEditFinding(element) {
	$(element).parent("td").parent("tr").remove();
}
function parseFindingQuantity(text) {
	var quantity = null;
	var positiveMatch = text.match(/^\++$/g);
	var negativeMatch = text.match(/^-$/g);
	var parsedQuantity = parseInt(text);
	if (positiveMatch != null) {
		quantity = positiveMatch[0].length;
	} else if (negativeMatch != null) {
		quantity = 0;
	} else if (!isNaN(parsedQuantity)){
		quantity = parsedQuantity;
	}
	return quantity;
}
function parseFindingProportion(text) {
	var proportion = null;
	var fullMatch = text.match(/^>([0-9]+)%$/);
	var parsedProportion = parseInt(text);
	if (!isNaN(parsedProportion)) {
		proportion = parsedProportion;
	} else  if (fullMatch != null) {
		proportion = parseInt(fullMatch[1]);
	}
	return proportion;
}

function goToPage(page)
{
	$('#resultPage').val(page);
	$('#p').val(page);
	$("#analysisSearchForm").submit();
}

function resetSearchForm()
{
	$("#analysisSearchForm > .formResults").empty();
	resetPaging();
}

function resetPaging()
{
	$('#p').val('1');
	$('#resultPage').val('1');
	$('#resultsPerPage').val('50');
}

function printResult()
{	
  var results = $("#analysisSearchForm > .formResults");
  var target = $("#analysisSearchForm > .formPrint");
  target.empty();
  var container = $('<div class="container"></div>').appendTo(target);
  $('<div><h1>' + appname + '-analyysit</h1></div>').appendTo(container);
  results.clone().appendTo(container);
  $('#footer').clone().appendTo(container);
  container.find('.noprint').hide();
  container.find('.pagelinks').hide();
	container.printArea({mode: 'popup', popWd: 780, popClose:true });
}

