/**************************************************/
/**************************************************/

/**************************************************/
/**************************************************/
/******************** AJAX      *******************/
/**************************************************/
/**************************************************/


var repReq;

/*****************************************************/
/* funciones les dos, però la segona és més elegant  */
/*****************************************************/

function getAjaxObj() 
{
	if (window.XMLHttpRequest)
	{
		return XMLHttpRequest; 	//no IE
	}else if (window.ActiveXObject)	
	{
		return new ActiveXObject("Microsoft.XMLHTTP");		//IE
	}else{
		//no ajax 
		alert('El teu navegador és compatible amb AJAX');
		return false;
	}
}

function getObj()
{
	var xh = null;
	try 
	{
		//FF,Opera,KHTML
		xh = new XMLHttpRequest();		
	}catch(e)
		{
			//IE
			try
			{
				xh = new ActiveXObject("Msxml12.XMLHTTP");				
			}catch(e)
				{
					try
					{
						xh = new ActiveXObject("Microsoft.XMLHTTP");
					}catch(e)
						{
							alert("El teu navegador no té suport per a AJAX");
							return false;
						}
				}
		}
	return xh;
}

/**************************************************/

var rDiv = '';

function handleReq()
{
	if (repReq.readyState == 4)
	{
		document.getElementById(rDiv).innerHTML = repReq.responseText;
		document.getElementById(rDiv).style.visibility = 'visible';
	}
}

function cridaiposa(pagina,situacio)
{
	rDiv = situacio;
	repReq = getObj();
	if ( repReq.readyState == 4 || repReq.readyState == 0 )
	{
		repReq.onreadystatechange = handleReq;
		repReq.open('GET',pagina,true);
		repReq.send(null);
	}
}

var tipus = '';
var contracte = '';
var portada = '';
var pagina_anterior = '';
var zona = '';

function crea_llista(url,situacio,pagina)
{
    var args = new Array();
    var i = 0;

    /*if ( pagina != ''  )
    {
        args[i] = "pagina=" + pagina;
        //pagina_anterior = pagina;
        i++;
    }else{*/
    
        /*if ( pagina_anterior != '' )
        {
            args[i] = "pagina=" + pagina_anterior;
            i++;
        }*/
    /*}    */
    
    if ( tipus != ''  )
    {
        args[i] = "t=" + tipus;
        i++;
    }    
    
    if ( contracte != ''  )
    {
        args[i] = "c=" + contracte;
        i++;
    }else{
        args[i] = "c=1";
        i++;
    }   
    
    if ( portada != ''  )
    {
        args[i] = "p=" + portada;
        i++;
    } 
    
    if ( zona != ''  )
    {
        args[i] = "z=" + zona;
        i++;
    } 

    //aixó per resetejar les vars de sessió
    args[i] = 'r=on';
    
    var cadena = url;
    for ( i = 0 ; i < args.length ; i++)
    {
        if ( i == 0 )
        {
             cadena = cadena + "?";
        }else{
            cadena = cadena + "&";
        }
        cadena = cadena + args[i];
    }
    //alert(cadena);
    cridaiposa(cadena,situacio)
}

/**************************************************/
/****************per manipular les vars globals **/

function setTipus(arg)
{
    tipus = arg;
}

function setPortada(arg)
{
    portada = arg;
}

function setContracte(arg)
{
    contracte = arg;
}

function setZona(arg)
{
	//alert("'" + arg + "'");
    zona = arg;
}


function setPaginaAnterior(arg)
{
    pagina_anterior = arg;
}


/**************************************************/
/**************************************************/

/**************************************************/
/**************************************************/
/******************** Altres Funcions *************/
/**************************************************/
/**************************************************/



function navegador()
{
            if ( navigator.appVersion.search(/MSIE 7.0/) != -1)
	        {
	            //alert("Uses IE7");
	            return "IE7";
	        }else if ( navigator.appVersion.search(/MSIE 6.0/) != -1 )
	        {
	            //alert("Uses IE6");
	            return "IE6";	            
	        }else{
	            return "NOIE";
	        }
	        document.autentificar.usuari.focus();
}



/*********************************************************/
//funcions per marcar i desmarcar el menú de tipus


function desmarca_ie(obj)
{
    obj.style.borderLeft = "none 1px #C0C0C0";
    obj.style.borderRight = "none 1px #C0C0C0";
    obj.style.borderTop = "none 1px #C0C0C0";
    if ( obj.name == "menu2_td")
    {
        obj.style.backgroundImage = "url('images/fonsmenu2.gif')";
    }
    obj.childNodes[0].style.color = "#888888";   
}

//marca els elements del menú pisos,locals ...
function marca_ie(obj)
{
    
    //ens carreguem el borde dels altres
    opare = obj.parentNode;
    
    var fills = opare.childNodes
    var n = fills.length
    
    for ( var i = 0 ; i < n ; i++ )    
    {
        if ( fills[i].nodeName == "TD" )
        {
            desmarca_ie(fills[i]);
        }
    }
        obj.childNodes[0].style.color = '#000000';
        obj.style.borderLeft = "solid 1px #C0C0C0";
        obj.style.borderRight = "solid 1px #C0C0C0";
        obj.style.borderTop = "solid 1px #C0C0C0";
        obj.style.backgroundColor = "#EEEEEE";
        obj.style.backgroundImage = "none";
    
}

function marca_ff(obj)
{
    var x = document.getElementsByName("menu2_td");
    for ( var i = 0 ; i < x.length ; i++)
    {
        tobj = x[i];
        tobj.style.borderStyle = "none"
        tobj.style.backgroundImage = "url('images/fonsmenu2.gif')";
        for ( var j = 0 ; j < tobj.childNodes.length ; j++ ) 
        {
            if ( tobj.childNodes[j].name == "menu2_a" )
            {
                tobj.childNodes[j].style.color = '#888888';        
            }
        }
    }  
        
        obj.style.borderStyle = "solid";
        obj.style.backgroundColor = "#EEEEEE";
        obj.style.backgroundImage = "none";         
        
        document.getElementById(obj.id).style.border = "1px solid #000000";
        
        for ( var i = 0 ; i < obj.childNodes.length ; i++ ) 
        {
            if ( obj.childNodes[i].name == "menu2_a" )
            {
                obj.childNodes[i].style.color = '#000000';        
            }
        }
}

function marca(obj)
{
     if ( navegador() == "NOIE" )   
     {
        marca_ff(obj);
     }else{
        marca_ie(obj);
     }  
}

/*********************************************************/
//funcions per marcar i desmarcar el menú de contractes


function marca_2(obj)
{
    var aobj = document.getElementsByName('mc');
    
    //alert(obj.id + ' - ' + aobj.length);
        
    for ( var i = 0; i < aobj.length ; i++ )
    {
        if ( aobj[i].id == obj.id )
        {
            aobj[i].style.fontWeight = 'bold';
            aobj[i].style.color = '#000000';
            //alert(aobj[i].style.fontWeight + ' - ' + aobj[i].style.color);
            
        }else{
            aobj[i].style.fontWeight = 'normal';
            aobj[i].style.color = '#747474';
            //alert(aobj[i].style.fontWeight + ' - ' + aobj[i].style.color);
        }
    }
}


/****************************************************************************************/

/*          MAPA                                                                        */

/****************************************************************************************/

function mapa(carrer,numero)
{
	window.open ("http://www.bcn.cat/cgi-guia/guiamap4/cgi-guia/?actives=&scl=1&cnt=27601.01%2C83987.71&plt=&plant=capes4c&txt1=&txt2=&idioma=0&zoom=1&calle=" + carrer + "&numero=" + numero,"mapa","scrollbars=YES,height=600,width=800,resizable=NO,status=NO,location=NO,directories=NO,menubar=NO")
}

/****************************************************************************************/

/*          Canvia la imatge donada la Id                                               */

/****************************************************************************************/

function canvia_img(src,alt,id)
{
    document.getElementById(id).src = src;
    document.getElementById(id).alt = alt;
}

/****************************************************************************************/

/*          Obre un popup amb la imatge de la qual passem l'objecte                       */

/****************************************************************************************/

function pop_up(img)
{
    var ruta = img.src;
    
    ruta = ruta.replace('mitjanes','grans');
    //alert(ruta);
    window.open('funcions/popup.asp?r=' + ruta,'popup_img','resizable=no');
}

