function $_(_id)
{
  var elem = document.getElementById(_id);
  if(elem)
  {
    //make sure that it is a valid match on id
    if(elem.attributes['id'].value == _id)
    {
      return elem;
    }
    else
    {
      //otherwise find the correct element
      for(var i=1;i<document.all[_id].length;i++)
      {
        if(document.all[_id][i].attributes['id'].value == _id)
        {
          return document.all[_id][i];
        }
      }
    }
  }
}

function log(msg)
{
	//$_("divConsole").innerHTML = msg + "<br/>" + $_("divConsole").innerHTML;
}


function formatCurrency(valor, _places)
{
	var places = 2;
	if (_places != undefined) places = _places;
	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(valor);
	num.setPlaces(places, false);
	num.setCurrencyValue('');
	num.setCurrency(false);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	if (places > 0) num.setSeparators(true, '.', ',');
	else num.setSeparators(true, '.', '');
	return num.toFormatted();
}

function reemplazarParteTexto(pTexto, pTxtReemplazar, pTxtReemplazo)
{
	if(pTexto != undefined && pTexto != null && pTexto.length > 0)
	{
		while(pTexto.indexOf(pTxtReemplazar) != -1)	pTexto = pTexto.replace(pTxtReemplazar, pTxtReemplazo);
	}

	return pTexto;
}

function textoDefecto(pObjecto, pTextoDefecto)
{
	if(pObjecto.value == '') 										pObjecto.value = pTextoDefecto;
	else if(pObjecto.value == pTextoDefecto)						pObjecto.value = '';
}

function borraCapa(pIdObjeto)
{
	$_(pIdObjeto).innerHTML = "";
	$_(pIdObjeto).style.display = "none";
}

function escribeCapa(pIdObjeto, pMsg)
{
	$_(pIdObjeto).innerHTML = pMsg;
}

function limitedText(pTexto, pLongitud, pNumeroPuntos)
{	
	if(pTexto == null || pTexto == "" || pTexto.length <= pLongitud)	return pTexto;
	else
	{
		var puntos = "";
		for(var cont = 0; cont < pNumeroPuntos; cont ++)	puntos += ".";
		return pTexto.substring(0, pLongitud - pNumeroPuntos) + puntos;
	}
}

function tamanoMaxTexto(nombreCampo, longitud)
{
	if(document.getElementById(nombreCampo).value.length > longitud)	
	{
		document.getElementById(nombreCampo).value = document.getElementById(nombreCampo).value.substring(0,longitud);
	}
}

function mostrarCapaError(idCapaError)
{
	idCapaError = "#" + idCapaError;
	
	$(idCapaError).dialog("open"); 
}

function mostrarCapaErrorPrivada(idCapaError)
{	
	mostrarCapa(idCapaError);
}

function cierraCapaErrorPrivada(idCapaError)
{
	cerrarCapa(idCapaError);
}

function cerrarCapa(idCapa)
{
	$_(idCapa).style.display = "none";
}

function mostrarCapa(idCapa)
{	
	$_(idCapa).style.display = "block";
}

function checkearCheck(idCheck)
{	
	$_(idCheck).checked = "checked";
}

function fRedirHttps(urlInsegura, urlSegura)
{
	var url = window.location.href;
	
	if(url.indexOf("https://") == -1)
	{
		window.location.href = urlSegura + url.substring(url.lastIndexOf(urlInsegura) + urlInsegura.length);
	}
}
/*
* css_browser_selector(navigator.userAgent);
* Funció que retorna el navegador del client
*/
function css_browser_selector(u) 
{
	var ua = u.toLowerCase(), is = function(t){return ua.indexOf(t) > -1;}
	, g = 'gecko', w = 'webkit', s = 'safari', o = 'opera', h = document.documentElement, b = [
		(!(/opera|webtv/i.test(ua)) && /msie\s(\d)/.test(ua)) ? ('ie ie' + RegExp.$1)
			: is('firefox/2') ? g + ' ff2'	
				: is('firefox/3.5') ? g + ' ff3_5'
					: is('firefox/3.6')? g+' ff3_6'
						: is('firefox/3') ? g + ' ff3'
							: is('gecko/') ? g
								: is('opera') ? o
									+ (/version\/(\d+)/
										.test(ua) ? ' ' + o + RegExp.$1
										: (/opera(\s|\/)(\d+)/
											.test(ua) ? ' ' + o + RegExp.$2
											: ''))
								: is('konqueror') ? 'konqueror'
									: is('chrome') ? w + ' chrome'
										: is('iron') ? w + ' iron'
											: is('applewebkit/') ? w + ' ' + s + (/version\/(\d+)/ .test(ua) ? ' ' + s + RegExp.$1 : '')
												: is('mozilla/') ? g
													: '',
			is('j2me') ? 'mobile'
				: is('iphone') ? 'iphone'
					: is('ipod') ? 'ipod'
						: is('mac') ? 'mac'
							: is('darwin') ? 'mac'
								: is('webtv') ? 'webtv'
									: is('win') ? 'win'
										: is('freebsd') ? 'freebsd'
											: (is('x11') || is('linux')) ? 'linux'
												: '',
			'js' ];
	c = b.join(' ');
	h.className += ' ' + c;
	return c;
}

