var hex="0123456789ABCDEF";
function ToHex(n) {
  return hex.charAt((n>> 4)& 0xf)+ hex.charAt(n& 0xf);
}

function OrangeFade(element,iteration) {
  if (typeof iteration == 'undefined')
    iteration = 120;
  var el = document.getElementById(element);
  el.style.backgroundColor = "#FF" + ToHex(iteration) + ToHex(parseInt((iteration-120)*1.5));
  if (iteration < 204)
    setTimeout("OrangeFade('"+element+"', " + (iteration + 8) + ")", 150);
  else
    el.style.backgroundColor = '';
}

function revolve(toHide, toShow) {
	
	if (typeof toHide == 'string')
		toHide = document.getElementById(toHide)
	toHide.style.display="none"
	if (typeof toShow == 'string')
		toShow = document.getElementById(toShow)
	toShow.style.display = 'block'
}

function confirmAction(action) {
	switch (action)
	{
		case "eliminarPregunta":
			confirm = "¿Seguro que quieres eliminar esta pregunta?"; break;
		case "eliminarTexto":
			confirm = "¿Seguro que quieres eliminar este texto?"; break;
	}
	return confirm(confirm);
}

function valRespuestaElegida(myForm)
{
	valido = false
	for (i = 0; i < myForm.respuestaCorrecta.length; i++)
	{
		if (myForm.respuestaCorrecta[i].checked && !myForm.respuestaCorrecta[i].disabled)
		{
			valido = true
		}
	}
	(!valido) ? alert("Selecciona la respuesta correcta") : "";
	return valido
}

function colorFila(myForm, fila) {
	if (myForm["respuesta["+fila+"][1]"].value || myForm["respuesta["+fila+"][2]"].value) {
		document.getElementById("fila_"+fila).className="activo"
		myForm["respuesta["+fila+"][1]"].style.background="#fff"
		myForm["respuesta["+fila+"][2]"].style.background="#fff"
		myForm.respuestaCorrecta[fila - 1].disabled = false
	} else {
		document.getElementById("fila_"+fila).className="inactivo"
		myForm["respuesta["+fila+"][1]"].style.background="#ccc"
		myForm["respuesta["+fila+"][2]"].style.background="#ccc"
		myForm.respuestaCorrecta[fila - 1].disabled = true
	}
}

function loadEstados(myForm, eleAni)
{
	mySelect = myForm.idEstadoAnimacion
	
	mySelect.disabled = true
	for (i = mySelect.length; i > 0; i--) {
		mySelect.options[i-1] = null
	}
	myLen = estados[eleAni.value].length
	for (i = 0; i < myLen; i++) {
		estado = estados[eleAni.value][i]
		mySelect.options[mySelect.length] = new Option(estado.str, estado.id)
	}
	mySelect.disabled = false
}

// fichaPregunta.php
function editarPregunta(idioma)
{
	document.getElementById("pregunta_"+idioma).style.display = "none"
	myForm = document.getElementById("form_pregunta_"+idioma)
	myForm.style.display = "block"
	myForm.texto.focus()
	myForm.texto.select()
}
function editarRespuesta(fila,idioma)
{
	document.getElementById("respuesta_"+fila+"_"+idioma).style.display = "none"
	myForm = document.getElementById("form_respuesta_"+fila+"_"+idioma)
	myForm.style.display = "block"
	myForm.texto.focus()
	myForm.texto.select()
}
function cambiarEstado(node, id, idioma)
{
	if (confirm("¿Seguro que quieres cambiar el estado de esta pregunta?")) {
		node.form.submit()
	} else {
		node.checked = node.defaultChecked
	}
}
function cambiarOpciones(node)
{
	if (confirm("¿Seguro que quieres cambiar el tipo de esta pregunta?")) {
		node.form.submit()
	} else {
		node.form.reset()
	}
}

// fichaUsuario.php
function valCambios(myForm) {
	if (myForm.estado.options[myForm.estado.selectedIndex].defaultSelected != true) {
		if (confirm("¿Seguro que quieres cambiar el estado de este usuario?")) {
			return true
		} else {
			myForm.estado.selectedIndex = Math.pow(myForm.estado.selectedIndex - 1, 2)
			return false
		}
	}
}
function confirmarValidar(usuario) {
	if (confirm("¿Seguro que quieres validar este usuario?\n\n" +
							"Al confirmar, un email será enviado al usuario para confirmar que su cuenta está activa.")) {
		window.location = "validaUsuarioAction.php?idUsuario=" + usuario
		return false
	} else {
		return false
	}
}

// listarTextos.php
function hlTexto()
{
	if (location.hash) { OrangeFade(location.hash.replace("#","")) }
}
