﻿String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

// Check for isDate formatted mm/dd/yyyy
String.prototype.isDate = function() {
    var b_return = false;
    var re = new RegExp(/(0[1-9]|1[012])[ /](0[1-9]|[12][0-9]|3[01])[ /](19|20)\d\d/);
    // check match
    if (this.match(re))
        b_return = true;

    return b_return;
}

// Check for isTime formatted ##:## am
String.prototype.isTime = function() {
    var b_return = false;
    var re = new RegExp(/^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$/);
    // check match
    if (this.match(re))
        b_return = true;
    return b_return;
}

/************************************************************************************
// Function:   formatdecimal
// Purpose:    Set commas to display numeric values (1234 = 1,234)
************************************************************************************/
String.prototype.formatdecimal = function() {   
   x = this.split('.');
   x1 = x[0];
   x2 = x.length > 1 ? '.' + x[1] : '';
   var rgx = /(\d+)(\d{3})/;
   while (rgx.test(x1)) {
      x1 = x1.replace(rgx, '$1' + ',' + '$2');
   }
   return x1 + x2;
}
/************************************************************************************
// Function:   formatCurrency
// Purpose:    Set commas and decimal to display currency values (1234 = $1,234.00)
************************************************************************************/
String.prototype.formatCurrency = function() {
   var num = this.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
      num = "0";
   sign = (num == (num = Math.abs(num)));
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100).toString();
   if(cents<10)
      cents = "0" + cents;
   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
      
   return (((sign)?'':'-') + '$' + num + '.' + cents);
}

// Check for isEmail
String.prototype.isEmail = function() {
    var b_return = false;
    var re = new RegExp(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,10}|[0-9]{1,3})(\]?)$/);
    // check match
    if (this.match(re))
        b_return = true;

    return b_return;
}

// Check for isEmailList
String.prototype.isEmailList = function(delimiter) {
    var b_return = true;
    var i=0;
    var value = this.replace(' ', '');
    
    // Remove ending delimiter
    if (value.toString().endsWith(delimiter))
        value = value.toString().substr(0, value.length-1);
    
    // declare array and loop through email addresses
    var tarray = value.split(delimiter);
    for (i=0; i<tarray.length; i++)
    {   // if not email set return false
        if (!tarray[i].isEmail())
            b_return = false;
    }
    return b_return;
}

// Check for isNumeric
String.prototype.isNumeric = function() {
    return parseFloat(this)==this;
}

String.prototype.format = function() {
    var s = this, i = arguments.length;
    while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); }
    return s;
}

Date.prototype.defaultDateFormat=function(){
  var dd=this.getDate();
  if(dd<10)dd='0'+dd;
  var mm=this.getMonth()+1;
  if(mm<10)mm='0'+mm;
  var yyyy=this.getFullYear();
  return String(mm+"\/"+dd+"\/"+yyyy)
}
