function hexa(n)	{
	return "0123456789ABCDEF".charAt(parseInt(n / 16)) + "0123456789ABCDEF".charAt(parseInt(n % 16));
}

function cadenaColor(r, g, b)	{
	return "#" + hexa(r) + hexa(g) + hexa(b);
}

function colorines1()	{
	with (document.forms.colores1)
	document.getElementById("muestra1").style.backgroundColor = cadenaColor(RR1.value, GG1.value, BB1.value);
}

function colorines2()	{
	with (document.forms.colores2)
	document.getElementById("muestra2").style.backgroundColor = cadenaColor(RR2.value, GG2.value, BB2.value);
}

function modificar(formulario, elemento)	{
	with (formulario)	{
		target = elemento.name;
		tipo.value = elemento.name.substr(0, 2);
		control.value = elemento.name;
		actual.value = elemento.value;
		if (isNaN(elemento.value))
			elemento.value = "0";
		submit();
		eval(evento.value + "()");
	}
}

var colorInicial, colorFinal, fondos, gradiente, anchura, altura, color1, color2;

function datosFormulario(g, c1, c2)	{
	colorInicial = "#" + c1;
	colorFinal = "#" + c2;
	fondos = transitar(c2, c1, 36);
	with (document.forms["degradados"])	{
		gradiente[0].checked = (g == "H") ? true : false;
		gradiente[1].checked = (g == "V") ? true : false;
		color1.value = c1;
		color2.value = c2;
	}
	var gH = "url(../scripts/gradientesH.php?anchura=50&color1=" + c1 + "&color2=" + c2 + ")";
	var gV = "url(../scripts/gradientesV.php?altura=50&color1=" + c1 + "&color2=" + c2 + ")";

	document.getElementById("gradienteHorizontal").style.backgroundImage = gH;
	document.getElementById("gradienteVertical").style.backgroundImage = gV;

	document.getElementById("gvtexto").style.backgroundColor = "#" + c1;
	document.getElementById("gvtexto").style.color = "#" + c2;

	document.getElementById("ghtexto").style.backgroundColor = "#" + c1;
	document.getElementById("ghtexto").style.color = "#" + c2;
}

function invertir()	{
	with (document.forms["degradados"])	{
		var g = (gradiente[0].checked) ? "H" : "V"; 
		datosFormulario(g, color2.value, color1.value);
	}
}

/*
function reInicio()	{
	elemento = document.getElementById("principal");
	altura = elemento.clientHeight;
	anchura = elemento.clientWidth;

	var fondo = "URL(../scripts/gradientes" + gradiente + ".php?";
	fondo += (gradiente == "V") ? "altura=" : "anchura=";
	fondo += (gradiente == "V") ? altura : anchura;
	fondo += "&color1=" + color1 + "&color2=" + color2 + ")";

	elemento.style.backgroundImage = fondo;
	
}
*/
//window.onresize = reInicio;
elemento = document.getElementById("principal");

function inicio()	{
	elemento = document.getElementById("principal");

//	elemento = document.body;
	color1 = "FFFCCD";
	color2 = "D68AA5";
	var lineaEntrada = location.search.substr(1);
	if (lineaEntrada == "") lineaEntrada = "gradiente=H&color1=" + color1 + "&color2=" + color2;
	var entrada = lineaEntrada.split("&");
	for (items in entrada)	{
		pareja = entrada[items].split("=");
		eval(pareja[0] + " = '" + pareja[1] + "'");
	}

	altura = elemento.clientHeight;
	anchura = elemento.clientWidth;

	var fondo = "URL(../scripts/gradientes" + gradiente + ".php?";
	if (!gradiente) gradiente = "H";
	fondo += (gradiente == "V") ? "altura=" : "anchura=";
	fondo += (gradiente == "V") ? altura : anchura;
	fondo += "&color1=" + color1 + "&color2=" + color2 + ")";

	elemento.style.backgroundImage = fondo;
	datosFormulario(gradiente, color1, color2);
	with	(document.forms["colores1"])	{
		tipo.value = "RR";
		control.value ="RR1";
		actual.value = hexadec(color1.substr(0, 2));
		RR1.value = actual.value;
		target = "RR1";
		submit();

		tipo.value = "GG";
		control.value ="GG1";
		actual.value = hexadec(color1.substr(2, 2));
		GG1.value = actual.value;
		target = "GG1";
		submit();

		tipo.value = "BB";
		control.value ="BB1";
		actual.value = hexadec(color1.substr(4, 2));
		BB1.value = actual.value;
		target = "BB1";
		submit();
		document.getElementById("muestra1").style.backgroundColor = 
			cadenaColor(RR1.value, GG1.value, BB1.value);
	}

	with	(document.forms["colores2"])	{
		tipo.value = "RR";
		control.value ="RR2";
		actual.value = hexadec(color2.substr(0, 2));
		RR2.value = actual.value;
		target = "RR2";
		submit();

		tipo.value = "GG";
		control.value ="GG2";
		actual.value = hexadec(color2.substr(2, 2));
		GG2.value = actual.value;
		target = "GG2";
		submit();

		tipo.value = "BB";
		control.value ="BB2";
		actual.value = hexadec(color2.substr(4, 2));
		BB2.value = actual.value;
		target = "BB2";
		submit();
		document.getElementById("muestra2").style.backgroundColor = 
			cadenaColor(RR2.value, GG2.value, BB2.value);
	}
	
	//setTimeout("dimContenido()", 1000);

}

function dimContenido()	{
	var yoSoy = document.getElementById("contenido");
	yoSoy.style.width = yoSoy.getElementsByTagName("table")[0].clientWidth + "px";
}

function asignaColor(color)	{
	with (document.forms["degradados"])
		var g = (gradiente[0].checked) ? "H" : "V";
	var dato = cadenaColor(
		document.forms["colores" + color]["RR" + color].value,
		document.forms["colores" + color]["GG" + color].value,
		document.forms["colores" + color]["BB" + color].value
	);
	document.forms["degradados"]["color" + color].value = dato.substring(1);
	with (document.forms["degradados"])
	datosFormulario(g, color1.value, color2.value);
}

function paso2()	{
	alert("mostrar siguiente capa");
}
