// ====================================================
// FUNCIONS JAVASCRIPT
//
// Creat: 10/05/2006
// Ultima modificacio: 10/05/2006
//
// Conté les funcions globals de javascript
// ====================================================

function enviarForm () {
	var _args = enviarForm.arguments, _form = _args[0], _enviar = true;

	var er_r = /^[\.\s\w\W]*\w[\.\s\w\W]*$/
	var er_n = /^[0-9]+$/
	var er_d = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/
	var er_e = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/

	for (i = 1; ((i < _args.length) && (_enviar)); i += 2) {
		_str = String (_args[i+1]);
		_camp = eval ("_form." + _args[i]);

		if(_enviar && (_str.indexOf("r")>-1) && !er_r.test(_camp.value)){
			_enviar=false;
			_camp.focus();
			alert ("Te has dejado un campo en blanco.");
			}
		if(_enviar && (_str.indexOf("n")>-1) && er_r.test(_camp.value) && !er_n.test(_camp.value)){
			_enviar=false;
			_camp.focus();
			alert ("El contenido de este campo debe ser numérico.");
			}
		if(_enviar && (_str.indexOf("d")>-1) && er_r.test(_camp.value) && !er_d.test(_camp.value)){
			_enviar=false;
			_camp.focus();
			alert ("Introduce la fecha con el formato dd/mm/aaaa.");
			}
		if(_enviar && (_str.indexOf("e")>-1) && er_r.test(_camp.value) && !er_e.test(_camp.value)){
			_enviar=false;
			_camp.focus();
			alert ("Introduce una dirección de correo válida.");
			}
	}
	if (_enviar) { _form.quies.value = "korekta"; _form.submit(); }
	return _enviar;
}

/// Submenuns ///
var menuns = new Array ();
menuns[101] = false;
menuns[102] = false;
menuns[103] = false;
menuns[104] = false;
function amagarSM (__SM) {
	if (menuns[__SM]) {
		new Effect.Fade ('submenu'+__SM, {duration:0.4, to:0.0});
		menuns[__SM] = false;
	}
}
function voreSM (_SM) {
	__vore = !menuns[_SM];
	amagarSM (104);
	amagarSM (103);
	amagarSM (102);
	amagarSM (101);
	if (__vore) {
		new Effect.Appear('submenu'+_SM,{duration:0.6, to:0.99});
		menuns[_SM] = true;
	}
}

/******************** TREBALLS *************************/
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth;
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src;
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href;
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

imgPreloader = new Array();
function carregarImage (_img, _rutaimage) {
		imgPreloader[_img] = new Image();

		// once image is preloaded, resize image container
		imgPreloader[_img].onload=function(){
			Element.setSrc(_img, _rutaimage);
			imgPreloader[_img].onload=function(){};	//	clear onLoad, IE behaves irratically with animated gifs otherwise
		}
		Element.setSrc(_img, (_img == 'fototreball') ? 'vida/loading.gif' : 'vida/loading-mini.gif');
		imgPreloader[_img].src = _rutaimage;
}

treball = null;
imagens = new Array (); // l'he d'inicialisar en pagina.php

function canviarMiniatures (__id) {
	for (x = 0; x < imagens[__id].length; x++) {
// 		carregarImage ('miniimg'+(x+1), 'redimensionar.php?w=132&h=99&img='+imagens[__id][x]);
		carregarImage ('miniimg'+(x+1), 'mini'+imagens[__id][x]);
		Element.show ('mini'+(x+1));
	}
	for (; x < 3; x++) {
		Element.setSrc ('miniimg'+(x+1), 'vida/spacer.gif');
		Element.hide ('mini'+(x+1));
	}
}

function voreTreball (_id) {
	if (treball != null) {
		Element.hide ('descripcio'+treball);
		Element.setSrc ('desplegar'+treball, 'vida/boto_desplegar.png');
		Element.addClassName ('h2treball'+treball, 'treballs');
	}
	if ((treball == null) || (treball != _id)) {
		Element.removeClassName ('h2treball'+_id, 'treballs');
		Element.setOpacity ('descripcio'+_id, 0.0);
		new Effect.Appear('descripcio'+_id,{duration:0.6, from: 0.0, to:0.99});
		Element.setSrc ('desplegar'+_id, 'vida/boto_plegar.png');
		canviarMiniatures (_id);
		new Effect.Appear('miniatures',{duration:0.6, to:0.99});
		treball = _id;
	} else {
		new Effect.Fade('miniatures',{duration:0.6, to:0.0});
		treball = null;
	}
}

function voreImage (_img) {
	carregarImage ('fototreball', imagens[treball][_img-1]);
	if (!Element.visible ('quadrofoto')) new Effect.Appear('quadrofoto',{duration:0.6, from: 0.0, to:0.99});
}

function amagarImage () {
	new Effect.Fade('quadrofoto',{duration:0.6, to:0.0});
}
