/************************* Predefined Text Management Macros********************************************/
function alertf(idText){alert (gText(idText));}
function confirmf(idText){return confirm(gText(idText));}
function printf(CharString){document.write (CharString);}
function Text(idText){printf (gText(idText));}
function TextControl(ControlId,idText){document.all[ControlId].value=gText(idText);}
function gText(idText){try{return TEXT.Item[idText].DATA;} catch(e) {return idText+'.undefined'}}
/*******************************************************************************************************/
function htmlEncode(source, display, tabs)
{
	function special(source)
	{
		var result = '';
		for (var i = 0; i < source.length; i++)
		{
			var c = source.charAt(i);
			if (c < ' ' || c > '~')
			{
				c = '&#' + c.charCodeAt() + ';';
			}
			result += c;
		}
		return result;
	}
	
	function format(source)
	{
		// Use only integer part of tabs, and default to 4
		tabs = (tabs >= 0) ? Math.floor(tabs) : 4;
		
		// split along line breaks
		var lines = source.split(/\r\n|\r|\n/);
		
		// expand tabs
		for (var i = 0; i < lines.length; i++)
		{
			var line = lines[i];
			var newLine = '';
			for (var p = 0; p < line.length; p++)
			{
				var c = line.charAt(p);
				if (c === '\t')
				{
					var spaces = tabs - (newLine.length % tabs);
					for (var s = 0; s < spaces; s++)
					{
						newLine += ' ';
					}
				}
				else
				{
					newLine += c;
				}
			}
			// If a line starts or ends with a space, it evaporates in html
			// unless it's an nbsp.
			newLine = newLine.replace(/(^ )|( $)/g, '&nbsp;');
			lines[i] = newLine;
		}
		
		// re-join lines
		var result = lines.join('<br />');
		
		// break up contiguous blocks of spaces with non-breaking spaces
		result = result.replace(/  /g, ' &nbsp;');
		
		// tada!
		return result;
	}

	var result = source;
	
	// ampersands (&)
	result = result.replace(/\&/g,'&amp;');

	// less-thans (<)
	result = result.replace(/\</g,'&lt;');

	// greater-thans (>)
	result = result.replace(/\>/g,'&gt;');
	
	if (display)
	{
		// format for display
		result = format(result);
	}
	else
	{
		// Replace quotes if it isn't for display,
		// since it's probably going in an html attribute.
		result = result.replace(new RegExp('"','g'), '&quot;');
	}
	

	// special characters
	result = special(result);
	
	// tada!
	return result;
}
/*******************************************************************************/
/* Redondeja un número */
function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
    //var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
    var newnumber = parseFloat(rnum).toFixed(rlength)
    return newnumber; // Output the result to the form field (change for your purposes)
}
/******************************************************************************************************************/
 //Formatear numero con decimales y separador de miles
//Objeto oNumero
function CNumero(numero)
{
    //Propiedades
    this.valor = numero || 0;
    this.dec = -1;
    //Metodos
    this.formato = numFormat;
    this.ponValor = ponValor;
    this.formato2 = numFormat2;
    //Definicion de los metodos
    function ponValor(cad)
    {
        if (cad =='-' || cad=='+') return
        if (cad.length ==0) return
        if (cad.indexOf('.') >=0)
            this.valor = parseFloat(cad);
        else
            this.valor = parseInt(cad);
    }
    function numFormat(dec, miles)
    {
    var num = this.valor, signo=3, expr;
    var cad = ""+this.valor;
    var ceros = "", pos, pdec, i;
    for (i=0; i < dec; i++)
        ceros += '0';
    pos = cad.indexOf('.')
    if (pos < 0)
        cad = cad+"."+ceros;
    else
        {
            pdec = cad.length - pos -1;
            if (pdec <= dec)
            {
                for (i=0; i< (dec-pdec); i++)
                    cad += '0';
            }
            else
            {
                num = num*Math.pow(10, dec);
                num = Math.round(num);
                num = num/Math.pow(10, dec);
                cad = new String(num);
            }
        }
    pos = cad.indexOf('.')
    if (pos < 0) pos = cad.lentgh
    if (cad.substr(0,1)=='-' || cad.substr(0,1) == '+')
       signo = 4;
    if (miles && pos > signo)
    do
      {
        expr = /([+-]?\d)(\d{3}[\.\,]\d*)/
        cad.match(expr)
        cad=cad.replace(expr, RegExp.$1+','+RegExp.$2)
      }
    while (cad.indexOf(',') > signo)
        if (dec<0) cad = cad.replace(/\./,'')
            return cad;
    
   }
   //-- comas per punts i punts per comes
   function numFormat2(dec, miles)
    {
        //-- obtinc el valor formatejat
        var cad = this.formato(dec, miles);
        //-- gestio de la coma
        while (cad.indexOf(',') > 0)
            cad = cad.replace (/\,/,'p');
        //-- gestio del punt
        while (cad.indexOf('.') > 0)
            cad = cad.replace (/\./,',');
        //-- gestio de la coma
        while (cad.indexOf('p') > 0)
            cad = cad.replace (/p/,'.');
        return cad;
    }
}//Fin del objeto oNumero:
//*************************************************************************
//** AQUESTA FUNCIÓ RETORNA EL FORMAT NUMERIC DE PUNTS I COMMES
//*************************************************************************
function CurFormat(sValor) {
    var cvalor = parseFloat(sValor).toFixed(2);
    var oNumero = new CNumero(cvalor);
    return oNumero.formato2(2, true);
}

