function CaptchaSound() {
  //document.getElementById("play_sound").innerHTML = "<embed src='"+surl+"' hidden=true autostart=true loop=false>";
  document.getElementById("play_sound").innerHTML = '<embed src="/manuscripts/export_captcha_sound" hidden="true" autostart="true" loop="false">';
}

function MidiSound(div, file) {
  document.getElementById(div).innerHTML = "<embed src='"+file+"' hidden=true autostart=true loop=false>";	
}

function folder_action() {

	if ($('fa_apply_to').value == "selected") {
		var selected = [];
		$$('form#folder-manuscript-actions input[type=checkbox]').each(function(input) {
			if (input.checked) {
				selected.push(input.value);
			}
		});
		
		if (selected.length <= 0) {
			alert("Opps, there are no selected items.");
			return 0;
		} else {
			$('fa_selected').value = selected.join(':');
		}
	}
	
	
	
	document.fa_form.submit();
}

function folder_select_all_items(folder_id) {
	$$('form#folder-manuscript-actions input[type=checkbox]').each(function(input) {
		input.checked = "checked";
	});
}

function folder_deselect_all_items(folder_id) {
	$$('form#folder-manuscript-actions input[type=checkbox]').each(function(input) {
		input.checked = "";
	});

}

function auto_complete_on_select(element, selectedElement) {
  var entityParts = selectedElement.id.split('::');
  var entityType = entityParts[0];
  var entityId   = entityParts[1];
  var entityLabel = entityType + '_label';
  document.getElementById(entityType).value = entityId;
}

function noenter() {
  return !(window.event && window.event.keyCode == 13);
}

function exportProgress(percentage) {
	$('progress_text').innerHTML = "" + percentage + "%";
	var pixmove = percentage * 1.2;
	$('progress_image').style.backgroundPosition = "" + (-119 + pixmove) + "px 2px";
}

function inline_add(tag) {
	$('add_tag_list').value = tag;
	add_new_tag_call();
}

function export_result_set() {
	document.queryfilter.action = '/manuscripts/export';
	document.queryfilter.submit();
}

function show_email_field() {
	new Effect.BlindDown($('export_to_email'), {duration: 0.2})
}

function hide_email_field() {
	new Effect.BlindUp($('export_to_email'), {duration: 0.2})	
}

function switch_af_lookup(id) {
	var which_lookup = $F('af_lookups_' + id);
	$("af_lookup_block_1_" + id).style.display = 'none';
	$("af_lookup_block_2_" + id).style.display = 'none';
	$("af_lookup_block_3_" + id).style.display = 'none';
	$("af_lookup_block_4_" + id).style.display = 'none';
	$("af_lookup_block_5_" + id).style.display = 'none';

	switch (which_lookup) {
		case "People":
			$("af_lookup_block_1_" + id).style.display = 'block';
			break;
		case "Institutions":
			$("af_lookup_block_2_" + id).style.display = 'block';
			break;
		case "Titles":
			$("af_lookup_block_3_" + id).style.display = 'block';
			break;
		case "Subjects":
			$("af_lookup_block_4_" + id).style.display = 'block';
			break;
		case "Library sigla":
			$("af_lookup_block_5_" + id).style.display = 'block';
			break;
	}
}

function open_advanced() {
	document.queryfilter.action = '/search/advanced';
	document.queryfilter.submit();
}

function add_to_folder() {
	document.queryfilter.action = '/folders/add_bulk';
	
	var selected = [];
	$$('form#folder-manuscript-actions input[type=checkbox]').each(function(input) {
		if (input.checked) {
			selected.push(input.value);
		}
	});
	
	if (selected.length > 0) {
		$('folder_selected_items').value = selected.join(':');
	}
	
	document.queryfilter.submit();	
}

function add_filter() {
	if ($('qf_fields').value == 'Any field' && $('qf_op').value == 'contains word(s) beginning with') {
		alert('Ooops, you must select a specific field/fieldset to use the "contains word(s) beginning with" filter');
		return false;
	} else {
		if ($('qf_values').value != "") {
			$('qf_page').value = 1;
			document.queryfilter.submit();
		}
	}	
}

function remove_filter(filter_count) {
	$('qf_page').value = 1;
	$('qf_filter_remove').value = filter_count
	document.queryfilter.submit();
}

function change_sort(sort) {
	var sort = (sort == null) ? "" : sort;
	
	var previous_sort = $('qf_sort').value;
	var parts = previous_sort.split(':');
	var previous_sort_field = parts[0];
	var previous_sort_order = "desc";
	if (parts[1]) { previous_sort_order = parts[1];	}
	var new_sort;
	var new_order = "asc";

	if (sort == previous_sort_field) {
		if (previous_sort_order == "asc") {
			new_order = "desc" 
		}
	// } else {
	// 	new_order = "asc"
	}

	$('qf_sort').value = sort + ":" + new_order;
	$('qf_page').value = 1;
	document.queryfilter.submit();
}

function change_page(page) {
	document.queryfilter.action = '/search/quick';
	var page = (page == null) ? 1 : page;
	$('qf_page').value = page;
	document.queryfilter.submit();
}

function browse_context(page, manuscript_id) {
	$('qf_page').value = page;
	document.queryfilter.action = '/manuscripts/browse/' + manuscript_id;
	// document.queryfilter.method = 'get';
	document.queryfilter.submit();
}

function show_source() {
	if ($('old_versions')) {
		new Ajax.Request (
			"/manuscripts/show_source?version=" + $('old_versions').value,
			{
				method: "post",
				contentType: "text/html"
			}
		);
	}
}

function clear_tag(div_to_clear, tag_to_clear) {
	if ($(div_to_clear)) {
		if ($(tag_to_clear)) {
			$(tag_to_clear).value = "";
			$(div_to_clear).remove();
			reset_ordering(div_to_clear);
		}
	}
}

function clear_subform(li_to_clear) {
	if ($(li_to_clear)) {
		$(li_to_clear).remove();
	}
}

function add_condition() {
	call_add_condition('add_condition');
}
function add_lookup_condition() {
	call_add_condition('add_lookup_condition');
}
 function add_incipit_condition() {
	call_add_condition('add_incipit_condition');
}

function call_add_condition(destination) {
	var all_conditions = $A($("advanced_search").childNodes);
	var highest_tag = 0;
	all_conditions.each(
		function(node) {
			if (node.id) {
				var parts = node.id.split('_');
				if (parts[1]) {
					if (highest_tag < parts[1]) {
						highest_tag = parts[1];
					}
				}
			}
		}
	);

	new Ajax.Request (
		"/search/" + destination + "?highest=" + highest_tag,
		{
			method: "post",
			contentType: "text/html"
		}
	);
}

function reset_ordering(element) {
	var parts = element.split('_');
	var ordering = parts[0] + '_ordering';
	if ($(ordering)) {
		var new_order = new Array();
		var order_bits = $(ordering).value.split(',');
		for (var i = 0; i < order_bits.length; ++i) {
			if (order_bits[i] != element) {
				new_order.push(order_bits[i]);
			}
		}		
		// alert(new_order.join(','));
		$(ordering).value = new_order.join(',');
	}
}

function add_new_tag_call() {
	var added_tag = $('add_tag_list').value;
	var singles = ["040",	"100",	"110",	"130",	"240",	"245",	"260",	"300",	"580",	"590",	"592",	"593",	"594",	"597",  "598",	"772",	"852"];
	var highest_tag = "000";

	if ($(added_tag)) {
	var all_tags_of_type = $A($(added_tag).childNodes);

	all_tags_of_type.each(
		function(node) {
			if (node.id) {
				if (node.id != added_tag + "_ordering") {
					var parts = node.id.split('_');
					if (parts[1]) {
						if (highest_tag < parts[1]) {
							highest_tag = parts[1];
						}
					}
				}
			}
		}
	);
	}

	if (highest_tag != "000" && singles.indexOf(added_tag) != -1) {
		alert('Ooops, you can\'t add another ' + added_tag + ' tag. (Only one instance allowed)');
	} else {

		var ordering = "";
		if ($(added_tag + '_ordering')) {
			ordering = $(added_tag + '_ordering').value;
		}

		new Ajax.Request (
			"/manuscripts/add_tag?tag_to_add=" + $('add_tag_list').value + "&highest=" + highest_tag + "&ordering=" + ordering,
			{
				method: "post",
				contentType: "text/html"
			}
		);
	}
}

function add_new_subfield_call(tag, tag_div, subfield) {
	var all_subfields_of_type = $A($(tag_div + "_" + subfield).childNodes);
	var highest_tag = 0;
	all_subfields_of_type.each(
		function(node) {
			if (node.id) {
				var parts = node.id.split('_');
				if (parts[3]) {
					if (highest_tag < parts[3]) {
						highest_tag = parts[3];
					}
				}
			}
		}
	);

	new Ajax.Request (
		"/manuscripts/add_subfield?subfield_to_add=" + subfield + "&tag_div=" + tag_div + "&tag=" + tag + "&highest=" + highest_tag,
		{
			method: "post",
			contentType: "text/html"
		}
	);
}
