/*

Various JavaScript enhancements. Bugfixes and missing feature implementations.

*/

// Current browsers do not implement Object.extend "correctly".
// Here's a simple workaround until they do.
Object.extend = function(newtarget, source)
{
	for (var property in source)
	{
		if (source.hasOwnProperty(property) && !newtarget.hasOwnProperty(property))
		{
			newtarget[property] = source[property];
		}
	}
	
	return newtarget;
}

// create a URL from a base path and an array of parameters
createUrl = function(url, params)
{
	var urlvars = createUrlVars(params);
	
	if (urlvars.length > 0)
	{
		url += "&" + urlvars;
	}
	
	// first separator is a ? and not an &
	if (url.indexOf("?") == -1)
	{
		url = url.replace(/&/, "?");
	}
	
	return url;
}

createUrlVars = function(params)
{
	var urldata = "";
	
	for (var parameter in params)
	{
		urldata += "&" + encodeURIComponent(parameter) + "=" + encodeURIComponent(params[parameter]);
	}
	
	// first & should not be there
	if (urldata.indexOf("&") != -1)
	{
		urldata = urldata.replace(/&/, "");
	}
	
	return urldata;
}

// allow functions to be bound to an object
Object.extend(Function.prototype,
{
	bind: function()
	{
		var handler = this, args = [].slice.call(arguments, 0), object = args.shift();
		
		return function()
		{
			return handler.apply(object, args.concat([].slice.call(arguments, 0)));
		}
	}
});


String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, "");
}

String.prototype.ltrim = function()
{
	return this.replace(/^\s+/, "");
}

String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}

String.prototype.ucFirst = function()
{
	return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
}

String.prototype.nl2br = function()
{
	return this.replace(/([^>]?)\n/g, "$1<br />\n");
}

String.prototype.htmlspecialchars = function()
{
	var replaces = new Array();
	// eerst deze, anders krijg je dubbele escape escapades
	replaces['38'] = "&amp;";
	replaces['34'] = "&quot;";
	replaces['39'] = "&#039;";
	replaces['60'] = "&lt;";
	replaces['62'] = "&gt;";
	
	var string = this;
	
	for (var i in replaces)
	{
		var entity = replaces[i];
		var char = String.fromCharCode(i);
		
		string = string.split(char).join(entity);
	}
	
	return string;
}

String.prototype.addslashes = function()
{
	var string = this;
	
	string=string.replace(/\\/g,'\\\\');
	string=string.replace(/\'/g,'\\\'');
	string=string.replace(/\"/g,'\\"'); // ' // syntax highlight fix
	string=string.replace(/\0/g,'\\0');
	
	return string;
}

String.prototype.stripslashes = function()
{
	var string = this;
	
	string=string.replace(/\\'/g,'\'');
	string=string.replace(/\\"/g,'"'); // ' // syntax highlight fix
	string=string.replace(/\\0/g,'\0');
	string=string.replace(/\\\\/g,'\\');
	
	return string;
}

Array.prototype.in_array = function (needle)
{
	var length = this.length;
	
	for (var i = 0; i <= length; i++)
	{
		if (this[i] == needle)
		{
			return true;
		}
	}
	return false;
}

// as the name suggests, get elements by selector (id, name, class etc.)
function getElementsBySelector(selector)
{
	var i;
	var s = [];
	var selid = "";
	var selclass = "";
	var tag = selector;
	var objlist = [];

	if (selector.indexOf(" ") > 0)
	{
		// descendant selector like "tag#id tag"
	    s = selector.split(" ");
	    var fs = s[0].split("#");
	   
	    if (fs.length == 1)
		{
			return(objlist);
		}
	    
		return(document.getElementById(fs[1]).getElementsByTagName(s[1]));
    }

	if (selector.indexOf("#") > 0)
	{
		// id selector like "tag#id"
	    s = selector.split("#");
	    tag = s[0];
	    selid = s[1];
    }
	if (selid != "")
	{
	    objlist.push(document.getElementById(selid));
	    return(objlist);
    }
	
	if (selector.indexOf(".") > 0)
	{
		// class selector like "tag.class"
	    s = selector.split(".");
	    tag = s[0];
    	selclass = s[1];
    }
	
	var v = document.getElementsByTagName(tag);  // tag selector like "tag"
	
	if (selclass == "")
	{
		return(v);
	}
	
	for (i = 0; i < v.length; i++)
	{
		var classes = v[i].className.split(" ");
		
		if (classes.in_array(selclass))
		{
			objlist.push(v[i]);
		}
    }
	
	return(objlist);
}

function isIE()
{
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
									
function parseMoney(value)
{
	value = value.toString();
	value = value.replace(/,/, ".");
	value = parseFloat(value);

	// not a number?
	if (isNaN(value))
	{
		return value;
	}
	
	value = Math.round(value * 100) / 100;
	
	value = value.toString();

	if (value.charAt(value.length - 2) == ".")
	{
		value += "0";
	} else if (value.charAt(value.length - 3) != ".")
	{
		value += ".00";
	}
	
	return value;
}

function get_cookie(name)
{
	var cookies = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
	
	if (cookies)
	{
		return unescape(cookies[2]);
	} else
	{
		return false;
	}
}

function set_cookie(name, value, expiry, path, domain, secure)
{
	var cookie = name + "=" + escape(value);
	
	if (expiry)
	{
		cookie += "; expires=" + expiry.toUTCString();
	}
	if (path)
	{
		cookie += "; path=" + escape(path);
	}
	if (domain)
	{
		cookie += "; domain=" + escape(domain);
	}
	if (secure)
	{
		cookie += "; secure";
	}

	document.cookie = cookie;
}

function delete_cookie(name)
{
	var cookie = new Date();
	cookie.setTime(cookie.getTime() - 1);
	document.cookie = name + "=; expires=" + cookie.toGMTString();
}
