var keyStr = "ABCDEFGHIJKLMNOP" +
	"QRSTUVWXYZabcdef" +
	"ghijklmnopqrstuv" +
	"wxyz0123456789+/" +
	"=";

function encode64(input) {
	input = escape(input);
	var output = "";
	var chr1, chr2, chr3 = "";
	var enc1, enc2, enc3, enc4 = "";
	var i = 0;

	do {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		}
		else
		if (isNaN(chr3)) {
			enc4 = 64;
		}

		output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";
	} while (i < input.length);

	return output;
}
function renderHeader(hdr)
{
	var txt = hdr.firstChild.nodeValue;
	if (!txt)
		return;
	txt = txt.toUpperCase();
	hdr.style.backgroundImage = 'url("/svg/box_hdr.svg?text=' + encode64(txt) + '&format=png&maxwidth=150&base64=true")';
}

function renderDocHeader(hdr)
{
	var txt = hdr.firstChild.nodeValue;
	if (!txt)
		return;
	//txt = txt.toUpperCase();
	hdr.style.backgroundImage = 'url("/svg/doc_hdr.svg?text=' + encode64(txt) + '&format=png&base64=true")';
}

function renderDocHeaders()
{
	var headers = $('h2.doc-title');
	jQuery.each(headers, function() {
		renderDocHeader(this);
	});
}

function renderHeaders()
{
	var headers = $('div.box h3');
	jQuery.each(headers, function() {
		renderHeader(this);
	});
}

function fixMenu()
{
	var items = $('#top-menu li a');
	if (items) {
		jQuery.each(items, function() {
			var t = this;
			var txt = t.firstChild.nodeValue;
			if (txt.length <= 15)
			{
				t.style.paddingTop = "12px";
			}
		});
	}
}

function renderGoButtons()
{
	var btns = $('input.go_btn');
	jQuery.each(btns, function() {
		var btn = this;
		var a = document.createElement('a');
		a.className = 'go_btn';
		a.href = document.location.href;
		a.onclick = function() { btn.parentNode.parentNode.submit(); return false; }
		a.appendChild(document.createTextNode(btn.value));
		this.parentNode.appendChild(a);
		this.style.display = 'none';
	});
}

function renderFormTable(table)
{
	var rows = table.rows;
	if (rows)
	{
		for (var i = 0; i < rows.length; i++)
		{
			var row = rows[i];
			if (row.className != '')
				continue;
			if (i % 2 == 0)
				row.className = 'even';
			else
				row.className = 'odd';

			var cells = row.cells;
			if (cells && cells.length == 2)
			{
				cells[0].className = 'first-cell';
				cells[1].className = 'last-cell';
			}
			else
			if (cells && cells.length == 3)
			{
				cells[0].className = 'first-cell';
				cells[0].valign = 'top';
				cells[0].width = '60px !important';
				cells[1].className = 'first-cell';
				cells[1].style.textAlign = 'left';
				cells[1].style.color = 'black';
				cells[1].style.fontWeight = 'normal';
				cells[1].valign = 'top';
				cells[2].className = 'last-cell';
				cells[2].valign = 'top';
			}
		}
	}
	//$(table).corners( { radio: 30 } );
}

function renderFormTables()
{
	var tables = $('table.form-table');
	if (tables)
	{
		jQuery.each(tables, function() {
			renderFormTable(this);
		});
	}
}

function checkLoginFormsFilled()
{
	var login = document.getElementById('login_fld');
	var pass = document.getElementById('password_fld');

	if (!login || !pass)
		return true;

	if (login.value != '')
	{
		login.style.background = '#fff';
	}
	if (pass.value != '')
	{
		pass.style.background = '#fff';
	}

	pass.onblur = function() {
		if (this.value == '')
			this.style.backgroundImage = 'url(/images/pass_bg.gif)';
		else
			this.style.background = '#fff';
		return false;
	}
	login.onblur = function() {
		if (this.value == '')
			this.style.backgroundImage = 'url(/images/login_bg.gif)';
		else
			this.style.background = '#fff';
		return false;
	}
}

setTimeout("checkLoginFormsFilled()", 5);

renderHeaders();
renderDocHeaders();
renderGoButtons();
renderFormTables();
fixMenu();

jQuery.each($('input[type=hidden]'), function() { this.style.visibility = 'hidden'; });
//$('div.box').dropShadow( { left: 1, top: 1 });
//$('#main-column').dropShadow( { left: 1, top: 1 });
//$('#product-listing #middle-promo').dropShadow( { left: 2, top: 2 } );
//$('#document-content h4').dropShadow( { left: 2, top: 2 } );*/