// SINOPSIS
// Funciones de autosuggest del form de carga de artistas para evento
// BITACORA
// GB, Octubre de 2009, Creacion

// GLOBALES
var idArtista = 0;

// SINOPSIS
// Comienza carga de datos para autosuggest de campos
// BITACORA
// GB Marzo de 2009 Creacion
function startAutosuggestFormCargaArtistas()
{
	startAutosuggestArtistas();
}

// SINOPSIS
// Comienza carga de datos para autosuggest de campos
// BITACORA
// GB Marzo de 2009 Creacion
function startAutosuggestArtistas()
{
	// alert( "Entrando a startAutosuggestArtistas()");
	var options = {
		script: "../common/php/v2-obtener-artistas.php?",
		varname: "input",
		json: false,
		timeout: 7500,
		cache: false,
		minchars: 2,
		shownoresults: false,		
		callback: function (obj) { 
			obtenerArtistaUpdateForm( obj );
		}
	};

	var artista = new bsn.AutoSuggest('nombre-artista', options);
}

// SINOPSIS
// Borra los valores del form
// BITACORA
// GB 10-3-20010 Creacion
// ASSUMPTIONS
// Usar antes de completar los datos del form con los datos de un nuevo artista
// No borrar id-artista!, solo el resto de los campos
function eraseFormArtista()
{
	// combo paises
	document.getElementById("id-pais-artista").selectedIndex = 1;
	
	// 17-3 es un anchor
	// document.getElementById("imagen-artista").value = "";
	var link = document.getElementById("imagen-artista");
	if ( link != null )
	{
		link.href = "";
		link.innerHTML = "";
		// link.value = "";
	}

	// borrar links
	document.getElementById("link0").value = "";
	document.getElementById("link1").value = "";
	document.getElementById("link2").value = "";
	document.getElementById("link3").value = "";
	document.getElementById("link4").value = "";	
	
	// gb 14-3-2010 borrar activo / no activo
	document.getElementById("activo").checked = false;	
}

// SINOPSIS
// Actualiza los datos del artista en pantalla
// BITACORA
// GB 4-3-2009 Creacion
// INPUT
// obj, contiene obj.id, obj.name, obj.info
function obtenerArtistaUpdateForm( obj )
{
	idArtista = obj.id;
	// alert ( "idArtista = " + idArtista );

	// Guardo el id del artista en campo oculto
	document.getElementById("id-artista").value = idArtista;

	// borro campos artista
	eraseFormArtista();
	
	// obtengo datos artista y actualizo form
	obtenerRestoInfoArtistaUpdateForm( idArtista );
}


// SINOPSIS
// Trae todos los datos de un artista
// BITACORA
// GB 4-3-2010 Creacion
// INPUT
// id del artista
function obtenerRestoInfoArtistaUpdateForm( id )
{
  	var ajax = creaAjax();	

	ajax.open("GET", "../common/php/v2-obtener-artista.php?id="+id, true);

	ajax.onreadystatechange = function() 
	{ 
      if (ajax.readyState == 1) 
	  {
		// alert("En obtenerArtista, readyState = 1");
      } 
	  else 
	  if (ajax.readyState == 4)
	  {
		  // alert( "En obtenerArtista, responseText = " + ajax.responseText );
		  updateFormRestoInfoArtista( ajax.responseText ); 
		  // obtengo y actualizo imagenes
		  obtenerImagenesArtista( idArtista );
		  // obtengo y actualizo links
		  obtenerLinksArtista( idArtista ); 
      } 
    }
    ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    ajax.send(null);	     	
};

// SINOPSIS
// Actualiza los valores del form
// BITACORA
// GB 4-3-20010 Creacion
// INPUT
// lista de atributos separados por coma. Ojo con el orden
// ASSUMPTIONS & BUGS
// *** REVISAR que el seteo de pais este bien
function updateFormRestoInfoArtista( response )
{
	// gb 14-3-2010 griso nombre
	document.getElementById("nombre-artista").readOnly = true;
	document.getElementById("nombre-artista").className = "form-solo-lectura";		

	// alert( response );
	var aux = response.split(",");	
	// Set pais
	setComboPais(aux[2]);
	// gb 14-3-2010 activo / no activo
	if ( aux[5] == 1 )
		document.getElementById("activo").checked = true;
	else
		document.getElementById("activo").checked = false;
		
}

// SINOPSIS
// Setea el pais en el combo de acuerdo al id del pais en la base
// BITACORA
// GB 11-3-2010 Creacion
function setComboPais( idPais )
{
	var i = 0;
	var combo = document.getElementById("id-pais-artista");
	// alert( "Setting pais, combo length = " + combo.length );
	for ( i = 0 ; i < combo.length; i++ )
		if ( combo.options[i].value == idPais )
			combo.selectedIndex = i;
}

// SINOPSIS
// Trae todas las imagenes relacionadas con un artista
// BITACORA
// GB 9-3-2010 Creacion
// INPUT
// id del artista
function obtenerImagenesArtista( idArtista )
{
	var idTipoEntidad = 4; // este numero es uan convencion que usamos en toda la aplicacion
  
  	var ajax = creaAjax();	
	
	// alert("Por obtener imagenes artista...");
	
	ajax.open("GET", "../common/php/v2-obtener-imagenes.php?idEntidad="+idArtista+"&idTipoEntidad="+idTipoEntidad, true);

	ajax.onreadystatechange = function() 
	{ 
      if (ajax.readyState == 1) 
	  {
		alert("readyState = 1");
      } 
	  else 
	  if (ajax.readyState == 4)
	  {
		  // alert( "En obtenerImagenesArtista, responseText = " + ajax.responseText );
		  updateFormImagenesArtista( ajax.responseText ); 
      } 
    }
    ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    ajax.send(null);	     	
};

// SINOPSIS
// Actualiza los valores de las imagenes en el form
// BITACORA
// GB 9-3-2010 Creacion
// GB 9-3-2010 No anda aun...
// INPUT
// lista de atributos imagenes separados por coma...
// OUTPUT
// None
function updateFormImagenesArtista( imagen )
{
	// obtengo nombre
	// alert (imagen.lastIndexOf('/'));
	var nombre = imagen.substring(imagen.lastIndexOf('/')+1, imagen.length );
	// alert( nombre );
	document.getElementById("imagen-artista").innerHTML = nombre;
	document.getElementById("imagen-artista").href = "http://www.vuenosairez.com/images/artistas/"+nombre;
}

// SINOPSIS
// Trae todos los links de un artista y actualiza el form
// BITACORA
// GB 11-3-2010 Creacion
// INPUT
// id del artista
function obtenerLinksArtista( idArtista )
{
	var idTipoEntidad = 4; // este numero es uan convencion que usamos en toda la aplicacion
  
  	var ajax = creaAjax();	
	
	// alert("Por obtener links artista...");
	
	ajax.open("GET", "../common/php/v2-obtener-links.php?idEntidad="+idArtista+"&idTipoEntidad="+idTipoEntidad, true);

	ajax.onreadystatechange = function() 
	{ 
      if (ajax.readyState == 1) 
	  {
		// alert("readyState = 1");
      } 
	  else 
	  if (ajax.readyState == 4)
	  {
		  // alert( "En obtenerLinksArtista, responseText = " + ajax.responseText );
		  updateFormLinksArtista( ajax.responseText ); 
      } 
    }
    ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    ajax.send(null);	     	
};


// SINOPSIS
// Actualiza los valores de los links en el form
// BITACORA
// GB 11-3-2010 Creacion
// INPUT
// lista de atributos imagenes separados por coma...
function updateFormLinksArtista( response )
{
	// alert( links );
	var i = 0;
	var aux = response.split(",");
	// alert( "Despues de split de links, aux[0] = " + aux[0] + " length = " + aux.length );
	for ( i = 0; i < aux.length ; i++ )
		document.getElementById("link"+i).value = aux[i];
}







// SINOPSIS
// Envia el form. Llamanda desde formulario de carga de evento.
// ASSUMPTIONS & BUGS
// BITACORA
// GB 9-3-2010 Creacion
function submitFormCargaArtista()
{
	// alert( "Entrando a submitFormCargaEventoUser");
	// GB 18-6 este if se podria sacar?, podemos asumir que si estamos aca el usuario esta logueado?
	if ( !userLoggedInLocal() )
	{
		var content = '<div style="background-color:#ffffff; height:100px; width: 250px; font-size:13px; padding:10px; text-align:center">Lo sentimos, debes estar logueado para poder grabar tu artista!<br/><br/>Hacelo desde la barra superior por favor, o registrate.</div>';
		// var content = "Logueate che!";
		var player = "html"
		var title = "Login!";
		openShadowbox(content, player, title);
	}
	else
	{	
		if ( preSubmitFormCargaArtista() == false )
			return;
	
		// gb 19-5-2009 Deshabilito el boton de enviar
		document.getElementById("divBotonEnviar").innerHTML = "<p style='color:#FF3399'>Enviando...</p>";
		// Submit form
		var form = document.forms['artista'];
		form.submit();
	}
}

// SINOPSIS
// Chequea condiciones antes de mandar form
// ASSUMPTIONS & BUGS
// BITACORA
// GB 31-3-2010 Creacion
function preSubmitFormCargaArtista()
{
	var titulo = document.getElementById("nombre-artista");
	if ( titulo == null )
		// Esto se hace por si estamos llamando la funcion desde el shadowbox de login 
		titulo = parent.document.getElementById("titulo-artista");
	
	// gb 22-6-2009 ojo no usar lenght para chequear que da undefined
	// alert ("Titulo = " + titulo.value + ", length = " + titulo.value.length );
	if ( titulo.value == "" )
	{
		alert( "Debes completar el nombre para grabar tu artista");
		return false;
	}
	
	return true;
}

