// JavaScript Document


jQuery.toNum = function(str) {
		// hackety hack hack
		str = str + " " ;
		
		str = str.replace(/[^\d\.-]/g, "");
		str-=1;
		str+=1;
		
		return str || 0;
};


jQuery.dp_old= function(val) {
	var val = jQuery.toNum(val || 0);
	var decimalPlaces = arguments[1] || 2;
	var debug = arguments[2] || 0;
	
	if (debug) {
		alert("dp " + val);
	}
	
	val = jQuery._round(val, decimalPlaces);	// actually get the right value
	val += ''; // make us a string;
//	alert("val:" + val + "    decimal:"+decimalPlaces);
	
	if (decimalPlaces > 0) {	
		if (val.indexOf('.') == -1) {val = val + "."};	// add a dot if there isn;t one, so we don't get stuck in a loop
		var count=0;
		while ((count < 10) && val.charAt(val.length - decimalPlaces -1) != '.') {
			val = val + "0";
			count++;
		}
	}
	return val;	
}
	
jQuery._round_old= function(n, places) {
	var factor = Math.pow(10, (places || 0));
	return parseInt((n * factor) + (n < 0 ? -1 : 1) * 0.5) / factor;
}




jQuery.dp = function(val) {
	var val = (val || 0) + '';
	var decimalPlaces = arguments[1];
	var debug = arguments[2] || 0;
	var recursing = arguments[3] || false;
	

	if (decimalPlaces == null) {
		decimalPlaces=2;	
	}

	
	if (debug) {
		console.log("calculating " + val + " to " + decimalPlaces + " dp");
	}
	
	val = jQuery._round(val, decimalPlaces, debug);	// actually get the right value
	
	if (debug) {
		console.log("rounded:" + val);	
	}
	val += ''; // make us a string;

	
	if (decimalPlaces > 0) {	
		if (val.indexOf('.') == -1) {val = val + "."};	// add a dot if there isn;t one, so we don't get stuck in a loop
		var count=0;
		while ((count < 10) && val.charAt(val.length - decimalPlaces -1) != '.') {
			val = val + "0";
			count++;
		}
	}

	return val;	
},
	
jQuery._round = function(n, places, debug) {
	// n is a string, so we can process it without letting JS mess up the rounding
	// move the decimal point places, places to the right
	
	var factor = Math.pow(10, places);	// we use this at the end
	n.match(/([\-\d]*).?(\d*)?/);
	if (debug) {
		console.log("int: " + RegExp.$1 + " decimal: " + RegExp.$2);
	}
	
	var decimals = RegExp.$2 || "";	// the original decimal places
	var integers = RegExp.$1 || "";	// the original integer part
	while (decimals.length < places) {	// add enough zeros to make our decimals a long enough string to move the decimal point 'places' to the right
		decimals = decimals + "0";
	}
	
	if (debug) {
		console.log("new decimals: " + decimals)
		console.log("integers: " + integers)
	}
	
	decimals = decimals.substr(0, places) + "." + decimals.substr(places, decimals.length); // insert a new decimal point places into the decimals numbers.

	if (debug) {
		console.log(integers + decimals);
	}
	
	var factored = integers + decimals;	// in theiry the same as Math.pow(n, factor), but it's not - try 0.285 as an example...
	
	var half = (n < 0 ? -0.5 : 0.5);		// add or take away 0.5

	huge = jQuery.toNum(factored) + half;	// we have to make it into a number to do the maths bit.
	
	if (debug) {
		console.log("half:" + half);
		console.log("huge:" + huge);
		console.log("added:" + huge);
	}
	
	return parseInt(huge) / factor;		// take the integer part of the result after our adding, divide it by the factor that we should have used in the first place
},



jQuery.sum = function(array) {
	var total = 0;
	$(array).each(
		function() {
			total += jQuery.toNum(this);
		}
	)
	return total;
},

jQuery.urlParam = function(name) {
	var query = location.search;
	query=query.replace(/^\?/,"");
	var pairs = query.split("&");
	for (var i =0; i < pairs.length; i++) {
		var bits = pairs[i].split("=");
		if (bits[0] == name) {
			return unescape(bits[1]);
		}
	}
	return "";

}




jQuery.getParams = function(params) {	// Turns a url into an array of params
		// Adjust
		params = String(params);
		// Remove url if need be
		params = params.substring(params.indexOf('?')+1);
		// params = params.substring(params.indexOf('#')+1);
		// Change + to %20, the %20 is fixed up later with the decode
		params = params.replace(/\+/g, '%20');
		// Do we have JSON string
	
		// We have a params string
		params = params.split(/\&|\&amp\;/);
		var json = {};
		// We have params
		for ( var i = 0, n = params.length; i < n; ++i )
		{
			// Adjust
			var param = params[i] || null;
			if ( param === null ) { continue; }
			param = param.split('=');
			if ( param === null ) { continue; }
			// ^ We now have "var=blah" into ["var","blah"]
			
			// Get
			var key = param[0] || null;
			if ( key === null ) { continue; }
			if ( typeof param[1] === 'undefined' ) { continue; }
			var value = param[1];
			// ^ We now have the parts
			
			// Fix
			key = decodeURIComponent(key);
			value = decodeURIComponent(value);
			try {
				// value can be converted
				value = eval(value);
			} catch ( e ) {
				// value is a normal string
			}
			
			// Set
			// console.log({'key':key,'value':value}, split);
			var keys = key.split('.');
			if ( keys.length === 1 )
			{	// Simple
				json[key] = value;
			}
			else
			{	// Advanced
				var path = '';
				for ( ii in keys )
				{	//
					key = keys[ii];
					path += '.'+key;
					eval('json'+path+' = json'+path+' || {}');
				}
				eval('json'+path+' = value');
			}
			// ^ We now have the parts added to your JSON object
		}
		return json;
	
}

	
