function editFormat() {
	doDisplay('tr_format', false);
	doDisplay('tr_formatedit', true);
	var input = document.getElementById('text_formatname_1');
	input.disabled = false;
	var format = document.getElementById('format');
	input.value = format.options[format.options.selectedIndex].value;
	var oldformat = document.getElementById('oldformatname');
	oldformat.value = format.options[format.options.selectedIndex].value;
	input.focus();
}

function undoEditFormat() {
	doDisplay('tr_format', true);
	doDisplay('tr_formatedit', false);
	var input = document.getElementById('text_formatname_1');
	input.disabled = true;
}

function updateFormat() {
	var input = document.getElementById('text_formatname_1');
	var format = input.value;
	var oldformatinput = document.getElementById('oldformatname');
	var oldformat = oldformatinput.value;
	
	// Update format
	var url = "index.php?go=format&oldformat="+oldformat+"&newformat="+format;
	new Ajax.Request(url);
	
	// Update format selection
	var select = document.getElementById('format');
	for(i = 0; i < select.options.length; i++) {
		if(select.options[i].value == oldformat) {
			select.options[i].value = format;
			select.options[i].text = format;
			break;
		}
	}
	
	undoEditFormat();
}

function newFormat() {
	doDisplay('tr_format', false);
	doDisplay('tr_formatnew', true);
	var input = document.getElementById('text_formatnewname_1');
	input.disabled = false;
	input.value += '';
	input.focus();
}

function undoNewFormat() {
	doDisplay('tr_format', true);
	doDisplay('tr_formatnew', false);
	var input = document.getElementById('text_formatnewname_1');
	input.disabled = true;
}

function addFormat() {
	var input = document.getElementById('text_formatnewname_1');
	var format = input.value;
	input.value = '';
	
	// Add format
	var url = "index.php?go=format&new="+format;
	new Ajax.Request(url);
	
	// Update format selection
	var select = document.getElementById('format');
	for(i = 0; i < select.options.length; i++) {
		// Don't add same format
		if(format.compareTo(select.options[i].value) == 0) {
			break;
		}
		if(format.compareTo(select.options[i].value) < 0) {
			var newOption = document.createElement('option');
			newOption.text = format;
			newOption.value = format;
		    try {
	    		select.add(newOption, select.options[i]); // standards compliant; doesn't work in IE
		    }
		    catch(ex) {
	    		select.add(newOption, i); // IE only
		    }
		    select.selectedIndex = i;
		    break;
		}
	}
	
	// Add to last option
	var newOption = document.createElement('option');
	newOption.text = format;
	newOption.value = format;
    try {
		select.add(newOption, null); // standards compliant; doesn't work in IE
    }
    catch(ex) {
		select.add(newOption); // IE only
    }
    select.selectedIndex = select.options.length - 1;
	
	undoNewFormat();
}

function removeFormat() {
	if(confirm('Are you sure you want to remove this format?')) {
		var select = document.getElementById('format');
		var format = select.options[select.options.selectedIndex].value;
		
		// Remove format
		var url = "index.php?go=format&remove="+format;
		new Ajax.Request(url);
		
		// Update format selection
		for(i = 0; i < select.options.length; i++) {
			if(select.options[i].value == format) {
				select.remove(i);
				break;
			}
		}
	}
}

function updateEditButtons() {
	var select = document.getElementById('format');
	if(select.options[select.selectedIndex].value == 'DVD') {
		doDisplay('newButton', true);
		doDisplay('editButton', false);
		doDisplay('removeButton', false);
	}
	else {
		doDisplay('newButton', true);
		doDisplay('editButton', true);
		doDisplay('removeButton', true);
	}
}

function isEnter(e) {
	if(window.event) {
		keynum = e.keyCode;
	}
	else if(e.which) {
		keynum = e.which;
	}
	if(keynum == 13) {
		if (e.preventDefault) 
			e.preventDefault();
	    if (e.stopPropagation) 
			e.stopPropagation();
	    return true;
	}
	return false;
}

String.prototype.compareTo = function(b) {
	var a = this+'';
	return (a===b) ? 0 : (a>b) ? 1 : -1;
}