function applyChanges(blockID) {
	document.getElementById('fldEchoCmd_'+blockID).value = 'applyChanges';
	document.getElementById('frm_'+blockID).submit();
}

function moveBlockDown(contentID, offset) {
	document.getElementById('fldEchoCmd_' + contentID).value = 'moveDownBlock,' + offset;
	document.getElementById('frm_' + contentID).submit();
}

function moveBlockUp(contentID, offset) {
	document.getElementById('fldEchoCmd_' + contentID).value = 'moveUpBlock,' + offset;
	document.getElementById('frm_' + contentID).submit();
}

function deleteEchoElement(contentID, offset) {
	if (confirm("Delete Element?\n\nPlease confirm that you want to delete this element completely.")) {
		document.getElementById('fldEchoCmd_' + contentID).value = 'deleteElement,' + offset;
		document.getElementById('frm_' + contentID).submit();
		return true;
	}
}

function deleteContentBlock(contentID, offset) {
	if (confirm("Remove Content Block from Page Region?\n\nPlease confirm you want this content block removed from this page region. It will NOT be deleted from the database unless you confirm the next question.")) {
		document.getElementById('fldEchoCmd_' + contentID).value = 'removeBlock,' + offset;
		if (confirm("Delete Content Block Permanently?\n\nPlease confirm that you want to delete this content block completely. All of its page elements will be deleted as well.\n\nBe sure to click 'Cancel' if you only want it removed from the page region.\n\n")) {
			document.getElementById('fldEchoCmd_' + contentID).value = 'deleteBlock,' + offset;
		}
		document.getElementById('frm_' + contentID).submit();
		return true;
	}
}

function previewContentBlock(contentID, offset) {
	document.getElementById('fldEchoCmd_' + contentID).value = 'previewBlock,' + offset;
	document.getElementById('frm_' + contentID).submit();
	return true;
}

/**** Color Picker (adapted from HTMLArea v3) ****/

function ShowColorPicker(pickerID) {
	// tell color picker the target of color selection
	document.getElementById('EchoColorPickerTargetID').value = pickerID;
	// set the initial color of the picker
	ViewColor(document.getElementById('ColorField' + pickerID).value);
	document.getElementById('EchoColorPicker').style.display = 'block';
}

function ViewColor(color) {                  // preview color
	document.getElementById("ColorPreview").style.backgroundColor = color;
	document.getElementById("ColorHex").value = color;
}

function SetColor(string) {						// select color
	document.getElementById('EchoColorPicker').style.display = 'none';
	var color = ValidateColor(string);
	if (color == null) {
		alert("Invalid color code: " + string);
	} else {									// valid color
		// ViewColor(color);						// show selected color
		targetID = document.getElementById('EchoColorPickerTargetID').value;
		// update color well
		document.getElementById('ColorWell' + targetID).style.backgroundColor = string;
		// update hidden element color field
		document.getElementById('ColorField' + targetID).value = string;
	}
}

function ValidateColor(string) {                // return valid color code
  string = string || '';
  string = string + "";
  string = string.toUpperCase();
  var chars = '#0123456789ABCDEF';
  var out   = '';
	if (string == '') {
		return out;
	}
  for (var i=0; i<string.length; i++) {             // remove invalid color chars
    var schar = string.charAt(i);
    if (chars.indexOf(schar) != -1) { out += schar; }
  }

  if (out.length != 7) { return null; }            // check length (ie. '#000000')
  return out;
}

function ToggleSubEditor(state, editorID) {
	document.getElementById(editorID).style.display = (state ? 'block' : 'none');
}

function ToggleElementEditor(editorID) {
	editor = document.getElementById(editorID);
	editor.style.display = (editor.style.display == 'none' ? 'block' : 'none');
	document.getElementById('editorFlag_'+editorID).value = (editor.style.display == 'none' ? '0' : '1');
}

function ToggleElementOptions(id) {
	editor = document.getElementById(id);
	editor.style.display = (editor.style.display == 'block' ? 'none' : 'block');
}

function DeleteAttachment(checkbox) {
	if (checkbox.checked) {
		checkbox.checked = confirm("Delete Attachment?\n\nPlease confirm you wish to delete the attached file.");
	}
}