// ****************************************************
// String functions
// ****************************************************

String.prototype.trim = function() {
	return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
};

String.prototype.titleCase = function()
{
   if(this.length == 0)
   {  return "";
   }
   
   var firstChar = this.substring(0, 1);
   var rest = this.substring(1, this.length);
   
   return firstChar.toUpperCase()+rest;

};

if (!String.prototype.startsWith) {
  String.prototype.startsWith = function(prefix) {
//    return (this.substr(0, prefix.length) == prefix);
    return (this.indexOf(prefix) === 0);
  };
}

if (!String.prototype.endsWith) {
  String.prototype.endsWith = function(suffix) {
    var startPos = this.length - suffix.length;
    if (startPos < 0) {
      return false;
    }
    return (this.lastIndexOf(suffix, startPos) == startPos);
  };
}

// BC 17 Apr 2009 - added the removeHTMLTags function so can strip out HTML from the tasklist cell data
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Robert Nyman | http://robertnyman.com/ */
function removeHTMLTags(strInputCode){

 		/* 
  			This line is optional, it replaces escaped brackets with real ones, 
  			i.e. < is replaced with < and > is replaced with >
 		*/	
 	 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){return (p1 == "lt")? "<" : ">";});
 		var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
 		return strTagStrippedText;	
   // Use the alert below if you want to show the input and the output text
   //		alert("Input code:\n" + strInputCode + "\n\nOutput text:\n" + strTagStrippedText);	
 	
}

function removeNonASCIICharacters(instr)
{
		// allow ASCII characters only
		var str = "";
		for(var i=0; i < instr.length; i++)
		{
			if( instr.charCodeAt(i) < 128 )
			{   str += instr.charAt(i);
			}
		}
		return str;
				                       
}

// ****************************************************
// Comparison functions
// ****************************************************


/* Compares two dates (in format dd/mm/yyyy)
 * Returns -1 if date1 is earlier than date2
 * Returns 0 if date1 is equal to date2
 * Returns 1 if date1 is later than date2
 */
function dateCompare(date1, date2)
{
   // split the date1 into day, month and year
   var date1Array = date1.split(/\//);
   var day1 = date1Array[0];
   var month1 = date1Array[1];
   var year1 = date1Array[2];  
   
   // split the date2 into day, month and year
   var date2Array = date2.split(/\//);
   var day2 = date2Array[0];
   var month2 = date2Array[1];
   var year2 = date2Array[2];  
   
   if(year1 > year2)
     return 1;
   else if(year1 < year2)
     return -1;
   
   if(month1 > month2)
     return 1;
   else if(month1 < month2)
     return -1;   
     
   if(day1 > day2)
     return 1;
   else if(day1 < day2)
     return -1; 
     
   return 0;  
}

/* Compares two times (in format hh:mm)
 * Returns -1 if time1 is earlier than time2
 * Returns 0 if time1 is equal to time2
 * Returns 1 if time1 is later than time2
 */
function compareTime(time1, time2)
{
   // split the time1 into hour, minute
   var time1Array = time1.split(":");
   var hour1 = time1Array[0];
   var minute1 = time1Array[1];
   
   var time2Array = time2.split(":");
   var hour2 = time2Array[0];
   var minute2 = time2Array[1];
   
   if(hour1 > hour2)
     return 1;
   else if(hour1 < hour2)
     return -1;
     
   if(minute1 > minute2)
     return 1;
   else if(minute1 < minute2)
     return -1; 
     
   return 0;    
}

// ****************************************************
// Array functions
// ****************************************************

function array_contains_value(v_array, v_value)
{
  for(i=0; i < v_array.length; i++)
  {   
      if(v_array[i] == v_value)
      { return true;
      }
  }
  
  return false;
}

// ****************************************************
// Numeric functions
// ****************************************************

function getRandomNumber() {
  var randomNumber=Math.floor(Math.random()*100000001);
  return randomNumber;
}

function getCurrentTimeMilliseconds() {
     var curDate = new Date();
	 return curDate.getTime(); 
}

/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string;
}



    