
// 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;
  }
}


// open a new window and display the passed for field text in that window
function viewWindow(formID, formName) {
  var from = document.getElementById(formID);
  var url = 'window.asp?field=' + formID + "&name=" + formName;
  
  var ww = parseInt(getWindowWidth());
  var wh = parseInt(getWindowHeight());
  
  var popwidth = parseInt(ww / 1.5);
  var popheight = parseInt(wh / 1.5);
  var popleft = parseInt(ww / 6);
  var poptop = parseInt(wh / 6);
  
  var winName = formID + 'Window';
  var winParams = 'width=' + popwidth + ',height=' + popheight + ',top=' + poptop + ',left=' + popleft + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=yes,resizable=yes';
  
  //alert("url = " + url + "\nname = " + winName + "\nparams = " + winParams);
  
  if (from && url && winName && winParams) {
    window.open(url, winName, winParams);
  }
}

// save the window forms value back into the parent form field
function saveWindow() {
  var parentID = gup("field");
  if (window.opener) {
    if (window.opener.document) {
      if (window.document.forms['winform']) {
        var parentField = window.opener.document.getElementById(parentID);
        var winField = window.document.forms['winform'].elements['text1'];
        if (winField && parentField) {
          parentField.value = winField.value;
          parentField.focus();
        }
      }
    }
  }
  window.close();
}

// get url param with javascript
function gup(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if (results == null) {
    return "";
  } else {
    return results[1];
  }
}

// get the inner browser window width
function getWindowHeight() {
  if (window.self && window.self.innerHeight) {
    return window.self.innerHeight;
  }
  if (document.documentElement && document.documentElement.clientHeight) {
    return document.documentElement.clientHeight;
  }
  return 0;
}


// get the inner browser window height
function getWindowWidth() {
  if (document.documentElement && document.documentElement.clientWidth) {
    return document.documentElement.clientWidth;
  }
  if (window.self && window.self.innerWidth) {
    return window.self.innerWidth;
  }
  return 0;
}

// resize the main div to match the inner browser window size
function resize(divID) {
  var div = document.getElementById(divID);
  
  var ww = (getWindowWidth() - 2);
  var wh = (getWindowHeight() - 2);
  var mh = (getWindowHeight() - 12);
  
  if (ww < 0) {
    ww = 0;
  }
  if (wh < 0) {
    wh = 0;
  }
  if (mh < 0) {
    mh = 0;
  }
  
  div.style.width = String(ww) + 'px';
  div.style.height = String(wh) + 'px';
}

// resize the main div to match the inner browser window size
function resizeOffset(divID, wid, hi) {
  var div = document.getElementById(divID);
  
  var ww = (getWindowWidth() - 2);
  var wh = (getWindowHeight() - 2);
  var mh = (getWindowHeight() - 12);
  
  if (ww < 0) {
    ww = 0;
  }
  if (wh < 0) {
    wh = 0;
  }
  if (mh < 0) {
    mh = 0;
  }
  
  // adjust
  ww = parseInt(ww) - wid;
  wh = parseInt(wh) - hi;
  
  div.style.width = String(ww) + 'px';
  div.style.height = String(wh) + 'px';
}

// 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";  
  }
}

// return true if checkbox is checked
function getCheckBox2(object) {
  if (object.checked) {
    return object.value;
  } else {
    return "No";
  }
}

// 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") {
			if (item.style.display == 'none') {
				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 {
				Spry.Effect.AppearFade(elementID, {duration:1000, from:100, to:0, toggle:true});
				Spry.Effect.Blind(elementID, {duration:1000, from:'100%', to:'0%', toggle:true});
			}
		} else {
			if (item.style.display == 'none') {
				item.style.display = 'block';
			} 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);
}

function sendEmail(a, s) {
	emailer = window.open('email.asp?'+a+'&s='+s,'emailer','resizable=1,scrollbars=1,width=560,height=560,left=10,top=10')
}

/* 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 addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
