
function setErrField(form, fielddId, message) {
  form.markInvalid([{
    'id': fielddId,
    msg: message
  }]);
  form.findField(fielddId).focus();
}

function showMessage(msg, callback) {
  Ext.MessageBox.buttonText.ok = NetBook.NETBOOKAPP_SHOWMESSAGE_BTN_OK;
  Ext.Msg.show({
    title:NetBook.NETBOOKAPP_SHOWMESSAGE_TITLE,
    msg: msg,
    buttons: Ext.Msg.OK,
    width: 300,
    icon: Ext.MessageBox.INFO,
    fn: callback == null ? Ext.emptyFn : callback
 });
}

function showConfirm(msg, callback) {
  /** Ext.MessageBox.confirm('Confirm', msg, callback);  */
  Ext.MessageBox.buttonText.yes = NetBook.NETBOOKAPP_SHOWCONFIRM_BTN_YES;
  Ext.MessageBox.buttonText.no = NetBook.NETBOOKAPP_SHOWCONFIRM_BTN_NO;
  Ext.Msg.show({
    title: NetBook.NETBOOKAPP_SHOWCONFIRM_TITLE,
    msg: msg,
    buttons: Ext.Msg.YESNO,
    fn: callback,
    width: 300,
    icon: Ext.MessageBox.QUESTION
 });
}

function showActionFailureDlg(action) {
  switch (action.failureType) {
  case Ext.form.Action.CLIENT_INVALID:
    showMessage(NetBook.MESSAGE_ERR1);
    break;
  case Ext.form.Action.CONNECT_FAILURE:
    showMessage(NetBook.MESSAGE_ERR2);
    break;
  }
}

function showWaitingDlg(id, mess) {
  element = Ext.fly(id);
  if(!element) {
    element = Ext.getBody();
  }
  element.mask(mess ? mess : NetBook.ALL_LOADING, 'x-mask-loading');
}

function hideWaitingDlg(id) {
  element = Ext.fly(id);
  if(!element) {
    element = Ext.getBody();
  }
  element.unmask();
}

function truncate(text, width, lineCount, element) {
  element = Ext.fly(element);
  if(!element) {
    return text;
  }

  var textMetric = Ext.util.TextMetrics.createInstance(element);
  width = width * lineCount;
  if(text && textMetric.getWidth(text) > width) {
    var i = 2;
    while (textMetric.getWidth(text.substr(0, i) + '...') < width) {
      i = i + 1;
    }
    return text.substr(0, i) + '...';
  }
  return text;
}

function roundNumber(num, dec) {
  return Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
}

function formatNumber(number) {
  var number = Ext.util.Format.usMoney(number);
  number = number.replace('.00', '');
  return number.replace('$', '');
}

function formatDate(sDate, iFormat, oFormat, timezone) {
  if (!iFormat) {
    if (sDate.split(' ').length == 3) {
      iFormat = 'Y-m-d H:i:s P';
    } else {
      iFormat = 'Y-m-d H:i:s';
    }
  }
  if (!oFormat) {
    oFormat = 'y/m/d H:i';
  }
  var date = Date.parseDate(sDate, iFormat);
  date = parseDate(date, timezone);
  return date.format(oFormat);
}

function convertTimezone(timezone) {
  if(timezone == '0:0') {
    return timezone;
  }
  var isNegative = (timezone.indexOf('-') == 0);
  timezone = timezone.replace("+", "");
  timezone = timezone.replace("-", "");
  var arr = timezone.split(':');
  var result = (isNegative ? '-' : '') + arr[0].toInt();
  result += ':' + (isNegative ? '-' : '') + arr[1].toInt();
  return result;
}

function parseDate(date, timezone) {
  if(!date || !Ext.isDate(date)){
      return date;
  }
  if (!timezone) {
    timezone = GMT_OFFSET;
  }
  var localTz = date.getGMTOffset(true);
  timezone = convertTimezone(timezone);
  localTz = convertTimezone(localTz);
  if(localTz != timezone) {
    var arr = timezone.split(':');
    var localArr = localTz.split(':');
    if (arr.length == 2) {
      var intervalH = - localArr[0].toInt() + arr[0].toInt();
      var intervalM = - localArr[1].toInt() + arr[1].toInt();
      date = date.add(Date.HOUR, intervalH).add(Date.MINUTE, intervalM);
    }
  }
  return date;
}

String.prototype.toInt = function() {
  var value = this;
  if (value.length > 1) {
    value = value.indexOf('0') == 0 ? value.substr(1, 1) : value;
  }
  return parseInt(value);
}

String.prototype.trim2 = function() {
  return this.replace(/\n/g,"").replace(/\r/g,"").replace(/\t/g,"").replace(/^\s+|\s+$/g, '');
}

String.prototype.replaceAll = function(strTarget, strSubString) {
  var strText = this;
  var intIndexOfMatch = strText.indexOf( strTarget );

   // Keep looping while an instance of the target string
   // still exists in the string.
   while (intIndexOfMatch != -1){
   // Relace out the current instance.
   strText = strText.replace( strTarget, strSubString )

   // Get the index of any next matching substring.
   intIndexOfMatch = strText.indexOf( strTarget );
   }

   // Return the updated string with ALL the target strings
   // replaced out with the new substring.
   return( strText );
}

function convertDecimalNumber(number) {
  var s = number + '';
  return parseInt(s);
}

function checkInputNumbers(e) {
  var keynum;
  var keychar;

  if(window.event) { // IE
      keynum = e.keyCode;
  } else if(e.which) { // Netscape/Firefox/Opera
    keynum = e.which;
  }
  keychar = String.fromCharCode(keynum);
  ok = true;
  if (keynum && (keynum != 8) && (keynum != 13)) {
    var numcheck = /\d/;
    return numcheck.test(keychar);
  }
  return ok;
}
