﻿/*
********************************************************************************************
From : http://stuffledge.blogspot.com/2007/08/getelementsbyname-una-ms-la-lista.html

*/

function elementsByName(tag, name) 
{
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++)
     {          
        att = elem[i].getAttribute("name");
          if(att == name)
          {
               arr[iarr] = elem[i];
               iarr++;          
          }     
     }     
     return arr;
}

function elementsByClass(tag,name)
{
    var elem = document.getElementsByTagName(tag);
    var arr = new Array();
    var n = 0;
    for(i = 0,iarr = 0; i < elem.length; i++)
     {          
        if (elem[i].className.indexOf(name) > -1) 
        {
            arr[n++] = elem[i];
        }
     }     
     return arr;
}

/*
********************************************************************************************
From : 

*/

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

//exemples
//addEvent(window, 'load', foo);
//addEvent(window, 'load', bar);

/*****************************************/
/*  Capa contactar                    */
/*****************************************/

function posaDiv()
{
    if (!document.getElementById("tapador"))
    {
        var ndiv = document.createElement('div');
        ndiv.id = 'tapador';
        ndiv.style.height = '1200';
        ndiv.style.width = '100%';
                
        ndiv.style.position = 'absolute';
        ndiv.style.top = '0';
        ndiv.style.left = '0';
        ndiv.style.backgroundColor = '#CCCCCC';
            
            ndiv.style.opacity = '0.4';
            ndiv.style.MozOpacity = '0.4';
            ndiv.style.filter = "alpha(opacity = 40";
        document.body.appendChild(ndiv);   
    }
   
    if (!document.getElementById("contenidor_missatge"))
    {
        var pdiv = document.createElement('div');
        pdiv.id = 'contenidor_missatge';
        pdiv.style.height = '100%';
        pdiv.style.width = '100%';
        pdiv.style.top = '0';
        pdiv.style.left = '0';
        pdiv.style.position = 'absolute';
        document.body.appendChild(pdiv);
    } 
        
        cridaiposa('../../../contactar-eng.htm','contenidor_missatge');
    
    
}

function posaDiva()
{
    if (!document.getElementById("tapador"))
    {
        var ndiv = document.createElement('div');
        ndiv.id = 'tapador';
        ndiv.style.height = '1200';
        ndiv.style.width = '100%';
                
        ndiv.style.position = 'absolute';
        ndiv.style.top = '0';
        ndiv.style.left = '0';
        ndiv.style.backgroundColor = '#CCCCCC';
            
            ndiv.style.opacity = '0.8';
            ndiv.style.MozOpacity = '0.8';
            ndiv.style.filter = "alpha(opacity = 80";
        document.body.appendChild(ndiv);   
    }
   
    if (!document.getElementById("contenidor_missatge"))
    {
        var pdiv = document.createElement('div');
        pdiv.id = 'contenidor_missatge';
        pdiv.style.height = '100%';
        pdiv.style.width = '100%';
        pdiv.style.top = '0';
        pdiv.style.left = '0';
        pdiv.style.position = 'absolute';
        document.body.appendChild(pdiv);
    } 
        
        cridaiposa('../../../avislegal-eng.htm','contenidor_missatge');
    
    
}

function posaDivAdvertencia()
{
    if (!document.getElementById("tapador"))
    {
        var ndiv = document.createElement('div');
        ndiv.id = 'tapador';
        ndiv.style.height = '100%';
        ndiv.style.width = '100%';
        ndiv.style.position = 'absolute';
        ndiv.style.top = '0';
        ndiv.style.left = '0';
        ndiv.style.backgroundColor = '#ec6e00';
        
            ndiv.style.opacity = '0.6';
            ndiv.style.MozOpacity = '0.6';
            ndiv.style.filter = "alpha(opacity = 60";
        document.body.appendChild(ndiv);   
    }
   
    if (!document.getElementById("contenidor_missatge"))
    {
        var pdiv = document.createElement('div');
        pdiv.id = 'contenidor_missatge';
        pdiv.style.height = '100%';
        pdiv.style.width = '100%';
        pdiv.style.top = '0';
        pdiv.style.left = '0';
        pdiv.style.position = 'absolute';
        document.body.appendChild(pdiv);
    } 
        
        cridaiposa('../contactar.htm','contenidor_missatge');
    
    
}


function tancaDiv()
{
    var obj = document.getElementById('contenidor_missatge');
    document.body.removeChild(obj);
    var obj = document.getElementById('tapador');
    document.body.removeChild(obj);
}


/*****************************************/
/*  Formulari camps obligats                   */
/*****************************************/


 function Valida()
 {
	 var cont=0;
	 var mis="Required fields:\n";
	 if(document.getElementById("nom").value=="")
	 {
		  mis=mis+"-name and surname\n";
		  cont++;
	 }  
	 if(document.getElementById("correuelectronic").value=="")
	 {
		  mis=mis+"-email\n";
		  cont++;
	 }  
	 if (cont>0)
	 {
		alert(mis);
	 }
	 else
	 {
		document.getElementById("meuform").submit();
	 }
 }


/*************************************************

		AJAX


***************************************************/
		    //variable  clobal que conté l'objecte d'ajax
		    //var repReq = getAjaxObj();
            var repReq;
		    
		    //amb aixó montem la cadena de post amb el formulari
		    function getFormValues(fobj,valFunc)
		     {			
			var str = "";
			var valueArr = null;
			var val = "";
			var cmd = "";
			for(var i = 0;i < fobj.elements.length;i++)
			{
				switch(fobj.elements[i].type)
				{
				case "text":
					{
						//use single quotes for argument so that the value of			
						//fobj.elements[i].value is treated as a string not a literal			
						cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
						val = eval(cmd);
					}
			
					str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
					break;
			
				case "select-one":
					str += fobj.elements[i].name + "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
					break;
				}
			}
			str = str.substr(0,(str.length - 1));
			return str;
			}	
		//capturar l'objecte
		
		//amb aixó escollim el component 
			function getObj()
			{
				var xh = null;
				try
				{
				//firefox, 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 Ajax!");
										return false;	
									}
							}
					}
					
					return xh;		
					
			}
		
             function getAjaxObj()
             {
                if (window.XMLHttpRequest)
                {
                    return XMLHttpRequest; //NOT IE                    
                }else if (window.ActiveXObject)
                {
                    return new ActiveXObject("Microsoft.XMLHTTP");  //IE
                }else{
                    //navegadors que no suporten l'objecte
                    alert('El teu navegador no és compatible amb AJAX');
                    return false;
                }                
             }
             
             
      
/***********************************************************************************************/	
            var rDiv = '';

            //gestiona la resposta
             function handleReq()
             {
             	//alert('hem rebut');
                //comprobem que la connexió ha acabat
                if ( repReq.readyState == 4 )
                {
                    //posem el contingut a l'span
                    //alert(repReq.responseText);
                    document.getElementById(rDiv).innerHTML = repReq.responseText;
		            document.getElementById(rDiv).style.visibility = 'visible';	
                    //document.meuform.resposta.value = repReq.responseText;
                }
             }

	
		    //amb aquesta crido un script asp i poso la resposta en una div 'situacio'
		    function cridaiposa(pagina,situacio)
		    {
		        rDiv = situacio;
		        repReq = getObj();
		        //alert();
                if ( repReq.readyState == 4 || repReq.readyState == 0 )
                {
                    //configurem la funció que gestiona l'enllaç quan canvia
                    repReq.onreadystatechange = handleReq;                	
                    //configurem la connexió com un  GET
					//el "true" expecifica que es asíncrona ( opció per defecte)
                    repReq.open('GET',pagina,true);
                    //repReq.open('POST','temps.php',true);
                    //fem el request al servidor
                    repReq.send(null);                    
                }
		    }
		    
		    function cridaiposa_post(pagina,situacio,qstring)
		    {
		        rDiv = situacio;
		        repReq = getObj();
             	//alert();
                if ( repReq.readyState == 4 || repReq.readyState == 0 )
                {
                	//configurem la funció que gestiona l'enllaç quan canvia
                    repReq.onreadystatechange = handleReq;                	
                    //configurem la connexió com un  GET
					//el "true" expecifica que es asíncrona ( opció per defecte)
                    repReq.open('POST',pagina,true);
                    repReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    //alert(qstring);
		            repReq.send(qstring);
                }
		    }

