// Funcions generals de javascript

//Objecte AJAX
function objetoAjax(){
	var xmlhttp=false;
    try {
    	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    	try {
        	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
        	xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	xmlhttp = new XMLHttpRequest();
	}
    return xmlhttp;
}

//Comprovar que el email te l'estructura correcta
function check_mail(texto){

    var mailres = true;            
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
    
    var arroba = texto.indexOf("@",0);
    if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
    
    var punto = texto.lastIndexOf(".");
                
     for (var contador = 0 ; contador < texto.length ; contador++){
        if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
            mailres = false;
            break;
     }
    }

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
     mailres = true;
    else
     mailres = false;
                
    return mailres;
}

function afegir_contacte(idioma){
	var e;
	e=document.getElementById("email");
	if(check_mail(e.value)){
		ajax=objetoAjax();
		a = "afegir_contacte.php?email="+e.value;
		ajax.open("GET", a);
		ajax.onreadystatechange=function(){
			if (ajax.readyState==4){
				if(idioma=="cat"){
					alert("Gracies per apuntar-te a la nostra newsletter.");
					e.value="introdueix el teu e-mail";
				}else if(idioma=="esp"){
					alert("Gracias por apuntarte a nuestra newsletter.");
					e.value="introduce tu e-mail";
				}
			}
		}
		ajax.send(null);
		
	}else{
		alert("El email no es correcte.")	
	}
}

function newVideo(aux){
	var ant,id,id_ant,list_id,list_id_antactual,anterior;
	//alert (aux);
	if(aux!="1"){
		//alert("entrem");
		ant=parseInt(aux);
		ant=ant-1;
		id="play"+aux;
		id_ant="play"+ant;
		actual=document.getElementById(id);
		//alert(actual.className);
		anterior=document.getElementById(id_ant);
		actual.className="";
		anterior.className="trans";
		//alert(actual.className);
		list_id="list"+aux;
		list_id_ant="list"+ant;
		actual=document.getElementById(list_id);
		//alert(actual.className);
		anterior=document.getElementById(list_id_ant);
		actual.className="";
		anterior.className="noVisible";
	}else{
		var num=document.getElementById("num_plays").value;
		id="play1";
		id_ant="play"+num;
		actual=document.getElementById(id);
		anterior=document.getElementById(id_ant);
		actual.className="";
		anterior.className="trans";
		list_id="list1";
		list_id_ant="list"+num;
		actual=document.getElementById(list_id);
		anterior=document.getElementById(list_id_ant);
		actual.className="";
		anterior.className="noVisible";
	}
	
}
