var flag_cambios;
var campo_fecha;
flag_cambios = false;
var cal_obj1 = null;
var format = '%d/%m/%Y';
var seleccion;
var bh = new Array();
var portada_actual;

// show calendar
function show_cal(el) {
	if (cal_obj1) return;

//var text_field = document.getElementById("f_fecha");
	var text_field = el;
	campo_fecha = el;
	var divcal = document.getElementById("divcal");

	cal_obj1 = new RichCalendar();
	cal_obj1.start_week_day = 1;
	cal_obj1.show_time = false;
	cal_obj1.auto_close = false;
	cal_obj1.language = 'es';
	cal_obj1.default_language = 'es';
	cal_obj1.user_onchange_handler = cal1_on_change;
	cal_obj1.user_onclose_handler = cal1_on_close;
	cal_obj1.user_onautoclose_handler = cal1_on_autoclose;
	cal_obj1.parse_date(text_field.value, format);
	cal_obj1.show_at_element(text_field, "adj_left-bottom");
}

// user defined onchange handler
function cal1_on_change(cal, object_code) {
	if (object_code == 'day') {
		campo_fecha.value = cal.get_formatted_date(format);
		cal.hide();
		cal_obj1 = null;
		document.frm_simplesearch.submit();
	}
}

// user defined onclose handler (used in pop-up mode - when auto_close is true)
function cal1_on_close(cal) {
	cal.hide();
	cal_obj1 = null;
}

// user defined onautoclose handler
function cal1_on_autoclose(cal) {
	cal_obj1 = null;
}

function objetoAjax(){
    var xmlhttp=false;

    try {
            xmlhttp = new XMLHttpRequest();
    } catch (trymicrosoft) {

    try {
           xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
           try {
              xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (E) {
                   xmlhttp = false;
           }
    }

    }

    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
           xmlhttp = new XMLHttpRequest();
    }

    return xmlhttp;
}

function add_news(tipo, id, sala) {
	if (tipo=="concierto")
		check_news = document.getElementById('news_' + id);
	else if (tipo=="club")
		check_news = document.getElementById('news_' + id + '_' + sala);

	valor = check_news.checked;
    ajax = objetoAjax();

    if (valor) { 
        ajax.open("GET", "/actions.php?op=addnews&t=" + tipo + "&id="+id+"&sala="+sala);
    }
    else {
    	ajax.open("GET", "/actions.php?op=delnews&t=" + tipo + "&id="+id+"&sala="+sala);
    }

    ajax.onreadystatechange=function() {
           if (ajax.readyState==4) {
        	   //alert (ajax.responseText);
           }
    }

    ajax.send(null); 
}

function cambios() {
	flag_cambios = true;
}

function salidaPagina(formulario) {
	if (flag_cambios) {
		if (confirm ("Ha realizado cambios en el formulario. �Desea guardarlos?")) 
			document.forms[formulario].submit();
	}
}

function iluminar(itemid) {
	document.getElementById(itemid).src = "/img/" + itemid + "_i.png";
}

function n(itemid) {
	el = document.getElementById(itemid);
	if (!( (el.id=='btn_age') && (seleccion==1) ) || !( (el.id=='btn_ent') && (seleccion==2 ) || !( (el.id=='btn_art') && (seleccion==3) ) ))
		el.src = "/img/" + itemid + "_n.png";
}

function getY( oElement )
{
	var iReturnValue = 0;
	while( oElement != null ) {
		iReturnValue += oElement.offsetTop;
		oElement = oElement.offsetParent;
	}
	return iReturnValue;
}

function senyala(el, despl) {
	aflecha = document.getElementById("flecha");
	aflecha.style.top=(getY(el)-despl) + "px";
}

function nosenyala() {
	aflecha = document.getElementById("flecha");
	aflecha.style.top="-1000px";
}

function seleccionar(el, despl) {
	aflecha = document.getElementById("seleccion");
	seleccion.style.top=(getY(el)-despl) + "px";
}


function noticia(i) {
	divnoti = document.getElementById("por_titulo");
	divtexto = document.getElementById("por_texto");
	
	divnoti.innerHTML=tit_noticias[i];
	divtexto.innerHTML=text_noticias[i];
	
	document.getElementById("alink" + i).style.color = "#c90c25";
	portada_actual=i;
}

function noticia_siguiente() {
	oTransition.loadImage(document.getElementById('alink' + getPortadaSig(num_portadas)));
	if (portada_actual==num_portadas)
		portada_actual=0;
	var t=setTimeout("noticia_siguiente()", 6000);
}

function show_adicionales() {
	check = document.getElementById('adicionales').checked;
	tr1 = document.getElementById('adi1');
	tr2 = document.getElementById('adi2');
	tr3 = document.getElementById('adi3');

	if (check) {
		tr1.style.display = 'table-row';
		tr2.style.display = 'table-row';
		tr3.style.display = 'table-row';
	} else {
		tr1.style.display = 'none';
		tr2.style.display = 'none';
		tr3.style.display = 'none';
	}
}

function acero(check, elid) {
	el = document.getElementById(elid);
	if (check.checked) {
		el.value="1";
	}
	else {
		bh[elid]=el.value;
		el.value=0;
	}
}

function getPortadaAnt() {
	if (portada_actual>1)
		portada_actual--
	return portada_actual;
}

function getPortadaSig(num_portadas) {
	if (portada_actual<num_portadas)
		portada_actual++;
	return portada_actual;
}

function columnize(buttonElement) {
	//buttonElement.parentNode.removeChild(buttonElement);
	
	multiColumnSettings=new MultiColumnSettings;
	multiColumnSettings.classNameScreen='screenContainer';
	multiColumnSettings.classNamePrint='printContainer';
	multiColumnSettings.minHeight=500;
	multiColumnSettings.extraHeight=60;
	multiColumnSettings.readOnText="Continue >>";
	multiColumnSettings.numberOfColumns=null;
	
	
	new MultiColumn(document.getElementById('columnas'),multiColumnSettings);	
}

function Newspaperize(id)
{
	elem = document.getElementById(id);
    var halflength = elem.innerText.length / 2; 
    do {
    	halflength--;
    } while (elem.innerText.charAt(halflength)!=" ");
    var col1 = elem.innerText.substring(0, halflength);
    var col2 = elem.innerText.substring(halflength);

    elem.innerHTML = '<TABLE WIDTH=100% cellpadding=0 cellspacing=0><TR>' + 
        '<TD WIDTH=50% VALIGN=TOP style="font-size: 13px;">' + col1 + '</TD>' +
        '<TD VALIGN=TOP style="font-size: 13px;">' + col2 + '</TD>' +
        '</TR></TABLE>';
}

var timer_id;
function scroll_iframe(frm,inc,dir) {
	if (timer_id) clearTimeout(timer_id);
	if (frm=="ifr_proximos_conc")
		frame = window.parent.frames[0];
	else if (frm=="ifr_proximos_club")
		frame = window.parent.frames[1];
	if (dir == "v") 
		frame.scrollBy(0, inc);
	else 
		frame.scrollBy(inc, 0);
	timer_id = setTimeout("scroll_iframe('" + frm + "'," + inc + ",'" + dir + "')", 20);
}

function stopScroll() { 
	if (timer_id) clearTimeout(timer_id); 
}

function addText(textarea, text) {
	ta = document.getElementById(textarea);
	ta.value += text;
}

function validate_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false)
      return false;
   else
	  return true;

}

function validar_news() {
	f_nombre = document.getElementById('f_nombre');
	f_email = document.getElementById('f_email');
	f_pais = document.getElementById('f_pais');
	f_provincia = document.getElementById('f_provincia');
	chk_acepto = document.getElementById('chk_acepto');
	
	retorno=true;
	
	if (f_nombre.value=="") {
		alert('Introduce tu nombre');
		return false;
	}
	
	if (!validate_email(f_email.value)) {
		alert('Introduce un e-mail v�lido');
		return false;
	}
	
	if ((f_pais.value=="Espa�a")&&(f_provincia.value==0)) {
		alert('Introduce tu provincia');
		return false;
	}
	
	if (chk_acepto.checked!=true) {
		alert ('Debes aceptar las condiciones de la nota legal');
		return false;
	}
	
	return retorno;
}
