//--------------------------------------------------------------------------------------------
// ContraValor
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
// createContraValor
//--------------------------------------------------------------------------------------------

function createContraValor () {
	if (existsContraValor()) return;

	var html = '<span id="contravalor"><table><tr><td id="contravalor1" nowrap></td></tr></table></span>';

	document.body.insertAdjacentHTML('BeforeEnd', html);

	document.all.contravalor.style.visibility = 'hidden';
	document.all.contravalor.style.background = '#FFFFDD';
	document.all.contravalor.style.color      = '#000000';
	document.all.contravalor.style.border     = 'solid 1 #000000';
	document.all.contravalor.style.position   = 'absolute';
	document.all.contravalor.style.padding    = '0 1 0 1';
}

//--------------------------------------------------------------------------------------------
// existsContraValor
//--------------------------------------------------------------------------------------------

function existsContraValor () {
	return document.all.contravalor;
}

//--------------------------------------------------------------------------------------------
// isHiddenContraValor
//--------------------------------------------------------------------------------------------

function isHiddenContraValor () {
	return document.all.contravalor.style.visibility == 'hidden';
}

//--------------------------------------------------------------------------------------------
// showContraValor
//--------------------------------------------------------------------------------------------

function hideContraValor () {
	if (!existsContraValor()) return;
	document.all.contravalor.style.visibility = 'hidden';
}

//--------------------------------------------------------------------------------------------
// showContraValor
//--------------------------------------------------------------------------------------------

function showContraValor (euro) {
	if (!existsContraValor()) createContraValor();

	var element = event.srcElement;

	var escudos = formatEscudos(Math.round(euro * 200.482));

	document.all.contravalor.style.left = getPixelLeft(element);
	document.all.contravalor.style.top = getPixelTop(element);

	document.all.contravalor1.innerText = escudos;
	formatStyle(document.all.contravalor1, element);

	document.all.contravalor.style.visibility = 'visible';
}

//--------------------------------------------------------------------------------------------
// showContraValorInput
//--------------------------------------------------------------------------------------------

function showContraValorInput () {
	var element = event.srcElement;
	if (typeof element.value == 'undefined') return;
	var euro = parseEuro(element.value);
	showContraValor(euro);
}

//--------------------------------------------------------------------------------------------
// changeContraValorInput
//--------------------------------------------------------------------------------------------

function changeContraValorInput () {
	if (!existsContraValor()) createContraValor();
	if (!isHiddenContraValor()) showContraValorInput();
}

//--------------------------------------------------------------------------------------------
// showingContraValor
//--------------------------------------------------------------------------------------------

function showingContraValor () {
	var element = event.srcElement;
	if (typeof element.innerText == 'undefined') return;
	var euro = parseEuro(element.innerText);
	element.onmouseover = new Function('showContraValor('+euro+')');
	element.onmousemove = element.onmouseover;
	element.onmouseout  = hideContraValor;
}

//--------------------------------------------------------------------------------------------
// showingContraValorInput
//--------------------------------------------------------------------------------------------

function showingContraValorInput () {
	var element = event.srcElement;

	element.onmouseover = showContraValorInput;
	element.onmousemove = showContraValorInput;
	element.onmouseout  = hideContraValor;
	element.onchange    = showContraValorInput;

	element.old_onkeypress = element.onkeypress;
	element.onkeypress     = new Function('changeContraValorInput(); this.old_onkeypress();');
	element.old_onkeydown  = element.onkeydown;
	element.onkeydown      = new Function('changeContraValorInput(); this.old_onkeydown();');
	element.old_onkeyup    = element.onkeyup;
	element.onkeyup        = new Function('changeContraValorInput(); this.old_onkeyup();');
}

//--------------------------------------------------------------------------------------------
// Utils
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
// getParentClassName
//--------------------------------------------------------------------------------------------

function getParentClassName (element) {
	if (!element) return;
	while (element) {
		var className = element.className;
		if (className) return className;
		element = element.parentElement;
	}
}

//--------------------------------------------------------------------------------------------
// getParentStyle
//--------------------------------------------------------------------------------------------

function getParentStyle (element) {
	if (!element) return null;
	while (element) {
		var style = element.style;
		if (style && style.font && style.font != '') alert(element.tagName);
		if (style && style.font && style.font != '') return style;
		element = element.parentElement;
	}
	return null;
}

//--------------------------------------------------------------------------------------------
// getPixelLeft
//--------------------------------------------------------------------------------------------

function getPixelLeft (element) {
	var offset = 0;
	element = element.offsetParent;
	while (element) {
		offset += element.offsetLeft;
		element = element.offsetParent;
	}
 	return offset + event.offsetX + 5;
}

//--------------------------------------------------------------------------------------------
// getPixelTop
//--------------------------------------------------------------------------------------------

function getPixelTop (element) {
	var offset = 0;
	element = element.offsetParent;
	while (element) {
		offset += element.offsetTop;
		element = element.offsetParent;
	}
	return offset + event.offsetY + 10;
}

//--------------------------------------------------------------------------------------------
// formatEscudos
//--------------------------------------------------------------------------------------------

function formatEscudos (escudos) {
	var str = escudos.toString();
	var fmt = '';
	for (var i = str.length-1; i >= 0; i--) {
		if (i != str.length-1 && (str.length-1 - i) % 3 == 0) fmt = '.' + fmt;
		fmt = str.charAt(i) + fmt;
	}
	return fmt + '$00';
}

//--------------------------------------------------------------------------------------------
// formatStyle
//--------------------------------------------------------------------------------------------

function formatStyle (target, source) {
	clearStyle(target);
	var parentClassName = getParentClassName(source);
	if (parentClassName) {
		target.className = parentClassName;
		return;
	}
	var parent = source;
	while (parent) {
		setStyleFromTags(parent, target);
		parent = parent.parentElement;
	}

	parent = source;
	while (parent) {
		setStyle(parent, target);
		parent = parent.parentElement;
	}
}

//--------------------------------------------------------------------------------------------
// parseEuro
//--------------------------------------------------------------------------------------------

function parseEuro (euro) {
	var whitespace = " \t\n\r";
	var str = '';
	var start = 0;
	for (; start < euro.length; start++) {
		var ch = euro.charAt(start);
		if (ch == '.') continue;
		if (whitespace.indexOf(ch) != -1) continue;
		break;
	}
	var comma = false;
	for (var i = start; i < euro.length; i++) {
		var ch = euro.charAt(i);
		if (whitespace.indexOf(ch)!=-1) {
			if (comma) ch = '0'; else break;
		}
		if (ch == '.') continue;
		if (ch == ',') {
			comma = true;
			ch = '.';
		}
		str += ch;
	}
	if (str == '') str = '0';
	return str;
}

//--------------------------------------------------------------------------------------------
// clearStyle
//--------------------------------------------------------------------------------------------

function clearStyle (element) {
	element.className = '';

	element.style.removeAttribute('color');

	element.style.removeAttribute('font');
	element.style.removeAttribute('fontFamily');
	element.style.removeAttribute('fontSize');
	element.style.removeAttribute('fontStyle');
	element.style.removeAttribute('fontVariant');
	element.style.removeAttribute('fontWeight');
}

//--------------------------------------------------------------------------------------------
// setStyle
//--------------------------------------------------------------------------------------------

function setStyle (source, target) {
	if (target.style.font == '' && source.style.font != '') target.style.font = source.style.font;
	if (target.style.fontFamily == '' && source.style.fontFamily != '') target.style.fontFamily = source.style.fontFamily;
	if (target.style.fontSize == '' && source.style.fontSize != '') target.style.fontSize = source.style.fontSize;
	if (target.style.fontStyle == '' && source.style.fontStyle != '') target.style.fontStyle = source.style.fontStyle;
	if (target.style.fontVariant == '' && source.style.fontVariant != '') target.style.fontVariant = source.style.fontVariant;
	if (target.style.fontWeight == '' && source.style.fontWeight != '') target.style.fontWeight = source.style.fontWeight;
}

//--------------------------------------------------------------------------------------------
// setStyleFromFont
//--------------------------------------------------------------------------------------------

function setStyleFromTags (source, target) {
	if (source.tagName == 'FONT') {
		if (target.style.fontFamily == '' && source.face != '') target.style.fontFamily = source.face;
		if (target.style.fontSize == '' && source.size != '') {
			if (source.size <= 1) {
				target.style.fontSize = 'xx-small';
			} else if (source.size == 2) {
				target.style.fontSize = 'x-small';
			} else if (source.size == 3) {
				target.style.fontSize = 'small';
			} else if (source.size == 4) {
				target.style.fontSize = 'medium';
			} else if (source.size == 5) {
				target.style.fontSize = 'large';
			} else if (source.size == 6) {
				target.style.fontSize = 'x-large';
			} else if (source.size >= 7) {
				target.style.fontSize = 'xx-large';
			}
		}
		if (target.style.color == '' && source.color != '') target.style.color = source.color;
		return;
	}
	if (source.tagName == 'B' || source.tagName == 'STRONG') {
		if (target.style.fontWeight == '') target.style.fontWeight = 'bold';
		return;
	}
	if (source.tagName == 'I' || source.tagName == 'EM') {
		if (target.style.fontStyle == '') target.style.fontStyle = 'italic';
		return;
	}
}

