
// 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";  
  }
}

// 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);
}

// 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];
  }
}


/* 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';
  }
}

/* get the current url to email to a friend */
function pageEmail() {
  // escape
  // encodeURI
  // encodeURIComponent
  var url = escape(location.href);
  var ptitle = escape(document.title);
  //alert(url + " - " + ptitle);
  location.href = "page_email.asp?url=" + url + "&title=" + ptitle;
}

/* share the page on facebook */
function pageFacebookShare() {
  var url = escape(location.href);
  var ptitle = escape(document.title);
  window.open('http://www.facebook.com/sharer.php?u=' + url,'sharer','toolbar=0,status=0,width=626,height=436');
}

/* get the current url and event, to email to a friend */
function eventEmail(eid) {
  var url = escape(location.href);
  var ptitle = escape(document.title);
  //alert(url + " - " + ptitle);
  if (isBlank(eid)) {
    location.href = "page_email.asp?url=" + url + "&title=" + ptitle;
  } else {
    location.href = "page_email.asp?eventID=" + eid + "&url=" + url + "&title=" + ptitle;
  }
}

/* get the current url and event */
function eventError(eid) {
  var url = escape(location.href);
  var ptitle = escape(document.title);
  //alert(url + " - " + ptitle);
  if (isBlank(eid)) {
    location.href = "submit_error.asp?&url=" + url + "&title=" + ptitle;
  } else {
    location.href = "submit_error.asp?eventID=" + eid + "&url=" + url + "&title=" + ptitle;
  }
}

/* get the current url and business */
function businessError(bid, gid) {
  var url = escape(location.href);
  var ptitle = escape(document.title);
  //alert(url + " - " + ptitle);
  if (isBlank(bid)) {
    location.href = "submit_correction.asp?&url=" + url + "&title=" + ptitle;
  } else {
    location.href = "submit_correction.asp?bizID=" + bid + "&guideID=" + gid + "&url=" + url + "&title=" + ptitle;
  }
}

/* get the current url and event, to email to a friend */
function venueError(eid) {
  var url = escape(location.href);
  var ptitle = escape(document.title);
  //alert(url + " - " + ptitle);
  if (isBlank(eid)) {
    location.href = "submit_error.asp?&url=" + url + "&title=" + ptitle;
  } else {
    location.href = "submit_error.asp?venueID=" + eid + "&url=" + url + "&title=" + ptitle;
  }
}

/* open a generic popup window */
function newWindow(mypage, myname, w, h, scroll) {
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 2;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=yes';
  //winprops = 'height=600,width=800,top='+wint+',left='+winl+',scrollbars=yes,resizable=yes,menubar=yes';
  win = window.open(mypage, myname, winprops);
}

/* dual list stuff ---------------- */

var selControl = null;
function setFocus(control) {
  selControl = control;
}

function MoveItemFrom(fromID, toID) {
  var from = document.getElementById(fromID);
  var to = document.getElementById(toID);

  if (from && to && from.length > 0) {
    var fromIndex = from.selectedIndex;
    var toIndex = to.selectedIndex;
    
    if (fromIndex <= -1) {
      fromIndex = from.length - 1;
    }
    if (toIndex <= -1) {
      toIndex = to.length;
    }
    
    var s_Value = from.options[fromIndex].value;
    var s_Text = from.options[fromIndex].text;

    if (fromIndex >= 0 && toIndex >= 0) {
      from.remove(fromIndex);
      if (fromIndex > from.length - 1) {
        from.selectedIndex = from.length - 1;
      } else {
        from.selectedIndex = fromIndex;
      }
      
      to.options.add(new Option(s_Text, s_Value), toIndex);
      to.selectedIndex = toIndex;
      
      setFocus(to);
    }
    
  }
  
  ProcessListsOrder();
}

function MoveItemUp(controlid) {
  var control = document.getElementById(controlid);
  
  if (control == null || selControl == null) {
    return false;
  } else if (control.id != selControl.id) {
    return false;
  }
  
  var i_Index = control.selectedIndex;
   
  if (i_Index > -1) {
    var s_Value = control.options[i_Index].value;
    var s_Text = control.options[i_Index].text;
      
    if (i_Index > 0) {
      control.remove(i_Index);
      control.options.add(new Option(s_Text, s_Value), i_Index - 1);
      control.selectedIndex = i_Index - 1;
    }
  }
  
  ProcessListsOrder();
}

function MoveItemDown(controlid) {
  var control = document.getElementById(controlid);
  
  if (control == null || selControl == null) {
    return false;
  } else if (control.id != selControl.id) {
    return false;
  }
  
  var i_Index = control.selectedIndex;
     
  if (i_Index > -1) {
    var s_Value = control.options[i_Index].value;
    var s_Text = control.options[i_Index].text;
      
    if (i_Index < control.options.length - 1) {
      control.remove(i_Index);
      control.options.add(new Option(s_Text, s_Value), i_Index + 1);
      control.selectedIndex = i_Index + 1;
    }
  }
  
  ProcessListsOrder();
}

// save the lists order to hidden form fields
function ProcessOrder(listID, orderID, textID) {
  var list = document.getElementById(listID);
  var order = document.getElementById(orderID);
  var txt = document.getElementById(textID);
  var s_order = "";
  var s_txt = "";
  
  if (list && order && txt) {
    for (var i = 0; i < list.length; i++) {
      s_order += list.options[i].value;
      s_txt += list.options[i].text;
      if (i < list.length - 1) {
        s_order += "-_-";
        s_txt += "-_-";
      }
    }

    order.value = s_order;
    txt.value = s_txt;
    
    // debug
    //bug.Debug(listID + ":<br>s_order: " + s_order + "<br>s_txt: " + s_txt);
  }
}

// read and display the hidden field values in the lists
function DisplayOrder(listID, orderID, textID) {
  var list = document.getElementById(listID);
  var order = document.getElementById(orderID);
  var txt = document.getElementById(textID);
  
  if (list && order && txt) {
    var a_order = order.value.split("-_-");
    var a_txt = txt.value.split("-_-");
  
    if (a_order != "") {
      for (var i = 0; i < a_order.length && i < a_txt.length; i++) {
        list.options.add(new Option(a_txt[i], a_order[i]), i);
      }
    }
  }
}

/* end dual list stuff ------------------ */
