
// obre un popup amb les diumensions necessaries per a l'administrador.
function obre_popup_admin(url)
{
	
	window.open(url,'','height=650,width=780,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,modal=yes');
	
}

// selecciona tots els check d'un formulari amb un nom i els posa a cert o fals

function select_all(nom_form,name, value) 
{

	formblock= document.getElementById(nom_form);
	forminputs = formblock.getElementsByTagName('input');
	
	for (i = 0; i < forminputs.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (regex.test(forminputs[i].getAttribute('name'))) 
		{
			if (value == '1') 
			{
				forminputs[i].checked = true;
			} 
			else 
			{
				forminputs[i].checked = false;
			}
		}
	}
}


function select_canvi_estat_nou(nom_form,name) 
{
	formblock= document.getElementById(nom_form);
	for (i=0;i<formblock.elements.length;i++)
	{
		if(formblock.elements[i].type == "checkbox")	
		{
			//tmp = String(formblock.elements[i].name);
			
			//if(formblock.elements[i].name);
			formblock.elements[i].checked = !formblock.elements[i].checked;			
		}
	}
}


function select_canvi_estat(nom_form,name) 
{
	formblock= document.getElementById(nom_form);
	forminputs = formblock.getElementsByTagName('input');
	
	for (i = 0; i < forminputs.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (regex.test(forminputs[i].getAttribute('name'))) 
		{
			forminputs[i].checked = !forminputs[i].checked;			
		}
	}
}

// MArca un grup de checkbox com a deshabilitats i seleccionats i viceversa
function select_canvi_estat_subgrup(nom_form,name,valor) 
{
	formblock= document.getElementById(nom_form);
	forminputs = formblock.getElementsByTagName('input');
	
	for (i = 0; i < forminputs.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (regex.test(forminputs[i].getAttribute('name'))) 
		{						
			forminputs[i].checked = valor;
			forminputs[i].disabled = valor;			
		}
	}
}


function canvia_imatge_finestra_pare(id_imatge,nom_variable_pare,url,text)
{
	document_pare = window.opener.document;
	document_pare.getElementById(nom_variable_pare).value = id_imatge;
	document_pare.getElementById("select_img_" + nom_variable_pare).src = url;
	
	if(document_pare.getElementById("desc_" + nom_variable_pare) != null) 
		document_pare.getElementById("desc_" + nom_variable_pare).value = text;
	window.close();	
}

function canvia_imatge(id_imatge,nom_variable_pare,url,text)
{
	document.getElementById(nom_variable_pare).value = id_imatge;
	
	if(document.getElementById("select_img_" + nom_variable_pare) != null) 
		document.getElementById("select_img_" + nom_variable_pare).src = url;	
	
	// la descripcio no hi ser� sempre 
	if(document.getElementById("desc_" + nom_variable_pare) != null) 
	{		
		 tmp = document.getElementById("desc_" + nom_variable_pare);
		 if(tmp.value != null)
		 	tmp.value = text;
		 else		 
		 	tmp.innerHTML = text;
	}
}




// envia a la finestra pare la selecci� d'un element seleccionat a popup
function canvia_seleccio_popup_finestra_pare(id,nom_variable_pare,text)
{
	
	
	document_pare = window.opener.document;
	document_pare.getElementById(nom_variable_pare).value =id;	
	
	// no �s necessari que la descripci� hi sigui sempre
	if(document_pare.getElementById("desc_" + nom_variable_pare) != null) 
		document_pare.getElementById("desc_" + nom_variable_pare).innerHTML = text;
		
	window.close();	
}

function canvia_seleccio_popup(id,nom_variable_pare,text)
{
	document.getElementById(nom_variable_pare).value = id;

	// no �s necessari que la descripci� hi sigui sempre
	if(document.getElementById("desc_" + nom_variable_pare) != null) 
		document.getElementById("desc_" + nom_variable_pare).innerHTML = text;
}



function valida_esborrat_url(url)
{
	if(confirm("Està segur que vol esborrar el registre seleccionat?"))
	{
        window.location=url;
		return true;
	}
	else
	{
		return false;
	}
}


function valida_esborrat_multiple(f)
{

	if(confirm("Està segur que vol esborrar \n els registres seleccionats??"))
	{
		return true;
	}
	else
	{
		return false;
	}
	
}


function submit_accio(codi_accio,f)
{
	// cambiem la accio del formulari 
	f.accio.value = codi_accio;
	// fem el submit
	//f.submit();
}


function submit_obre_url(url)
{
	// cambiem la accio del formulari 
	//f.accio.value = codi_accio;
	// fem el submit
	//f.submit();
	//boto.disabled=1;
	
	//alert(url);
	
	window.open(url,'_blank');
	
	return false;
}


function num_seleccionats(nom_form,name) 
{

	formblock= document.getElementById(nom_form);
	forminputs = formblock.getElementsByTagName('input');
	
	seleccionat = 0;
	for (i = 0; i < forminputs.length; i++) 
	{
		// regex here to check name attribute
		var regex = new RegExp(name, "i");
		if (regex.test(forminputs[i].getAttribute('name'))) 
		{
			if(forminputs[i].checked) seleccionat++;
		}
	}
	
	return seleccionat;
}

function algun_seleccionat(nom_form,name) 
{
	return num_seleccionats(nom_form,name) >0;
}


function nova_finestra_modal(url,nom,parametres,ample,alt) 
{
	if (window.showModalDialog) 
	{
		window.showModalDialog(url,nom,"dialogWidth:" + String(ample) + "px;dialogHeight:" +  String(alt) + "px");
	}
	else 
	{
		
		
		window.open(url,nom,parametres);
		//window.open('xpopupex.htm','name','height=255,width=250,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes');
	}
} 



	/*
	 * Part del funcionament per permetre valors per defecte als formularis (w3c AAA)
	 *  i que es puguin disingir per l'usuari amb un altre color 
	 * Canvia el color i selecciona la informaci� d'un input.
	 *
	 * Cal afegir aquest comportament al event onfocus de l'input en q�esti�
	 * 
	 */ 
	function input_color_ini_edicio(o_input,color_actiu)
	{
		o_input.select();
		o_input.style.color = color_actiu; 
		
	}

	
	/*
	 * Part del funcionament per permetre valors per defecte als formularis (w3c AAA)
	 *  i que es puguin disingir per l'usuari amb un altre color 
	 * deixi en blanc, per exemple a un cercador: clau....
	 * Cal afegir aquest comportament a l'event on_blur  de l'input en q�esti�
	 * 
	 */ 
	function input_color_final_edicio(o_input,valor_defecte,color_actiu,color_inactiu)
	{ 
		if (o_input.value=='') 
		{ 
			o_input.value=valor_defecte; 
			o_input.style.fontWeight = "normal"; 
			o_input.style.color = color_inactiu; 
		} 
		
		
		if (o_input.value != valor_defecte) 
		{ 
			o_input.style.color = color_actiu; 
		}

	}	