// POSSE Internet API Functions   --   Last updated Nov 05, 2002

function PosseNavigate(aHRef) {
  PosseSubmitLink(aHRef, null, null, null);
}

function PosseSubmit(aHRef) {
  PosseSubmitLink(aHRef, 2, null, null);
}

function PosseSetChangesXML(aXML) {
  document.possedocumentchangeform.changesxml.value = aXML;
}

function PosseAppendChangesXML(aXML) {
  document.possedocumentchangeform.changesxml.value = possedocumentchangeform.changesxml.value + aXML;
}

function PosseChangeColumn(aObjectID, aColumnName, aValue) {
  PosseAppendChangesXML('<object id="' + aObjectID + '"><column name="' + aColumnName + '"><![CDATA[' + aValue + ']]></column></object>');
}

// POSSE Internet Support Functions   --   Last updated Nov 05, 2002
// Note: For original unmodified toolbox version of Calendar Popups see www.mattkruse.com

// Posse change form submit used by Posse functions and tabs
function PosseSubmitLink(aHRef, aFunctionDefID, aPaneID, aSortColumn) {
  if (aHRef > "") {
    document.possedocumentchangeform.action = aHRef;
  } else {
    document.possedocumentchangeform.action = window.location.href;
  }

  if (aFunctionDefID != null) {
    document.possedocumentchangeform.functiondef.value = aFunctionDefID;
  }

  if (aPaneID != null) {
    document.possedocumentchangeform.paneid.value = aPaneID;
  }

  if (aSortColumn > "") {
    var tValue = document.possedocumentchangeform.sortcolumns.value;
    if (tValue == "") {
      document.possedocumentchangeform.sortcolumns.value = aSortColumn;
    } else {
      document.possedocumentchangeform.sortcolumns.value = tValue + "," + aSortColumn;
    }
  }
  PosseOnRoundTrip();
  document.possedocumentchangeform.submit();
}

// Posse Popup Window setup
window.PossePwObj = null;
window.PossePwRef = null;
window.PossePwXon = null;

// Posse Popup Window global functions
function PossePwRefresh() {
  if (window.PossePwRef != null && !window.PossePwRef.closed && window.PossePwObj.content != null) {
    window.PossePwRef.document.open();
    window.PossePwRef.document.writeln(window.PossePwObj.content);
    window.PossePwRef.document.close();
  }
}

function PossePwDirty(aBoolean) {
  if (window.PossePwRef != null && !window.PossePwRef.closed) {
    window.PossePwObj.dirty = aBoolean;
  }
}

function PossePwClose(aUnload) {
  if (window.PossePwRef != null) {
    if (!window.PossePwRef.closed) {
      if (!aUnload && window.PossePwObj.dirty == true) {
        window.PossePwRef.focus()
        if (!window.PossePwRef.confirm('Outstanding changes will be lost. Continue?')) return false;
      }
      window.PossePwRef.close();
    }
  window.PossePwRef = null;
  window.PossePwObj = null;
  return true;
  }
}

function PossePwFocus() {
  if (window.PossePwRef != null) {
    if (!window.PossePwRef.closed) {
      window.PossePwRef.focus();
      return false;
    }
  }
  return true;
}

// Posse Popup Window Constructor
function PossePw() {
  if (!window.listenerAttached) {
    window.listenerAttached = true;
    if (document.layers) {
      document.captureEvents(Event.MOUSEUP);
    }
    window.PossePwXon = document.onmouseup;
    if (window.PossePwXon != null) {
      document.onmouseup = new Function("window.PossePwXon();window.PossePwFocus();");
    } else {
      document.onmouseup = window.PossePwFocus;
    }
  }
  this.xoffset=0;
  this.yoffset=0;
  this.width=100;
  this.height=100;
  this.content = null;
  this.dirty = false;
  this.href = "about:blank";
  this.scrollbars = "yes";
  this.resizable = "yes";
  this.status = "no";
  this.features = "toolbar=no, location=no, menubar=no, titlebar=no";
  this.getPosition = PossePwPosition;
  this.openPopup = PossePwOpen;
}

// (method of PossePw)
function PossePwPosition(aAnchor) {
  this.x=200; this.y=200;
  if (document.all) {
    var el = document.all[aAnchor];
    var ol = el.offsetLeft;
    var ot = el.offsetTop;
    while ((el = el.offsetParent) != null) {
      ol += el.offsetLeft;
      ot += el.offsetTop;
    }
    if (!isNaN(window.screenLeft)) {
      this.x = ol - document.body.scrollLeft + window.screenLeft;
      this.y = ot - document.body.scrollTop + window.screenTop;
    }
  }
  else if (document.getElementById) {
    var oe = document.getElementById(aAnchor);
    if (isNaN(window.screenX)) {
      this.x = oe.offsetLeft - document.body.scrollLeft + window.screenLeft;
      this.y = oe.offsetTop - document.body.scrollTop + window.screenTop;
    } else {
      this.x = oe.offsetLeft + window.screenX + (window.outerWidth-window.innerWidth) - window.pageXOffset;
      this.y = oe.offsetTop + window.screenY + (window.outerHeight-24-window.innerHeight) - window.pageYOffset;
    }
  }
  else if (document.layers) {
    for (var i=0; i<document.anchors.length; i++) {
      if (document.anchors[i].name == aAnchor) {
        this.x = document.anchors[i].x + window.screenX + (window.outerWidth-window.innerWidth) - window.pageXOffset;
        this.y = document.anchors[i].y + window.screenY + (window.outerHeight-24-window.innerHeight) - window.pageYOffset;
        break;
      }
    }
  }
}

 // (method of PossePw)
function PossePwOpen(aAnchor) {
  if (window.PossePwFocus()) {
    window.PossePwObj = this;
    this.getPosition(aAnchor);
    this.x += this.xoffset;
    if (this.x<0) {
      this.x=0;
    }
    this.y += this.yoffset;
    if (this.y<0) {
      this.y=0;
    }
    if (screen && screen.availHeight) {
      if ((this.y + this.height) > screen.availHeight) {
        this.y = screen.availHeight - this.height;
        if (this.y<0) {
          this.y=0;
        }
      }
    }
    if (screen && screen.availWidth) {
      if ((this.x + this.width) > screen.availWidth) {
        this.x = screen.availWidth - this.width - 10;
        if (this.x<0) {
          this.x=0;
        }
      }
    }
    window.PossePwRef = window.open(this.href,"_blank",this.features+",scrollbars="+this.scrollbars+",resizable="+this.resizable+",status="+this.status+",width="+this.width+",height="+this.height+",screenX="+this.x+",left="+this.x+",screenY="+this.y+",top="+this.y+"");
    PossePwRefresh();
    window.setTimeout("window.PossePwFocus()", 10);
  }
}

function PosseLookup(aAnchor, aLookupURL, aComponentId, aObjectId, aObjectDefId, aPaneId, aWidgetId, aTitle, aSearchValue, aWidth, aHeight) {
  var vHRef = "";
  if(aLookupURL == "") {
    vHRef = location.href;
    aLookupURL = vHRef.substring(0, vHRef.indexOf("?"));
  }

  vHRef = aLookupURL + "?Title="+escape(aTitle)+"&PosseObjectId="+aObjectId+"&PosseObjectDefId="+aObjectDefId;
  vHRef+= "&PossePaneId="+aPaneId+"&PosseWidgetId="+aWidgetId+"&PosseSearchValue="+escape(aSearchValue);

  if(aComponentId == "1") {
    location.href = vHRef;
  } else {
    eval("Possew"+aWidgetId+"_"+aObjectId+"Changed('\t\t')");
    var lu = new PossePw();
    lu.xoffset = 0 - (aWidth / 3);
    lu.yoffset = -20;
    lu.width = aWidth;
    lu.height = aHeight;
    lu.href = vHRef;
    lu.openPopup(aAnchor);
  }
}

function PosseUpload(aAnchor, aUploadURL, aObjectId, aObjectDefId, aTitle, aWidth, aHeight) {
  var vHRef;
  vHRef = aUploadURL + "?Title="+escape(aTitle)+"&PosseObjectId="+aObjectId+"&PosseObjectDefId="+aObjectDefId;
  if (aWidth == null) {aWidth = 500;}
  if (aHeight == null) {aHeight = 200;}
  var ul = new PossePw();
  ul.xoffset = 0 - (aWidth / 3);
  ul.yoffset = -20;
  ul.width = aWidth;
  ul.height = aHeight;
  ul.scrollbars = "no";
  ul.resizable = "no";
  ul.status = "yes";
  ul.href = vHRef;
  ul.openPopup(aAnchor);
}

function PosseNote(aAnchor, aNoteURL, aObjectId, aTitle, aWidth, aHeight) {
  var vHRef;
  vHRef = aNoteURL + "?Title=" + escape(aTitle) + "&PosseObjectId="+aObjectId;
  if (aWidth == null) {aWidth = 300;}
  if (aHeight == null) {aHeight = 200;}
  var ul = new PossePw();
  ul.xoffset = 0 - (aWidth / 3);
  ul.yoffset = -20;
  ul.width = aWidth;
  ul.height = aHeight;
  ul.scrollbars = "yes";
  ul.resizable = "yes";
  ul.status = "no";
  ul.href = vHRef;
  ul.openPopup(aAnchor);
}

function PosseAlert(aMsg, aEl) {
  var vEl = "document." + aEl.form.name + "." + aEl.name
  alert(aMsg);
  eval(vEl + ".select()");
  window.setTimeout(vEl + ".focus()", 10);
}

function LTrim(str) {
  var i;
  i = 0;
  while (str.substr(i,1) == " ") {
    i++;
  }
  if (i > 0) {
    str = str.substr(i);
  }
  return str;
}

function RTrim(str) {
  var i;
  i = str.length - 1;
  while (str.substr(i,1) == " " && i > -1) {
    i--;
  }
  str = str.substr(0, i + 1);
  return str;
}

function Trim(str) {
  return LTrim(RTrim(str));
}

function PosseAddDataChanges(aChange) {
  if (document.possedocumentchangeform.datachanges.value == "") {
    document.possedocumentchangeform.datachanges.value = aChange;
  } else {
    document.possedocumentchangeform.datachanges.value += ("," + aChange);
  }
}

function PosseDelete(aHRef, aObjectID) {
  PosseAddDataChanges("('D','" + aObjectID + "')");
  PosseSubmitLink(aHRef, 3, document.possedocumentchangeform.currentpaneid.value);
}

function PosseFindOption(aId, aObjectId) {
  var vReturn = -1
  var vLength = aId.options.length
  var vValues = ""

  for (var vIndex = 0; vIndex < vLength; vIndex ++) {
    vValues = aId.options[vIndex].value.split("\t")
    if (aObjectId == vValues[0]) {
      vReturn = vIndex;
    }
  }
  if (vReturn == -1)
    vReturn = vLength - 1;
  return vReturn;
}

function PosseAppend(aHRef, aPaneId, aEndpointId, aObjectDefId, aRows) {
  PosseAddDataChanges("('A'," + aPaneId + "," + aEndpointId + "," + aObjectDefId + "," + aRows + ")");
  PosseSubmitLink(aHRef, 3, document.possedocumentchangeform.currentpaneid.value);
}

function PosseValidateDate(aEl, aObjectId, aColumnDefId, aFormat) {
  if (aEl.value == "") {
    PosseDataChanged(aObjectId, aColumnDefId, "", 0);
    return true;
  }
  var rDate = PosseReadDate(aEl.value, aFormat);
  if (rDate ==  null) {
    PosseAlert("Invalid date. Try using '" + aFormat + "' format.", aEl);
    return false;
  }
  PosseSetDate(aEl, aObjectId, aColumnDefId, aFormat, rDate.getFullYear(), rDate.getMonth()+1, rDate.getDate());
  return true;
}

function PosseReadDate(aDateVal, aFormat) {
  var d = 0, m = 0, y = 0, i = 0, l;
  var i = 0, l;
  badChars = "., -/\\";
  newString = "";
  prevGood = true;
  l = aDateVal.length;
  while (i < l) {
    str = aDateVal.substr(i,1);
    if (badChars.indexOf(str) > -1) {
      if (prevStr) {
        newString += "/";
        prevStr = false;
      }
    } else {
      prevStr = true;
      newString += str;
    }
    i++;
  }
  aDateVal = newString;

  var parts = aDateVal.split("/");
  for (part in parts) {
    if (m == 0) {
      var pos = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC".indexOf(parts[part].substr(0,3).toUpperCase());
      if (pos >= 0) {
        m = pos / 3 + 1;
      }
    }
  }

  var i = 0;
  var pts = new Array();
  for (part in parts) {
    var p = parts[part];
    if (! isNaN(Number(p))) {
      if (p == 0 || p > 31) {
        y = p;
      } else {
        pts[i] = p;
        i ++;
      }
    }
  }
 aFormat = aFormat.toUpperCase();

  var p=0, f=0, c = "";
  if (pts.length == 1 || m > 0 && y > 0) {
    d = pts[p];
  } else {
    while (f < aFormat.length) {
      c = aFormat.charAt(f);
      if (p >= pts.length) {
        break;
      }
      if (c == "Y" && y == 0) {
        y = pts[p]; p++;
      } else if (c == "M" && m == 0) {
        m = pts[p]; p++;
      } else if (c == "D" && d == 0) {
        d = pts[p]; p++;
      }
      f++;
    }
  }

  var today = new Date();
  if (y == 0) {
    y = today.getFullYear();
  } else if (y < 1000) {
    y = 2000 + (y * 1);
  }
  if (m == 0) {
    m = today.getMonth() + 1;
  }

  var rDate = new Date(y, m - 1, d);
  if (rDate.getFullYear() != y || rDate.getMonth() != m - 1 || rDate.getDate() != d) {
    rDate = null;
  }
  return rDate;
}

function PosseSetDate(aEl, aObjectId, aColumnDefId, aFormat, aYear, aMonth, aDay){
  var yyyy, yy, y, m, mm, mmm, mmmm, d, dd;
  yyyy = aYear + "";
  yy = yyyy.substr(2,2);
  y =  yyyy.substr(3,1);
  m = aMonth + "";
  mm = "0" + m;
  mm = mm.substr(mm.length - 2);
  var mths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  var mthx = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  mmm = mths[m-1];
  mmmm = mthx[m-1];
  d = aDay + "";
  dd = "0" + d;
  dd = dd.substr(dd.length - 2);

  PosseDataChanged(aObjectId, aColumnDefId, "" + yyyy + "-" + mm + "-" + dd + " 00:00:00", 0);

  var dic = new Object();
  dic["YYYY"] = yyyy;
  dic["YY"] = yy;
  dic["Y"] = y;
  dic["MMMM"] = mmmm;
  dic["MMM"] = mmm;
  dic["MM"] = mm;
  dic["M"] = m;
  dic["DD"] = dd;
  dic["D"] = d;

  aFormat = aFormat.toUpperCase();

  var token, f = 0, c = "", val = "";
  while (f < aFormat.length) {
    c = aFormat.charAt(f);
    token = "";
    while ((aFormat.charAt(f) == c) && (f < aFormat.length)) {
      token += aFormat.charAt(f);
      f++;
    }
    if (dic[token] != null) {val = val + dic[token];}
    else {val = val + token;}
  }
  aEl.value = val;
  return true;
}

// Create Posse Calendar
function PosseCalendar(aAnchor, aEl, aObjectId, aColumnDefId, aFormat) {
  var cal = new PosseCal();
  cal.objectId = aObjectId;
  cal.columnDefId = aColumnDefId;
  cal.format = aFormat;

  var d = PosseReadDate(aEl.value, aFormat);
  if (d != null) {
    cal.startyear = d.getFullYear();
    cal.startmonth = d.getMonth()+1;
    cal.startdate = d.getDate();
  }
  cal.openCalendar(aAnchor, aEl);
}

// Refresh Posse Calendar
function PosseCalendarRefresh() {
  var c = window.PossePwObj;
  if (arguments.length>0) {c.content = c.getCalendar(arguments[0],arguments[1]);}
  else {c.content = c.getCalendar();}
  PossePwRefresh();
}

// Return value from Posse Calendar
function PosseCalendarReturn(aYear, aMonth, aDate) {
  PosseSetDate(window.PossePwObj.returnField
                       , window.PossePwObj.objectId
                       , window.PossePwObj.columnDefId
                       , window.PossePwObj.format, aYear, aMonth, aDate);
}

// Posse Calendar Constructor
function PosseCal() {
  var pc = new PossePw();
  var sd = new Date();
  pc.width = 135;
  pc.height = 135;
  pc.xoffset = 0;
  pc.yoffset = 0;
  pc.scrollbars = "no";
  pc.resizable = "no";
  pc.startyear = sd.getFullYear();
  pc.startmonth = sd.getMonth()+1;
  pc.startdate = sd.getDate();
  pc.months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
  pc.days = new Array("S","M","T","W","T","F","S");
  pc.wkstartday = 0;
  pc.returnField = null;
  pc.openCalendar = PosseCalOpen;
  pc.getCalendar = PosseCalHTML;
  return pc;
}

// (method of PosseCal)
function PosseCalOpen(aAnchor, aEl) {
  this.returnField = aEl;
  this.content = this.getCalendar();
  this.openPopup(aAnchor);
}

// (method of PosseCal)
function PosseCalHTML() {
  if (arguments.length > 0) { var month = arguments[0];}
  else {var month = this.startmonth;}

  if (arguments.length > 1) { var year = arguments[1];}
  else {var year = this.startyear;}

  var daysinmonth= new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
  if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) { daysinmonth[2] = 29;}

  var current_month = new Date(year,month-1,1);
  var display_year = year;
  var display_month = month;
  var display_date = 1;
  var weekday= current_month.getDay();
  var offset = 0;

  if (weekday >= this.wkstartday) {offset = weekday - this.wkstartday;}
  else {offset = 7-this.wkstartday+weekday;}

  if (offset > 0) {
    display_month--;
    if (display_month < 1) { display_month = 12; display_year--; }
    display_date = daysinmonth[display_month]-offset+1;
  }

  var next_month = month+1;
  var next_month_year = year;
  if (next_month > 12) { next_month=1; next_month_year++; }

  var last_month = month-1;
  var last_month_year = year;

  if (last_month < 1) { last_month=12; last_month_year--; }

  var date_class;
  var result = "";
  result += '<html><head><title>Calendar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</title>\n';
  result += "<style>\n";
  result += "body         {background-color: seashell;}\n";
  result += "a               {text-decoration: none; color: blue;}\n";
  result += ".pcheader {font-family: sans-serif; font-size: 8pt; background-color: silver; font-weight: bold;}\n";
  result += ".pcline      {border-color: gray; border-top-width: 1px; border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-style: solid;}\n";
  result += ".pcbody    {font-family: sans-serif; font-size: 8pt;}\n";
  result += "th.pcbody {font-weight: normal;}\n";
  result += "a.pcthismonth {color: black;}\n";
  result += "a.pcothermonth{color: silver;}\n";
  result += "a.pcstartdate {color: red;}\n";
  result += "a.pctodaylink {text-decoration: underline;}\n";
  result += "</style>\n</head>\n";
  result += '<body leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0 marginwidth=0 marginheight=0>\n';
  result += '<table border=0 cellspacing=0 cellpadding=0 width="100%"><tr>\n';
  result += '<td class="pcheader" align=center width="100%">\n';
  result += '<a class="pcheader" href="javascript: window.opener.PosseCalendarRefresh('+last_month+','+last_month_year+');">&lt;&lt;</A>&nbsp;&nbsp;'+this.months[month-1]+'&nbsp;&nbsp;<A CLASS="pcheader" HREF="javascript:window.opener.PosseCalendarRefresh('+next_month+','+next_month_year+');">&gt;&gt;</a>\n';
  result += '&nbsp;&nbsp;&nbsp;';
  result += '<a class="pcheader" href="javascript:window.opener.PosseCalendarRefresh('+month+','+(year-1)+');">&lt;&lt;</A>&nbsp;&nbsp;'+year+'&nbsp;&nbsp;<A CLASS="pcheader" HREF="javascript:window.opener.PosseCalendarRefresh('+month+','+(year+1)+');">&gt;&gt;</a></td>';
  result += '</tr></table>\n';
  result += "<center>\n";
  result += '<table border=0 cellspacing=0 cellpadding=0 width=120><tr>\n';
  result += ' <th class="pcbody" align=right width="14%">'+this.days[(this.wkstartday)%7]+'</td>\n';
  result += ' <th class="pcbody" align=right width="14%">'+this.days[(this.wkstartday+1)%7]+'</td>\n';
  result += ' <th class="pcbody" align=right width="14%">'+this.days[(this.wkstartday+2)%7]+'</td>\n';
  result += ' <th class="pcbody" align=right width="14%">'+this.days[(this.wkstartday+3)%7]+'</td>\n';
  result += ' <th class="pcbody" align=right width="14%">'+this.days[(this.wkstartday+4)%7]+'</td>\n';
  result += ' <th class="pcbody" align=right width="14%">'+this.days[(this.wkstartday+5)%7]+'</td>\n';
  result += ' <th class="pcbody" align=right width="14%">'+this.days[(this.wkstartday+6)%7]+'</td>\n';
  result += '</tr><tr><td colspan=7 vlign=top><div class="pcline"><img width=120 height=1></div></td></tr>\n';
  for (var row=1; row<=6; row++) {
    result += '<tr>\n';
    for (var col=1; col<=7; col++) {
      if ((display_month == this.startmonth) && (display_date==this.startdate) && (display_year==this.startyear)) {date_class = "pcstartdate";}
      else if (display_month == month) {date_class = "pcthismonth";}
      else {date_class = "pcothermonth";}

      result += ' <td class="pcbody" align=right><a href="javascript:window.opener.PosseCalendarReturn('+display_year+','+display_month+','+display_date+');window.opener.PossePwClose();" class="'+date_class+'">'+display_date+'</a></td>\n';
      display_date++;
      if (display_date > daysinmonth[display_month]) {
        display_date=1;
        display_month++;
      }
      if (display_month > 12) {
        display_month=1;
        display_year++;
      }
    }
    result += '</tr>';
  }
  result += '<tr><td colspan=7><div class="pcline"><img width=120 height=1></div></td></tr>\n';
  result += '<tr><td class="pcbody" colspan=7 align=center>\n';
  var now = new Date();
  result += ' <a class="pctodaylink" href="javascript:window.opener.PosseCalendarReturn(\''+now.getFullYear()+'\',\''+(now.getMonth()+1)+'\',\''+now.getDate()+'\');window.opener.PossePwClose();">Today</a>\n';
  result += '</td></tr></table></center></body></html>\n';
  return result;
  }

function PosseValidateNumber(aEl, aObjectId, aColumnDefId, aMin, aMax, aPrecision, aGranularity) {
  if (aEl.value == "") {
    PosseDataChanged(aObjectId, aColumnDefId, "", 0);
    return true;
  }
  var val = aEl.value;
  var ok = true;
  var i = 0;

  nbr = val;
  i = nbr.indexOf(",")
  while (i >= 0) {
    nbr = nbr.substr(0,i) + nbr.substr(i + 1);
    i = nbr.indexOf(",")
  }

  if (isNaN(Number(nbr))) {
    ok = false;
  } else {
    if (aGranularity == null) {
      if (aPrecision > -1 && aPrecision != null) {
        var pos = nbr.indexOf(".");
        if (pos > -1) {
          if (nbr.substr(pos + 1).length > aPrecision) ok = false;
        }
      }
    } else {
      nbr = Math.round(nbr / aGranularity) * aGranularity;
      var strGran = "" + aGranularity;
      var pos = strGran.indexOf(".");
      if (pos >= 0) {
        var precision = strGran.length - pos - 1;
      } else {
        var precision = 0;
      }
      var pwr = Math.pow(10, precision);
      nbr = Math.round(nbr * pwr) / pwr;
    }
    if (ok) {
      nbr = nbr * 1; // change to a number...
      if (nbr < aMin && aMin != null) {
        ok = false;
      } else {
        if (nbr > aMax && aMax != null) ok = false;
      }
    }
  }
  if (ok) {
    aEl.value = nbr;
    PosseDataChanged(aObjectId, aColumnDefId, "" + nbr, 0);
    return true;
  } else {
    var msg = "You must enter a number";
    if (aMin != null && aMax != null) {
      msg += " between " + aMin + " and " + aMax;
    } else {
      if (aMin != null) {
        msg += " greater than or equal to " + aMin;
      } else {
        if (aMax != null) {
          msg += " less than or equal to " + aMax;
        }
      }
    }
    if (aPrecision == 0) {
      msg += " without decimal places";
    } else {
      if (aPrecision > 0) {
        msg += " with no more than " + aPrecision + " decimal places";
      }
    }
    if (aGranularity > 0) {
      msg += ".  The number will be rounded to the nearest " + aGranularity;
    }
    msg += ".";

    PosseAlert(msg, aEl);
    return false;
  }
}

function PosseValidateEditMask(aEl, aMask) {
  var val = aEl.value;
  var mask = aMask;
  var ok = true;
  var checkedval = "";
  var alphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  var numerics = "0123456789"

  if (val == "") return true;

  while (val > "" && mask > "") {
    var c = val.substr(0, 1);
    val = val.substr(1);
    var msk = mask.substr(0, 1);
    mask = mask.substr(1);

    if (msk == "A" && !(alphas.indexOf(c) >= 0) || (msk == "#" || msk == "0") && !(numerics.indexOf(c) >= 0)) {
      ok = false;
      break;
    } else {
      if (msk == "!") {
        checkedval += "" + c.toUpperCase();
      } else {
        if (msk == "^") {
          checkedval += "" + c.toLowerCase();
        } else {
           checkedval += "" + c;
        }
      }
    }
  }

  if (val > "" || mask > "") ok = false;

  if (ok) {
    aEl.value = checkedval;
    return true;
  } else {
    var msg = "You must enter a value that is in the format " + aMask + " where";
    mask = aMask;
    first = true;
    ADone = false;
    XDone = false;
    NumberDone = false;
    UpperDone = false;
    LowerDone = false;

    while (mask > "") {
      msk = mask.substr(0, 1);
      mask = mask.substr(1);
      switch (msk) {
        case "A":
          if (!ADone) {
            if (first) first = false; else msg += ", and";
            msg += "\n    A is an alphanumeric character (A-Z, a-z, or 0-9, but no special characters)"
            ADone = true;
          }
          break;
        case "X":
          if (!XDone) {
            if (first) first = false; else msg += ", and";
            msg += "\n    X is any character, including special characters"
            XDone = true;
          }
          break;
        case "#":
          if (!NumberDone) {
            if (first) first = false; else msg += ", and";
            msg += "\n    # or 0 is a number (0-9)"
            NumberDone = true;
          }
          break;
        case "0":
          if (!NumberDone) {
            if (first) first = false; else msg += ", and";
            msg += "\n    # or 0 is a number (0-9)"
            NumberDone = true;
          }
          break;
        case "!":
          if (!UpperDone) {
            if (first) first = false; else msg += ", and";
            msg += "\n    ! is any character (will be changed to upper case)"
            UpperDone = true;
          }
          break;
        case "^":
          if (!LowerDone) {
            if (first) first = false; else msg += ", and";
            msg += "\n    ^ is any character (will be changed to lower case)"
            LowerDone = true;
          }
          break;
      }
    }
    PosseAlert(msg + ".", aEl);
    return false;
  }
}

function PosseEscapeData(aValue) {
  var tPos = aValue.indexOf("\\");
  while (tPos >= 0) {
    aValue = aValue.substr(0, tPos) + "\\" + aValue.substr(tPos);
    tPos = aValue.indexOf("\\",tPos + 2);
  }

  var tPos = aValue.indexOf("\"");
  while (tPos >= 0) {
    aValue = aValue.substr(0, tPos) + "\\" + aValue.substr(tPos);
    tPos = aValue.indexOf("\"",tPos + 2);
  }

  var tPos = aValue.indexOf("'");
  while (tPos >= 0) {
    aValue = aValue.substr(0, tPos) + "\\" + aValue.substr(tPos);
    tPos = aValue.indexOf("'",tPos + 2);
  }

  var tPos = aValue.indexOf("\r");
  while (tPos >= 0) {
    aValue = aValue.substr(0, tPos) + aValue.substr(tPos + 1);
    tPos = aValue.indexOf("\r");
  }
  var tPos = aValue.indexOf("\n");
  while (tPos >= 0) {
    aValue = aValue.substr(0, tPos) + "\\n" + aValue.substr(tPos + 1);
    tPos = aValue.indexOf("\n");
  }
  return aValue;
}

function PosseLengthCheck(aValue, aLength) {
  if (aLength > 0 && aValue.length > aLength) {
    alert("You have entered " + aValue.length + " characters.  Only " + aLength + " are allowed.");
    return false;
  }
  return true;
}

function PosseDataChanged(aObjectID, aColumnDefID, aValue, aLength) {
  if (aObjectID==null) {return true;}
  if (PosseLengthCheck(aValue, aLength)) {
    aValue = PosseEscapeData(aValue)
    PosseAddDataChanges("('C','" + aObjectID + "'," + aColumnDefID + ",'" + aValue + "')");
    return true;
  } else {
    return false;
  }
}

function PosseNoteChanged(aObjectID, aReferencedObjectID, aValue, aLength) {
  if (aObjectID==null) {return true;}
  if (PosseLengthCheck(aValue, aLength)) {
    aValue = PosseEscapeData(aValue);
    PosseAddDataChanges("('N','" + aObjectID + "','"+ aReferencedObjectID + "','" + aValue + "')");
    return true;
  } else {
    return false;
  }
}

function PosseAddClause(aElement, aObjectID, aReferencedObjectID, aClause) {
  var tValue = aElement.value;
  if (tValue > "") {tValue = tValue + " ";}
  tValue = tValue + aClause;
  if (PosseNoteChanged(aObjectID, aReferencedObjectID, tValue, 32000)) {
    aElement.value = tValue;
    return true;
  } else {
    return false;
  }
}
