var on = '#84838b';
var off = '#5d5c61';

function onC(row){
row.style.backgroundColor = on;
}

function offC(row){
row.style.backgroundColor = off;
}


function  mirarcaractersraros(b)
{
       // 26 &
       // 25 %
       // 23 #
       // 2b +
       // 2d -
       var c = '';
       var i;
       //c = b;

       //window.alert('entro' + b[3]);
       for (i=0;i<b.length;i++)
       {
               switch(b.substring(i,i+1))
               {
                       case '-':
                               c = c + "%2d";
                               break;
                       case '+':
                               c = c + "%2b";
                               break;
                       case '#':
                               c = c + "%23";
                               break;
                       case '%':
                               c = c + "%25";
                               break;
                       case '&':
                               c = c + "%26";
                               break;
                       default:
                               //window.alert('default:' + b.substring(i,i+1));
                               c = c + b.substring(i,i+1);
                               break;
               }
       }
       //window.alert(c);
       return c;
}


function carregarURL(tipus,param1,param2)
{
//		window.alert('carallot' + tipus + param1);
		var continuar = 1;
		var b = "";
		var c = "";
		var d = "";
		var e = "";
		var f = "";



		var cadena = "";
		if (tipus=="poblacions")
		{
			b = document.getElementById('selectp').value;
			cadena = "poblacionsProvincia.php?val=" + b;
		}
		else if (tipus=="grabEmail")
		{
			b=param1;
			cadena = "grabEmail.php?sEmail=" + b;
		}

                else if ((tipus=="addtema") && (param2==''))
                {
                        b = param1;
                        c = param2;
                        //window.alert(b);
                        if (b=="")
                        {
                                b=document.formQuefas.sNom.value;
                                if (b.length <  3)
                                {
                                      window.alert("You must type in something");
                                        continuar = 0;
                                }
                        }
                        // Mirar caracters raros
            //            b = mirarcaractersraros(b);
                        cadena = c + "regThought2.php?val=" + b;
 //                       window.alert(cadena);
                }

                else if (tipus=="addtema")
                {
                        b = param1;
                        c = param2;
                        //window.alert(b);
                        if (b=="")
                        {
                                b=document.formQuefas.sNom.value;
                                if (b.length <  3)
                                {
                                      window.alert("You must type in something");
                                        continuar = 0;
                                }
                        }
                        // Mirar caracters raros
            //            b = mirarcaractersraros(b);
                        cadena = c + "regThought2.php?val=" + b + "&carregar=1";
//                        window.alert(cadena);
                }

                else if ((tipus=="addVideo"))
                {
                        b=document.formComent.sTitle.value;
                        
                        c=document.formComent.sURL.value;
                        //window.alert(b);
/*                        if (b=="")
                        {
                                b=document.formQuefas.sNom.value;
                                if (b.length <  3)
                                {
                                      window.alert("You must type in something");
                                        continuar = 0;
                                }
                        } */
                        // Mirar caracters raros
            //            b = mirarcaractersraros(b);
                        cadena = "regVideo2.php?val=" + b + "&URL=" + c;
//                        window.alert(cadena);
                }

                else if (tipus=="addComment2")
                {
                        b=document.formComent.iTipusComentari.value;
                        c=document.formComent.idComentat.value;
                        e=document.formComent.sTexte.value;
//                      replace(e,'&','and');
                        e=e.replace('&','and');
                        f=document.formComent.id.value;
                        if (e.length <  2)
                        {
                                window.alert("You must type in something");
                                continuar = 0;
                        }
                        cadena = "../../../grabComment2.php?iTipusComentari="
+ b  + "&idComentat=" + c + "&sTexte=" + e + "&id=" + f ;
                        //window.alert(cadena);
                }

                else if (tipus=="removeItem")
                {
                        b = param1;
                        c = param2;
                        cadena = "/remove.php?id=" + b + "&iTipus=" + c;
//                        window.alert(cadena);
                }

                else if (tipus=="addPrivate")
                {
                        b=document.fresponder.texte.value;
                        c=document.fresponder.Usuari2.value;
                        if (b == "")
                        {
                                window.alert("Message required");
                                continuar = 0;
                        }
                        cadena = "/regPrivate.php?id1=" + c + "&sTexte=" + b;
                        //window.alert(cadena);
                }
                else if (tipus=="borrarMensaje")
                {
                        b = param1;
                        c = param2;
//                      window.alert(b + '-' + c);
                        cadena = "/removeMessage.php?val=" + b + "&mensaje=" + c;
                }

		if (continuar)
			makeRequest(cadena,tipus);
}




    function makeRequest(url,tipus) {

        http_request = false;
//window.alert('entro al make');

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // Ver nota sobre esta linea al final
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Falla :( No es posible crear una instancia XMLHTTP');
            return false;
        }
	if (tipus=="poblacions")
	{
	  //      http_request.onreadystatechange = mostrarPoblacionsURL;
	}
	else if (tipus=="grabEmail")
	{
	        http_request.onreadystatechange = mostrargrabEmail;
	}
        else if (tipus=="addtema")
     	{
	     http_request.onreadystatechange = mostrarResultatAddTema;
	}
	else if (tipus=="addComment2")
	{
	        http_request.onreadystatechange = mostrarResultatAddComment;
	}
        else if (tipus=="addVideo")
     	{
             // Afegir VIdeo
	     http_request.onreadystatechange = mostrarResultatAddVideo;
	}
        else if (tipus=="removeItem")
     	{
             // Afegir VIdeo
	     http_request.onreadystatechange = mostrarResultatRemoveItem;
	}
        else if (tipus=="addPrivate")
        {
        http_request.onreadystatechange = mostrarResultatAddPrivate;
        }
        else if (tipus=="borrarMensaje")
        {
                //alert(url);
        http_request.onreadystatechange = mostrarResultatBorrarMensaje;
        }

        http_request.open('GET', url, true);
        http_request.send(null);

    }

    //Crida al fitxer grabEmail.php i enregistra el mail
    function mostrargrabEmail() {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
			document.formEmail.email.value='';
                	alert('Succesful');
            } else {
                	alert('Error.');
            }
        }

    }


    function mostrarResultatAddTema() {
        // Borrar el tema i tornar a lindex
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                                var cadena = http_request.responseText;
                                //window.alert("-" + cadena.substr(0,8) + "-");

                                if (cadena.substr(0,13) == "CorrecteIndex")
                                {
                                  javascript:location.href="/";
                                }
                                else if (cadena.substr(0,8) == "Correcte")
                                {
//                                       window.alert('OK');
                                        //javascript:location.href="/";
					url = '/autoNotification.php';

				        $.get(url, {},
			                    function(html) {
			                       if (html == 'NOIDENTIFICAT') {
		                                   return;
			                       }
			                       else {
                         			  temporal = unescape(html);
//window.alert(temporal);
                                                  document.getElementById('contingut').innerHTML = temporal;
                                                  document.getElementById('contador').innerHTML = '150';
                                                  document.formQuefas.sNom.value = '';
                                                 document.formQuefas.sNom.focus();
                                           	}
                                	}
                                	);
                                }
                                else
                                {
                                        javascript:location.href="/login.php";
                                }
            } else {
                                        javascript:location.href="/loginUser.php";
            }
        }
    }

    function mostrarResultatAddVideo() {
        // Borrar el tema i tornar a lindex
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                                var cadena = http_request.responseText;
			    	var url2 = cadena.substr(9,cadena.length-9);

                                javascript:location.href=url2;

            } else {
                                        javascript:location.href="/loginUser.php";
            }
        }
    }


    function mostrarResultatAddComment() {
    	if (http_request.readyState == 4) {
        	if (http_request.status == 200) {
             		var cadena = http_request.responseText;
                        if (cadena.substr(0,8) == "Correcte")
                        {
                        	url = '/autotemaultimcomentari.php?val=' + param + '&lastCom=' + lastCom + '&ruta=' + '3' + '&carrega=1&tipus=' + tipus;
//                                window.alert(url);
                                 $.get(url, {},
                                         function(html) {
                                            if (html == 'NOIDENTIFICAT') {
                                                        return;
                                            }

                                           else {
//                                     window.alert(html);

                                                temporal = unescape(html);
        //                              window.alert(temporal.length);
        //                              window.alert(temporal);
                                                if (temporal.length < 70)
                                                {
                                                        window.location.reload();
                                                }
                                                else
                                                {
                                                document.getElementById('contingut').innerHTML = temporal;
                                                document.formComent.sTexte.value = '';
        //                                      document.formComent.sTexte.text= '';
                                                document.formComment.sTexte.focus();
                                                }
                                           }
                                }
                                );
//                              window.location.reload();
                                }
                                else
                                {
                                        javascript:location.href="/login.php";
                                }
            } else {
                                        javascript:location.href="/login.php";
            }
                }
        }


    function mostrarResultatRemoveItem() {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                                        window.location.reload();
            } else {
                alert('Hubo problemas con la petición.');
            }
        }
    }


function items(ruta,type,direccio)
{
        var url = ruta + direccio;
        $.get(url, {},
                 function(html) {
                        if (html == 'NOIDENTIFICAT') {
                                return;
                        } else {
                                $('#'+type).html(html);
                                return;
                        }
                }
        );
        //return
}

function follow (type, field, type2,ruta)
{
        var url = ruta + 'regFollow.php?user=' +  field;
        $.get(url, {},
                 function(html) {
                        if (html == 'NOIDENTIFICAT') {
                                return;
                        } else {
                        window.location.reload();
//                                $('#'+type).html('<span style="color:black">' + '<div class="notice"><p>' +  'Contacto añadido a favoritos' + '</p></div>'  + '</span>');
  //                              $('#'+type2).html('<a style="cursor:pointer" onClick="borrarFavorit(\'missatge\',\'' + field + '\',\'checkit\',\'' + ruta + '\');\"><font size=2>  Eliminar como favorito</font>');
                                return;
                        }
                }
        );
       // return false;
}

function unfollow (type, field, type2,ruta)
{
        var url = ruta + 'unFollow.php?user=' +  field;

        $.get(url, {},
                 function(html) {
                        if (html == 'NOIDENTIFICAT') {
                                return;
                        } else {
                        window.location.reload();
//                                $('#'+type).html('<span style="color:black">' + '<div class="notice"><p>' +  'Contacto añadido a favoritos' + '</p></div>'  + '</span>');
  //                              $('#'+type2).html('<a style="cursor:pointer" onClick="borrarFavorit(\'missatge\',\'' + field + '\',\'checkit\',\'' + ruta + '\');\"><font size=2>  Eliminar como favorito</font>');
                                return;
                        }
                }
        );
       // return false;
}


function mostrarResultatAddPrivate() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                     var cadena = http_request.responseText;
                     if (cadena.substr(0,8) == "Correcte")
                     {
                            url = '/autoPrivatsV2.php?id1=' + jo + '&id2=' + laltre;
                            $.get(url, {},
                             function(html) {
                                      if (html == 'NOIDENTIFICAT') {
                                                  return;
                                       }
                                      else {

                                                temporal = unescape(html);
        //                              window.alert(temporal.length);
        //                              window.alert(temporal);
                                        if (temporal.length < 500)
                                        {
                                             window.location.reload()
                                        }
                                        else
                                        {
                                              document.getElementById('mastemes').innerHTML = temporal;
                                              document.fresponder.texte.value = '';
                                              document.formComment.sTexte.focus();
                                        }
                                     }
                                }
                                );
//                              window.location.reload();
                  }
                else
                {
//window.alert(cadena);
                }
            }
     }
}


    function mostrarResultatBorrarMensaje()
        {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                                        window.location.reload();
            } else {
                alert('Hubo problemas con la petición.');
            }
        }
    }

