

/*******************/
/*  Control Panel  */
/*******************/

function testForEnter(event) {    
	if (!event) var event = window.event;
	if (event.keyCode == 13) {        
		event.cancelBubble = true;
		event.returnValue = false;
         }
} 


function valueChanged (inputID) {
	$("#" + inputID + "_changed").val(1);
}

function saveEntity (formID) {
	$("#formID").submit();
}

function deleteEntity (tableName, entityID) {
	var confirmation = confirm("Are you sure you want to delete this record? This operation cannot be undone.");
	if (!confirmation)
		return;
	else {
		$.post("schema/delete_entity.php", { table: tableName, entity_ID: entityID }, function () { deleteEntityCallback(tableName); });
	}
}

function deleteEntityCallback (tableName) {
	window.location = "control_panel.php?func=browse&table=" + tableName;
}

function deleteUploadedFile (tableName, fieldName, entityID, filename) {
	var confirmation = confirm("Are you sure you want to delete this file? This operation cannot be undone.");
	if (!confirmation)
		return;
	else {
		$.post("schema/delete_uploaded_file.php", { table_name: tableName, field_name: fieldName, entity_ID: entityID, filename: filename }, function () { deleteUploadedFileCallback(fieldName); });
	}
}

function deleteUploadedFileCallback (fieldName) {
	$("#preview_" + fieldName).fadeOut("slow");
	$("#delete_" + fieldName).fadeOut("slow");
}

function imagePreview (event, imageURL) {
	var x = event.clientX + 10;
	var y = event.clientY + 10;
	var imageHTML = "<IMG src='" + imageURL + "' style='width: 150px;'>";

	if ($("#image_preview").is("DIV"))
		$("#image_preview").stop().html(imageHTML).css("left", x + "px").css("top", y + "px").fadeIn();
	else $("<DIV id=\"image_preview\">" + imageHTML + "</DIV>").css("position", "absolute").css("left", x + "px").css("top", y + "px").appendTo("body");
	// $.post("ajax/preview_image.php", { image_URL: imageURL }, function (response) { imagePreviewCallback(response, x, y) });
}

function closeImagePreview () {
	$("#image_preview").stop().fadeOut();
	// $("body").remove($("#image_preview"));
}

function registerErrors (errorDivID, errors) {
	var errorMessage = "The following errors occured when trying to submit:<UL>";
	for (var i in errors) {
		errorMessage += "<LI>" + errors[i];
	}
	errorMessage += "</UL>";
	$("#" + errorDivID).html(errorMessage).fadeIn("slow");
}

function exportTable (tableName) {
	
}

function browseFile (fieldName, directory, extensions) {
	if (!extensions)
		extensions = "";
	if (!directory)
		directory = "";

	openInnerWindow("file_browse_window", "schema/select_file.php", 800, "", "Select File", "input_name=" + fieldName + "&directory=" + directory + "&extensions=" + extensions, "", "", 1);
}

function chooseFile (event, browserName, filename, fileID) {
	var inputID = $("#browse_input").val();

	$("#" + inputID + "_changed").val(1);
	$("#" + inputID).val(filename.substring(3, filename.length));
	closeInnerWindow("file_browse_window");
}