/* Check for, and if necessary, create the namespace - TRUKZ */
var TRUKZ = (typeof(TRUKZ) === "undefined") ? {} : TRUKZ;

/* Create a new child object to TRUKZ, access via TRUKZ.maps */
TRUKZ.utils = function () {
    
    /* Publicly Accessible */
    return {
    
        /**
        * Grabs a URL parameter value (like response.querystring in ASP) and allows a default value to be set.
        * @param {String} name    Key part of "?key=value" in a querystring.
        * @param {String} value   Default value returned if the key does not exist.
        * @return {String}        Returns a string value containing value assigned to key (or default value).
        */
        rq: function (name, val)
        {
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\?&]" + name + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(window.location.href);
            if (results === null) {
                return val;
            } else {
                return results[1];
            }
        },
        /**
        * Conversion from miles to kilometres
        * @param {number} miles   Number of miles
        * @return {number}        Equivalent numbr of kilometres
        */
        milesToKilometres: function (miles)
        {
            return 1.609344 * parseFloat(miles);
        },
        /**
        * Conversion from miles to kilometres
        * @param {number} miles   Number of miles
        * @return {number}        Equivalent numbr of kilometres
        */
        fToC: function (f)
        {
            return 5 * (f - 32)/9;
        },
        /**
        * Easily format a number
        * @param {number}   num the number to format
        * @param {number}   decimalNum the number of decimal places to format the number to
        * @param {boolean}  bolLeadingZero true / false - display a leading zero for	numbers between -1 and 1
        * @param {boolean}  bolParens true / false - use parenthesis around negative numbers
        * @param {boolean}  bolCommas true / false - put commas as number separators.
        * @example formatNumber(1234.56, 2, true, true, true) outputs 1,234.56
        * @return {number}  formatted number
        */
        formatNumber: function (num,decimalNum,bolLeadingZero,bolParens,bolCommas)
        { 
            if (isNaN(parseInt(num))) return "NaN";

	          var tmpNum = num;
	          var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	          // Adjust number so only the specified number of numbers after the decimal point are shown.
	          tmpNum *= Math.pow(10,decimalNum);
	          tmpNum = Math.round(Math.abs(tmpNum))
	          tmpNum /= Math.pow(10,decimalNum);
	          tmpNum *= iSign;					// Readjust for sign
	
	
	          // Create a string object to do our formatting on
	          var tmpNumStr = new String(tmpNum);

	          // See if we need to strip out the leading zero or not.
	          if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		        if (num > 0) {
			          tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		        } else {
			        tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
			      }
		
	          // See if we need to put in the commas
	          if (bolCommas && (num >= 1000 || num <= -1000)) {
		            var iStart = tmpNumStr.indexOf(".");
		            if (iStart < 0) {
			              iStart = tmpNumStr.length;
			          }
		            iStart -= 3;
		            while (iStart >= 1) {
			              tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			              iStart -= 3;
		            }		
	          }

	          // See if we need to use parenthesis
	          if (bolParens && num < 0) {
		            tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";
		        }
		        
		        // See if we need to pad the decimal places
		        iStart = tmpNumStr.indexOf(".");
		        var currentDecimals = tmpNumStr.length - iStart - 1;
		        if (currentDecimals < decimalNum) {
		            for (var i=0; i < decimalNum - currentDecimals; i++ ) {
		                tmpNumStr += "0";
		            }
		        }

	          return tmpNumStr;		// Return our formatted string!
          }
    };
}();

/* Simple fix for browsers with no console */
if (typeof console === "undefined") {
    var logsOutput = document.createElement('ul');
    //@dok this causes errors in IE 8 and IE 7.  Cannot append child before parent is closed!
    //document.getElementsByTagName('body')[0].appendChild(logsOutput);
    console = {
        log: function(msg) {
            //logsOutput.innerHTML += '<li>' + msg + '</li>';
        }
    };
}