var UserAgent = navigator.userAgent.toLowerCase();
var isIE = ((UserAgent.indexOf("msie") != -1) && (UserAgent.indexOf("opera") == -1) && (UserAgent.indexOf("webtv") == -1)); 
var isGecko = (UserAgent.indexOf("gecko") != -1);
var isOpera = (UserAgent.indexOf("Opera") != -1);
var isDesconhecido = (!(isIE || isGecko || isOpera));

function insertJS(url)
{
	var headID = document.getElementsByTagName("head")[0];         
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	newScript.src = url;
	headID.appendChild(newScript);
}

function ltrim( s )
{
  return s.replace( /^\s*/, "" );
}

function rtrim( s )
{
  return s.replace( /\s*$/, "" );
}

function trim( s )
{
  return rtrim(ltrim(s));
}

function isFunction(a)
{
    return typeof a == 'function';
}

function isNull(a)
{
    return typeof a == 'object' && !a;
}

function isNumber(a)
{
    return typeof a == 'number' && isFinite(a);
}

function isObject(a)
{
    return (a && typeof a == 'object') || isFunction(a);
}

function isArray(a)
{
   return (a.constructor.toString().indexOf("Array") != -1)
}

function isString(a)
{
    return typeof a == 'string';
}

function isBoolean(a)
{
    return typeof a == 'boolean';
}

function isUndefined(a)
{
    return typeof a == 'undefined';
}

function getAvailableHeight()
{
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' )
  {
	//Non-IE
	myHeight = window.innerHeight;
  }
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
  {
	//IE 6+ in 'standards compliant mode'
	myHeight = document.documentElement.clientHeight;
  }
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  {
	//IE 4 compatible
	myHeight = document.body.clientHeight;
  }
  return (myHeight);
}

function getAvailableWidth()
{
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' )
  {
	//Non-IE
	myWidth = window.innerWidth;
  }
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
  {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
  }
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
  }
  return (myWidth);
}

function getCheckedValue(radioObj)
{
  if(!radioObj) {return "";}
  var radioLength = radioObj.length;
  if(radioLength == undefined)
  {
    if(radioObj.checked) {return radioObj.value;}
    else {return "";}
  }
  for(var i = 0; i < radioLength; i++)
  {
    if(radioObj[i].checked)
    {
      return radioObj[i].value;
    }
  }
  return "";
}

function setCheckedValue(radioObj, newValue)
{
  if(!radioObj) {return;}
  var radioLength = radioObj.length;
  if(radioLength == undefined)
  {
    radioObj.checked = (radioObj.value == newValue.toString());
    return;
  }
  for(var i = 0; i < radioLength; i++)
  {
    radioObj[i].checked = false;
    if(radioObj[i].value == newValue.toString())
    {
      radioObj[i].checked = true;
    }
  }
}

function getObjById(oID)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(oID);
		if (this.style)
		{
			this.style = document.getElementById(oID).style;
		}
	}
	else if (document.all)
	{
		this.obj = document.all[oID];
		if (this.style)
		{
			this.style = document.all[oID].style;
		}
	}
	else if (document.layers)
	{
		this.obj = getObjNN4(document,oID);
		if (this.style)
		{
			this.style = this.obj;
		}
	}
}

function getObjNN4(obj,oID)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == oID)
			foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],oID);
		if (tmp)
			foundLayer = tmp;
	}
	return foundLayer;
}

function getElementLeft(Elem)
{
	var elem = new getObj(Elem);
  if (elem != null)
  {
    return (getLeft(elem));
  }
  else
  {
    return 0;
  }
}

function getLeft(elem)
{
  if (isObject(elem))
  {
    xPos = elem.offsetLeft;
    tempEl = elem.offsetParent;
    while (tempEl != null)
    {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
    return xPos;
  }
  else
  {
    return 0;
  }
}

function getElementTop(Elem)
{
	var elem = new getObj(Elem);
  if (elem != null)
  {
    return getTop(elem);
  }
  else
  {
    return 0;
  }
}

function getTop(elem)
{
  if (isObject(elem))
  {
    yPos = elem.offsetTop;
    tempEl = elem.offsetParent;
    while (tempEl != null)
    {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    return yPos;
  }
  else
  {
    return 0;
  }
}

function enter2br(id)
{
	if (document.all)
	{
		var obj = document.all[id];
	}
	else
	{
		var obj = document.getElementById(id);
	}
	var sobj = obj.value;
	sobj = sobj.replace(/\r|\n/g, '\r\n');
	sobj = sobj.replace(/\r\n/g, '<br />');
	return (sobj);
}

function valueTextArea(id)
{
	if (document.all)
	{
		var obj = document.all[id];
	}
	else
	{
		var obj = document.getElementById(id);
	}
	var txt64 = encodeBase64(obj.value);
	if (isIE)
	{
		txt64 = txt64.replace(/\n/g, '');
	}
	return (txt64);
}
