function display_galleries(id){
	x_display_galleries(id, display_galleries_cb);	
}

function display_galleries_cb(z){
	var results = z.split("|");
	if(results[0] == "0") {
		alert(results[1]);	
	}else{
		document.getElementById("mainControlsArea").innerHTML = z;		
		document.getElementById("creator").innerHTML = '';
	}
}

function add_new_category_form(id){
	x_add_new_category_form(id, add_new_category_form_cb);
}

function add_new_category_form_cb(z){
	var results = z.split("|");
	if(results[0] == "0") {
		alert(results[1]);	
	}else{
		document.getElementById("mainControlsArea").innerHTML = z;
	}
}

function insert_linkcat_into_db(cat_id){
	var cat_name = document.getElementById("cat_name").value;
	x_insert_linkcat_into_db(cat_id, cat_name, insert_linkcat_into_db_cb);
}

function insert_linkcat_into_db_cb(z){
	display_galleries(1);
}

function remove_gallerycategories(id){
	x_remove_gallerycategories(id, remove_gallerycategories_cb);	
}

function remove_gallerycategories_cb(z){
	display_galleries(1);	
}

function removeGalleryPhoto(id) {
	if (confirm("This will DELETE the photo from the database and remove it from all categories. Do you wish to continue?")) {x_removeGalleryPhoto(id, document.getElementById("cat_id").value, removeGalleryPhoto_cb);}
}
function removeGalleryPhoto_cb(z) { putGalleryPhotoInDb_cb(z); }
function putGalleryPhotoInDb(p) {
	i = document.getElementById("cat_id").value;
	x_putGalleryPhotoInDb(p, i, putGalleryPhotoInDb_cb);
}
function putGalleryPhotoInDb_cb(z) {
	var results = z.split("|");
	if (results[0] == "0") {
		alert(results[1]);
	} else {
		document.getElementById("photos").innerHTML = results[1];
		if (results[2]) {
			var cats = results[2].split("^");
			for(i = 0; i < cats.length; i++) {
				var c = cats[i].split("=");
				if (document.getElementById("gal-count-"+c[0])) {
					document.getElementById("gal-count-"+c[0]).innerHTML = c[1];
				}
			}
			document.getElementById("photo-msg").innerHTML = "Changes made successfully";
			Fat.fade_all();
		}
		
	}
}

function submitGalleryPhoto(p) {
	i = document.getElementById("cat_id").value;
	text = document.getElementById("textCaption").value;
	x_submitGalleryPhoto(p, i, text, submitGalleryPhoto_cb);
}
function submitGalleryPhoto_cb(z) {
	var results = z.split("|");
	if (results[0] == "0") {
		alert(results[1]);
	} else {
		document.getElementById("photos").innerHTML = results[1];
		if (results[2]) {
			var cats = results[2].split("^");
			for(i = 0; i < cats.length; i++) {
				var c = cats[i].split("=");
				if (document.getElementById("gal-count-"+c[0])) {
					document.getElementById("gal-count-"+c[0]).innerHTML = c[1];
				}
			}
			Fat.fade_all();
		}
		
	}
}






function orderGalleryPhotos(id, o) {
	x_orderGalleryPhotos(id, o, document.getElementById("cat_id").value, orderGalleryPhotos_cb);
}
function orderGalleryPhotos_cb(z) { putGalleryPhotoInDb_cb(z); }
function orderGalleryCats(id, o) {
	x_orderGalleryCats(id, o, orderGalleryCats_cb);
}
function orderGalleryCats_cb(z) { 
	document.getElementById("gallery-categories").innerHTML = z;
}
function optionsGalleryPhoto(id) {
	x_optionsGalleryPhoto(id, document.getElementById("cat_id").value, optionsGalleryPhoto_cb);
}
function optionsGalleryPhoto_cb(z) {
	var results = z.split("|");
	document.getElementById("opt-"+results[0]).innerHTML = results[1];
	document.getElementById("opt-"+results[0]).style.display = "block";
}
function closeOptionsGalleryPhoto(id) {
	document.getElementById("opt-"+id).style.display = "none";
}
function saveOptionsGalleryPhoto(id) {
	var checks = document.getElementsByTagName("input");
	var cats = '';
	var x = 0;
	for (i = 0; i < checks.length; i++) {
		if (checks[i].type == "checkbox" && checks[i].className == "p"+id) {
			cats += checks[i].value;
			if (checks[i].checked == true) {cats += '|'; x++; } else cats += '-|';			 
		}
	}
	cats = cats.replace(/cat-/g, "");
	if (x > 0) {
		x_saveOptionsGalleryPhoto(id, cats, document.getElementById("cat_id").value, saveOptionsGalleryPhoto_cb);
	} else {
		if (confirm("NO CATEGORIES SELECTED! This will DELETE the photo from the database. Do you wish to continue?")) {x_removeGalleryPhoto(id, document.getElementById("cat_id").value, removeGalleryPhoto_cb);}
		else {return false;}
	}
}
function saveOptionsGalleryPhoto_cb(z) {
	var results = z.split("|");
	if (results[0] == "0") {
		alert(results[1]);
	} else {
		document.getElementById("photos").innerHTML = results[1];
		if (results[2]) {
			var cats = results[2].split("^");
			for(i = 0; i < cats.length; i++) {
				var c = cats[i].split("=");
				if (document.getElementById("gal-count-"+c[0])) {
					document.getElementById("gal-count-"+c[0]).innerHTML = c[1];
				}
			}
		}
		document.getElementById("photo-msg").innerHTML = "Changes made successfully";
		Fat.fade_all();
	}
}
function deleteCatWarn() {
	if (confirm("WARNING: Deleting this category will also delete all items within this category. Click OK to delete this category or Cancel to end this action.")) {return true;}
	else {return false;}
}

function saveCaption(id) {
	var x = document.getElementById("textCaption_"+id).value;
	x_saveCaption(id, x, saveCaption_cb);
}
function saveCaption_cb(z) {
	var results = z.split("|");
	if (results[0] == "0") {
		alert(results[1]);
	}	

}