// LyStringScript.js
// Copyright (c) 2000-2009, Lyria-W4.
// All rights reserved.
// @version	$Id: LyStringScript.js,v 1.2 2009/01/06 11:04:03 jls Exp $

// This function replace a string in a text by another given string.
function ReplaceString(text, search, replace)
{
	if (text == null)
		return null;

	if (search == null)
		return text;

	var	result = "";
	var	i, j;
	var	length = text.length;
	var	sl = search.length;
	var	rl = 0;

	if (replace != null)
		rl = replace.length;

	for (i = 0, j = 0; (i < length) && ((j = text.indexOf(search, i)) >= 0); i = j + sl)
	{
		result = result + text.substring(i, j);

		if (rl > 0)
			result = result + replace;
	}

	if (i < length)
		result = result + text.substring(i, length);

	return result;
}

// This function tests if a text length is lower than the length value.
function isLongerThan(text, length)
{
	if (length < 0)
		return false;

	var textLength = text.length;

	if (textLength > length)
		return true;
	else
		return false;
}

function endsWith(source, pattern)
{
	n = pattern.length;
	m = source.length;

	temp = source.substring(m-n);

	if (temp == pattern)
		return true;
	else
		return false;
}

function StringToArray(str, sep)
{
	var words = new Array;
	var	end = false;

	while (str != "" && !end)
	{
		var index = 0;

		index = str.indexOf(sep);

		if (index < 0)
		{
			end = true;
			word = str;
		}
		else
			word = str.substring(0, index);

		words[words.length] = word;

		str = str.substring(index + sep.length, str.length);
	}

	return words;
}

// This function splits a string into an array using two separators
function SplitString(text, sep1, sep2)
{
	var scripts = new Array;
	var end = false;

	while (text != "" && !end)
	{
		var index1 = 0;
		var index2 = 0;

		index1 = text.indexOf(sep1);
		index2 = text.indexOf(sep2, index1);

		if ((index1 < 0) || (index2 < 0))
		{
			end = true;
			script = "";
		}
		else
			script = text.substring(index1 + sep1.length, index2);

		scripts[scripts.length] = script;

		text = text.substring(index2 + sep2.length, text.length);
	}
	
	return scripts;
}
