// TABLE FUNCTIONS

function init_table(table_id) {
	$("#"+table_id+" td").click( function() { td_click($(this)); });
	$("#"+table_id+" input").change( function() { table_set_colors(); });
}

function td_click(td) {
	if (td.html().substr(0,6).toLowerCase() != "<input") {
		var input_id = get_id_from_td(td.parent().html());
		document.getElementById(input_id).checked = !document.getElementById(input_id).checked;
	}
	table_set_colors();
}

function table_set_colors() {
	$("tr").each( function() {
		var input_id = get_id_from_td($(this).html());
		if (input_id != "") {
			if (document.getElementById(input_id).checked) {
				$(this).addClass("active");
			} else {
				$(this).removeClass("active");
			}
		}
	});
}

function get_id_from_td(td) {
	td = td.toLowerCase();
	if (td.indexOf("</th>") > 0) {
		td = "";
	} else {
		td = td.substr(td.indexOf('id=')+4);
		td = td.substr(0,td.indexOf(' ')-1);
	}
	return td;
}

function clear_selection(theForm) {
	document.getElementById(theForm).reset();
	table_set_colors();
	return false;
}

function invert_selection() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		var checkbox = inputs[i];
		if (checkbox.getAttribute("type") == "checkbox") {
			checkbox.checked = (checkbox.checked) ? false : true;
		}
	}
	table_set_colors();
	return false;
}


// PICTURES LIST FUNCTIONS

function init_pictures() {
	$(".pictures .info td").click( function() { pic_td_click($(this)); });
	$(".pictures .info th").click( function() { pic_td_click($(this)); });
	$(".pictures .info input").change( function() { picures_set_colors(); });
}

function pic_td_click(td) {
	if (td.html().substr(0,6).toLowerCase() != "<input") {
		var input_id = pic_get_id_from_table(td.parent().parent().html());
		document.getElementById(input_id).checked = !document.getElementById(input_id).checked;
	}
	picures_set_colors();
}

function picures_set_colors() {
	$(".pictures .info").each( function() {
		var input_id = pic_get_id_from_table($(this).html());
		if (input_id != "") {
			if (document.getElementById(input_id).checked) {
				$(this).addClass("active");
			} else {
				$(this).removeClass("active");
			}
		}
	});
}

function pic_get_id_from_table(td) {
	td = td.toLowerCase();
	td = td.substr(td.indexOf('id=')+4);
	td = td.substr(0,td.indexOf(' ')-1);
	return td;
}

function pic_clear_selection(theForm) {
	document.getElementById(theForm).reset();
	picures_set_colors();
	return false;
}

function pic_invert_selection() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		var checkbox = inputs[i];
		if (checkbox.getAttribute("type") == "checkbox") {
			checkbox.checked = (checkbox.checked) ? false : true;
		}
	}
	picures_set_colors();
	return false;
}


// FORM FUNCTIONS

function check_form(theform, successMsg, failureMsg) {
	var has_checked = false;
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		var checkbox = inputs[i];
		if (checkbox.getAttribute("type") == "checkbox") {
			if (checkbox.checked) {
				has_checked = true;
				break;
			}
		}
	}
	if (has_checked) {
		show_message(successMsg);
	} else {
		show_message(failureMsg);
	}
	return false;
}

function show_message(theMessage) {
	$("#popup_space #main").html(get_message(theMessage));
	$("#popup_space").show();
	return false;
}

function show_message_dyn(theMessage, value) {
	$("#popup_space #main").html(get_message(theMessage).replace("%%%", value));
	$("#popup_space").show();
	return false;
}

function hide_message() {
	$("#popup_space").hide();
	return false;
}

function init_message() {
	setTimeout("$('#msg_box').animate({height: 0, marginTop: 0}, 600);",4000)
}

function color_change() {
	var new_css = document.getElementById('color_scheme').options[document.getElementById('color_scheme').selectedIndex].value;
	var css_href = document.getElementsByTagName("link")[1].href;
	css_href = css_href.substr(0,css_href.lastIndexOf("/"))+"/"+new_css;
	document.getElementsByTagName("link")[1].href = css_href;
}

function check_userform() {
	var password = document.getElementsByName('password')[0].value;
	var password_repeat = document.getElementsByName('password_repeat')[0].value;
	if (password != '' && password != password_repeat) {
		show_message('passdiff');
		return false;
	} else {
		return true;
	}
}


// MISC

function open_uploader(url) {
	window.open(url,"dznr uploader",'width=400, height=420, resizable=no');
	return false;
}

function update_uploader(input) {
	var new_value = input.value;
	new_value = new_value.substr(new_value.lastIndexOf("/")+1);
	$("#file table td").html(new_value);
}

function clean_invite_form() {
	document.getElementById("invite_username").value = document.getElementById("invite_username").value.replace(/[^a-z0-9]/gi, '');	
}

function submit_invite_form() {
	clean_invite_form();
	if (document.getElementById("invite_username").value == "") {
		show_message("invempty");
	} else {
		show_message_dyn("invconfirm", $("#invite_username").attr("value"));
	}
	return false;
}


// GROUP FUNCTIONS

function assign_group(theform) {
	var has_checked = false;
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		var checkbox = inputs[i];
		if (checkbox.getAttribute("type") == "checkbox") {
			if (checkbox.checked) {
				has_checked = true;
				break;
			}
		}
	}
	if (has_checked) {
		document.getElementsByName("action")[0].value = "assign_group";
		document.list.submit();
	} else {
		show_message("nofiles_g");
	}
	return false;
}

function update_url() {
	document.getElementById('url').value = clean_url(document.getElementById('name').value);
}

function check_url() {
	document.getElementById('url').value = clean_url(document.getElementById('url').value);
}

function clean_url(dirty) {
	var filter = /[^a-z0-9_-]/gi;
	dirty = dirty.toLowerCase().replace(/ /g, "_");
	return dirty.replace(filter, '');
}

function check_groupform() {
	if (document.getElementById('name').value == "" || document.getElementById('url').value == "") {
		show_message('allfields');
		return false;
	} else if (!check_userform()) {
		return false;
	} else {
		return true
	}
}

function show_prot_form() {
	$("#protection_link").hide();
	$("#protection").show();
	document.groupform.protection.value = "new";
	return false;
}

function hide_prot_form() {
	$("#protection_link").show();
	$("#protection").hide();
	document.groupform.protection.value = "keep";
	return false;
}

