function Browser() 
{
  var b = navigator.appName;

  if (!b) 
    alert('Unidentified browser.\nThis browser is not supported,');

  if (b.indexOf('Netscape')!=-1) 
    this.b = "NS";
  else if ((b=="Opera") || (navigator.userAgent.indexOf("Opera")>0)) 
    this.b = "Opera";
  else if (b=="Microsoft Internet Explorer") 
    this.b = "IE";

  var ua = navigator.userAgent.toLowerCase();
  if (ua.indexOf("win")>-1) 
    this.platform="win32";
  else if (ua.indexOf("mac")>-1) 
    this.platform="mac";
  else 
    this.platform="other";

  var ver   = navigator.appVersion;
  this.V    = parseInt(ver);

  this.NS   = (this.b=="NS" && this.V>=4);
  this.NS4  = (this.b=="NS" && this.V==4);
  this.NS6  = (this.b=="NS" && this.V>=5);

  this.IE   = (this.b=="IE" && this.V>=4);
  this.IE4  = (ver.indexOf('MSIE 4')>0);
  this.IE5  = (ver.indexOf('MSIE 5')>0);
  this.IE55 = (ver.indexOf('MSIE 5.5')>0);
  this.IE6  = (ver.indexOf('MSIE 6.0')>0);

  this.Opera = (this.b=="Opera");
  
  this.Dom = (document.createElement && document.appendChild && document.getElementsByTagName) ? true : false;
  this.Def = (this.IE || this.Dom); // most used browsers, for faster if loops

  this.ValidBrowser = this.NS6 || this.IE5 || this.IE55 || this.IE6;
}

var Is = new Browser();

if (!Is.ValidBrowser)
  alert("Internet browser is not valid version.\n  We support netscape 7 higher or Microsoft Internet Explorer 5 or higher");


if (Is.NS)
{
  HTMLElement.prototype.__defineGetter__
  ("FirstChild",
    function ()
    {
      var tmpNode = this.childNodes;
      for(var i=0; i<tmpNode.length; i++)
      {
        if(tmpNode[i].nodeType==1 ||
          (tmpNode[i].nodeType==3 && IsValidString(tmpNode[i].nodeValue)))
          return tmpNode[i];
      }

      return null;
    }
  );
}

// trim space charactors in string
function trimString(s)
{
	var l = s.length;
	var begin, end;
	for (begin = 0; begin<l; begin++)
	{ 
		c = s.charAt(begin);
		if (c!=" " && c!="\t" && c!="\r" && c!="\n")
			break;
	}

	for (end = l-1; end>=0; end--)
	{ 
		c = s.charAt(end);
		if (c!=" " && c!="\t" && c!="\r" && c!="\n")
			break;
	}

	if (begin>end)
		return '';

	return s.substring(begin, end+1);
}

function DomObj(id)
{
  return document.getElementById(id);
}

function openModalWin(url, args, sFeatures)
{
  if(window.navigator.appName=="Microsoft Internet Explorer")
  {
    return window.showModalDialog(url, args, sFeatures);
  }
  else
  {
    var nFeatures = "";
    var width  = ParseInt(getFeatureParam(sFeatures, "dialogWidth"));
    var height = ParseInt(getFeatureParam(sFeatures, "dialogHeight"));
    var left   = ParseInt(getFeatureParam(sFeatures, "left"));
    var top    = ParseInt(getFeatureParam(sFeatures, "top"));
    var status = getFeatureParam(sFeatures, "status");
    var resize = getFeatureParam(sFeatures, "resizable");
    var center = getFeatureParam(sFeatures, "center");

    if(center=="yes" || center=="1")
    {
      left = (window.screen.width  - width)/2;
      top  = (window.screen.height - height)/2;
    }

    nFeatures += "left=" + String(left);
    nFeatures += ", top=" + String(top);

    nFeatures += ", width=" + width;
    nFeatures += ", height=" + height;

    if(status=="yes" || status=="1")
      nFeatures += ", status=yes";
    else
      nFeatures += ", status=no";

    if(resize=="yes" || resize=="1")
      nFeatures += ", resizable=yes";
    else
      nFeatures += ", resizable=no";

    nFeatures += ", modal=yes";

    window.args = args;
    window.open(url, "", nFeatures);
    return window.returnValue;
  }
}

function getFeatureParam(sFeatures, name)
{
  var se = new RegExp(name, "g");
  var beginPos = sFeatures.search(se);
  var endPos   = 0;

  
  if(beginPos!=-1)
  {
    while(sFeatures.charAt(beginPos)!=':' && beginPos<sFeatures.length)
      beginPos++;

    if(beginPos>=sFeatures.length)
      return "";

    beginPos++;
    
    while(sFeatures.charAt(beginPos)==' ' && beginPos<sFeatures.length)
      beginPos++;

    if(beginPos>=sFeatures.length)
      return "";

    endPos = beginPos;

    while(sFeatures.charAt(endPos)!=' ' && sFeatures.charAt(endPos)!=';' && endPos<sFeatures.length)
      endPos++;

    return sFeatures.substring(beginPos, endPos);
  }

  return "";
}

function getDialogArguments()
{
  if(window.navigator.appName=="Microsoft Internet Explorer")
    return window.dialogArguments;
  else
    return window.opener.args;
}

function setDlgReturnValue(w, value)
{
  if (window.navigator.appName=="Microsoft Internet Explorer")
    w.returnValue = value;
  else
    w.opener.returnValue = value;
}

function TextToHtml(text)
{
  var divObj = document.createElement("DIV");
  divObj.innerText = text;
  return divObj.innerHTML;
}

function HtmlToText(html)
{
  var divObj = document.createElement("DIV");
  divObj.innerHTML = html;
  return divObj.innerText;
}

function AppendFormValue(form, name, value)
{
  var tInput = null;
  if(window.navigator.appName=="Microsoft Internet Explorer")
    tInput = document.createElement("<INPUT name='" + name + "'>");
  else
  {
    tInput = document.createElement("INPUT");
    tInput.name  = name;
  }

  tInput.type  = "hidden";
  tInput.value = value;

  form.appendChild(tInput);
}

// 看看SELECT中是否有给定的Text
function CheckOptionRepeat(Options, CheckText)
{
  for (var i=0; i<Options.length; i++)
  {
    var si = Options[i].text;
    if (si == CheckText)
      return true
  }

  return false;
}

function IsValidString(str)
{
  if(str==null || typeof(str)!="string" || str=="")
    return false;

  for(var i=0; i<str.length; i++)
  { 
    var ch = str.charAt(i);

    if(ch!=" " && ch!="\t" && ch!="\r" && ch!="\n")
      return true;
  }

  return false;
}

function GetXmlRootByID(xmlId)
{
  return Is.IE ? DomObj(xmlId).XMLDocument.firstChild : DomObj(xmlId).FirstChild;
}

function XomObj(node, tagName)
{
  return node.getElementsByTagName(tagName)[0];
}

function ParseInt(anyObj)
{
  if(anyObj==null)
    return 0;

  if(typeof(anyObj)=="number")
    return anyObj;

  var retNum = parseInt(anyObj)

  if(isNaN(retNum))
    return 0;

  return retNum;
}

function ShowSubmit()
{
  if(arguments.length)
    window.submitObj = arguments[0];

  if(window.submitObj==null)
  {
    var imgObj = document.createElement("IMG");
    imgObj.src = "img/submit.gif";
    imgObj.zIndex = 1;
    imgObj.style.position = "absolute";
    imgObj.style.display = "none";
    document.body.appendChild(imgObj);

    window.submitObj = imgObj;
  }

  var width = document.body.clientWidth;
  var height = document.body.clientHeight;

  var left = ParseInt((width-300)/2) + document.body.scrollLeft;;
  var top  = ParseInt((height-100)/2) + document.body.scrollTop;

  window.submitObj.style.left = left;
  window.submitObj.style.top = top;
  window.submitObj.style.display = "";
}

function HideSubmit()
{
  if(window.submitObj)
    window.submitObj.style.display = "none";
}
