//read url query string using mootools
/*
Function: $get
This function provides access to the "get" variable scope + the element anchor

Version: 1.3 (c)webfreak.no

Arguments:
key - string; optional; the parameter key to search for in the url’s query string (can also be “#" for the element anchor)
url - url; optional; the url to check for “key" in, location.href is default

Example:
>$get("foo","http://example.com/?foo=bar"); //returns “bar"
>$get("foo"); //returns the value of the “foo" variable if it’s present in the current url(location.href)
>$get("#","http://example.com/#moo"); //returns “moo"
>$get("#"); //returns the element anchor if any, but from the current url (location.href)
>$get(,"http://example.com/?foo=bar&bar=foo"); //returns {foo:’bar’,bar:’foo’}
>$get(,"http://example.com/?foo=bar&bar=foo#moo"); //returns {foo:’bar’,bar:’foo’,hash:’moo’}
>$get(); //returns same as above, but from the current url (location.href)
>$get("?"); //returns the query string (without ? and element anchor) from the current url (location.href)
*/

function $get(key,url){
	if(arguments.length < 2) url =location.href;
	if(arguments.length > 0 && key != ''){
		if(key == '#'){
			var regex = new RegExp("[#]([^$]*)");
		} else if(key == "?"){
			var regex = new RegExp("[?]([^#$]*)");
		} else {
			var regex = new RegExp("[?&]"+key+"=([^&#]*)");
		}
		var results = regex.exec(url);
		return (results == null )? "" : results[1];
	} else {
		url = url.split("?");
		var results = {};
		if(url.length > 1){
			url = url[1].split("#");
			if(url.length > 1) results["hash"] = url[1];
			url[0].split("&").each(function(item,index){
				item = item.split("=");
				results[item[0]] = item[1];
			});
		}
		return results;
	}
}


//return a formatted price
function formatCurrency(price, currencyFormat, currencySign){
	//if you modified this function, don't forget to modify the PHP function displayPrice (in the Tools.php class)
	var numberOfDecimal = 2;
	if(currencyFormat == 1)
		return currencySign + formatNumber(price,numberOfDecimal,',', '.');
	if(currencyFormat == 2)
		return (formatNumber(price,numberOfDecimal,' ',',')) + ' ' + currencySign;
}

//return a formatted number
function formatNumber(value,numberOfDecimal,thousenSeparator, virgule) {
	value = value.toFixed(numberOfDecimal);
	var val_string = value+'';
	var tmp = val_string.split('.');
	var abs_val_string = (tmp.length == 2) ? tmp[0] : val_string;
	var deci_string = ('0.' + (tmp.length == 2 ? tmp[1] : 0)).substr(2);
	var nb = abs_val_string.length;
	for (var i=1;i<4;i++)
		if (value>=Math.pow(10,(3*i)))
			abs_val_string=abs_val_string.substring(0,nb-(3*i))+thousenSeparator+abs_val_string.substring(nb-(3*i));
	return abs_val_string + (deci_string > 0 ? virgule+deci_string : '');
}

//show a JS debug
function dbg(value){
	var active = false;//true for active
	var firefox = true;//true if debug under firefox

	if (active)
		if (firefox)
			console.log(value);
		else
			alert(value);	
}

/**
* Function : print_r()
* Arguments: The data  - array,hash(associative array),object
*            The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function print_r(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

//verify if value is in the array
function in_array(value, array)
{
	for (var i in array) if (array[i] == value) return true;
	return false;
}
