// LyIntegerScript.js
// Copyright (c) 2000-2009, Lyria-W4.
// All rights reserved.
// @version	$Id: LyFloatScript.js,v 1.2 2009/01/06 11:04:03 jls Exp $

var ACCURACY = 1000000;
var netscapeNavigation = (navigator.appName == "Netscape");

// This function tests if the argument is a float and return the result.
function IsFloat(str)
{
	var number = parseFloat(str);
	var result = !isNaN(number);

	if (!result)
		return false;

	var nPoints=0;
	var i;
	var car;

	for (i = 0; i < str.length && result; i++)
	{
		car = str.substring(i,i+1);

		if ((i==0) && (car == "-"))
			continue;

		if (((car >= "0") && (car <= "9"))
			|| (car == "."))
		{
			if (car == ".")
				nPoints++;
			continue;
		}
		else
			result = false;
	}

	if (nPoints >= 2)
		result = false;

	if (!result)
		return false;

	var numberString = number.toString();

	if ((numberString.substring(0,1)) == ".")
		numberString = "0" + numberString;

	if ((numberString.substring(0,2)) == "-.")
		numberString = "-0." + numberString.substring(2);

	if (numberString.indexOf('.') == -1)
		numberString = numberString + ".0";

	if ((str.substring(0,2)) == "-.")
		str = "-0." + str.substring(2);

	if ((str.substring(0,1)) == ".")
		str = "0" + str;

	if (str.indexOf('.') == -1)
		str = str + ".0";

	if (str.indexOf('.') == (str.length - 1))
		str = str + "0";

	if ((nPoints == 1) && (numberString.length < str.length))
	{
		for (i = numberString.length; i < str.length; i++)
			numberString = numberString + "0";
	}

	if (numberString == str)
		return true;
	else
		return false;
}

// This function tests if the field has a valid value.
function CheckFloatField(form, field, min, max, error, sep, decimals)
{
 	var	value = form.elements[field].value;
	var	newValue = value;

	// Replace the separator by a dot (for IsFloat function)
	if (sep != '.')
		newValue = newValue.replace(sep, '.');

	// Check if the value is a float
	var result = IsFloat(newValue);

 	// Check if the value is <= max and >= min
 	if (((!result) && (newValue != ''))
		|| (parseFloat(newValue) < min)
		|| (parseFloat(newValue) > max))
 	{
 		var message = ReplaceString(error, "%1", value);
		alert(message);

		// Reset field
 		form.elements[field].value = form.elements[field].defaultValue;
 	}

	// Check the number of decimals
	var valueChecked;

	if (decimals > 0)
		valueChecked = CheckDecimals(newValue, decimals);

	if (valueChecked != null)
	{
 		var message = ReplaceString(error, "%1", value);
		alert(message);

		if (sep != '.')
		{
			if (netscapeNavigation)
				valueChecked = replaceNetscape(valueChecked,'.', sep);
			else
				valueChecked = valueChecked.replace('.', sep);	
		}

		// Reset field
 		form.elements[field].value = valueChecked;
	}
}

// This function replace a char by an another char for netscape browser
// FFT00892
function replaceNetscape(initVal, sep1, sep2)
{
	var	pos1 = initVal.indexOf(sep1);
	initVal = initVal.substring(0, pos1) + sep2 + initVal.substring(pos1 +1);
	return initVal;
}

// This function tests the number of decimals of the value.
function CheckDecimals(value, decimals)
{
	if ((decimals <= 0) || (value == ''))
		return null;

	var	pos = value.indexOf('.');

	if (pos < 0)
		return null;

	if (value.length - pos - 1 > decimals)
		return value.substring(0, pos + decimals + 1);

	return null;
}

// This function increments the value of the field.
function IncrementFloatField(form, field, min, max, pitch, sep, check)
{
	var value = form.elements[field].value;
	var newValue = value;

	// Replace the separator by a dot (for IsFloat function)
	if (sep != '.')
		newValue = newValue.replace(sep,'.');

	// Check if the value is a float
	var result = IsFloat(newValue);


 	if (value == "") // case of an empty value
	{
		if ((min <= 0) && (max >= 0))
			newValue = "0";
		else if (min > 0)
			newValue = min.toString();
		else
			newValue = max.toString();
	}
 	else if ((!result) || (parseFloat(newValue) < min - pitch))
 		newValue = min.toString();
 	else if ((parseFloat(newValue) > (max - pitch)))
 		newValue = max.toString();
 	else
 	{
 		pitch *= ACCURACY;
 		var fval = parseFloat(newValue);
 		fval *= ACCURACY;

 		fval += parseFloat(pitch);

 		fval = fval / ACCURACY;

 		newValue = fval.toString();
 	}

	if (sep != '.')
	{
		if (navigator.appName == "Netscape")
			newValue = newValue.replace('\\.', sep);
		else
			newValue = newValue.replace('.', sep);
	}

	form.elements[field].value = newValue;

	if (check != "false")
	{
		var fieldInst = form.elements[field];
		FieldChange(form, fieldInst, check);
	}
}
