function displayAlarm() {
   if (arguments.length > 1) {
      for (var i = 1; i < arguments.length; ++i) {
         var e = $(arguments[i]);
         if (e.tagName.toLowerCase() == 'td') e.addClassName('alarm_lab');
         else e.parentNode.addClassName('alarm_lab');
      }
   }
   $('_alarm_area').innerHTML = arguments[0];
   $('_alarm_area').show();
   return false;
}
function isFieldsEmpty(fields) {
   var count = 0;
   fields.each(function(e){
      if ($(e) && $F(e) == '') {
         $(e).parentNode.addClassName('alarm_lab');
         ++count;
      } else if (!$(e) && getRadioValue(e) == null && (es=document.getElementsByName(e)).length > 0) {
         es[0].parentNode.addClassName('alarm_lab');
         ++count;
      }
   });
   return count > 0;
}
function getDate(prefix) {
   var t =
      document.getElementsByName(prefix+'Year')[0].value + '/' +
      document.getElementsByName(prefix+'Month')[0].value + '/' +
      document.getElementsByName(prefix+'Day')[0].value;
   return new Date(t);
}
var _dialogBodies = [];
var Dialog = Class.create({
   id: null,
   width: null,
   srcElement: null,
   bodyName:null,
   onEvent: function(callback, e) {
      var r = callback();
      if (r) {
         /*
         if (e) {
            new Insertion.Bottom(document.body, e);
            e.hide();
         }
         */
         if (r == 'showVeil') funpDlg.beforeClose = showLoading.curry(true);
         funpDlg.close();
      }
   },
   updateBody: function() {
      if (this.bodyName && this.id) {
         _dialogBodies[this.bodyName] = $$('#'+this.id+' .dlgPanel')[0].innerHTML;
      }
   }, 
   initialize: function() {},
   initDialog: function(type, title, body, confirm_text, cancel_text, init) {
      this.id = '_DlgInstance'+new Date().getTime();
      this.bodyName = body;
      var bodyElem = $(body);
      if (bodyElem) {
         _dialogBodies[body] = bodyElem.innerHTML;
         bodyElem.remove();
      }
      if (typeof _dialogBodies[body] != 'undefined') {
         body = _dialogBodies[body];
      }
      confirm_text = confirm_text ? confirm_text : '確認';
      cancel_text = cancel_text ? cancel_text : '取消';
      mClass = 'class="dialog xp"';
      switch (type) {
         case 'choice':
            new Insertion.Bottom(document.body, getRawDialog(this.id, title, body, sprintf(
               '<input class="confirm" type="button" value="%s" id="%s_ConfirmBtn"/> <input type="button" value="%s" id="%s_CancelBtn" class="cancel"/>',
               confirm_text, this.id, cancel_text, this.id
            )).replace('class="dialog"', mClass));
            this.onInitDialog();
            $(this.id+'_ConfirmBtn').observe('click', this.onEvent.curry(this.onConfirmed, this.srcElement));
            $(this.id+'_CancelBtn').observe('click', this.onEvent.curry(this.onCanceled, this.srcElement));
            break;
         case 'message':
            new Insertion.Bottom(document.body, getRawDialog(this.id, title, body, sprintf(
               '<input type="button" value="%s" id="%s_ConfirmBtn" class="confirm"/>', confirm_text, this.id
            )).replace('class="dialog"', mClass));
            this.onInitDialog();
            $(this.id+'_ConfirmBtn').observe('click', this.onEvent.curry(this.onConfirmed, this.srcElement));
            break;
      }
      if (this.width) $(this.id).style.width = this.width + 'px';
      if ($(this.id+'_ConfirmBtn')) {
         $(this.id+'_ConfirmBtn').observe('mouseover', function(){ this.addClassName('onhover'); });
         $(this.id+'_ConfirmBtn').observe('mouseout', function(){ this.removeClassName('onhover'); });
      }
      if ($(this.id+'_CancelBtn')) {
         $(this.id+'_CancelBtn').observe('mouseover', function(){ this.addClassName('onhover'); });
         $(this.id+'_CancelBtn').observe('mouseout', function(){ this.removeClassName('onhover'); });
      }
      funpDlg.show(this.id);
   },
   setWidth: function(width) {this.width = width;},
   onConfirmed:  function() {return true;},
   onCanceled:   function() {return true;},
   onInitDialog: function() {},
   showChoice:   function(title, body, confirm_text, cancel_text) { this.initDialog('choice', title, body, confirm_text, cancel_text); },
   showMessage:  function(title, body, confirm_text) { this.initDialog('message', title, body, confirm_text); }
});

function setCursorPosition(elemId, caretPos) {
   var elem = document.getElementById(elemId);
   if(elem != null) {
      if(elem.createTextRange) {
         var range = elem.createTextRange();
         range.move('character', caretPos);
         range.select();
      } else {
         if(elem.selectionStart) {
            elem.focus();
            elem.setSelectionRange(caretPos, caretPos);
         } else elem.focus();
      }
   }
}

function getCursorPosition(id){
   var textarea = $(id);
   textarea.focus();

   // get selection in firefox, opera, …
   if (typeof(textarea.selectionStart) == 'number') {
      return textarea.selectionStart;
   } else if(document.selection) {
      var selection_range = document.selection.createRange().duplicate();
      if (selection_range.parentElement() == textarea) {
         // Check that the selection is actually in our textarea
         // Create three ranges, one containing all the text before the selection,
         // one containing all the text in the selection (this already exists), and one containing all
         // the text after the selection.
         var before_range = document.body.createTextRange();
         before_range.moveToElementText(textarea); // Selects all the text
         before_range.setEndPoint("EndToStart", selection_range); // Moves the end where we need it

         var after_range = document.body.createTextRange();
         after_range.moveToElementText(textarea); // Selects all the text
         after_range.setEndPoint("StartToEnd", selection_range); // Moves the start where we need it

         var before_finished = false, selection_finished = false, after_finished = false;
         var before_text, untrimmed_before_text, selection_text, untrimmed_selection_text, after_text, untrimmed_after_text;

         // Load the text values we need to compare
         before_text = untrimmed_before_text = before_range.text;
         selection_text = untrimmed_selection_text = selection_range.text;
         after_text = untrimmed_after_text = after_range.text;

         // Check each range for trimmed newlines by shrinking the range by 1 character and seeing
         // if the text property has changed. If it has not changed then we know that IE has trimmed
         // a \r\n from the end.
         do {
            if (!before_finished) {
               if (before_range.compareEndPoints("StartToEnd", before_range) == 0) {
                  before_finished = true;
               } else {
                  before_range.moveEnd("character", -1)
                  if (before_range.text == before_text) {
                     untrimmed_before_text += "\r\n";
                  } else {
                     before_finished = true;
                  }
               }
            }
            if (!selection_finished) {
               if (selection_range.compareEndPoints("StartToEnd", selection_range) == 0) {
                  selection_finished = true;
               } else {
                  selection_range.moveEnd("character", -1)
                  if (selection_range.text == selection_text) {
                     untrimmed_selection_text += "\r\n";
                  } else {
                     selection_finished = true;
                  }
               }
            }
            if (!after_finished) {
               if (after_range.compareEndPoints("StartToEnd", after_range) == 0) {
                  after_finished = true;
               } else {
                  after_range.moveEnd("character", -1)
                  if (after_range.text == after_text) {
                     untrimmed_after_text += "\r\n";
                  } else {
                     after_finished = true;
                  }
               }
            }
         } while ((!before_finished || !selection_finished || !after_finished));
         
         // Untrimmed success test to make sure our results match what is actually in the textarea
         // This can be removed once you’re confident it’s working correctly
         var untrimmed_text = untrimmed_before_text + untrimmed_selection_text + untrimmed_after_text;
         var untrimmed_successful = false;
         if (textarea.value == untrimmed_text) {
            untrimmed_successful = true;
         }
         
         // ** END Untrimmed success test
         var startPoint = untrimmed_before_text.length;
         return startPoint;
      }
   }
}


