/* variaveis globais para as funções */
var objHttp;

var $obj = {
	create: function(type, id, parentObj) {
		var newObj = document.createElement(type);
		newObj.setAttribute('id', id);
		parentObj.appendChild(newObj);
    return newObj;
	}
}

function $(obj) {
  return document.getElementById(obj);
}

function $name(who, obj) {
  return who.getElementsByTagName(obj);
}

var $ajax = {
  connect: function() {
    if (window.XMLHttpRequest) {
      objHttp = new XMLHttpRequest();
    }
    else {
      if (window.ActiveXObject) {
        try {
          objHttp = new ActiveXObject('MSXML2.XMLHttp');
        }
        catch(error) {
          try {
            objHttp = new ActiveXObject('Microsoft.XMLHttp');
          }
          catch(error) {
            alert('Vá instalar um navegador que suporte Ajax!!!');
          }
        }
      }
    }
  },

  get: function(url) {

    objHttp.open('GET', url, true);
    objHttp.send(null);
  },

  post: function(url, fields) {
    objHttp.open('POST', url, true);
    objHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    objHttp.setRequestHeader('Content-Type', "application/x-www-form-urlencoded; charset=iso-8859-1");
    objHttp.send(fields);
  }
}

function _loadXML(docXML) {
  /* tenta carregar o documento pelo Internet Explorer */
  try {
    objXML = new ActiveXObject("Microsoft.XMLDOM");
  }
  catch(error) {
    /* tenta carregar o documento pelo Firefox */
    try {
      objXML = document.implementation.createDocument('', '', null);
    }
    catch(error) {
      alert(error.message);
    }
  }

  try {
    objXML.async = false;
    objXML.load(docXML);

    return(objXML);
  }
  catch(error) {
    alert(error.message);
  }

  return(null);
}

function maskFormat(obj, sMask, evtKeyPress) {
  var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

  /* captura o evento correto para o tipo de navegador usado */
  if (document.all) { /* Internet Explorer */
    nTecla = evtKeyPress.keyCode;
  }
  else if (document.layers) { /* Mozzila Firefox */
    nTecla = evtKeyPress.which;
  }
  else {
    nTecla = evtKeyPress.which;
    if (nTecla == 8) {
      return true;
    }
  }

  sValue = obj.value;

  /* Limpa todos os caracteres de formatação que já estiverem no campo. */
  sValue = sValue.toString().replace( "-", "" );
  sValue = sValue.toString().replace( "-", "" );
  sValue = sValue.toString().replace( ".", "" );
  sValue = sValue.toString().replace( ".", "" );
  sValue = sValue.toString().replace( "/", "" );
  sValue = sValue.toString().replace( "/", "" );
  sValue = sValue.toString().replace( ":", "" );
  sValue = sValue.toString().replace( ":", "" );
  sValue = sValue.toString().replace( "(", "" );
  sValue = sValue.toString().replace( "(", "" );
  sValue = sValue.toString().replace( ")", "" );
  sValue = sValue.toString().replace( ")", "" );
  sValue = sValue.toString().replace( " ", "" );
  sValue = sValue.toString().replace( " ", "" );
  fldLen = sValue.length;
  mskLen = sMask.length;

  i = 0;
  nCount = 0;
  sCod = "";
  mskLen = fldLen;

  while (i <= mskLen) {
    bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"));
    bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "));

    if (bolMask) {
      sCod += sMask.charAt(i);
      mskLen++;
    }
    else {
      sCod += sValue.charAt(nCount);
      nCount++;
    }

    i++;
  }

  obj.value = sCod;

  if (nTecla != 8) { /* tecla backspace */
    if (sMask.charAt(i-1) == "9") { /* teclas numerais */
      return ((nTecla > 47) && (nTecla < 58));
    }
    else { /* demais teclas e caracteres */
      return true;
    }
  }
  else {
    return true;
  }
}

/* funções destinadas a formatar os campos de moeda */
function clear(valor, validos) {
  /* retira caracteres invalidos da string */
  var result = "";
  var aux;
  for (var i=0; i < valor.length; i++) {
    aux = validos.indexOf(valor.substring(i, i+1));
    if (aux>=0) {
      result += aux;
    }
  }
  return result;
}

function currency(campo, tammax, teclapres, decimal) {
  var tecla = teclapres.keyCode;
      vr  = clear(campo.value, '0123456789');
      tam = vr.length;
      dec = decimal;

  if (tam < tammax && tecla != 8) {
    tam = vr.length + 1 ;
  }
  if (tecla == 8) {
    tam = tam - 1;
  }
  if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105) {
    if (tam <= dec) {
      campo.value = vr;
    }
    if ((tam > dec) && (tam <= 5)) {
      campo.value = vr.substr(0, tam - 2) + "," + vr.substr(tam - dec, tam);
    }
    if ((tam >= 6) && (tam <= 8)) {
      campo.value = vr.substr(0, tam - 5) + "." + vr.substr(tam - 5, 3) + "," + vr.substr(tam - dec, tam);
    }
    if ((tam >= 9) && (tam <= 11)) {
      campo.value = vr.substr(0, tam - 8) + "." + vr.substr(tam - 8, 3) + "." + vr.substr(tam - 5, 3) + "," + vr.substr(tam - dec, tam);
    }
    if ((tam >= 12) && (tam <= 14)) {
      campo.value = vr.substr(0, tam - 11) + "." + vr.substr(tam - 11, 3) + "." + vr.substr(tam - 8, 3) + "." + vr.substr(tam - 5, 3) + "," + vr.substr(tam - dec, tam);
    }
    if ((tam >= 15) && (tam <= 17)) {
      campo.value = vr.substr(0, tam - 14) + "." + vr.substr(tam - 14, 3) + "." + vr.substr(tam - 11, 3) + "." + vr.substr(tam - 8, 3) + "." + vr.substr(tam - 5, 3) + "," + vr.substr(tam - 2, tam);
    }
  }
}

function mostra(bullet, quem) {
	if (document.getElementById(quem).style.display == 'none') {
		document.getElementById(quem).style.display = 'block';
		document.getElementById(bullet).style.background = "url('../../images/bullet_menos.png') no-repeat";
	}
	else {
		document.getElementById(quem).style.display = 'none';
		document.getElementById(bullet).style.background = "url('../../images/bullet_mais.png') no-repeat";
	}
}
