//------------------------------------------				//
//         Smart Strings ver. 0.2.2					//
//  This is module with useful javascript		//
//      functions for working with					//
//               Strings.										//
// You can use it with no warranty as part  //
//   of your site. It can not be used for		//
//        commercial goals alone.					//
//      You can not change the script.			//
//       Author: Alexey Chekulaev					//
//       E-mail: smartov@gmail.com				//
//------------------------------------------				//

//strpos:
//Find position of first occurrence of a string
//Returns the numeric position of the first 
//occurrence of needle in the haystack string
function strpos(haystack, needle)
{
	var i;
	for (i=0;i<=(haystack.length - needle.length);i++)
	{
		if (haystack.substr(i, needle.length) == needle)
			return i;
			
	}
	return -1;
}

//strdelete
//Deletes a 'count' of characters of 'string'
//starting from 'start' point
function strDelete(string, start, count)
{
	if (string=='' | count==0)
		return string;
		
	return string.substring(0, start-1) + string.substring(start+count, string.length);
}

//trim
//Strip whitespace from the beginning and end of a string 
function trim(string)
{
	result = string;
	re = /^\s*/g
	result = result.replace(re, '');
	re = /\s*$/g
	result = result.replace(re, '');
	return result;
}

//explode:
//Split a string by string
//Returns an array of strings, each of which is a substring
//of string formed by splitting it on boundaries formed by the string separator
function explode(separator, string)
{
	var j, tmp;

	if (separator==null)
		return;

	var result = new Array();

	if (string=='' | string==null)
		return result;

	if (strpos(string, separator)<0) {
		result[0]=string;
		return result;
	}

	//-- errors handled
	tmp = string;
	j=0;
	
	while (strpos(tmp, separator)>-1) {
		result[j] = tmp.substring(0, strpos(tmp, separator));
		tmp = strDelete(tmp, 0, strpos(tmp, separator) + separator.length);
		j++;
	}

	if (tmp!='')
		result[j] = tmp;

	return result;
}

//implode:
//Returns a string containing a string representation of all the array
//elements in the same order, with the glue string between each element.
//Note: there is join() of String object in JS that makes the same
function implode (glue, pieces)
{
	var i;
	var result = '';
	for (i=0;i<pieces.length;i++) 
	{
		if (i == pieces.length-1)
		{
			result = result + pieces[i];
			break;
		}
		result = result + pieces[i] + glue;
	}
	return result;
}
