function nuevoAjax()
{ 
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false;
	try
	{
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			// Creacion del objet AJAX para IE
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(E)
		{
			if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		}
	}
	return xmlhttp; 
}



// Declaro los selects que componen el documento HTML. Su atributo ID debe figurar aqui.


function buscarEnArray(array, dato)
{
	// Retorna el indice de la posicion donde se encuentra el elemento en el array o null si no se encuentra
	var x=0;
	while(array[x])
	{
		if(array[x]==dato) return x;
		x++;
	}
	return null;
}


function str_replace (search, replace, subject, count) {
    // Replaces all occurrences of search in haystack with replace  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/str_replace    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
     for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}        }
    }
    return sa ? s : s[0];
}


function trim (str, charlist) {
    // Strips whitespace from the beginning and end of a string  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/trim    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
    // +      input by: Erkekjetter
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // +      input by: DxGx
    // +   improved by: Steven Levithan (http://blog.stevenlevithan.com)
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // *     example 1: trim('    Kevin van Zonneveld    ');    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: trim('Hello World', 'Hdle');
    // *     returns 2: 'o Wor'
    // *     example 3: trim(16, 1);
    // *     returns 3: 6    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
              whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {            str = str.substring(i);
            break;
        }
    }
        l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}







function seleccionar(id){
	
if($("#"+id).is(":checked")){
$("#"+id).parents("label").addClass("checked");
}else{
$("#"+id).parents("label").removeClass("checked");
}

	
}






var listadoFunc=new Array();

var nivel_=0;

(function($) {
	

$.select = {

cargar : function(idSelectOrigen, cad, callback,selects){
// Parametro funcion indica que funcion utilizar para cargar el select

listadoSelects = (selects == undefined) ? listadoSelects : selects;

// Obtengo la posicion que ocupa el select que debe ser cargado en el array declarado mas arriba
var posicionSelectDestino=buscarEnArray(listadoSelects, idSelectOrigen)+1;


// Obtengo la opcion que el usuario selecciono
var opcionSeleccionada=$('select[name='+idSelectOrigen+']').val();



if($('#tabla_div'))
$('#tabla_div').html("");


// Si el usuario eligio la opcion "Elige", no voy al servidor y pongo los selects siguientes en estado "Selecciona opcion..."
/*
if(opcionSeleccionada==0){
		*/
x=posicionSelectDestino, selectActual=null;

nivel_=x;

// Busco todos los selects siguientes al que inicio el evento onChange y les cambio el estado y deshabilito
	while(listadoSelects[x]){
			
	if(typeof(listadoSelects[x]) == "object"){

	
	for(j=0;j<listadoSelects[x].length;j++){
		
	var funcion = (listadoFunc[x] == undefined || listadoFunc[x][j] == undefined) ? "Select" : listadoFunc[x][j];
	eval("vaciar"+funcion+"(listadoSelects[x][j],cad);");
	
	}
			
	}else{

	var funcion = (listadoFunc[x] == undefined) ? "Select" : listadoFunc[x];
	eval("vaciar"+funcion+"(listadoSelects[x],cad);");
	
	}
			 
	x++;
	
	}
	/*
}
*/
// Compruebo que el select modificado no sea el ultimo de la cadena

if(opcionSeleccionada!=0 && idSelectOrigen!=listadoSelects[listadoSelects.length-1]){


// Obtengo el elemento del select que debo cargar
var idSelectDestino=listadoSelects[posicionSelectDestino];

// Si es un array (un select tiene 2 o mas selects que dependen de el)
if(typeof(idSelectDestino) == "object"){
	
for(j=0;j<idSelectDestino.length;j++){

if(cad == undefined)
cad=new Array();

var funcion = (listadoFunc[posicionSelectDestino] == undefined || listadoFunc[posicionSelectDestino][j] == undefined) ? "Select" : listadoFunc[posicionSelectDestino][j];

eval("$.select._carga"+funcion+"(idSelectDestino[j],opcionSeleccionada,cad[j],callback);");
}

}else{

var funcion = (listadoFunc[posicionSelectDestino] == undefined) ? "Select" : listadoFunc[posicionSelectDestino];

eval("$.select._carga"+funcion+"(idSelectDestino,opcionSeleccionada,cad,callback);");

}		


} 



},


_cargaSelect: function(idSelectDestino,opcionSeleccionada,cad,callback){

	
if(cad == undefined)
cad="Elige";
			
var options = '<option value=0>'+cad+'</option>';

			$.getJSON(base+"reservas/select_dependientes/"+opcionSeleccionada+"/"+idSelectDestino,function(json){//cargamos la lista dependiente
			

			
			for(var i=0;i<json.length;i++){
			options+='<option value="'+json[i].optionValue+'"';
			
			if(json.length == 0)
			options+="selected=\"selected\"";
			
			options+='>'+json[i].optionDisplay+'</option>';
			
			}
			

			
			
			
			$('select[name='+idSelectDestino+'] option').remove();
			$('select[name='+idSelectDestino+']').css("display","inline")
								    .removeAttr("disabled")
			                                                           .append(options);
			if($('div[id='+idSelectDestino+'_d]').css("display") == "none")
			$('div[id='+idSelectDestino+'_d]').css("display","block");
/*			
if(json.length == 1){
	
var fun = trim($('select[name='+idSelectDestino+']').attr("onchange"));

fun =str_replace(["function onchange(event) {","\n}","this"],["","","document.getElementById('"+idSelectDestino+"')"],fun);


eval(fun);

}
*/

if( callback ) callback();

		})
		
}
,
_cargaAlumno: function(idSelectDestino,opcionSeleccionada,cad,callback){

			$.getJSON(base+"select_dependientes_proceso.php?select="+idSelectDestino+"&opcion="+opcionSeleccionada,function(json){//cargamos la lista dependiente
				
			var options = "";
			for(var i=0;i<json.length;i++){
				
	

			options+='<label for="" class=\"checked\"><input type="checkbox" id="per'+json[i].optionValue+'" name="personas[]" value="'+json[i].optionValue+'" onclick="seleccionar(this.id);" checked="checked">'+json[i].optionDisplay+'</option>';
			}
			
			$('div[id='+idSelectDestino+'] > * ').remove();
			$('div[id='+idSelectDestino+']').append(options);
			$('div[id='+idSelectDestino+'_d]').css("display","block");
			
			if( callback ) callback();
			
		})
	/*	
	$(".ListaMultiseleccion input[type='checkbox']").each(function(){
		if($(this).is(':checked')){
			$(this).parents("label").addClass("checked");
		}else{
			$(this).parents("label").removeClass("checked");
		}

	});
*/



}



}

cargaContenido = function(message, title, callback,selects) {
		$.select.cargar(message, title, callback,selects);
}

})(jQuery);








	


function vaciarSelect(idSelect,cad){
	
if(cad == undefined)
cad = "Selecciona opci&oacute;n";

	//alert(idSelect);
	var option= '<option value=0>'+cad+'</option>';
	
	$('select[name='+idSelect+'] option').remove();
	$('select[name='+idSelect+']').append(option)
					 .attr("disabled","true");

}













function vaciarAlumno(idSelectDestino,cad){

	//alert(idSelectDestino);
			$('div[id='+idSelectDestino+'] > * ').remove();
			
}













