///////////////////////////////////////////////////////////////////
// Mensaje de esperando
///////////////////////////////////////////////////////////////////
var div = null;
function ponerEspera(aux_div) {
	document.getElementById(aux_div).innerHTML = '<center><br/><img src="/img/bigrotation2.gif" /></center>';
	if (document.getElementById(aux_div).style.display = 'none') {
		document.getElementById(aux_div).style.display = ''
	}
}

function exit_mostrarTPL() {
	div_old = div;

	if(http1.readyState == 4) {
		var html = http1.responseText;
		//Compruebo que no haya que ejecutar nada.
		ini = html.indexOf('##');
		fin = html.lastIndexOf('##');
		if (fin != -1) {
			codigo = html.substring(ini+2, fin); 
			eval(codigo);
			html = html.substring(fin+2);
			ponerEspera(div);
			//return;
		}

		document.getElementById(div_old).innerHTML = html;
		document.getElementById(div_old).style.display = '';
	}
}

function exit_mostrarTPL2() {
	if(http2.readyState == 4) {
		var html = http2.responseText;

		document.getElementById(div).innerHTML = html;
		document.getElementById(div).style.display = '';
	}
}

function puedoMostrarDIV(aux_div) {
	/*if (document.getElementById(aux_div).style.display != 'none')
		return false;
	else*/
		return true;
}

function cerrarBloque() {
	hideOverlay();
}

///////////////////////////////////////////////////////////////////
// Pide el menú de Administración si corresponde
///////////////////////////////////////////////////////////////////
function get_panel_admin(id_video,id_artista) {
	div = 'panel_admin';
	daleAjaxPOST(2,'/video_opciones.php','type=GET_PANEL_ADMIN&id_video='+id_video+'&id_artista='+id_artista,'exit_mostrarTPL2');
}

function show_panel_admin(id_video, panel) {
	div = 'overlayFrame';
	showOverlay();

	daleAjaxPOST(2,'/video_opciones.php','type=SHOW_PANEL_ADMIN&id_video='+id_video+'&panel='+panel,'exit_mostrarTPL2');
}

function exitAdminVideos() {
	if(http1.readyState == 4) {
		var html = http1.responseText;
		var ret = html.split("||");

		txt_div = ret[1]+ret[3];
		if (document.getElementById(txt_div) != null) {
			document.getElementById(txt_div).innerHTML = ret[2];
		}
	}
}

function enviarModeracion(id_video) {
	desactivar = "venta";
	if (document.getElementById('mod_desactivar').checked)
		desactivar = "desact";

	var i
	for (i=0;i<document.form_mod.mod_publico.length;i++){
		if (document.form_mod.mod_publico[i].checked)
			break;
	}
	adultos = document.form_mod.mod_publico[i].value 
		
	cerrarBloque();
	div = 'overlayFrame';
	showOverlay();
		
	daleAjaxPOST(1,'/video_opciones.php','type=ENVIAR_MODERACION&id_video='+id_video+"&desactivar="+desactivar+"&adultos="+adultos,'exit_mostrarTPL');
}

function enviarPublicidad(id_video) {

	var i
	for (i=0;i<document.form_publi.publi.length;i++){
		if (document.form_publi.publi[i].checked)
			break;
	}
	publi = document.form_publi.publi[i].value 
		
	cerrarBloque();
	div = 'overlayFrame';
	showOverlay();
		
	daleAjaxPOST(1,'/video_opciones.php','type=ENVIAR_PUBLICIDAD&id_video='+id_video+"&publi="+publi,'exit_mostrarTPL');
}

function enviarGestion(id_video) {
	destacar = 0;
	destacar = document.getElementById('ges_detacar').checked;
	destacar_player = document.getElementById('ges_detacar_player').checked;
	boletin = document.getElementById('ges_boletin').checked;

	cerrarBloque();
	div = 'overlayFrame';
	showOverlay();
		
	daleAjaxPOST(1,'/video_opciones.php','type=ENVIAR_GESTION&id_video='+id_video+"&destacar="+destacar+"&destacar_player="+destacar_player+"&boletin="+boletin,'exit_mostrarTPL');
}

/////////////////////////////////////////////////////////////////
// Cambia el icono de Subtitular vídeo.
///////////////////////////////////////////////////////////////////
function cambiar_subtitulos(id_video) {
	//div = 'menu_subtitulos';
	daleAjaxPOST(1,'/video_opciones.php','type=GET_MENU_SUB&id_video='+id_video,'exit_cambiar_subtitulos');
}
function exit_cambiar_subtitulos() {
	if(http1.readyState == 4) {
		var res = http1.responseText;
		if (res == "OK") {
			if (document.getElementById('menu_subtitulos') != null) {
				document.getElementById('menu_subtitulos').style.display = '';
				if (document.getElementById('menu_op') != null) {
					document.getElementById('menu_op').style.display = 'none';
				}
			}
		} else {
			//alert("KO");
		}
	}
}

///////////////////////////////////////////////////////////////////
// Comentarios
///////////////////////////////////////////////////////////////////
function abrirAnadirComentario(id_video, config) {
	div = 'overlayFrame';
	showOverlay();

	if (config == 't') {
		daleAjaxPOST(2,'/video_opciones.php','type=MOSTRAR_ANADIR_COMENTARIO&id_video='+id_video,'exit_mostrarTPL2');
	} else {	
		if (logado) {
			daleAjaxPOST(2,'/video_opciones.php','type=MOSTRAR_ANADIR_COMENTARIO&id_video='+id_video,'exit_mostrarTPL2');
		} else {
			login_retorno = 'daleAjaxPOST(2,\'/video_opciones.php\',\'type=MOSTRAR_ANADIR_COMENTARIO&id_video='+id_video+'\',\'exit_mostrarTPL2\');';
			ajaxpage('/load.php?tpl=includes/login', 'overlayFrame');
		}
	}
}

function comprobarComentario(nombre, post, id_video) {
	if (nombre.value == "" || post == "")
		msg_error("no_tema_comentario");
	else {
		ponerEspera('overlayFrame');
		daleAjaxPOST(1,'/video_opciones.php','type=ANADIR_COMENTARIO&id_video='+id_video+'&post='+encodeString(post)+'&nombre='+encodeString(nombre.value),'exit_mostrarTPL');
		//alert((post));
	}
}

function recargarComentarios(aux_div, url) {
	div = aux_div;
	ponerEspera(div);

	ajaxpage(url+'&comentarios', div);
}

function eliminarComentario(aux_div, id_comentario, url) {
	div = aux_div;
	ponerEspera(div);

	daleAjaxPOST(1,'/video_opciones.php','type=ELIMINAR_COMENTARIO&id_comentario='+id_comentario+'&url='+url,'exit_eliminarComentario');
}

function exit_eliminarComentario() {
	if(http1.readyState == 4) {
		var html = http1.responseText;
		recargarComentarios(div, html)
	}
}

function encodeString(var_txt) {
	var_txt = escape(var_txt);
	var_txt = var_txt.replace("+","%2B");
	var_txt = var_txt.replace("/","%2F");
	return var_txt;
}
///////////////////////////////////////////////////////////////////
// Videos Relacionados.
///////////////////////////////////////////////////////////////////
function getRelacionados(aux_div, url) {
	div = aux_div;
	ponerEspera(div);

	ajaxpage(url+'&relacionados', div);
}

function getMasUser(aux_div, url, id_artista) {
	div = aux_div;
	ponerEspera(div);

	ajaxpage(url+'&relacionados&usuario='+id_artista, div);
}
///////////////////////////////////////////////////////////////////
// Denunciar vídeo.
///////////////////////////////////////////////////////////////////
function anadirVideoGrupos(id, aux_div) {
	div = aux_div;
	if (!puedoMostrarDIV(aux_div))
		return;

	showOverlay();

	if (logado) {
		ponerEspera(aux_div);
		daleAjaxPOST(1,'video_opciones.php','type=LISTADO_GRUPOS&id='+id,'exit_mostrarTPL');
	} else {
		login_retorno = 'daleAjaxPOST(2,\'video_opciones.php\',\'type=LISTADO_GRUPOS&id='+id+'\',\'exit_mostrarTPL2\');';
		ajaxpage('/load.php?tpl=includes/login', aux_div);
	}
}

function anadirAGrupo(max_grupos, id_video, codigo) {
	arr_grupos = "";
	for (i = 1; i < (max_grupos+1); i++) {
		check = eval("document.anadir_a_grupo.grupo_"+i);
		if (check.checked)
			arr_grupos += + check.value + ",";
	}
	if (arr_grupos == "")
		msg_error("no_grupo");
	else {
		ponerEspera(div);
		daleAjaxPOST(1,'video_opciones.php','type=ANADIR_VIDEO_GRUPOS&grupos='+arr_grupos+'&id_video='+id_video+'&codigo='+codigo,'exit_mostrarTPL');
	}
}

///////////////////////////////////////////////////////////////////
// Denunciar vídeo.
///////////////////////////////////////////////////////////////////
function denunciarVideo(id, aux_div, titulo_video, id_artista, nombre_artista) {
	div = aux_div;
	if (!puedoMostrarDIV(aux_div))
		return;

	showOverlay();

	ajaxpage('/load.php?tpl=includes/denunciar_video&vars=id='+id+'|titulo_video='+titulo_video+'|id_artista='+id_artista+'|nombre_artista='+nombre_artista, aux_div);
}

function enviarDenuncia(id_video,motivo,mail) {
	if (validateForm_denunciarVideo())
	{
		document.getElementById('denunciar_video').innerHTML = '<center><br/><img src="/img/bigrotation2.gif" /></center>';
		daleAjaxPOST(1,'video_opciones.php','type=DENUNCIAR_VIDEO&id='+id_video+'&motivo='+motivo+'&mail='+mail,'exit_mostrarTPL');
	}
}

function validateForm_denunciarVideo(){
	var emailID=document.formulario.email;
	var motivo=document.formulario.motivo.selectedIndex; 
	if (motivo == 0) {
		msg_error("no_motivo");
		return false;
	}
	if ((emailID.value==null)||(emailID.value=="")){
		msg_error("no_mail");
		emailID.focus()
		return false
	}
	if (mailCheck(emailID.value)==false){
		emailID.value=""
		msg_error("no_mail");
		emailID.focus()
		return false
	}
	return true
 }

///////////////////////////////////////////////////////////////////
// Enviar a un amigo.
///////////////////////////////////////////////////////////////////
function enviarAmigo(id, aux_div) {
	div = aux_div;
	if (!puedoMostrarDIV(aux_div))
		return;

	showOverlay();

	ajaxpage('/load.php?tpl=includes/enviar_amigo&vars=id='+id, aux_div);
}

function enviarVideoAmigo(id_video,n_remitente,e_remitente,n_destinatario,e_destinatario,comentario) {
	if (validateForm_enviarAmigo()) {
		document.getElementById('enviar_amigo').innerHTML = '<center><br/><img src="/img/bigrotation2.gif" /></center>';
		daleAjaxPOST(1,'video_opciones.php','type=ENVIAR_VIDEO&id='+id_video+'&nr='+n_remitente+'&eR='+e_remitente+'&nD='+n_destinatario+'&eD='+e_destinatario+'&co='+comentario,'exit_mostrarTPL');
	}
}

function validateForm_enviarAmigo(){
	var nR=document.formulario.nombre_remitente;
	var eR=document.formulario.email_remitente;
	var nD=document.formulario.nombre_destinatario;
	var eD=document.formulario.email_destinatario;
	
	if ((nR.value==null)||(nR.value=="")){
		msg_error("no_nombre");
		nR.focus()
		return false
	}
	if ((eR.value==null)||(eR.value=="")||(mailCheck(eR.value)==false)){
		msg_error("no_mail");
		eR.focus();
		return false
	}
	
	if ((nD.value==null)||(nD.value=="")){
		msg_error("no_nombre_des");
		nD.focus()
		return false
	}
	if ((eD.value==null)||(eD.value=="")||(mailCheck(eD.value)==false)){
		msg_error("error_mail_des");
		eD.focus();
		return false
	}
	return true
 }

///////////////////////////////////////////////////////////////////
// Añadir un vídeo a favoritos.
///////////////////////////////////////////////////////////////////
function anadirFavoritos(id, aux_div) {
	div = aux_div;
	if (!puedoMostrarDIV(aux_div))
		return;

	showOverlay();

	if (logado) {
		daleAjaxPOST(1,'video_opciones.php','type=ANADIR_FAVORITOS&id='+id,'exit_mostrarTPL');
	} else {
		//document.location.href="login.php?ret=informaciondecontenido.php?con="+id;
		login_retorno = 'daleAjaxPOST(2,\'video_opciones.php\',\'type=ANADIR_FAVORITOS&id='+id+'\',\'exit_mostrarTPL2\');';
		ajaxpage('/load.php?tpl=includes/login', aux_div);
	}
}

///////////////////////////////////////////////////////////////////
// Mostrar popup agregadores
///////////////////////////////////////////////////////////////////
function popupAgregadores(aux_div) {
	div = aux_div;
	if (!puedoMostrarDIV(aux_div))
		return;

	showOverlay();

	daleAjaxPOST(1,'agregadores.php','','exit_mostrarTPL');
}




///////////////////////////////////////////////////////////////////
// Login POPUP
///////////////////////////////////////////////////////////////////
var login_retorno = null;
function enviarLogin(auxemail,auxpwd) {
	if (checkLogin(auxemail,auxpwd)) {
         usuario= escape(auxemail.value);
         key = escape(auxpwd.value);
        document.getElementById('Login').innerHTML = '<center><br/><img src="/img/bigrotation2.gif" /></center>';
		daleAjaxPOST(1,'/login.php','type=log&capa=1&userid='+usuario+'&pwd='+key,'exit_mostrarTPL');
	}
}  

function loginContinuar(codigo) {
	codigo_java = codigo;
	logado = true;
	eval(login_retorno);
}

function checkLogin(emailcheck,pwdcheck) {
	return (checkemail(emailcheck) && checkPwd(pwdcheck));
}

function checkemail(emailcheck) {
	var testresults;
	var str=emailcheck.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;		

	if (filter.test(str))
		testresults=true;
	else {
		msg_error("no_mail");
		testresults=false;
	}

	return (testresults);
}

function checkPwd(pwdcheck)
{
	var valid;
	valid = (pwdcheck.value.length>0);
	if (!valid)
		msg_error("no_password");

	return valid;
}


///////////////////////////////////////////////////////////////////
// CONTENIDO INAPROPIADO
///////////////////////////////////////////////////////////////////
function inapropiado_aceptar() {
	createCookie("inapropiado","aceptar",1);
	showPlayer();
}

function inapropiado_rechazar() {
	eraseCookie("inapropiado");
	document.location.href = '/index.php';
}

///////////////////////////////////////////////////////////////////
// Chequea si un mail es correcto.
///////////////////////////////////////////////////////////////////
function mailCheck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	var err=false;
	if (str.indexOf(at)==-1){
	   err = true
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   err = true
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    err = true
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    err = true
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    err = true
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    err = true
	 }

	 if (str.indexOf(" ")!=-1){
	   err = true
	 }
		if (err) {
			return false;
		} else {
			return true;
		}					
}

     ///////////////////////////////////////////////////////////////////
    // Parsea datos AJAX de los videos del buscador.
    ///////////////////////////////////////////////////////////////////
    function parsear_datos_video_ajax(cadena)
    {
        array_resultados=cadena.split('X');
        for(i=1; i<array_resultados.length; i++)
        {
           array_datos_video=array_resultados[i].split('#');
           document.getElementById('fecha_'+array_datos_video[0]).innerHTML =   array_datos_video[1] ;  
           document.getElementById('vistos_'+array_datos_video[0]).innerHTML =   array_datos_video[2] ; 
           document.getElementById('duracion_'+array_datos_video[0]).innerHTML =   array_datos_video[3] ;   
        }       
    }
    function vistos_parsear_datos_video_ajax(cadena)
    {
        array_resultados=cadena.split('X');
        for(i=1; i<array_resultados.length; i++)
        {
           array_datos_video=array_resultados[i].split('#');
           document.getElementById('vistos_'+array_datos_video[0]).innerHTML =   array_datos_video[2] ; 
        }       
    }
    function AJAX_proceso(){
        if(http2.readyState == 4) {
            var html = http2.responseText;
            ini = html.indexOf('##');
            fin = html.lastIndexOf('##');
             codigo = html.substring(ini+2, fin); 
             eval(codigo);
            }
         }
    function AJAX_proceso_vistos(){
        if(http2.readyState == 4) {
             var html = http2.responseText;
              ini = html.indexOf('##');
              fin = html.lastIndexOf('##');
              codigo = html.substring(ini+2, fin);
               eval('vistos_'+codigo);
            }
         }
         

