/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

FUNCTIONS:

RemDollarSign   : RETURN: Removes the $ sign at the beginning of a string

RemSpaces       : RETURN: Removes ALL spaces in a string

IsNumeric       : RETURN: boolean value; true if string is numeric else false

IsNull          : RETURN: boolean value; true if string is null else false

LTrim           : RETURN: Trims all spaces to the left of a string

RTrim           : RETURN: Trims all spaces to the right of a string

Trim            : RETURN: Trims all spaces to the left and right of a string
                  REQUIRE: LTrim(); RTrim();

SelectAll       : ACTION: Selects all text in a text or textarea field

NoRightClick:   : ACTION: Disables right mouse click with no msg
                  EXAMPLE: Place the following in the document to execute:
                     document.onmousedown = NoRightClick;
                  REQUIRE: NoContextMenu()

NoContextMenu   : ACTION: Disables right click context menu.
                          Required with NoRightClick()

NoRightClickMsg : ACTION: Disables right mouse click with msg
                  EXAMPLE: Place the following in the document to execute:
                     document.onmousedown = NoRightClickMsg;
NullToZero      : RETURN: 0 where value is ""

cvCurrency      : RETURN: Converts a numeric string to currency
                  FORMAT: $0.00

IsCurrency      : RETURN: boolean value; true if string is currency else false

SetCurrency     : RETURN: Sets the value of a field with a valid currency
                  FORMAT: $0.00
                  WARNS: if string is not currency
                  REQUIRE: RemSpaces(); RemDollarSign(); cvCurrency();

setCaretToStart : ACTION: Sets cursor position at the start in a textarea

setCaretToEnd   : ACTION: Sets cursor position at the end in a textarea

scrollToTop     : ACTION: Scrolls to the top in a textarea
                  REQUIRE: setCaretToStart()
                  EXAMPLE:
                     setCaretToStart([FieldName]);
                     scrollToTop([FieldName]);

scrollToBottom  : ACTION: Scrolls to the bottom in a textarea
                  REQUIRE: setCaretToEnd()
                  EXAMPLE:
                     setCaretToEnd([FieldName]);
                     scrollToBottom([FieldName]);

GetDateStamp    : RETURN: curent date & time
                  FORMAT: 0/0/0000 0:00:00 AM/PM

StoreCaret      : STATIC: Stores carot position

InsertAtCaret   : ACTION: Inserts a string at the insertion point in a textarea
                  REQUIRE: StoreCaret();
                  EXAMPLE:
                     <textarea class=inputText name="[NAME]" wrap=virtual rows=10 cols=50 onDblclick="StoreCaret(this);" onSelect="StoreCaret(this);" onClick="StoreCaret(this);" onKeyup="StoreCaret(this);">[VALUE]</textarea>
                     <a href=Javascript:InsertAtCaret(document.forms[INDEX].[FIELD_NAME],[VALUE])>[LINK_OBJECT]</a>

AppendText      : ACTION: Appends a string to a textarea string

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function RemDollarSign(Num) {

   return Num.toString().replace(/\\\$|\,/g,'');

}

function RemSpaces(Str) {

   var temp = "";

   Str = '' + Str;
   splitStr = Str.split(" ");

   for(i = 0; i < splitStr.length; i++) temp += splitStr[i];

   return temp;

}

function IsNull(Str) {

   var IsNull = false;

   if (!Str) IsNull = true;

   return IsNull;

}

function IsNumeric(Str) {

   var ValidChars = "0123456789";
   var IsNumeric = true;
   var Char;

   for (i = 0; i < Str.length && IsNumeric == true; i++)
      Char = Str.charAt(i);
      if (ValidChars.indexOf(Char) == -1) IsNumeric = false;

   return IsNumeric;

}

function cvCurrency(Num) {

   Num = Num.toString().replace(/\\\$|\,/g,'');

   if (isNaN(Num)) { Num = "0"; Num = eval(Num); }

   workNum=Math.abs((Math.round(Num*100)/100));

   workStr = "" + workNum

   if (workStr.indexOf(".")==-1) { workStr+=".00" }

   dStr = workStr.substr(0,workStr.indexOf("."));

   dNum = dStr-0;

   pStr = workStr.substr(workStr.indexOf("."));

   while (pStr.length<3) { pStr += "0" }

   if (dNum >= 1000) {
      dLen = dStr.length
      dStr = parseInt("" + (dNum / 1000)) + "," + dStr.substring(dLen-3,dLen) }

   if (dNum >= 1000000) {
      dLen = dStr.length
      dStr = parseInt("" + (dNum / 1000000)) + "," + dStr.substring(dLen-7,dLen) }

   retval = dStr + pStr

   if (Num < 0) { retval = "(" + retval + ")" }

   return "\$" + retval

}

function IsCurrency(Str) {

   var ValidChars = "0123456789.";
   var IsCurrency = true;
   var Char;

   for (i = 0; i < Str.length && IsCurrency == true; i++)
      Char = Str.charAt(i);
      if (ValidChars.indexOf(Char) == -1) IsCurrency = false;

   return IsCurrency;

}

function SetCurrency(FieldNm,FieldCap,FormIndex) {

   var F = document.forms[FormIndex];

   var FieldValue = document.forms[FormIndex][FieldNm].value;

   FieldValue = RemSpaces(FieldValue)

   FieldValue = RemDollarSign(FieldValue)

   if (!FieldValue) return false

   if (IsCurrency(FieldValue) == false) {
      alert("Please enter a valid currency amount in the '" + FieldCap + "' field.")
      F[FieldNm].focus(); F[FieldNm].select()
   } else {
      F[FieldNm].value = cvCurrency(FieldValue)
   }

}

function LTrim(Str) {

   var whitespace = new String(" \\t\\n\\r");

   var s = new String(Str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }

   return s;

}

function RTrim(Str) {

   var whitespace = new String(" \\t\\n\\r");

   var s = new String(Str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }

   return s;

}

function Trim(Str) {

   return RTrim(LTrim(Str));

}

function NullToZero(Str) {

   if (Str == "") { Str = 0; }

   return Str

}

function scrollToTop (element) {

  if (document.all) element.scrollTop = 0;

}

function scrollToBottom (element) {

  if (document.all) element.scrollTop = element.scrollHeight;

}

function setCaretToStart (input) {

   if (input.createTextRange) {
      var range = input.createTextRange();
      range.collapse(true);
      range.select();
   }

}

function setCaretToEnd (input) {

   if (input.createTextRange) {
      var range = input.createTextRange();
      range.collapse(false);
      range.select();
   }

}

function GetDateStamp() {

   var DayPart = "";
   var Today = new Date();
   var hour = Today.getHours();

   if (hour >= 12) { DayPart = "PM"; } else if (hour < 12) { DayPart = "AM"; }

   if (hour > 12) { hour = hour - 12; } else if (hour <= 12) {
   if (hour == 0) { hour = 12; } else { hour = hour; } }

   var minute = Today.getMinutes();
   if (minute < 10  ) { minute = "0" + minute; } else { minute = minute; }

   var second = Today.getSeconds();
   if (second < 10  ) { second = "0" + second; } else { second = second; }

   var month = Today.getMonth(); month = month + 1;
   var day = Today.getDate();
   var year = Today.getYear(); if(year < 2000) { year = year + 1900; }

   var time = hour + ':' + minute + ':' + second;
   var date = month + '/' + day + '/' + year;

   return date + " " + time + " " + DayPart;

}

function StoreCaret (textEl) {

   if (textEl.createTextRange) textEl.caretPos =
      document.selection.createRange().duplicate();

}

function InsertAtCaret (textEl, text) {

   if (textEl.createTextRange && textEl.caretPos) {
      var caretPos = textEl.caretPos;
      caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
   } else textEl.value = text;

}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

USAGE: <a href=Javascript:AppendText(0,'Notes',InsertText,true,true)>[LINK_OBJRCT]</a>

FormIndex  The index number of the form to which the FieldName belongs
FieldName  The name of the textarea field to insert text into
AppendText The text to append. It is best to set the AppendText as a
           variable outside the call, i.e.:
           var AppendText = "* * * * * * * * \\nNEW ENTRY...\\n" + GetDateStamp() + "\\n";
UseRturn   Appends AppendText without a return if set to false
MoveDown   sets the cursor to the end of the appended string and scrolls
           down to the cursor if set to true.

*/

function AppendText(FormIndex,FieldNm,AppendText,UseRturn,MoveDown) {

   var F = document.forms[FormIndex];

   var FieldValue = document.forms[FormIndex][FieldNm].value;

   if (FieldValue != "" && UseRturn == true)
      AppendText = "\n" + AppendText;

   F[FieldNm].value = FieldValue + AppendText;

   if (MoveDown == true) {
      setCaretToEnd(F.Notes);
      scrollToBottom(F.Notes); }

}

function SelectAll(FormIndex,FieldNm) {

   var F = document.forms[FormIndex];

   F[FieldNm].focus();
   F[FieldNm].select();

}

function NoRightClickMsg(e) {

   var msg = "Right Click has been disabled.";

   if (navigator.appName == 'Netscape' && e.which == 3) {
      alert(msg);
      return false;
   } else if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) {
      alert(msg);
      return false;
   }

   return true;

}

function NoRightClick(e) {

   document.oncontextmenu = NoContextMenu;

   if (window.Event) {
      if (e.which == 2 || e.which == 3)
         return false;
      } else if (event.button == 2 || event.button == 3) {
         event.cancelBubble = true
         event.returnValue = false;
         return false;
      }

}

function NoContextMenu() {

   event.cancelBubble = true
   event.returnValue = false;
   return false;

}
