/* --------------------------------------------------- */
/* name:     j5display.js                              */
/* author:   J5 Development                            */
/* purpose:  javascript display functions              */
/* --------------------------------------------------- */

/* --------------------------------------------------- */
/* display current date                                */
/* --------------------------------------------------- */
function currentDate() {
  monthName = new Array("January","February","March","April","May","June",
                        "July","August","September","October","November","December");
  var today = new Date();
  document.write(monthName[today.getMonth()] + " " + today.getDate() + ", " + today.getFullYear());
}

/* --------------------------------------------------- */
/* display current month                               */
/* --------------------------------------------------- */
function currentMonth() {
  monthName = new Array("January","February","March","April","May","June",
                        "July","August","September","October","November","December");
  var today = new Date();
  document.write(monthName[today.getMonth()]);
}

/* --------------------------------------------------- */
/* display current year                                */
/* --------------------------------------------------- */
function currentYear() {
  var today = new Date();
  document.write(today.getFullYear());
}

/* --------------------------------------------------- */
/* display timestamp                                   */
/* --------------------------------------------------- */
function timeStamp() {
  dayName = new Array("SUN","MON","TUE","WED","THU","FRI","SAT");
  var now = new Date();
  var dateTime = dayName[now.getDay()] + " ";
  dateTime += ((now.getMonth() < 9) ? "0"  : "" ) + (now.getMonth() + 1) + "/";
  dateTime += ((now.getDate() < 10)    ? "0"  : "" ) + now.getDate() + "/";
  dateTime += now.getFullYear() + " ";
  dateTime += ((now.getHours() < 10)   ? "0"  : "" ) + now.getHours();
  dateTime += ((now.getMinutes() < 10) ? ":0" : ":") + now.getMinutes();
  dateTime += ((now.getSeconds() < 10) ? ":0" : ":") + now.getSeconds();
  document.write(dateTime);
}

/* --------------------------------------------------- */
/* set date field to today's date                      */
/* --------------------------------------------------- */
function setAsToday(dateForm, dateField) {
  today = new Date();
  theField = eval("document." + dateForm + "." + dateField);
  theField.value = formatUSDate(today);
}

/* --------------------------------------------------- */
/* adjust date field by specified increment            */
/* --------------------------------------------------- */
function adjustDate(dateForm, dateField, dateAdjustment) {
  theField = eval("document." + dateForm + "." + dateField);
  yyyy = parseInt(theField.value.substring(6,theField.value.length));
  mm = parseInt(theField.value.substring(0,2)-1);
  dd = parseInt(theField.value.substring(3,5));
  if (dd == 0)
  {
    dd = parseInt(theField.value.substring(4,5));
  }  
  theDate = new Date(yyyy,mm,dd);
  adjustment = theDate.getTime() + (1000*60*60*24*dateAdjustment);
  adjDate = new Date();
  adjDate.setTime(adjustment);
  theField.value = formatUSDate(adjDate);
}

/* --------------------------------------------------- */
/* capitalize field value                              */
/* --------------------------------------------------- */
function capitalize(theField) {
  theField.value = theField.value.toUpperCase();
}

/* --------------------------------------------------- */
/* format date as MM/DD/YYYY                           */
/* --------------------------------------------------- */
function formatUSDate(objDate) {
  var strDate;
  strDate = ((objDate.getMonth() < 9) ? "0"  : "" ) + (objDate.getMonth() + 1); 
  strDate += "/";
  strDate += ((objDate.getDate() < 10) ? "0"  : "" ) + objDate.getDate(); 
  strDate += "/";
  strDate += objDate.getFullYear();
  return strDate;
}

/* --------------------------------------------------- */
/* format currency                                     */
/* --------------------------------------------------- */
function formatCurrency(num) {
    num = num.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);
}

/* --------------------------------------------------- */
/* format phone as (123) 456-7890                      */
/* --------------------------------------------------- */
function formatPhone(theField) {
  var unformatted = theField.value;
  var formatted;
  if (validatePhone(unformatted)) {
    formatted = "(" + unformatted.substring(0,3) + ") "; 
    formatted += unformatted.substring(3,6) + "-" + unformatted.substring(6,10); 
  } else {
    formatted = unformatted;
  }
  theField.value = formatted;
}

/* --------------------------------------------------- */
/* remove formatting from a given field                */
/* --------------------------------------------------- */
function stripFormat(theField, stripChars) {
  var formatted = theField.value;
  var unformatted = "";
  for (var i=0; i < formatted.length; i++) {
    if (stripChars.indexOf(formatted.charAt(i)) == -1) {
      unformatted += formatted.charAt(i);
    }
  }
  theField.value = unformatted;
  theField.select();
}
