﻿//====
//use the Common variable in your script.
//====
function _CommonCls_() {}
//----
_CommonCls_.prototype.trim = function(text) {
  if (text == null) return null;
  else return (typeof(text) != "string") ? null : text.replace(/(^\s*)|(\s*$)/g, '');
}
//----
_CommonCls_.prototype.getTag = function(tagID) {
	if (typeof(tagID) != "string") return null;
	else {
		if (document.getElementById) return document.getElementById(tagID);
		if (document.all) return document.all[tagID];
		if (document.layers) return document.layers[tagID];
	}
}
//----
_CommonCls_.prototype.getTags = function(tagID) {
	if (arguments.length > 1) {
		var m = [], c = arguments.length;
		for (var i = 0; i < c; i++) m.push(this.getTag(arguments[i]));
		return m;
	}
	else {
	  if (typeof(tagID) != "string") return null;
	  else {
		  if (document.getElementById) return document.getElementById(tagID);
		  if (document.all) return document.all[tagID];
		  if (document.layers) return document.layers[tagID];
	  }
	}
}
//----
_CommonCls_.prototype.getSelectedRadio = function(tagName) {
  if (typeof(tagName) == "string") {
    var m = document.getElementsByName(tagName);
    for (var i = 0; i < m.length; i++) { 
      if (m[i].type == "radio" && m[i].checked == true) return m[i];
    }
  }
  return null;
}
//----
_CommonCls_.prototype.getSelectedRadioValue = function(tagName, valueIfMissed) {
  if (valueIfMissed == undefined) valueIfMissed = null;
  var n = this.getSelectedRadio(tagName);
  return (n == null) ? valueIfMissed : n.value;
}
//----
_CommonCls_.prototype.submitForm = function(formTagID) {
  var n = this.getTag(formTagID);
  if (n != null) n.submit();
}
//----
_CommonCls_.prototype.cancelEvent = function(message) {
  if (message != undefined) alert(message);
  window.event.returnValue = false;
  window.event.cancel = true;
}
//----
_CommonCls_.prototype.confirm = function(message) {
  return window.confirm(message);
}
//----
_CommonCls_.prototype.alert = function(message, eventCancelable) {
  if (eventCancelable == undefined) eventCancelable = false;
  window.alert(message);
  if (eventCancelable == true) this.cancelEvent();
}
//----
_CommonCls_.prototype.getString = function(tagID, valueIfMissed) {
  if (valueIfMissed == undefined) valueIfMissed = null;
  try {
    var tagType = typeof(tagID);
    if (tagType != "string") return (tagType != "object") ? null : tagID.value;
    else {
		  var s = this.getTag(tagID).tagName.toLowerCase();
		  if (s == "input" || s == "select" || s == "textarea") return this.getTag(tagID).value;
		  if (s == "div" || s == "span") return this.getTag(tagID).innerText;
    }
  }
  catch (e) {
    return valueIfMissed;
  }
}
//----
_CommonCls_.prototype.getInteger = function(tagID, valueIfMissed)	{
  if (valueIfMissed == undefined) valueIfMissed = 0;
  try {
    var s = this.getString(tagID);
    return (s == null) ? valueIfMissed : parseInt(s, 10);
  }
  catch (e) {
    return valueIfMissed;
  }
}
//----
_CommonCls_.prototype.openWindow = function(title, width, height, url) {
  if (title == undefined) title = "";
  if (width == undefined) width = 560;
  if (height == undefined) height = 360;
  var x = window.open(url, title, "width=" + width + ",height=" + height);
  return x;
}
//----
_CommonCls_.prototype.showMessage = function(message, title, width, height)	{
  if (title == undefined) title = "";
  if (width == undefined) width = 560;
  if (height == undefined) height = 160;
  var x = window.open(null, title, "width=" + width + ",height=" + height + ",scrollbars=yes");
  x.document.write("<html><title>"+ title + "</title><style>body{margin:10px;font:9pt Arial;}</style><body>" + message + "</body></html>");
  x.document.close();
  return x;
}
//----
_CommonCls_.prototype.parseYYYYMMDD = function(yyyyMMdd)	{
  var tagType = typeof(yyyyMMdd);
  if (tagType != "string") return null;
  else {
    if (yyyyMMdd.length < 6) return null;
    else {
      var x = new Date();
      x.setFullYear(parseInt(yyyyMMdd.substr(0, 4), 10));
      x.setMonth(parseInt(yyyyMMdd.substr(4, 2), 10) - 1);
      var dy = (yyyyMMdd.length < 8) ? 1 : parseInt(yyyyMMdd.substr(6, 2), 10);
      x.setDate(dy);
      x.setHours(0);
      x.setMinutes(0);
      x.setSeconds(0);
      return x;
    }
  }
}
//----
_CommonCls_.prototype.parseHHMMSS = function(hhmmss)	{
  var tagType = typeof(hhmmss);
  if (tagType != "string") return null;
  else {
    if (hhmmss.length < 4) return null;
    else {
      var x = new Date();
      x.setFullYear(1970);
      x.setMonth(0);
      x.setDate(1);
      x.setHours(parseInt(hhmmss.substr(0, 2), 10));
      x.setMinutes(parseInt(hhmmss.substr(2, 2), 10));
      var sec = (hhmmss.length < 6) ? 0 : parseInt(hhmmss.substr(4, 2), 10);
      x.setSeconds(sec);
      return x;
    }
  }
}
//----
_CommonCls_.prototype.toUTF8 = function(str) {
  var c, d = new Array();
  for (var i = 0; i < str.length; i++) {
    c = str.charCodeAt(i);
    if (c <= 0x7f) d.push(str.charAt(i));
    else {
      if (c >= 0x80 && c <= 0x7ff) {
        d.push(String.fromCharCode(((c >> 6) & 0x1f) | 0xc0));
        d.push(String.fromCharCode((c & 0x3f) | 0x80));
      }
      else {
        d.push(String.fromCharCode((c >> 12) | 0xe0));
        d.push(String.fromCharCode(((c >> 6) & 0x3f) | 0x80));
        d.push(String.fromCharCode((c & 0x3f) | 0x80));
      }
    }
  }
  return d.join("");
}
//----
var Common = new _CommonCls_();