$(document).ready(function(){
	
	var celdas = $('.clic_celda');
	$('.shid').hide();
		
	celdas.click(function(){
	
			var id = this.id;
			$(this).hide();
			document.getElementById(id + "_sel").style.display = 'block';
			//$(id + "_sel").css({backgroundColor: 'red'});
			//$(id + "_sel").show();
			var b;
			//('.shid').show();
			
			//$('.shid').show()
			//$(this).css({backgroundColor: 'red'});
			
		});

});

function in_array(aguja, pajar) {
	
	for(var i in pajar) {
		if(pajar[i] == aguja) return true;
		pajar[i];
	} // for
	return false;
	
} // in_array

function validaFormGenera() {
	
	var formGenera = document.getElementById("formGenera");
	
	if((formGenera.user1.value == '' && formGenera.user2.value == '' && formGenera.user3.value == '' && formGenera.user4.value == '' && formGenera.user5.value == '') ||
		(!formGenera.z1.checked && !formGenera.z2.checked && !formGenera.z3.checked && !formGenera.z4.checked && !formGenera.z5.checked)) {
		alert('Para generar la tabla se requiere como mínimo un usuario y una zona.');
		return false;
	} // if
	
	var a = new Array();
	var i = 0;
	if(formGenera.user1.value != '') {
		a[i] = formGenera.user1sel.value;
		i++;
	} // if
	if(formGenera.user2.value != '') {
		if(in_array(formGenera.user2sel.value, a)) {
			alert('Cada usuario debe tener un color diferente.');
			return false;
		} // if
		a[i] = formGenera.user2sel.value;
		i++;
	} // if
	if(formGenera.user3.value != '') {
		if(in_array(formGenera.user3sel.value, a)) {
			alert('Cada usuario debe tener un color diferente.');
			return false;
		} // if
		a[i] = formGenera.user3sel.value;
		i++;
	} // if
	if(formGenera.user4.value != '') {
		if(in_array(formGenera.user4sel.value, a)) {
			alert('Cada usuario debe tener un color diferente.');
			return false;
		} // if
		a[i] = formGenera.user4sel.value;
		i++;
	} // if
	if(formGenera.user5.value != '') {
		if(in_array(formGenera.user5sel.value, a)) {
			alert('Cada usuario debe tener un color diferente.');
			return false;
		} // if
		a[i] = formGenera.user5sel.value;
		i++;
	} // if

} // validaFormGenera

/** Funciones de Ajax **/

var READY_STATE_COMPLETE = 4;
var peticion_http = null;

function inicializa_xhr() {

	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} // if

} // inicializa_xhr

function selUser(capa_sel, capa_bullet, sel, zona, dia) {
	
	// Oculta todos los selects que hayan quedado visibles
	/*$('.shid').hide();
	$('.clic_celda').show();*/
	
	var index = document.getElementById(sel).selectedIndex;
	actualizaUsr(capa_sel, capa_bullet, index, zona, dia);	
	
} // selUser

function ocultaSelects() {

	$('.shid').hide();
	$('.clic_celda').show();

} // ocultaSelects

/**
 *	Cambia el usuario de la tarea mediante Ajax
 *	Envio
 */
function actualizaUsr(capa_sel, capa_bullet, index, zona, dia) {

	var peticion_http = inicializa_xhr();

	if(peticion_http) {
		// Preparacion del envio de datos
		
		peticion_http.onreadystatechange = function() {
			respuestaActualizaUsr(peticion_http, capa_sel, capa_bullet);
		}
		peticion_http.open("POST", "sc_actualiza_plan.php", true);

		// Construccion del string de parametros
		var query_string = "numero=" + index + "&zona=" + zona + "&dia=" + dia + "&nocache=" + Math.random();
		
		// Envio
		peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		peticion_http.send(query_string);
	} // if

} // actualizaUsr

/**
 *	Cambia el usuario de la tarea mediante Ajax
 *	Respuesta
 */
function respuestaActualizaUsr(peticion_http, capa_sel, capa_bullet) {

	if(peticion_http.readyState == READY_STATE_COMPLETE) {
		if (peticion_http.status == 200) {
			var documento_xml = peticion_http.responseXML;
			var root = documento_xml.getElementsByTagName("xml")[0];
			var resultado = root.getElementsByTagName("resultado")[0].firstChild.data;
			if(resultado == 0) {
				// Resultado correcto
				document.getElementById(capa_sel).style.display = "none";
				var color = root.getElementsByTagName("color")[0].firstChild.data;
				document.getElementById(capa_bullet).className = "clic_celda bullet " + color;
				document.getElementById(capa_bullet).style.display = "block";
			} else {
				switch(resultado) {
					case '-1':
						// Error en el acceso a base de datos
						alert('Se ha producido un error interno, consulte con el administrador de la aplicación');
						break;
					default:
						// Error desconocido
						alert('Atención! Se ha producido un error interno, consulte con el administrador de la aplicación');
						break;
				} // switch
			} // if
		} // if
	} // if

} // respuestaActualizaUsr
