
// add trim function to JavaScript String object
String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/, ''); 
};

function LTrim(str) {
  if (str == null) { 
    return str;
  }
  for (var i = 0; str.charAt(i) == " " || str.charAt(i) == "\n" || str.charAt(i) == "\t"; i++);
  return str.substring(i, str.length);
}

function RTrim(str) {
  if (str == null){
    return str;
  }
  for (var i = str.length - 1; str.charAt(i) == " " || str.charAt(i) == "\n" || str.charAt(i) == "\t"; i--);
  return str.substring(0, i + 1);
}

function Trim(str){
  return LTrim(RTrim(str));
}

// datagrid confirm delete prompt
function confirmDelete(url) {
  if (confirm('This item will be permanently removed from the database!\nClick OK to delete item.')) {
    window.location.href = url;
  }
}

// form confirm copy prompt
function confirmCopy(url) {
  if (confirm('Do you wish to make a copy of this item?\nRemember to save changes before copying item!\nClick OK to copy the item.')) {
    window.location.href = url;
  }
}

// display a textarea's character count
function charCount(object, eventobject) {
  var ret = "";  // string to display
  var length;    // length of textarea
  var maxlength; // maxlength of textarea
  var display = ""; // display element id value
  var dis; // display element object
  
  if (object) {
    display = "chars_" + object.getAttribute("name");
    dis = document.getElementById(display);
  }

  if (object && dis) {
    length = object.value.length;
    maxlength = object.getAttribute("maxlength");

    if (parseInt(length) <= 0) {
      length = "0";
    }
    if (parseInt(maxlength) <= 0) {
      maxlength = "0";
    }
    if (maxlength != null) {
      ret = length + "/" + maxlength;
    } else {
      ret = length;
    }
    dis.innerHTML = ret;

    //dis.innerHTML = eventobject + " = [" + eventobject.type + "]";

    if (maxlength != null && parseInt(length) > parseInt(maxlength)) {
      dis.style.color = "red";
    } else {
      dis.style.color = "";
      if (eventobject.type == "blur") {
        dis.innerHTML = "";
      }
    }
  }
}

// Convert: mm/dd/yyyy => m/d/yyyy
function calendarDateToVbDate(calDate) {
  var ret = "";
  if (calDate && calDate != "") {
    var array = calDate.split("/");
    if (array.length == 3) {
      if (array[0] < 10) {
        array[0] = array[0].replace("0", "");
      }
      if (array[1] < 10) {
        array[1] = array[1].replace("0", "");
      }
      ret = array[0] + "/" + array[1] + "/" + array[2];
    }
  }
  return ret;
}


// check if a variable is null or empty
function isBlank(object) {

  if (typeof object == 'undefined') {
    return true;
  } else if (object == null) {
    return true;
  } else if (object.length == 0) {
    return true;
  } else if (object == "") {
    return true;
  } else {
    return false;
  }
}

// check if a variable is n/a
function isNA(object) {

  if (typeof object == 'undefined') {
    return false;
  } else if (object == null) {
    return false;
  } else if (object.length == 0) {
    return false;
  } else if (object.trim() == "") {
    return false;
  } else if (object.trim().toLowerCase() == "n/a") {
    return true;
  } else {
    return false;
  }
}

// get the radio-button/check-box checked value 
function getChecked(object) {
  for (var i = 0; i < object.length; i++) {
    if (object[i].checked) {
      return object[i].value;
    }
  }
  return "";
}

// return true if checkbox is checked
function getCheckBox(object) {
  if (object.checked) {
    return object.value;
  } else {
    return "False";  
  }
}

// get the three times list values and set into the fulltime value: 1:24 PM
function getTimeLists(time1, time2, time3) {
  var fulltime;
  fulltime = "";
  if (time1 && time2 && time3) {
    if (time3 == "AM") {
      fulltime = time1 + ":" + time2 + " AM";
    } else {
      fulltime = time1 + ":" + time2 + " PM";
    }
  }
  return fulltime;
}

// toggle item display
function toggle(elementID) {
  var item = document.getElementById(elementID);

  if (item) {
    if (typeof Spry != "undefined") {
      Spry.Effect.AppearFade(elementID, {duration:1000, from:0, to:100, toggle:true});
      Spry.Effect.Blind(elementID, {duration:1000, from:'0%', to:'100%', toggle:true});
    } else if (item.style.display == 'none') {
      item.style.display = 'inline';
    } else {
      item.style.display = 'none';
    }    
  }
}

// write when the file was updated
function fileUpdated() {
  var a;
  a = new Date(document.lastModified); // fileCreatedDate
  lm_year=a.getYear();lm_year=((lm_year<1000)?((lm_year<70)?2000:1900):0)+lm_year;
  lm_month=a.getMonth()+1;lm_month=((lm_month<10)?'0':'')+lm_month;
  lm_day=a.getDate();
  //lm_day=((lm_day<10)?' ':'')+lm_day;
  monthName = new Array(12);
  monthName[0] = 'January';
  monthName[1] = 'February';
  monthName[2] = 'March';
  monthName[3] = 'April';
  monthName[4] = 'May';
  monthName[5] = 'June';
  monthName[6] = 'July';
  monthName[7] = 'August';
  monthName[8] = 'September';
  monthName[9] = 'October';
  monthName[10] = 'November';
  monthName[11] = 'December';
  return (monthName[lm_month-1]+' '+lm_day+', '+lm_year);
}

// write when the file was created
function fileCreated() {
  var a;
  a = new Date(document.fileCreatedDate);
  lm_year=a.getYear();lm_year=((lm_year<1000)?((lm_year<70)?2000:1900):0)+lm_year;
  lm_month=a.getMonth()+1;lm_month=((lm_month<10)?'0':'')+lm_month;
  lm_day=a.getDate();
  //lm_day=((lm_day<10)?' ':'')+lm_day;
  monthName = new Array(12);
  monthName[0] = 'January';
  monthName[1] = 'February';
  monthName[2] = 'March';
  monthName[3] = 'April';
  monthName[4] = 'May';
  monthName[5] = 'June';
  monthName[6] = 'July';
  monthName[7] = 'August';
  monthName[8] = 'September';
  monthName[9] = 'October';
  monthName[10] = 'November';
  monthName[11] = 'December';
  return (monthName[lm_month-1]+' '+lm_day+', '+lm_year);
}

// personalized greeting from QN in the status bar
function greeting() {
  var message = '';
  var now= new Date();
  var hrs = now.getHours();
  var mns = now.getMinutes();
  var disp = ((hrs>12) ? (hrs-12) : hrs) + ':';
  if (mns < 10) {
    disp += '0' + mns;
  } else {
    disp += mns;
  }
  disp += ((hrs > 12) ? ' pm.' : ' AM');
  if (hrs < 12) {
    message = "It's " + disp + '.  Good morning and welcome to QUID NOVIS!';
  } else if (hrs < 18) {
    message =  "It's " + disp + '.  Good afternoon and welcome to QUID NOVIS!';
  } else if (hrs <  24) {
    message = "It's " + disp + '.  Good evening and welcome to QUID NOVIS!';
  }
  window.status = message;
}

/* to prevent spam */
function noSpam(estring){
  return ("mail" + "to:" + estring);
}


/* form field border coloring functions */
var oldBorderColor = '#BDC7D8';

function borderBlur(ele) {
  if (ele) {
    // blur light blue
    ele.style.borderColor = oldBorderColor;
  }
}
function borderFocus(ele) {
  if (ele) {
    // focus green
    oldBorderColor = ele.style.borderColor;    
    ele.style.borderColor = '#6AB94B';
  }
}

function formatCurrency(strValue) {
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

function makediv(value) {
	return "<div>" + value + "</div>";	
}

function makebold(value) {
	return "<b>" + value + "</b>";	
}
