
// scroll de texte 
var idScroll
var idScrollHorit
var altScroll
var idInt

function initScroll (box) {
	idScroll = document.getElementById(box);
	altScroll = idScroll.scrollHeight
	//alert(altScroll);
	scrollUpid = document.getElementById("scroll-top")
	scrollBottomid = document.getElementById("scroll-bottom")
	if (altScroll<356) { // amagar scroll si no es necessari
		scrollUpid.style.display="none";
		scrollBottomid.style.display="none";
	} else {
		scrollUpid.style.display="block";
		scrollBottomid.style.display="block";
	}
}

function initScrollHoritzontal (box) {
	idScrollHorit = document.getElementById(box);
	idScrollHorit_interior = document.getElementById('caja-ancho-scroll');
	if (idScrollHorit_interior) {
		ampleScroll = idScrollHorit_interior.scrollWidth
		//alert(ampleScroll);
		scrollLeftid = document.getElementById("scroll-left")
		scrollRightid = document.getElementById("scroll-right")
		// no s'ha pogut provar amb archivo, puja fins a 700 i veure si funciona amb itinerancia
		if (ampleScroll<660) { // amagar scroll si no es necessari
			scrollLeftid.style.display="none";
			scrollRightid.style.display="none";
		} else {
			scrollLeftid.style.display="block";
			scrollRightid.style.display="block";
		}
	}
}

function scrollUp () {
	idInt = setInterval("scrollar(-10)",50);
}

function scrollDown () {
	idInt = setInterval("scrollar(10)",50);
}

function scrollHLeft () {
	idInt = setInterval("scrollarhorit(-10)",50);
}

function scrollHRight () {
	idInt = setInterval("scrollarhorit(10)",50);
}

function scrollOut() {
	clearInterval(idInt);
}

function scrollar(interval) {	
	idScroll.scrollTop+=interval;
}

function scrollarhorit(interval) {	
	idScrollHorit.scrollLeft+=interval;
}

function overCell(quinID, col, bgcol) {
  	obj = document.getElementById(quinID);
  	//obj.style.fontWeight = "bold";
	obj.style.color = col;
	obj.style.backgroundColor=bgcol;		
}

function borrar (id,txt) {
	if (confirm('Borrar ítem: ' + txt + '?')) {
		this.location = "acciones.asp?action=borrar&id="+id;
	} 
}

function validar (id,txt) {
	if (confirm('Validar ítem: ' + txt + '?')) {
		this.location = "acciones.asp?action=validar&id="+id;
	} 
}

function restaurar (id,txt) {
	if (confirm('Restaurar ítem: ' + txt + '?')) {
		this.location = "acciones.asp?action=restaurar&id="+id;
	} 
}

function finestracentre (url, nom, ample, alta) {
	ampl = screen.availWidth/2;
	alt = screen.availHeight/2;
	_left=ampl-ample/2;
	_top=alt-alta/2;			
	finestra = window.open(url, nom, "toolbar=no,location=no,scrollbars=yes,resizable=no,width="+ample+",height="+alta+",top=" + _top + ",left=" + _left);			
	finestra.focus(); 
}

function finestracentresenseSC (url, nom, ample, alta) {
	ampl = screen.availWidth/2;
	alt = screen.availHeight/2;
	_left=ampl-ample/2;
	_top=alt-alta/2;			
	finestra = window.open(url, nom, "toolbar=no,location=no,scrollbars=no,resizable=no,width="+ample+",height="+alta+",top=" + _top + ",left=" + _left);			
	finestra.focus(); 
}

function amagarTots (_array) {
	n=_array.length;
	for (i=0;i<n;i++) {				
		if (document.getElementById) {
			thisMenu = document.getElementById(_array[i]).style;
			thisMenu.display = "none"
		}
	}
}
function toggleMenu(currMenu) {
	if (document.getElementById) {
		thisMenu = document.getElementById(currMenu).style		
		if (thisMenu.display == "") {
			thisMenu.display = "none"
		}
		else {
			thisMenu.display = ""
		}
		return false
	}
	else {
		return true
	}
}
function canviarTots (_array) {
	n=_array.length;
	for (i=0;i<n;i++) {		
		toggleMenu(_array[i]);
	}
}

var nimg = 0;
var maximg = 5;
var img_id = 0;
var to;
function galeriapublicaciones (sentit,path,str_array_img) {
	
	eval('array_img=Array('+str_array_img+')');
	var n_total = array_img.length;
	var n = n_total - 1

	var img = document.getElementById("diaposimg");
	var flecha_izq = document.getElementById("flecha-img-izq");
	var flecha_der = document.getElementById("flecha-img-der");

	if (sentit=='dret' && img_id<n) {
		img_id++;	
	} else if (sentit=='esq' && img_id>0) {
		img_id--;			
	} 
	//alert(img_id)
	if (img_id==0) {
		flecha_izq.style.display="none";
		flecha_der.style.display="block";
	} else if (img_id==n) {
		flecha_der.style.display="none";
		flecha_izq.style.display="block";
	} else {
		flecha_izq.style.display="block";
		flecha_der.style.display="block";
	}
	
	for (i=0;i<n_total;i++) {
		img = document.getElementById("diaposimg"+i);
		img.style.display = "none"
	}
	img = document.getElementById("diaposimg"+img_id);
	img.style.display = "block"
		
	//divimg = document.getElementById("imgs");
	//divimg.innerHTML = '<img src="'+path+'/'+array_img[img_id]+'" alt="imagen interior" title="imagen interior" id="diaposimg" />';
	//document.getElementById("diaposimg").style.display = "block"
	//alert(path+'/'+array_img[img_id]);
	/*img_temp = new Image();
	img_temp.src = path+'/'+array_img[img_id];
	img.src=img_temp.src;*/
	return false;
}

function preload_img (path,str_array_img) {
	eval('array_img=Array('+str_array_img+')');
	for (i=0;i<array_img.length;i++) {
		img_temp = new Image();
		img_temp.src = path+'/'+array_img[i]
	}
}

function cambioficha (tipo,id,entorno) {
	switch (tipo) {
		case 'documentales':
			document.location="new_documentales.asp?id="+id+"&entorno="+entorno;
			break;
		case 'publicaciones':
			document.location="publicaciones.asp?id="+id+"&entorno="+entorno;
			break;
		case 'exposiciones':
			document.location="exposiciones.asp?id="+id+"&entorno="+entorno;
			break;
	}
		
}

// scroll amb la roda del mouse
/** This is high-level function.
 * It must react to delta being more/less than zero.
 */
function handle(delta) {		
        if (delta < 0) {
			int = 10;
		} else if (delta > 0) {
			int = -10;
		} 
		
		idScroll = document.getElementById("contentmain");
		if (idScroll) {
			altScroll = idScroll.scrollHeight
			if (altScroll>=356) {
				idScroll.scrollTop+=int;
			}
		}
}

/** Event handler for mouse wheel event.
 */
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;

}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */
if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;

/*********************/

function validEmail(email) {

			invalidChars = " /:,;"


			if (email == "") {

				return false

			}

			for (i=0; i<invalidChars.length; i++) {

				badChar = invalidChars.charAt(i)

				if (email.indexOf(badChar,0) > -1) {

					return false

				}

			}

			atPos = email.indexOf("@",1)

			if (atPos == -1) {

				return false

			}

			if (email.indexOf("@",atPos+1) > -1) {

				return false

			}

			periodPos = email.indexOf(".",atPos)

			if (periodPos == -1) {

				return false

			}

			if (periodPos+3 > email.length)	{

				return false

			}

			return true

}

function submitItMultiple(form,array_camps,array_strs,frase) {
	if (frase == undefined ) {
		frase='es un campo obligatorio';
	}
	// aqui form es un string
	for (var i=0;i<array_camps.length;i++) {
		camp = eval("form."+array_camps[i]);	
		val = camp.value;
		val = val.trim();
		if (camp.id=='email' && !validEmail(val)) {
				//alert('Formato de email incorrecto');
				alert(array_strs[i] + " " + frase);
				return false;
		} else if (val=="") {
			alert(array_strs[i] + " " + frase);
			return false;
		}
	}
}

String.prototype.trim = function() {
		return this.replace( /(^\s*)|(\s*$)/g, '' ) ;
}


// Esta es la función que usa Heinle para recuperar una cookie
// name - nombre de la cookie deseada
// devuelve un string conteniendo el valor de la cookie especificada o null si la cookie no existe

function getCookie(name){
  var cname = name + "=";               
  var dc = document.cookie;             
  if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
    if (begin != -1) {           
      begin += cname.length;       
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    } 
  }
  return null;
}


// Esta es una adaptación de la función de Dorcht para colar una cookie
// name - nombre de la cookie
// value - valor de la cookie
// [expires] - fecha de caducidad de la cookie (por defecto, el final de la sesión)
// [path] - camino para el cual la cookie es válida (por defecto, el camino del documento que hace la llamada)
// [domain] - dominio para el cual la cookie es válida (por defecto, el dominio del documento que hace la llamada)
// [secure] - valor booleano que indica si la trasnmisión de la cookie requiere una transmisión segura
// al especificar el valor null, el argumento tomará su valor por defecto

function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}


// Esta es una adaptación de la función de Dorcht para borrar una cookie
// name - nombre de la cookie
// [path] - camino de la cookie (debe ser el mismo camino que el especificado al crear la cookie)
// [domain] - dominio de la cookie (debe ser el mismo dominio que el especificado al crear la cookie)
// se considera el camino y dominio por defecto si se especifica null o no se proporcionan argumentos

function delCookie (name,path,domain) {
  if (getCookie(name)) { 
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function cambiaIdioma0 (idioma) {
	// usat per as la admin
	act_url = String(document.location);
	str = "idioma=";
	index = act_url.indexOf(str,0);
	if (index!=-1) {
		act_url = act_url.substring(0,index-1);
	}
	index = act_url.indexOf("?",0);
	if (index!=-1) {
		char_link = "&"
	} else {
		char_link = "?"
	}
	new_url = act_url + char_link + "idioma="+idioma;
	document.location = new_url;
}

function cambiaIdioma (idioma) {
	// usat per idioma.asp
	document.location = "idioma.asp?idioma="+idioma;
}

function cambiaIdiomaPagina (idioma) {
	// cridat des de idioma.asp per a actualitzar la pŕgina
	if (parent.mainFrame.location!='about:blank') {
		act_url = String(parent.mainFrame.location);
		str = "idioma=";
		index = act_url.indexOf(str,0);
		if (index!=-1) {
			act_url = act_url.substring(0,index-1);
		}
		index = act_url.indexOf("?",0);
		if (index!=-1) {
			char_link = "&"
		} else {
			char_link = "?"
		}
		if (idioma+""=="") {
			idioma = 'esp';
		}
		new_url = act_url + char_link + "idioma="+idioma;
		parent.mainFrame.location = new_url;
	} 
}

function login_concurso(){
	user = document.formulario.user.value;
	pass = document.formulario.pass.value;
	if (user.length < 3 || pass.length < 3) {
		alert("Debe completar los campos de Usuario y Contraseńa");
	} else {		
		//document.location = "becas.asp?pagina=becaslogin";
		document.formulario.submit();
		obj_frame = document.getElementById("frame-becaslogin");
		obj_frame.style.display="block";
		obj_frame = document.getElementById("login-becas");
		obj_frame.style.display="none";
		obj_frame = document.getElementById("box-resultados");
		obj_frame.style.display="none";
		obj_frame = document.getElementById("frame-dret-img");
		obj_frame.style.display="none";
		obj_frame = document.getElementById("frame-dret-info");
		obj_frame.style.display="none";
		obj_frame = document.getElementById("scroll-top");
		obj_frame.style.display="none";
		obj_frame = document.getElementById("scroll-bottom");
		obj_frame.style.display="none";
		//window.frames[0].location = "http://concurso-becas.arquia.es/concurso.php";
	}
}