/*
/*****************************************************************************************
 ______       __     ______     __  __    
/\  __ \     /\ \   /\  __ \   /\_\_\_\   
\ \  __ \   _\_\ \  \ \  __ \  \/_/\_\/_  
 \ \_\ \_\ /\_____\  \ \_\ \_\   /\_\/\_\ 
  \/_/\/_/ \/_____/   \/_/\/_/   \/_/\/_/ 

******************************************************************************************/

/*************************************************

		AJAX

        Dani HeadMaster 2007 from empunyando.com

***************************************************/

function oajax(divid)
{
    var repReq;     //objecte  per al request
    var rDiv = divid;
	
    
    //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;		
			
	}
	
	//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;   
            
            //per si ha saltat el loading
            //no_loading();                                    
        }
        
     }
	
	//Variables semi-públiques
	this.setDiv = function(id)
	{
	    rDiv = id;
	}
	this.getDiv = function()
	{
	    return rDiv;
	}
	
			
    //codi CONSTRUCTOR
    repReq = getObj();
    
    //mètodes públics
    //amb aquesta crido un script asp i poso la resposta en una div 'situacio'
		    this.cridaiposa = function (pagina)
		    {
		        //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);                    
                }
		    }
		    
		    this.cridaiposa_post = function(pagina,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);
                }
		    }
}

function 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;   
                    
                    //per si ha saltat el loading
                    no_loading();                                    
                }
                
             }

	
		    //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);
                }
		    }
		    
		    
		    		    
		    
		    function cridaiposa_post2(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);
                }
		    }
}		    
