// 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 "";

}


	