// #############################################################################
// Emulate PHP's function for Javascript
//
// Useage: $PHP.function(...)
//
// function list:
// stripos(haystack, needle, offset)
// trim(str)
// htmlspecialchars(str)
// strpad(text, length, padstring)
// urlencode(str)
// urldecode(str)
// ucfirst(str, cutoff)
// in_array(needle, haystack, strict)
// rand(min, max)
// str_replace(search, replace, subject)
// #############################################################################

/**
* PHP Function Emulator Class
*/
function xPHP()
{
}

// =============================================================================
// xPHP Methods

xPHP.prototype.stripos = function(haystack, needle, offset)
{
	if (typeof offset == 'undefined')
	{
		offset = 0;
	}

	index = haystack.toLowerCase().indexOf(needle.toLowerCase(), offset);

	return (index == -1 ? false : index);
}

xPHP.prototype.trim = function(str)
{	
	return str.replace(/^\s+/g, '').replace(/(\s+)$/g, '');
}

xPHP.prototype.htmlspecialchars = function(str)
{
	//var f = new Array(/&(?!#[0-9]+;)/g, /</g, />/g, /"/g);
	var f = new Array(
		((navigator.userAgent.toLowerCase().indexOf('mac') != -1) && ((navigator.userAgent.toLowerCase().indexOf('msie') != -1) && (!((navigator.userAgent.toLowerCase().indexOf('opera') != -1) || (typeof(window.opera) != 'undefined'))) && (!is_saf) && (!(navigator.userAgent.toLowerCase().indexOf('webtv') != -1))) ? new RegExp('&', 'g') : new RegExp('&(?!#[0-9]+;)', 'g')),
		new RegExp('<', 'g'),
		new RegExp('>', 'g'),
		new RegExp('"', 'g')
	);
	var r = new Array(
		'&amp;',
		'&lt;',
		'&gt;',
		'&quot;'
	);

	for (var i = 0; i < f.length; i++)
	{
		str = str.replace(f[i], r[i]);
	}

	return str;
}

xPHP.prototype.strpad = function(text, length, padstring)
{
	text = new String(text);
	padstring = new String(padstring);

	if (text.length < length)
	{
		padtext = new String(padstring);

		while (padtext.length < (length - text.length))
		{
			padtext += padstring;
		}

		text = padtext.substr(0, (length - text.length)) + text;
	}

	return text;
}

xPHP.prototype.urlencode = function(text)
{
	return encodeURIComponent(text);
}


xPHP.prototype.urldecode = function(text)
{
	return decodeURIComponent(text);
}

xPHP.prototype.ucfirst = function(str, cutoff)
{
	if (typeof cutoff != 'undefined')
	{
		var cutpos = str.indexOf(cutoff);
		if (cutpos > 0)
		{
			str = str.substr(0, cutpos);
		}
	}

	str = str.split(' ');
	for (var i = 0; i < str.length; i++)
	{
		str[i] = str[i].substr(0, 1).toUpperCase() + str[i].substr(1);
	}
	return str.join(' ');
}

xPHP.prototype.in_array = function(needle, haystack, strict)
{
    var found = false, key, strict = !!strict;
 
    for (key in haystack)
    {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle))
        {
            found = true;
            break;
        }
    }
 
    return found;
}

xPHP.prototype.rand = function(min, max) {
	return Math.floor(Math.random() * (max - min + 1) + min);
}

xPHP.prototype.str_replace = function(search, replace, subject)
{
	return subject.replace(search, replace);
}

var $PHP = new xPHP();
