
function isIEVersion(min,max)
{
  if(!window.clientInformation)
    return false;
  var app = window.clientInformation.appVersion;
  app.match(/MSIE (\d+|\d+\.\d+);/);
  var n = parseFloat(RegExp.$1);
  return (typeof min != "number" || n >= min)
      && (typeof max != "number" || n <= max);
}

function xmlhttpInit(o)
{
  if(o.xmlhttp)
  {
    if(!isIEVersion())
      return o.xmlhttp;
    delete o.xmlhttp;
  }
  
  if(window.XMLHttpRequest
  && (!window.ActiveXObject || location.protocol != "file:"))
  {
    o.xmlhttp = new XMLHttpRequest();
    return o.xmlhttp;
  }
  
  // Conditional Compilation of JScript/JavaScript in IE4+
  // IE4:  try~catch not used, "Microsoft.XMLHTTP"
  // Other: try~catch used, "Msxml2.XMLHTTP"
  /*@cc_on
  @if (@_jscript_version < 5)
  o.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  return o.xmlhttp;
  @else@*/
  try
  {
    o.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); // IE5~6
    return o.xmlhttp;
  }catch(e){ }
  /*@end@*/
}

function getHTMLElementsByTagName(e, tagName)
{
  if(e.getElementsByTagName && (e.msxmldom || !isIEVersion(0, 5.5)))
  {
    var dochtml = document.getElementsByTagName("html")[0];
    var ns = e.msxmldom ? dochtml.xmlns : dochtml.namespaceURI;
    return ns ? e.getElementsByTagNameNS(ns, tagName)
            : e.getElementsByTagName(tagName);
  }
  if(e.all)
    return tagName == "*" ? e.all : e.all.tags(tagName);
}

function getHTMLElementsByTagAndClass(e, tagName, className)
{
  var r = [];
  var es = getHTMLElementsByTagName(e, tagName);
  for(var i = 0, len = es.length; i < len; i++)
  {
    if(es[i].className == className)
      r[ r.length ] = es[i];
  }
  return r;
}

function getElementBySelfScript()
{
  if(document.scripts)
    return document.scripts[ document.scripts.length - 1 ];
  for(var e = document; e.nodeName.toLowerCase() != "script"; e = e.lastChild);
  return e;
}

function getHTMLContentByMetaHttpEquiv(httpEquiv)
{
  httpEquiv = httpEquiv.toLowerCase();
  var metas = getHTMLElementsByTagName(document, "meta");
  for(var i = 0, len = metas.length; i < len; i++)
    if(metas[i].httpEquiv.toLowerCase() == httpEquiv)
      return metas[i].content;
}

function createHTMLElement(tagName)
{
  var namespaceURI = document.getElementsByTagName("html")[0].namespaceURI;
  return typeof namespaceURI == "string"
    ? document.createElementNS(namespaceURI, tagName)
    : document.createElement(tagName)
    ;
}

function includeJS(filename, charset)
{
  var myself = getElementBySelfScript(); // alert(myself.src);
  if(myself.src.match(/^(.*\/)[^\/]+$/))
    filename = RegExp.$1 + filename;
  
  if(!filename.match(".js$"))
    filename += ".js";
  
  var includeJS;
  
  if(!document.includeJS)
  {
    document.includeJS = 
    {
      xmlhttp : null,
      filenames : [ filename ],
      selfsrc : myself.src,
      code : ""
    };
    xmlhttpInit(document.includeJS)
    includeJS = document.includeJS;
  }
  else
  {
    includeJS = document.includeJS;
    if(includeJS.selfsrc != myself.src)
    {
      includeJS.selfsrc = myself.src;
      includeJS.code = "";
    }
    
    var filenames = includeJS.filenames;
    for(var i = 0, len = filenames.length; i < len; i++)
      if(filenames[i] == filename)
        return "";
    filenames[ filenames.length ] = filename;
  }
  
  var xmlhttp = includeJS.xmlhttp;
  
  xmlhttp.open("GET", filename, false);
  
  if(xmlhttp.overrideMimeType)
  {
    var ctype = "text/plain";
    if(charset && charset.length > 0)
      ctype += ";charset=" + charset;
    xmlhttp.overrideMimeType(ctype);
  }
  else
  {
    if(charset)
    {
      var xmlhttp_charset;
      if(myself.charset && myself.charset.length > 0)
        xmlhttp_charset = myself.charset;
      else
      {
        var content = getHTMLContentByMetaHttpEquiv("content-type");
        if(content && content.match(/charset=([^;]+)/))
          xmlhttp_charset = RegExp.$1;
      }
      if(xmlhttp_charset != charset)
      {
        alert("includeJS(): Encode error."
          + "\ncurrent file:<" + myself.src + "> encode:" + xmlhttp_charset
          + "\ntarget file:<" + filename + "> demand:" + charset
          );
        return;
      }
    }
  }
  xmlhttp.send(null);
  
  if(window.execScript) // IE, Chrome
    window.execScript(xmlhttp.responseText, "javascript");
  else
    if(navigator.userAgent.indexOf("AppleWebKit/") < 0) // Firefox, Opera, etc.
      window.eval(xmlhttp.responseText);
    else
    { // Safari
      var parent = myself.parentNode;
      var script = createHTMLElement("script");
      var node;
      /*@cc_on
      @if (@_jscript_version >= 5)@*/
      try{ node = document.createCDATASection(xmlhttp.responseText); } // XHTML
      catch(e){ node = document.createTextNode(xmlhttp.responseText); }
      /*@end@*/
      script.type = myself.type;
      script.appendChild(node);
      parent.insertBefore(script, myself);
    }
}

/* End of include.js */

