//Added 2/5/08 by GC// requires AjaxRequest.js, $()String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };String.prototype.atMiddle = function( startSub, endSub ) {      var startPos = 0  var endPos = 0  var middle = "";  startPos = this.indexOf(startSub);  if ( startPos != -1 ) {    var rightOfStart = this.substring( startPos + startSub.length, this.length );    endPos = rightOfStart.indexOf(endSub);      if ( endPos == -1 ) {      middle = rightOfStart;         } else {      middle = rightOfStart.substring(0, endPos );    }  } else {    middle = "";  }  return middle;   }/* new array prototype for testing if an element is in an array */Array.prototype.inArray = function (value) {  var i;  for (i=0; i < this.length; i++) {    if (this[i] === value) {      return true;    }   }  return false;};function EmbeddedView(target, view, parameters, headers, colWidths, controls, popupParms, form,  title, createNew ) {    /* constants */  LOADING_HTML = "<img src=\"" + thisDb + "/msload.gif\" alt=\"loading\">";  popupParms = 'scrollbars=1,resizable=1,status=0,' + popupParms;   maxRecords = 9999;  if( target.style.height != "") {    SCROLL_AT = 100; // height at which to start scrolling  } else {    SCROLL_AT = 330; // Sets height at which to start scrolling  was 160  180 for 11, 260 for 16  }    // create random number (for forcing no cache)  function rndNo()  {    var d = new Date();     return '' + d.getMonth() + d.getDate() + d.getYear() + d.getHours() + d.getMinutes() +d.getSeconds() + '';  }  function construct() {    this.target = target;         // target div in which to create the table      this.view = view;             // full path to view    this.parameters = parameters; // url params to pass ?RVE    this.headers = headers;       // array of column headers    this.colWidths = colWidths;   // array of column widths    this.controls = controls;     // array of controls to display    this.count = 0;               // counters for data    this.interval = 0;    this.targetform="b5c29001946e7596ca257438000cab42/"    this.start = 0;    this.last = 0;    this.form = form;             // form used when creating new docs    this.createNew = createNew;   // bool - can we create new docs    this.rtc = null;              // restrictToCategory value    this.showAll = true;		// shows Nav controls    this.NavCon = true;			// shows 1 to x of 35 in header    // window.open parameters for edit,view and create    if ( popupParms ) this.popupParms = popupParms; else this.popupParms = "";        // make sure title is not null    if ( title ) this.title = title; else this.title = "";        this.construct = function() {     } // end construct        /* load the table into the target */    this.load = function(newParms) {      var o = this;      // allow user to override what's loaded using .load() method      if ( newParms ) p = parameters + newParms; else p = parameters;      if ( p.toLowerCase().indexOf("restricttocategory=") != -1 ) {        this.rtc = p.atMiddle( "&RestrictToCategory=","&");      }            if ( p.toLowerCase().indexOf("count=-1") != -1 ) {        this.showAll = true;      }            AjaxRequest.get(      {        'url':  view + '?ReadViewEntries&Preformat' +  p        ,'onSuccess':function(req){            var counts = o.getCounts(req.responseXML);           if ( counts[0] ) o.count = counts[0];           if ( counts[1] ) o.last = counts[1];           if ( counts[2] ) o.start = counts[2];           if ( o.interval == 0 ) o.interval = counts[0];                      // get xml from the view and create a table out of it           var tableHTML = o.makeHTMLTableFromXML( req.responseXML );                      // place the html in the target element           target.innerHTML = tableHTML;             }            ,'onError':function(req){            alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);        }        ,'onLoading':function(){             target.innerHTML = LOADING_HTML;             }      }) // end AjaxRequest        }        // return an array containing count, last and start    this.getCounts = function( xml ) {      var root = xml.documentElement;      var rows = root.getElementsByTagName("viewentry");           var count = rows.length;      if ( root.attributes[0] )        var last = parseInt(root.attributes[1].value, 10);        if ( rows[0] )            var start = parseInt(rows[0].attributes[0].value ,10);    return [count,last,start];       }        /* reload the embedded view */    this.reload = function() {      setTimeout( "window.embeddedViews['" + target.id + "'].load()", 300 );    }        /* navigate to the index that is i positions from the current index */    this.nav = function( i ) {      if (( this.start + i ) < 1 ) {        this.start = 1;        i = 0;      }      window.embeddedViews[target.id].load("&Start=" + (this.start + i) + "&Count=" + this.interval);    }    /* jump to index i */    this.startAt = function( i ) {      window.embeddedViews[target.id].load("&Start=" + i + "&Count=" + this.interval);    }        /* edit the record */    this.ed = function( id ) {      var url = thisDb + "/0/" + id + "?EditDocument";      window.open( url, 'ed', this.popupParms );    }    /* view the record (read) */    this.vw = function( id ) {      var url = thisDb + "/0/" + id + "?OpenDocument";      window.open( url, 'ad', this.popupParms );          }        /* target for "new" button - create a new doc using form */    this.create = function() {      if ( form )  {       var url = thisDb + "/" + form + "?Open";       window.open( url, 'cr', this.popupParms );            } else {       alert( "Please specify a form to use in the constructor." );      }    }        /* add a record to the list  */    this.ad = function( id ) {      if ( confirm("Are you sure you want to add this record?") ) {        // call agent to erase this doc then execute the refersh functoin        AjaxRequest.get(        {          'url': thisDb + "/(upd)?Open&action=add&" + "&id=" + id + "&rnd=" + rndNo(),          'onSuccess':function(req){             if ( req.responseText.indexOf("Error") != -1 ) {               alert( req.responseText )             }                               reloadviews();           }         ,'onError':function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}         ,'onLoading':function(){  target.innerHTML = LOADING_HTML;}        }        );      }    }        /* remove a record from 'selected people' list */    this.rm = function( id ) {      if ( confirm("Are you sure you want to remove this record from the list?") ) {        // call agent to erase this doc then execute the refersh functoin        AjaxRequest.get(        {          'url': thisDb + "/(upd)?Open&action=rm&" + "&id=" + id + "&rnd=" + rndNo(),          'onSuccess':function(req){              if ( req.responseText.indexOf("Error") != -1 ) {               alert( req.responseText )             }                              reloadviews();           }         ,'onError':function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}         ,'onLoading':function(){  target.innerHTML = LOADING_HTML;}        }        );      }    }        /* draw the table */    this.makeHTMLTableFromXML = function( xml ) {              var root = xml.documentElement;      var rows = root.getElementsByTagName("viewentry");      var columnCount = 1 + this.controls.length;            if ( rows.length > 0)        columnCount = rows[0].getElementsByTagName("entrydata").length + this.controls.length;      else         columnCount = 1 + this.controls.length;      // start table html      var htm = "<table>";                  if ( headers ) columnCount += headers.length;     //alert(columnCount) //GC;      // add nav controls in first row     htm += "<tr><td colspan=\"" + (columnCount) +  "\">";     htm += "<table style=\"width:100%;\"><tr>";           if (! this.NavCon ){      htm += "<td style=\"width:50%;\">";      if ( createNew ) {        htm += "<img src=\"" + thisDb + "/new.gif\" style=\"cursor:pointer;\" onclick=\"embeddedViews['" + target.id + "'].create()\" alt=\"create new\">";        columnCount++;      } else         htm += "&nbsp;";          htm += "</td><td style=\"width:100% ;text-align:right;\">" ;     htm += "<div style=\"display:inline;font-weight:bold;margin-right:10px;\">" + this.title;       if ( this.start + this.count == 0 )          htm +=  " (" + this.start + " to " + (this.start + this.count) + " of " + this.last + ") </div>";      else          htm +=  " (" + this.start + " to " + (this.start + this.count - 1) + " of " + this.last + ") </div>";            if ( ! this.showAll ){        if ( this.start != 1 && this.start != 0 ) {           htm += "<img src=\"" + thisDb + "/first.gif\" onclick=\"embeddedViews['" + target.id + "'].reload()\" style=\"cursor:pointer;\" alt=\"next\">";          htm += "<img src=\"" + thisDb + "/previous.gif\" onclick=\"embeddedViews['" + target.id + "'].nav(-" + this.interval + ")\" style=\"cursor:pointer;\" alt=\"next\">";        } else {          htm += "<img src=\"" + thisDb + "/first_off.gif\" alt=\"next\">";          htm += "<img src=\"" + thisDb + "/previous_off.gif\"  alt=\"next\">";         }          if (( (this.start + this.count - 1) < this.last )  && this.start != 0 ){          htm += "<img src=\"" + thisDb + "/next.gif\" onclick=\"embeddedViews['" + target.id + "'].nav(" + this.interval + ")\" style=\"cursor:pointer;\" alt=\"next\">";          htm += "<img src=\"" + thisDb + "/last.gif\" onclick=\"embeddedViews['" + target.id + "'].startAt(" + (this.last-this.interval+1) + ")\" style=\"cursor:pointer;\" alt=\"last\">";                              } else {          htm += "<img src=\"" + thisDb + "/next_off.gif\" alt=\"\">";          htm += "<img src=\"" + thisDb + "/last_off.gif\" alt=\"\">";                         }       }      }         htm += "</table></td></tr>";            // add headers if necessary      htm += "<tr>"      if ( headers) {                 // headers        for ( var h = 0; h < headers.length; h++ ) {          if ( colWidths )          htm += "<th width=\"" + colWidths[h] + "%\">" + headers[h] + "</th>";                    else           htm += "<th>" + headers[h] + "</th>";             }              }      htm += "</table>"; // Table cell data width can be controled by adding ---width:400;      htm += "<div style=\"height:" + SCROLL_AT + "px;overflow:auto;\"><table>";                 // add data as rows in html table      if ( rows.length > 0 ) {        var cols = rows[0].getElementsByTagName("entrydata");        for ( var i = 0; i < rows.length;  i++ ) {                         var rowDocId = rows[i].attributes[1].value;                       // new row               htm += "<tr>";          // cells containing action icons          if( controls.length > 0 ) {            if ( controls.inArray("a") )               htm += "<td style=\"width:12px\"><img onclick=\"embeddedViews['" + target.id + "'].ad('" + rowDocId + "')\" src=\"" + thisDb + "/add.gif\" alt=\"Add\" style=\"cursor:pointer\"></td>";                                   if ( controls.inArray("e") )                           htm += "<td style=\"width:12px\"><img onclick=\"embeddedViews['" + target.id + "'].ed('" + rowDocId + "');\" src=\"" + thisDb + "/edit.gif\" alt=\"Edit\" style=\"cursor:pointer\"></td>"                                    if ( controls.inArray("v") )                           htm += "<td style=\"width:12px\"><img onclick=\"embeddedViews['" + target.id + "'].vw('" + rowDocId + "');\" src=\"" + thisDb + "/vw.gif\" alt=\"View\" style=\"cursor:pointer\"></td>"                                    if ( controls.inArray("r") )                           htm += "<td width=\"12px\"><img onclick=\"embeddedViews['" + target.id + "'].rm('" + rowDocId + "');\" src=\"" + thisDb + "/rm.gif\" alt=\"Remove\" style=\"cursor:pointer\"></td>"                                  }                    // cells containing data          for ( var j = 0; j < cols.length; j++ ) {             if ( colWidths ) {                  htm += "<td width=\"" + colWidths[j] + "%\">" + getInnerText(rows[i].getElementsByTagName("entrydata")[j]) + "</td>";                          } else {             htm += "<td >" + getInnerText(rows[i].getElementsByTagName("entrydata")[j]) + "</td>";                                 }          }                     htm += "</tr>";          if ((i % (maxRecords-1) == 0) && ( i > 0) ) i = rows.length;        }            } else {        // no rows = no data        htm += "<tr><td colspan=\"" + columnCount + "\">No records found"         if ( this.rtc ) htm += " for " + this.rtc;        htm += "</td></tr>";      }            htm += "</table></div>";      return htm;    }             }    return new construct();} 
