function esImatge(strImatge)
{
// retorna true si strImatge conte una extensió vàlida d'imatge
return ((strImatge.indexOf('.jpg') > -1) || (strImatge.indexOf('.jpeg') > -1) || (strImatge.indexOf('.gif') > -1) || (strImatge.indexOf('.bmp') > -1) || (strImatge.indexOf('.png') > -1)); 
}    

function esArxiu(strArxiu)
{
// retorna true si té una extensió vàlida d'arxiu.
return ((strArxiu.indexOf('.doc') > -1) || (strArxiu.indexOf('.pdf') > -1) || (strArxiu.indexOf('.txt') > -1) || (strArxiu.indexOf('.css') > -1) ||( esImatge(strArxiu))); 
}    

function InicialitzarServidor(strLayer, strImatge)
{
        if (moz)
        {
            if (MM_findObj(strImatge).src != "") 
            {
                 if (MM_findObj(strImatge).src != null)
                 {
                    RedimensionarImatgeServidor(strImatge);
                 }
                 else {OcultarLayersImatge(strLayer);}
            }
            else
            {
                 OcultarLayersImatge(strLayer);
            }
        }
        else
        {        
            if (MM_findObj(strImatge).src != "")
            {
               if (MM_findObj(strImatge).src != "http://localhost/recursos/imatges/edicio/caixaarxiu/arxiu.gif")
       	       {
                    RedimensionarImatgeServidor(strImatge);
               } 
            }
            else
            {
               OcultarLayersImatge(strLayer);
            }
        } 
}

function InicialitzarPrevia(strLayer, strImatge)
{
        if (moz)
        {
            if (MM_findObj(strImatge).src != "")
            {
                if (MM_findObj(strImatge).src != null)
                {
                    RedimensionarImatgePrevia(strImatge);
                }
                else {OcultarLayersImatge(strLayer);} 
            }
            else
            {
               OcultarLayersImatge(strLayer);
            }
        }    
        else {
            if (MM_findObj(strImatge).src != "")
            {
               RedimensionarImatgePrevia(strImatge);
            }
            else
            {
               OcultarLayersImatge(strLayer);
            }
       }     
}

function MostrarLayersImatge(strLayer) 
{
    MM_showHideLayers(strLayer,'','show');
}

function OcultarLayersImatge(strLayer)
{
   	MM_showHideLayers(strLayer,'','hidden');
}

function MM_findObj(n, d) 
{ 
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() 
{ 
    var i,p,v,obj,args=MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3) 
	   if ((obj=MM_findObj(args[i]))!=null) 
	   {
	    v=args[i+2];
		if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v='hide')?'none':v; }
		obj.display=v; 
	   }
}

function popUp(page, name)
{
    var newWin;
    var ample,alt,sobre,esquerra;
    objImga = new Image();
    objImga.src = MM_findObj(name).src;
    ample = objImga.width + 20;
	alt = objImga.height + 30;
	
	if ((ample >= window.screen.width) || (alt >= window.screen.height))
    {
        ample = 800;
        alt = 600;
        detalls = "width=" + ample + "," + "height=" + alt + "," + "top=" + sobre + "," + "left=" + esquerra + "," + "directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no";
    }
    else
    {
        detalls = "width=" + ample + "," + "height=" + alt + "," + "top=" + sobre + "," + "left=" + esquerra + "," + "directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no";
    }
    
	sobre = (window.screen.height - alt)/2;
    esquerra = (window.screen.width - ample)/2;
   
  	
    newWin=window.open(page,"", detalls);
    newWin.focus();
    return false;
}

function popUp2(page, imatge)
{
    var newWin;
    var ample,alt,sobre,esquerra;
    objImga = new Image();
    objImga.src = imatge;
    ample = objImga.width + 20;
	alt = objImga.height + 30;
	
	if ((ample >= window.screen.width) || (alt >= window.screen.height))
    {
        ample = 800;
        alt = 600;
        detalls = "width=" + ample + "," + "height=" + alt + "," + "top=" + sobre + "," + "left=" + esquerra + "," + "directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no";
    }
    else
    {
        detalls = "width=" + ample + "," + "height=" + alt + "," + "top=" + sobre + "," + "left=" + esquerra + "," + "directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no";
    }
    
	sobre = (window.screen.height - alt)/2;
    esquerra = (window.screen.width - ample)/2;
   
  	
    newWin=window.open(page,"", detalls);
    newWin.focus();
    return false;
}

function RefrescarImatgeServidor(strLayer, strImatge, strArxiu) 
{
		if (strArxiu != "")
		{
       	    MM_findObj(strImatge).src = strArxiu;
       	    RedimensionarImatgeServidor(strImatge);
       	    MostrarLayersImatge(strLayer);
		}
}


function RefrescarImatgePrevia(strLayer, strImatge, strArxiu) 
{
        if (strArxiu != "")
		{
		        if (esImatge(strArxiu))
				{
				    MM_findObj(strImatge).src = strArxiu;
				    RedimensionarImatgePrevia(strImatge);
			        MostrarLayersImatge(strLayer);
			        if (op){OcultarLayersImatge(strLayer);}
				}
			    else
			    {
			        OcultarLayersImatge(strLayer);
			    }
		}
	    else 
	    {
	        InicialitzarPrevia(strLayer, strImatge);
	    }    
}

function RedimensionarImatgeServidor(strImatge) 
{
		var iAmplada, iAlcada;
		var iRelacioAmplada, iRelacioAlcada;
		var timer;

		objImg = new Image();
		objImg.src = MM_findObj(strImatge).src;
		iAmplada = objImg.width;
		iAlcada = objImg.height;
					
		if (iAmplada == 0 || iAlcada == 0) {
			timer = setTimeout('RedimensionarImatgeServidor (\'' + strImatge + '\')', 1000);
			//clearTimeout(timer);
			return;
		}
		iRelacioAmplada = 80 / iAmplada;
		iRelacioAlcada = 80 / iAlcada;
		if (iRelacioAmplada > 1 && iRelacioAlcada > 1) {
			MM_findObj(strImatge).width = iAmplada;
			MM_findObj(strImatge).height = iAlcada;
		}
		else {
		 	if (iRelacioAmplada < iRelacioAlcada) {
				MM_findObj(strImatge).width = iAmplada * iRelacioAmplada;
				MM_findObj(strImatge).height = iAlcada * iRelacioAmplada;
			}
			else {
				MM_findObj(strImatge).width = iAmplada * iRelacioAlcada;
				MM_findObj(strImatge).height = iAlcada * iRelacioAlcada;
			}	
		}

}

function RedimensionarImatgePrevia(strImatge) 
{
		var iAmplada = 0;
		var iAlcada = 0;
		var iRelacioAmplada, iRelacioAlcada;
        var timer;
        
		if (moz)
		{
		    objImgMoz = new Image();
		    MM_findObj(strImatge).src = "file:///" + replace(MM_findObj(strImatge).src, "\\", "/");
		    objImgMoz.src = MM_findObj(strImatge).src;
		    
		    iAmplada = 150;
		    iAlcada = 150;
		    	   		 	
		 	if (iAmplada == 0 || iAlcada == 0) 
	    	{
                timer = setInterval("RedimensionarImatgePrevia()", 1000, "strImatge");
	    	    return;
		    } 	
		   	clearInterval(timer);	
		    iRelacioAmplada = 150 / iAmplada;
	    	iRelacioAlcada = 150 / iAlcada;
	    	if (iRelacioAmplada > 1 && iRelacioAlcada > 1) 
	    	{
	    		MM_findObj(strImatge).width = iAmplada;
	    		MM_findObj(strImatge).height = iAlcada;
	    	}
		    else
		    {
		    	if (iRelacioAmplada < iRelacioAlcada) {
		    		MM_findObj(strImatge).width = iAmplada * iRelacioAmplada;
		    		MM_findObj(strImatge).height = iAlcada * iRelacioAmplada;
		    	}
		        else {
			    	MM_findObj(strImatge).width = iAmplada * iRelacioAlcada;
			    	MM_findObj(strImatge).height = iAlcada * iRelacioAlcada;
			    }	
		    }
        }
	    else
	    {
	        objImg = new Image();
	        objImg.src = MM_findObj(strImatge).src;
			iAmplada = objImg.width;
		    iAlcada = objImg.height;
		   		    
		    if (iAmplada == 0 || iAlcada == 0) 
	    	{
		    	timer = setTimeout('RedimensionarImatgePrevia(\'' + strImatge + '\')', 1000);
		    	return;
		    } 	
		
	    	iRelacioAmplada = 150 / iAmplada;
	    	iRelacioAlcada = 150 / iAlcada;
	    	if (iRelacioAmplada > 1 && iRelacioAlcada > 1) {
	    		MM_findObj(strImatge).width = iAmplada;
	    		MM_findObj(strImatge).height = iAlcada;
	    	}
		    else {
		    	if (iRelacioAmplada < iRelacioAlcada) {
		    		MM_findObj(strImatge).width = iAmplada * iRelacioAmplada;
		    		MM_findObj(strImatge).height = iAlcada * iRelacioAmplada;
		    	}
		        else {
			    	MM_findObj(strImatge).width = iAmplada * iRelacioAlcada;
			    	MM_findObj(strImatge).height = iAlcada * iRelacioAlcada;
			    }	
		    }
		    clearTimeout(timer);
		}
}


