//	strings

//	uses utilities;

function instr(str, search)
{
	return 1 + str.indexOf(search);
};

function mid(str, pos, nb)
{
	if (nb != undefined)
		return str.substr(pos - 1, nb);
	else
		return str.substr(pos - 1);
};

function left(str, nb)
{
	return mid(str, 1, nb);
};

function stringDup(st, n)
{
	var tmp = "";
	for (var i = 1; i <= n; i ++)
		tmp += st;
	return tmp;
};

function deleteBlanks(s)
{
	var rs;
	var tmp = s;
	do
	{
		rs = tmp.indexOf(nbsp);
		if (rs != -1)
			tmp = tmp.substr(0, rs) + tmp.substr(rs + 6);
	} while (rs != -1);
	return stripWhitespace(tmp);
};

function stripCharsInBag (s, bag)
{
	var returnString = "";
	for (var i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	};
	return returnString;
};

function stripWhitespace (s)
{
	return stripCharsInBag (s, whitespace);
};

function charInString (c, s)
{
	for (i = 0; i < s.length; i++)
	{
		if (s.charAt(i) == c) return true;
	};
	return false;
};

function stripInitialWhitespace (s)
{
	var i = 0;
	while ((i < s.length) && charInString (s.charAt(i), whitespace)) i++;
	return s.substring (i, s.length);
};

function isEmpty(s)
{
	return ((s == null) || (s.length == 0));
};

function isOnlySpaces(s)
{
	if (isEmpty(s)) return true;
	for (var i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	};
	return true;
};

function isContainingSpaces(s)
{
	if (isEmpty(s)) return true;
	var fnd = false;
	for (var i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		fnd = ((fnd) || (whitespace.indexOf(c) != -1))
	};
	return fnd;
};

function isEmail (s)
{
	if (isContainingSpaces(s)) return false;
	var i = 0;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@")) i++;
	if ((i >= sLength) || (s.charAt(i) != "@"))
		return false;
	else
		i += 2;
	while ((i < sLength) && (s.charAt(i) != decSep)) i++;
	return ((i < sLength - 1) && (s.charAt(i) == decSep));
};

//	génération HTML dynamique

function doTag(tag, propertiesTag)
{
	//	fabricant HTML de balise ouvrante (propriétés facultatives) ou fermante
	var temp = tag;
	
	if (arguments.length == 1) 
		temp = endingChar + temp;
	else
		if (propertiesTag != undefined) temp += propertiesTag;
	return openingChar + temp + closingChar;
};

function doProp(name, value)
{
	//	fabricant HTML de propriété de balise
	return " " + name + "=" + char34 + value + char34;
};

function doSeq(tag, text, propertiesTag)
{
	//	fabricant HTML de séquence balise ouvrante (propriétés facultatives) + texte + balise fermante
	return doTag(tag, propertiesTag) + text + doTag(tag);
};

//	formats

function format(number, nbDecimals)
{
	var init = nbDecimals;
	var s = number.toString();
	var intIndex = s.indexOf(decSep);
	if (intIndex == -1)
	{
		var tmp = s;
		if (nbDecimals != 0)
			tmp += decSep + stringDup("0", nbDecimals);
		return tmp;
	}
	else
	{
		var temp = "";
		var i = intIndex + 1;
		while ((i <= s.length - 1) && (nbDecimals != 0))
		{
			temp += s.substring(i, i + 1);
			i ++;
			nbDecimals --;
		};
		while (nbDecimals > 0)
		{
			temp += "0";
			nbDecimals --;
		};
		var tmp = s.substring(0, intIndex);
		if (init != 0)
			tmp += decSep + temp ;
		return tmp;
	};
};

function toInt(v)
{
	if (v == undefined)
		return 0;
	else
		if (v == NaN)
			return 0;
		else
			if (v == "")
				return 0;
			else
				return parseInt(v);
};

function numeric(s)
{
	if ((s == "") || (s == nbsp))
		return 0;
	else
		return parseFloat(s);
};

function warnEmpty (theField, s)
{
	theField.focus();
	alert(mPrefix + s + mSuffix);
	return false;
};

function warnInvalid (theField, s)
{
	theField.focus();
	theField.select();
	alert(s);
	return false;
};

function checkString (theField, s)
{   
	if (isOnlySpaces(theField.value))
		return warnEmpty(theField, s);
	else
		return true;
};

function checkEmail (theField, isMandatory)
{   
	if (isMandatory == undefined) isMandatory = true;
	if ((!isMandatory) && (theField.value == "")) return true;
	if (isEmail(theField.value)) return true;
	warnInvalid (theField, iEmail);
	return false;
};

function getListBox(start, stop, step, props, defaultValue)
{
	var htmlSrc = "";
	for (var i = start; i <= stop; i += step)
	{
		var selected = "";
		if (i == defaultValue) selected = doProp("selected", "1");
		htmlSrc += doSeq("option", i, doProp("value", i) + selected)
	}
	return doSeq("select", htmlSrc, props)
}

function getIndexedListBoxTab(props, tab, defaultValue)
{
	var htmlSrc = "";
	for (var i = 0; i <= tab.length - 1; i += 1)
	{
		var selected = "";
		if (i == defaultValue) selected = doProp("selected", "1");
		htmlSrc += doSeq("option", tab[i], doProp("value", i) + selected);
	}
	return doSeq("select", htmlSrc, props)
}

function getListBoxTab(props, tab, defaultValue)
{
	var htmlSrc = "";
	for (var i = 0; i <= tab.length - 1; i += 1)
	{
		var selected = "";
		if (tab[i] == defaultValue) selected = doProp("selected", "1");
		htmlSrc += doSeq("option", tab[i], doProp("value", tab[i]) + selected);
	}
	return doSeq("select", htmlSrc, props)
}
