/* perso.js */

/* galerie */

var orig_default = -1;

function checkAll(cat) {
	for (var j = 1; j <= $('cat_'+cat).descendants().length; j++)
	{
		box = eval("document.form_"+cat+".check_"+j); 
		if (box.checked == false) box.checked = true;
	}
}

function uncheckAll(cat) {
	for (var j = 1; j <= $('cat_'+cat).descendants().length; j++)
	{
		box = eval("document.form_"+cat+".check_"+j); 
		if (box.checked == true) box.checked = false;
	}
}

function switchAll(cat) {
	for (var j = 1; j <= $('cat_'+cat).descendants().length; j++)
	{
		box = eval("document.form_"+cat+".check_"+j); 
		box.checked = !box.checked;
	}
}

function uncheckEveryCheckbox() {
	inputs = window.document.getElementsByTagName('input');
	for(var i=0; i < inputs.length; i++){ //iterate through all input elements
		if (inputs[i].type.toLowerCase() == 'checkbox') { //if the element is a checkbox
			if (inputs[i].checked == true) inputs[i].checked = false;
		}
	}
}

function getCheckedBoxes(array) {
	result = new Array();
	
	inputs = document.getElementsByTagName('input');
	
	for(var i=0; i < inputs.length; i++) {
		if(inputs[i].getAttribute('type') == 'checkbox')
		{
			if(inputs[i].checked)
			{
				result.push(inputs[i].id.substring(2,10));
			}
		}
	}
	if(array == true)
	{
		return result;
	}
	else
	{
		return result.join('/');
	}
}

function update_cats() {
	var doc = document.getElementById('categories');
	if(doc.options[doc.selectedIndex].value != '')
	{
		new Ajax.Request('http://10mbulle.ch/admin/galerie/changecat', {
		method: 'post',
		onSuccess: function(transport){
			var response = transport.responseText || "no response text";
			move_images(getCheckedBoxes(true), doc.options[doc.selectedIndex].value);
		},
		onLoading: function(){
			Effect.Appear('notification',{duration: 0.5, queue: 'end'});
		},
		onComplete: function(){
			Effect.Fade('notification',{duration: 0.5, queue: 'end'});
			document.getElementById('categories').selectedIndex = orig_default;
			uncheckEveryCheckbox();
		},
		parameters: {ids: getCheckedBoxes(false), cat: doc.options[doc.selectedIndex].value}
		});
	}
}

function move_images(ids, cat)
{
	for(i=0;i<ids.length;i++){
		elem = $('photo_'+ids[i]).remove();
		$('cat_'+cat).insert(elem);
	}
}

function remove_images(ids)
{
	for(i=0;i<ids.length;i++){
		$('photo_'+ids[i]).remove();
	}
}

function deleteSelectedPhotos()
{
	if(window.confirm('Etes-vous sûr de vouloir supprimer ces photos?'))
	{
		new Ajax.Request('http://10mbulle.ch/admin/galerie/photos/delete', {
		method: 'post',
		onSuccess: function(transport){
			var response = transport.responseText || "no response text";
			remove_images(getCheckedBoxes(true));
		},
		onLoading: function(){
			Effect.Appear('notification',{duration: 0.5, queue: 'end'});
		},
		onComplete: function(){
			Effect.Fade('notification',{duration: 0.5, queue: 'end'});
		},
		parameters: {ids: getCheckedBoxes(false)}
		});
	}
}

function setDefault() {
	if (orig_default < 0) {
		orig_default = document.getElementById('categories').selectedIndex;
	}
}

/* resultats */

function doubleCombo() {
	new Ajax.Request('http://10mbulle.ch/admin/resultats/tours/ajax', {
	method: 'post',
	onSuccess: function(transport){
		var response = transport.responseText || "";
		for(i=1;i<=8;i++) {
			$('membre'+i).update('');
			$('membre'+i).insert('<option value="">Choisir un membre</option>');
			$('membre'+i).insert(response);
			$('membre'+i).selectedIndex = 0;
		}
		for(i=1;i<=3;i++) {
			$('rempl'+i).update('');
			$('rempl'+i).insert('<option value="">Choisir un membre</option>');
			$('rempl'+i).insert(response);
			$('rempl'+i).selectedIndex = 0;
		}
		$('results').update('');
		$('results').insert('<tr><th>Nom</th><th>Passe 1</th><th>Passe 2</th><th>Passe 3</th><th>Passe 4</th></tr>');
	},
	onLoading: function(){
		Effect.Appear('notification',{duration: 0.5, queue: 'end'});
	},
	onComplete: function(){
		Effect.Fade('notification',{duration: 0.5, queue: 'end'});
	},
	parameters: {id: $('equipe').options[$('equipe').selectedIndex].value}
	});
}

function updateTourNo() {
	new Ajax.Request('http://10mbulle.ch/admin/resultats/tours/ajax2', {
	method: 'post',
	onSuccess: function(transport){
		var response = transport.responseText || "";
		$('tour').value = response;
	},
	onLoading: function(){
		Effect.Appear('notification',{duration: 0.5, queue: 'end'});
	},
	onComplete: function(){
		Effect.Fade('notification',{duration: 0.5, queue: 'end'});
	},
	parameters: {id: $('equipe').options[$('equipe').selectedIndex].value}
	});
}

function updateTableau(id) {
	/*$(id).options[$(id).selectedIndex].value*/
	if($('form_'+id) == null) {
		$('results').insert('<tr id="form_'+id+'"><td>'+$(id).options[$(id).selectedIndex].text+'</td><td><input type="text" name="'+id+'_1" value="" maxlength="3" class="text" style="width: 40px;" /></td><td><input type="text" name="'+id+'_2" value="" maxlength="3" class="text" style="width: 40px;" /></td><td><input type="text" name="'+id+'_3" value="" maxlength="3" class="text" style="width: 40px;" /></td><td><input type="text" name="'+id+'_4" value="" maxlength="3" class="text" style="width: 40px;" /></td></tr>');
	}
	else {
		$('form_'+id).remove();
		$('results').insert('<tr id="form_'+id+'"><td>'+$(id).options[$(id).selectedIndex].text+'</td><td><input type="text" name="'+id+'_1" value="" maxlength="3" class="text" style="width: 40px;" /></td><td><input type="text" name="'+id+'_2" value="" maxlength="3" class="text" style="width: 40px;" /></td><td><input type="text" name="'+id+'_3" value="" maxlength="3" class="text" style="width: 40px;" /></td><td><input type="text" name="'+id+'_4" value="" maxlength="3" class="text" style="width: 40px;" /></td></tr>');
	}
}