//Javascript 1.3 
//Set of .NET-control accessor classes 
// - HtmlGauges reusable interface to read data from and write to .NET controls, entry point: ImplementHtmlGauge(...)
// - Point screen coordinates, entry point MakePoint

//Html gauges interface functions
function HtmlGauge_FormatFloat(value)
{
    var text = String(Math.round(value*100)/100).replace(".", ",");
	if(text.length == 0)
	{
		text = "0";
	}
	if(text.lastIndexOf(",")==-1) 
	{
		text += ",";
	}
	var len = text.lastIndexOf(",") + 3;
	if(text.length > len)
	{
		text = text.substr(0, len);
	}
	else
	{
		while(text.length < len)
		{
			text += "0";
		}
	}
	return text;
}

function HtmlGauge_SetLabelFloat(label, value)
{
	label.innerText = this.FormatFloat(value);
}

function HtmlGauge_GetLabelFloat(label)
{
	//var text = label.innerText;
	var text;
	if(label.innerText!=null && label.innerText.length>0)
		text=label.innerText;
	else
		text=label.value;
	//alert(text);
	var value = parseFloat(text.replace(",", "."))
	if(isNaN(value))
	{
		value = 0;	
	}
	return value;
}

function HtmlGauge_SetTextBoxFloat(textBox, value)
{
	textBox.value = this.FormatFloat(value);
}

function HtmlGauge_GetTextBoxFloat(textBox)
{
	var text = textBox.value;
	var value = parseFloat(text.replace(",", "."))
	if(isNaN(value))
	{
		value = 0;	
	}
	return value;
}

function HtmlGauge_GetTextBoxInt(textBox)
{
	var text = textBox.value;
	var value = parseInt(text)
	if(isNaN(value))
	{
		value = 0;	
	}
	return value;
}

function HtmlGauge_SetTextBoxInt(textBox, value)
{
	if(value == 0)
	{
		textBox.value = "";
	}
	else
	{
		textBox.value = value;
	}
}

function HtmlGauge_GetTextBoxString(textBox)
{
	return textBox.value;
}

function HtmlGauge_SetTextBoxString(textBox, value)
{
	textBox.value = value;
}

function HtmlGauge_GetLabelInt(label)
{
	var text = label.innerText;
	var value = parseInt(text)
	if(isNaN(value))
	{
		value = 0;	
	}
	return value;
}

function HtmlGauge_SetLabelInt(label, value)
{
	if(value == 0)
	{
		label.innerText = "";
	}
	else
	{
		label.innerText = value;
	}
}

function ImplementHtmlGauge(anObject)
{
	anObject.FormatFloat = HtmlGauge_FormatFloat;
	anObject.SetLabelFloat = HtmlGauge_SetLabelFloat;
	anObject.GetLabelFloat = HtmlGauge_GetLabelFloat;
	anObject.SetTextBoxFloat = HtmlGauge_SetTextBoxFloat;
	anObject.GetTextBoxFloat = HtmlGauge_GetTextBoxFloat;
	anObject.SetTextBoxInt = HtmlGauge_SetTextBoxInt;
	anObject.GetTextBoxInt = HtmlGauge_GetTextBoxInt;
	anObject.SetTextBoxString = HtmlGauge_SetTextBoxString;
	anObject.GetTextBoxString = HtmlGauge_GetTextBoxString;
	anObject.SetLabelInt = HtmlGauge_SetLabelInt;
	anObject.GetLabelInt = HtmlGauge_GetLabelInt;
}

function Point(aX, aY)
{
	this.x = aX;
	this.y = aY;
	this.toString = new Function("return( this.x+':'+ this.y);"); 
}


/*function MakePoint(aTag)
{
		var oTmp = aTag;
		var pt = new Point(0,0);
		do 
		{
	        pt.x += oTmp.offsetLeft;
	        pt.y += oTmp.offsetTop;
	        oTmp = oTmp.offsetParent;
		} 
		while(oTmp != null && oTmp.tagName!="BODY");
		return pt;
}*/


function MakePoint(anEntity)
{
	//try	{
	var dummy = anEntity;
	var p = new Point(0, 0);
	p.x += dummy.offsetWidth;
	p.y += dummy.offsetHeight;
	do 
	{
	    var sl = 0, st = 0;
	    var is_div = /^div$/i.test(dummy.tagName);
	    if (is_div && dummy.scrollLeft)
		    sl = dummy.scrollLeft;
	    if (is_div && dummy.scrollTop)
		    st = dummy.scrollTop;
		// alert("TAG = " + dummy.tagName + "; " + dummy.offsetTop + "; " + dummy.offsetHeight);
		p.x += dummy.offsetLeft - sl;
		p.y += dummy.offsetTop - st;
		dummy = dummy.offsetParent;
	} 
	while (dummy.tagName.toUpperCase() != "BODY");
	//}catch(e){	alert(dummy);}
	return p;
}



function InScope(anEntity)
{
	var dummy = event.toElement;
	do 
	{
		if(dummy == null || dummy.offsetParent == null)
		{
			return false;
		}
		if(dummy == anEntity)
		{
			return true;
		}
		dummy = dummy.offsetParent;
	} 
	while (dummy.tagName.toUpperCase() != "BODY");
	return false;
}

function LimitMultiLineLength(field, maxLength)
{
	var text = field.value;
	if (text.length > maxLength)
	{
		field.value = text.substring(0, maxLength);
		return false;
	}
	return true;
}

