/**
 * Count the number of occurances of a character in a string in Javascript
 */
String.prototype.count=function(s1) { 
    return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length;
}

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

var Utility = {
		
	/*
	* 
	* checks if a value is undefined 
	* 
	* 
	* 
	*/
		
	is_undefined: function (value) {
		var undefined_check; // instantiate a new variable which gets initialized to the real undefined value
		return value === undefined_check;
	},
		
	/**
	*
	*	Linear Interpolation
	*	Takes a normalised value and returns the actual value
	*	for the given range
	*	@param {value} - the normalised value
	*	@param {range_min} - the range minimum
	*	@param {range_max} - the range maximum
	*	@return {normal_value}
	*/
	
	linear_interpolate: function (value, range_min, range_max){
		return range_min + (range_max - range_min) * value;
	},
	
	/**
	*  	Normalise
	*	Takes a value, and its minimum and maximum possible values
	* 	and returns its decimal of the range
	*	@param {number}	The value to be normalised
	*	@param {min}	The minimum allowable value
	*	@param {max}	The maximum allowable value
	*	@return {interpolated_value}
	*/
	
	normalise: function (number, min, max) {
		return (number - min) / (max - min);
	},
	
	/**
	 * Get a random number
	 * @return a random number
	 */
	
	get_random_number: function () {
		var random_generated = Math.floor((Math.random()*99999)+10000);
		if(typeof random_generated == "string") {
			return Number(random_generated);
		} else {
			return random_generated;
		}
	},
	
	shortMonth: function (num) {
		var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
		return months[num+1];
	},
	
	timeConverter: function (timestamp){
		 var a = new Date(timestamp);
		 var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
		 var year = a.getFullYear();
		 var month = months[a.getMonth()];
		 var date = a.getDate();
		 var hour = a.getHours();
		 if (hour < 10) {
			 hour = "0" + hour;
		 }
		 
		 var min = a.getMinutes();
		 
		 if (min < 10) {
			 min = "0" + min;
		 }
		 
		 var sec = a.getSeconds();
		 if (sec < 10) {
			 sec = "0" + sec;
		 }
		 var time = date+' '+month+' '+year+' @ '+hour+':'+min ;
		 return time;
	},
	
	getBaseURL: function () {
	    var url = location.href;  // entire url including querystring - also: window.location.href;
	    var baseURL = url.substring(0, url.indexOf('/', 14));


	    if (baseURL.indexOf('http://localhost') != -1) {
	        // Base Url for localhost
	        var url = location.href;  // window.location.href;
	        var pathname = location.pathname;  // window.location.pathname;
	        var index1 = url.indexOf(pathname);
	        var index2 = url.indexOf("/", index1 + 1);
	        var baseLocalUrl = url.substr(0, index2);

	        return baseLocalUrl + "/";
	    }
	    else {
	        // Root Url for domain name
	        return baseURL + "/";
	    }

	},
	
	/**
	 * Turns special characters into HTML entities
	 * 
	 * @param {str} - 
	 * @returns {String} - The new string
	 */
	
	 htmlEntities: function (str) {
		return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
	},

	/**
	 * Sanitizes a string by putting <wbr></wbr> every fifteen characters
	 * 
	 * @param {orig} - the original string
	 * @returns {String}
	 */
	
	
	sanitize: function (orig) {
		orig = this.htmlEntities(orig);
		var times = (orig.length/15);
		var modified = "";
		for (var i = 0; i < (times + 1); i++) {
			modified += orig.substr(i*15, 15);
			modified += '<wbr></wbr>';
		}

		return modified;
	},
	
	/**
	 * Console.log
	 * 
	 * @param {obj} - object to be printed on console.log
	 */
	log: function (obj) {
		console.log(obj);
	},
	
	
	/**
	 * Convert number of seconds into time object
	 *
	 * @param integer secs Number of seconds to convert
	 * @return object
	 */
	secondsToTime: function (secs) {
	    var hours = Math.floor(secs / (60 * 60));
	   
	    var divisor_for_minutes = secs % (60 * 60);
	    var minutes = Math.floor(divisor_for_minutes / 60);
	 
	    var divisor_for_seconds = divisor_for_minutes % 60;
	    var seconds = Math.ceil(divisor_for_seconds);

	    if (hours < 10)		{ hours = "0" + hours; }
	    if (minutes < 10)	{ minutes = "0" + minutes; }
	    if (seconds < 10)	{ seconds = "0" + seconds; }
	    if (hours)			{ hr = "00"; }
	  
	    var obj = {
	        "h": hours.toString(),
	        "m": minutes.toString(),
	        "s": seconds.toString()
	    };
	    return obj;
	},
	
	/**
	 * Insert commas at every thousand
	 * @param interger or String
	 * @return String
	 */
	thousandSeparator: function(num){
		//console.log(typeof num);
		
		if(num != undefined){
			var result = num.toString();
			
			result = result.split(""); // convert string into array
			result.reverse();
			//console.log(result);
			    
			var result_length = result.length;
			var iterator = 0;
			
			for(var i=3; i<result_length; i+=3){
				//console.log(i);
				result.splice((i+iterator), 0, ",");
				iterator++;
			}
			
			result.reverse();
			return result.join('');
		} else {
			return false;
		}
	}


}
