/*************************************************************************************************
 * funcions AJAX per omplir combos
 *************************************************************************************************/

var req;

function fillCombo(oElem, oTarget, oQuery, oSize) 
{
    var strValue = oElem.options[oElem.selectedIndex].value;
    var url = "fillCombo.php?id="+ strValue +"&formName="+ oTarget.form.name +"&controlName="+ oTarget.name +"&query="+ oQuery +"&size="+ oSize;

    if (typeof XMLHttpRequest != "undefined") {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("GET", url, true);
    req.onreadystatechange = callback;
    req.send(null);
}

function callback() 
{
    if (req.readyState == 4) 
    {
        if (req.status == 200) 
        {
            // update the HTML DOM based on whether 
            fillDropDown()
        } else {
            alert("There was a problem retrieving the XML data:\n"+ req.statusText);
        }
    } 
}

function fillDropDown() 
{
    var xmlDoc = req.responseXML.documentElement;

    var strFName = xmlDoc.getElementsByTagName('formName')[0].firstChild.nodeValue;
    var strEName = xmlDoc.getElementsByTagName('formElem')[0].firstChild.nodeValue;

    var objDDL = document.forms[strFName].elements[strEName];
    
    /*
     * si la prima 'option' es buida, es deixa buida (opcio no informar)
     */
    if( objDDL.options.length > 0 )
    {
        if( objDDL.options[0].value == "" )
        {   
            objDDL.options.length = 1;
        } else {
            objDDL.options.length = 0;
        }
    } else {
        objDDL.options.length = 0;
    }

    var xRows = xmlDoc.getElementsByTagName('entry');

    if( xRows != null )
    {
        for ( i = 0; i < xRows.length; i++ ) 
        {
            var entry = xRows[i];
            var optionText = entry.getElementsByTagName("optionText")[0];
            var optionValue = entry.getElementsByTagName("optionValue")[0];

            var option = new Option( optionText.childNodes[0].nodeValue, optionValue.childNodes[0].nodeValue );

            try 
            {
                objDDL.add(option, null);
            } catch(e) {
                objDDL.add(option, -1);
            }
        }
    }
}

/*
 * funcio per veure la pestanya selccionada (mouseover)
 */
function PestanyaOver( ElObjeto )
{
	ElObjeto.style.background = "#EE9999";
}
function PestanyaOut( ElObjeto )
{
	ElObjeto.style.background = "#FFEEEE";
}

/*
 * funcio per ressaltar l'input seleccionat
 */
function TextOver( ElObjeto )
{
	ElObjeto.style.background = "#EAEAF6";
}
function TextOut( ElObjeto )
{
	ElObjeto.style.background = "#F6F6F6";
}

/*
 * funcions per canviar el color de les files al posar-te sobre
 */
// posar/treure color en les files 1
function CeldaIlumin1( ElObjeto )
{
	ElObjeto.style.background = "#EAEAF6";
}
function CeldaUndo1( ElObjeto )
{
	ElObjeto.style.background = "#FFFFFF";
}
// posar/treure color en les files 2
function CeldaIlumin2( ElObjeto )
{
	ElObjeto.style.background = "#EAEAF6";
}
function CeldaUndo2( ElObjeto)
{
	ElObjeto.style.background = "#F6F6F6";
}

// generica posar posar color
function CeldaIlumin( ElObjeto, col )
{
	if( col == null ) 
 		ElObjeto.style.background = "#EAEAF6";
 	else 
 		ElObjeto.style.background = col;
}
// generica de treure color
function CeldaUndo( ElObjeto, col)
{
 	if( col == null ) 
 		ElObjeto.style.background = "#FFFFFF";
 	else 
 		ElObjeto.style.background = col;
}

/*
 * canvia el color del fons d'una cel.la
 */
function cellcolor( cell, background_color )
{
	cell.bgColor = background_color;
}

/*************************************************************************************************
 * FUNCIONS DE VISUALITZACIÓ
 *************************************************************************************************/

/*
 * funcio per veure/ocultar 
 */
function Alternar( Seccion )
{ 
    if( Seccion.style.display == "none" ) 
    {
    	Seccion.style.display = "";
    } 
    else 
    {
    	Seccion.style.display = "none";
    } 
}

function Mostrar( Seccion )
{ 
    Seccion.style.display = "";
}

function Ocultar( Seccion )
{ 
    Seccion.style.display = "none";
}

function switchImage()
{
	if( taula1.style.display == "none" )
		document.cercador1.src = 'images/cercador1.jpg';
	else
		document.cercador1.src = 'images/cercador1_sel.jpg';
		
	if( taula2.style.display == "none" )
		document.cercador2.src = 'images/cercador2.jpg';
	else
		document.cercador2.src = 'images/cercador2_sel.jpg';
}

/*
 * popup per les fotos (amb la imatge centrada)
 */
function popUpFoto(lloc) 
{
	novawin = window.open('', '_blank', 'toolbar=no, status=no, scrollbars=no, resizable=no, menubar=no, location=no, height=600, width=800');
	novawin.document.write("<html><head><title>Ampliación de fotografia</title>" );
	novawin.document.write("<meta content='text/html; charset=ISO-8859-1' http-equiv='Content-Type'><!--tag meta--></meta>");
	novawin.document.write("<meta content='no-cache' http-equiv='Pragma'><!--Pragma content set to no-cache tells the browser not to cache the page This may or may not work in IE--></meta>");
	novawin.document.write("<meta content='0' http-equiv='expires'><!--Setting the page to expire at 0 means the page is immediately expired Any vales less then one will set the page to expire some time in past and not be cached. This may not work with Navigator--></meta>");
	novawin.document.write("<link type='text/css' href='css/privat.css' rel='stylesheet'>");
	
	novawin.document.write("</head><body>" );
	
	novawin.document.write("<table width='100%' height='600'>");
	novawin.document.write("<tr>");
	novawin.document.write("<td align='center' valign='middle' width='100%' height='100%'>");
	novawin.document.write("<img src='"+ lloc +"'>");
	novawin.document.write("</td>");
	novawin.document.write("</tr>");
	novawin.document.write("</table>");

	novawin.document.write("</body></html>" );
}
/*
 * Permet substituir un substring (strORI) per un altre (strDST) dins d'una cadena (str)
 *
 */
function Change( str, strORI, strDST )
{	
	dec = str.indexOf(strORI);
	if( dec != -1 ) 
	{
		tmp = str.substring(0, dec);
		tmp += strDST;
		tmp += str.substring(dec + 1, str.length);
		ret = tmp;
	} else {
		ret = str;	
	}

	return ret;
}

// obra una finestra amb una foto (li passes el URL)
function popUp(url, width, height) 
{
	if(width == undefined)
		width = 610;
	if(height == undefined)
		height = 500;
		
	document.open(url, "_blank", "toolbar=no, menubar=no, resizable=yes, status=no, scrollbars=yes, width="+ width +", height="+ height);
	return true;
}

// barra d'estat
function Status(txt) {
	window.status = txt;
}

// redirecciona la URL si es respon afirmativament davant d'una pregunta
function redireccioNoConfirm( txtUrl ) {
	window.open( txtUrl, 'mainFrame');
}

// redirecciona la URL si es respon afirmativament davant d'una pregunta
function redireccio(url, txt) {
	
	if (arguments.lenth>1){
		if( window.confirm (txt) ) {
			window.location = url;}		
	}else{
		window.location = url;}
		
}	

// redirecciona la URL si es respon afirmativament davant d'una pregunta destruint els frames
function redireccioTop(url, txt) {
	if( window.confirm (txt) ) {
		top.location.href = url;
	}
}

// redirecciona la URL si es respon afirmativament davant d'una pregunta en una nova finestra
function redireccioBlank(url, txt) {
	if( window.confirm (txt) ) {
		var options = "toolbar=no, status=no, scrollbars=no, resizable=no, menubar=no, location=no, height=600, width=800, left=50, top=50";

		window.open( url, '_blank', options );
	}
}

// redirecciona la URL si es respon afirmativament davant d'una pregunta en una nova finestra per impressio
function redireccioPRINT(url, txt) {
	if( window.confirm (txt) ) {
		var options = "toolbar=no, status=no, scrollbars=yes, resizable=no, menubar=no, location=no, height=600, width=700, left=50, top=50";

		window.open( url, '_blank', options );
	}
}
// redirecciona la URL si es respon afirmativament davant d'una pregunta en una nova finestra per impressio
function redireccioPRINTPDF(url, txt) {
	if( window.confirm (txt) ) {
		var options = "toolbar=no, status=no, scrollbars=no, resizable=yes, menubar=no, location=no, height=600, width=700, left=50, top=50";

		window.open( url, '_blank', options );
	}
}

// funcio per canviar el 'title' 
function canviTitle( txt ) {
	parent.document.title = "IndCat - IntraNet: "+ txt;
	
	return true;
}

// funcio que comprova la validesa d'una data
// params: dia, mes , any
function y2k(number) 
{ 
	return (number < 1000) ? number + 1900 : number; 
}
function fncValidarData(d, m, a)
{
	var date  = d;
        var month = m;
        var year  = a;
	
	var test = new Date(year, month-1, date);

	if( year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate()) ) {
		return true;
	} else {
		return false;
	}
}

function imprimeix()
{
	window.print();
}

function fncPrintarData() 
{
	var data = new Date();
	
	var days = new Array('diumenge','dilluns','dimarts','dimecres','dijous','divendres','dissabte'); 
	var months = new Array('de gener','de febrer','de març','d abril','de maig','de juny','de juliol','d agost','de septembre','d octubre','de novembre','de desembre'); 
	
	document.write( days[ data.getDay() ] +", "+ data.getDate() +" "+ months[ data.getMonth() ] +" "+ longyear( data.getYear() ) );
}
function fncPrintarDataEsp() 
{
	var data = new Date();
	
	var days = new Array('domingo','lunes','martes','miercoles','jueves','viernes','sabado'); 
	var months = new Array('de enero','de febrero','de marzo','de abril','de mayo','de junio','de julio','de agosto','de setiembre','de octubre','de noviembre','de deciembre'); 
	
	document.write( days[ data.getDay() ] +", "+ data.getDate() +" "+ months[ data.getMonth() ] +" "+ longyear( data.getYear() ) );
}
function fncPrintarDataAng() 
{
	var data = new Date();
	
	var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); 
	var months = new Array('of January','of February','of March','of April','of May','of June','of July','of August','of September','of October','of November','of December'); 
	
	document.write( days[ data.getDay() ] +", "+ data.getDate() +" "+ months[ data.getMonth() ] +" "+ longyear( data.getYear() ) );
}

function longyear(number) 
{ 
	return (number < 1000)? number + 1900 : number; 
} 

