//
// Takes the passed in hour, minute, and am/pm meridian indicator and formats
// it into military time. Hour should be "00" to "12". Minute should be "00" to "59".
// Meridian should be "am" or "pm".
//
function formatTime(hour, minute, meridian) {
  var formatted_time = "";
  if (hour == '12' && meridian == 'am') {
    formatted_time = "00:" + minute;
  }
  else if (meridian == 'am') {
    formatted_time = hour + ":" + minute;
  }
  else if (hour == '12') {
    formatted_time = hour + ":" + minute;
  }
  else {
    var int_hour = parseInt(hour) + 12;
    formatted_time = int_hour + ":" + minute;
  }
  return formatted_time;
}

//
// Checks the specified hour/minute/meridian dropdowns to ensure that everything
// is filled out correctly. If something is missing then the fields are highlighted
// in yellow, otherwise reset to white if everything is okay. If all is filled in
// okay then function returns either the military-formatted time, empty string (if
// nothing was entered and is_required = false, or FALSE if nothing was entered and
// is_required = true.
//
function validateTimeSelectors(hour_field, min_field, meridian_field, is_required) {
  var hour = $j('#'+hour_field).val();
  var min = $j('#'+min_field).val();
  var meridian = $j('#'+meridian_field).val();

  if (hour !== '--' && min !== '--' && meridian !== '--') {
    $j('#'+hour_field).css("background-color", "white");
    $j('#'+min_field).css("background-color", "white");
    $j('#'+meridian_field).css("background-color", "white");
    return formatTime(hour, min, meridian);
  }
  else if (hour == '--' && min == '--' && meridian == '--') {
    if (is_required == true) {
      $j('#'+hour_field).css("background-color", "yellow");
      $j('#'+min_field).css("background-color", "yellow");
      $j('#'+meridian_field).css("background-color", "yellow");
      return false;
    }
    else {
      $j('#'+hour_field).css("background-color", "white");
      $j('#'+min_field).css("background-color", "white");
      $j('#'+meridian_field).css("background-color", "white");
      return '';
    }
  }
  else {
    if (hour == '--') { $j('#'+hour_field).css("background-color", "yellow"); } else { $j('#'+hour_field).css("background-color","white"); }
    if (min == '--') { $j('#'+min_field).css("background-color", "yellow"); } else { $j('#'+min_field).css("background-color","white"); }
    if (meridian == '--') { $j('#'+meridian_field).css("background-color", "yellow"); } else { $j('#'+meridian_field).css("background-color","white"); }
    return false;
  }
}

//
// Ensures that the date is in YYYY-MM-DD format. If it's required and it's either
// empty or doesn't pass the REGEX then boolean FALSE is returned, otherwise TRUE.
//
function validateDateFormat(date_field, is_required) {
  var my_date = $j('#'+date_field).val();
  if (is_required == true && my_date == '') {
    $j('#'+date_field).css("background-color", "yellow");
    return false;
  }
  else if (my_date !== '') {
    if (!my_date.match(/(19|20|21)[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])/)) {
      $j('#'+date_field).css("background-color", "yellow");
      return false;
    }
    else {
      $j('#'+date_field).css("background-color","white");
      return true;
    }
  }
  else {
    $j('#'+date_field).css("background-color","white");
    return true;
  }
}

//
// Ensures that the email is a validly formatted email address. If it's required and it's either
// empty or doesn't pass the REGEX then boolean FALSE is returned, otherwise TRUE.
//
function validateEmailFormat(email_field, is_required) {
  var email = $j('#'+email_field).val();
  if (is_required == true && email == '') {
    $j('#'+email_field).css("background-color", "yellow");
    return false;
  }
  else if (email !== '') {
    if (!email.match(/^\b[A-Z0-9._%+-]+@(?:[A-Z0-9\-]+\.)+[A-Z]{2,4}\b$/i)) {
      $j('#'+email_field).css("background-color", "yellow");
      return false;
    }
    else {
      $j('#'+email_field).css("background-color","white");
      return true;
    }
  }
  else {
    $j('#'+email_field).css("background-color","white");
    return true;
  }
}

//
// Ensures that the text field has content in it at least as long as min_length. If the content
// is non-existent or less than min_lengh characters then the field is highlighted and returns FALSE,
// otherwise the highligh is removed and returns TRUE.
//
function validateTextField(form_field_id, min_length, auto_highlight) {
  var field_value = $j('#'+form_field_id).val();
  if (field_value.length < min_length) {
    if (auto_highlight == true) {
      $j('#'+form_field_id).css("background-color", "yellow");
    }
    return false;
  }
  else {
    if (auto_highlight == true) {
      $j('#'+form_field_id).css("background-color", "white");
    }
    return true;
  }
}

//
// Ensures that the text field has content in it at least as long as min_length. If the content
// is non-existent or less than min_lengh characters then the field is highlighted and returns FALSE,
// otherwise the highligh is removed and returns TRUE.
//
function validateFCKEditor(form_field_id, min_length, auto_highlight) {
  var field_value = FCKeditorAPI.GetInstance(form_field_id).GetHTML();
  if (field_value.length < min_length) {
    if (auto_highlight == true) {
      $j('#'+form_field_id).parent().css("background-color", "yellow");
    }
    return false;
  }
  else {
    if (auto_highlight == true) {
      $j('#'+form_field_id).parent().css("background-color", "white");
    }
    return true;
  }
}

//
// Takes the value of a text input field, strips out any non-numeric characters, and then puts
// the resulting value back into the same text input field.
//
function stripNonDigits(form_field_id) {
  var field_value = $j('#'+form_field_id).val();
  field_value = field_value.replace(/[^0-9]/g, ''); //replace anything that's not a NUMBER character
  $j('#'+form_field_id).val(field_value);
}


//
// Strips all white space first, then ensures that the value contains only digits and a decimal
// sign. No other characters allowed or FALSE will be returned.
//
// TODO - at some point add a minimum and/or maximum value check.
//
function  validateAmount(form_field_id, auto_highlight) {
  var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  //Get rid of all white space first, then put value back into form input field
  var field_value = $j('#'+form_field_id).val();
  field_value = trim_all(field_value);
  $j('#'+form_field_id).val(field_value);

  //Remove any thousand separators (commas) or dollar signs, then put value back into form input
  field_value = remove_currency_formatting(field_value);
  $j('#'+form_field_id).val(field_value);

  //check for numeric characters
  if (objRegExp.test(field_value) == true) {
    if (auto_highlight == true) {
      $j('#'+form_field_id).css("background-color", "white");
    }
    return true;
  }
  else {
    if (auto_highlight == true) {
      $j('#'+form_field_id).css("background-color", "yellow");
    }
    return false;
  }
}










/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
//
//         F O R M A T T I N G      F U N C T I O N S
//
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////

//
// Removes leading and trailing white spaces.
//
function trim_all( strValue ) {
  var objRegExp = /^(\s*)$/;

  //check for all spaces
  if(objRegExp.test(strValue)) {
    strValue = strValue.replace(objRegExp, '');
    if( strValue.length == 0) {
      return strValue;
    }
  }

  //check for leading & trailing spaces
  objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
  if(objRegExp.test(strValue)) {
    //remove leading and trailing whitespace characters
    strValue = strValue.replace(objRegExp, '$2');
  }
  return strValue;
}

//
// Removes currency formatting characters "(", ")", ",", and "$" from source string.
//
function remove_currency_formatting( strValue ) {
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

//
// Formats a number as currency. This assumes that the number passed is a valid
// numeric value with 2 decimal places. If not, the original value is returned.
// Adds a dollar sign and any necessary thousand separators (commas). It will wrap
// the numeric portion of the string in parenthesis if the value is negative.
//
function add_currency_formatting( strValue ) {
  var objRegExp = /-?[0-9]+\.[0-9]{2}$/;

  if( objRegExp.test(strValue)) {
    objRegExp.compile('^-');
    strValue = add_commas(strValue);
    if (objRegExp.test(strValue)) {
      strValue = '(' + strValue.replace(objRegExp,'') + ')';
    }
    return '$' + strValue;
  }
  else {
    return strValue;
  }
}

//
// Does the equivalent of str_replace to replace commas with nothing. This
// gets rids of commas from the string.
//
function remove_commas( strValue ) {
  var objRegExp = /,/g; //search for commas globally

  //replace all matches with empty strings
  return strValue.replace(objRegExp,'');
}

//
// Insert commas into numeric string. If the source string is not numeric then the
// original source string is returned unchanged. Use this with numbers that have
// 2 or less decimal places.
//
function add_commas( strValue ) {
  var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');

  //check for match to search criteria
  while(objRegExp.test(strValue)) {
    //replace original string with first group match,
    //a comma, then second group match
    strValue = strValue.replace(objRegExp, '$1,$2');
  }
  return strValue;
}
