// Oculta o Muestra un layer, compatible con EXPLORER, N4, NN6
// Update: usando prototype.js

function set_visibility(name, f) {
	var obj = $(name);
	if (obj) {
		obj.style.visibility = (f) ? 'visible' : 'hidden';
	}
}

function set_visibility_bot_o(f)
{
	set_visibility('layer_bot_o', f);
}

function set_visibility_bot_t(f)
{
	set_visibility('layer_submit_t', f);
}

function set_disabled(name, value) {
	var obj = $(name);
	if (obj) {
		obj.disabled = value;
	}
}

function set_disabled_array(arr, value) {
	$A(arr).each(function dis(elem) {
		set_disabled(elem, value);
	});
}


function cambiando(opciones, tiempo) 
{

	var sentence = "" + tiempo; // forzando string

	// Modificacion para casos especiales 
	if( (tiempo=='RED') || (tiempo=='RES')|| (tiempo=='VISIT') || (tiempo=='PAGIN') || (tiempo=='PAGOUT')) {
		tiempo='DIAS'; 
	}

	if (sentence.indexOf("HORAS")!=-1){ tiempo='HORAS'; }
	if (sentence.indexOf("DIA")!=-1){ tiempo='DIAS'; }
	if (sentence.indexOf("SEMANAS")!=-1){ tiempo='SEMANAS'; }
	if (sentence.indexOf("MES")!=-1){ tiempo='MES'; }


	var d = (tiempo == 'DIAS' || tiempo == 'SEMANAS' || tiempo == 'HORAS');

	set_disabled('t0_Dat_ID', d);
	set_disabled('t1_Dat_ID', d);


	if (opciones && opciones[0] == ' -- ') {
		set_disabled_array(new Array('t0_Day_ID', 't0_Month_ID', 't0_Year_ID', 't1_Day_ID', 't1_Month_ID', 't1_Year_ID', 'submit_t'), true);

		$('t0_ID_Link').removeAttribute('href');
		$('t1_ID_Link').removeAttribute('href');

		set_visibility('layer_bot_o', true);
                $('submit_o').disabled = false;
		set_visibility('layer_submit_t', false);

		document.ucal_dcontrol.t1.value = '';
		return 0;
        }

	// elementos_iniciales es inicializada justo despues de dibujar el segundo control
	// calendario... ver HTMLScreen.php (y de paso ver como cambiarlo)
	var d = document.ucal_dcontrol;
	for(var i = elementos_iniciales; i < d.childNodes.length; i++) {
		d.removeChild(d.childNodes.item(i));
	}

// No funciona, lo dejare a prueba por unos dias - cmeza
//	for(var i = 0; i < opciones[1].length; i++) {
//		d.appendChild(opciones[1][x]);
//	}

	for(x in opciones[1] ){
        name = opciones[1][x].name;
        if (keep_dates && (name == 't0' || name == 't1')) {
            if (d[name]) {
                d[name].value = opciones[1][x].value;
            } else {
                d.appendChild(opciones[1][x]);
            }
        } else {
            d.appendChild(opciones[1][x]);
        }
	}

	if(!opciones[2]) { 
		set_disabled_array(new Array('t0_Day_ID', 't0_Month_ID', 't0_Year_ID', 't0_ID_Link'), true);
        if (!keep_dates) {
            d.t0.value = '';
        }
// Cambio para no submitir cuando se selecciona un rango - cmeza
//		d.submit();
		$('submit_o').disabled = false;
		set_visibility_bot_o(true);
		$('submit_t').disabled = true;
		set_visibility_bot_t(false);
// Fin cambio

	} else {
		set_disabled('t0_Day_ID', !(tiempo == 'DIAS' || tiempo == 'SEMANAS' || tiempo == 'HORAS'));

		set_disabled_array(new Array('t0_Month_ID', 't0_Year_ID'), false);
		$('t0_ID_Link').setAttribute('href', 'javascript:t0_Object.show()');

		set_disabled('submit_o', true);
		set_visibility_bot_o(false);
		set_disabled('submit_t', false);
		set_visibility_bot_t(true);
	}

	if(!opciones[3]) { 
		set_disabled_array(new Array('t1_Day_ID', 't1_Month_ID', 't1_Year_ID', 't1_ID_Link'), true);
        if (!keep_dates) {
            d.t1.value = '';
        }
	} else {
		set_disabled('t1_Day_ID', !(tiempo == 'DIAS' || tiempo == 'SEMANAS' || tiempo == 'HORAS'));

		set_disabled_array(Array('t1_Month_ID', 't1_Year_ID'), false);
		$('t1_ID_Link').setAttribute('href', 'javascript:t1_Object.show()');

		$('submit_o').disabled = true;
		set_visibility_bot_o(false);
		$('submit_t').disabled = false;
		set_visibility_bot_t(true);

		d.t1.value = $('t1_Year_ID').value + '-' + (Number($('t1_Month_ID').selectedIndex) +1 ) + '-' + (Number($('t1_Day_ID').selectedIndex) + 1 );
	} 
}

function cambiando_1(index,array_op) 
{
	var i=0;
	var d = document.ucal_dcontrol;
	for(var j=0;j < d.select_2_lateral.length; j++ ){ 
		d.select_2_lateral.options[j] = null; 
	}

	d.select_2_lateral.length = 0;
    
	var a = $A(array_op[d.select_1_lateral.options[index].value]);

	for(x in array_op[d.select_1_lateral.options[index].value]){
		document.ucal_dcontrol.select_2_lateral.options[i] = new Option(array_op[document.ucal_dcontrol.select_1_lateral.options[index].value][x][0],x);
		i++;
	}
        
}
