function getIdioma(){
	// Buscamos la subcadena ".xx.html" para extraer el idioma.
	var i=location.href.search(/\...\.html/)+1;
	
	// Devolvemos el idioma. Si no existe, el idioma por defecto es el español.
	return (i==0) ? 'es' : location.href.substring( i, i+2 );
}

function escribeSelector(){    
	var idActual=getIdioma();
	//var idiomas=new Array('fr','de','en','es');
    var idiomas=new Array('fr','de','en','es');
	var SEPARADOR='<td width="5"><img src="/img/sp.gif" width="5" height="1" border="0"></td>';		
	var estado,width,height;	
	var html='';
	html+='<div id="selectorIdioma">';
	html+='<table cellspacing="0" cellpadding="0" border="0"><tr>';
	
	for( var i=0; i < idiomas.length; i++ ){
		estado = ( idiomas[i]==idActual ) ? 'on' : 'off';

		if( i != 0 ) {
            html+=SEPARADOR;
        }
		
		html+='<td valign="top">';
		if( estado == 'off' ){
			width='13';
			height='11';
			html+='<a href="javascript:wmsCambioIdioma(\''+idiomas[i]+'\')">';
		}
		else{
			width='23';
			height='21';
		}
		html+='<img src="/img/cabecera/band_'+estado+'_'+idiomas[i]+'.gif" width="'+width+'" height="'+height+'" border="0">';
		if( estado=='off' ){
			html+='</a>';
		}
		html+='</td>';	
	}
	
	html+='</tr></table>';
	html+='</div>';
    			
	document.write( html );			
}

// Borra un select
function borrarSelect( sel ){						
	while( sel.options.length > 0 ){
		sel.options[0] = null;
	}				
}				

function marcarSelect(objSelect,valor){
    if (objSelect.options){
        for (i = 0 ; i < objSelect.options.length ; i++){
            if (objSelect.options[i].value == valor){
                objSelect.options[i].selected = true;
            }else{
                objSelect.options[i].selected = false;
            }
        }
    }
}

function sendForm( id ){	
	if( (objForm = document.getElementById(id)) ){
		objForm.submit();
	}
}

function format_number(num){
    
    var number_string = num.toString()

    if (number_string.lastIndexOf(".")!=-1){
        //si el número és decimal
        aux=number_string.split('.');
        number_string=aux[0];
        part_decimal=aux[1]+'00';
        part_decimal=part_decimal.substr(0,2);
    } else {
        part_decimal='00';
    }
    
    if (parseFloat(number_string) >= 1000 || parseFloat(number_string) <= -1000) {
    
        var insert_position;

        // Calculate the position of the first comma
        switch (number_string.length % 3) {
            case 1 :
                insert_position = 1;
                break;
            case 2 :
                insert_position = 2;
                break;
            case 0 :
                insert_position = 3;
                break
        }
        while (insert_position < number_string.length) {
            number_string = number_string.left(insert_position) + "." + number_string.substring(insert_position)
            insert_position += 4
        }
        
    }
    
    return number_string+=','+part_decimal;

}

String.prototype.left = extract_left        
function extract_left(total_chars) {
    return this.substring(0, total_chars)
}

//funció per redonejar els decimals
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return (result3)
}

//funció per afegir zeros a les posicions decimals
function pad_with_zeros(rounded_value, decimal_places) {
    var value_string = rounded_value.toString()
    var decimal_part_length, lon;
    var decimal_location = value_string.indexOf(".") 

    if (decimal_location == -1) {
        decimal_part_length = 0;
        value_string += decimal_places > 0 ? "." : "";
    }else {
        if (decimal_location == 0) {
            // Estamos pasando algo del estilo .00 y lo transformamos a 0.00
            value_string = '0' + value_string;
            decimal_location++;
        }
        decimal_part_length = value_string.length - decimal_location - 1;
    }
    var pad_total = decimal_places - decimal_part_length;
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++){
            value_string += "0";
        }
    } else if (pad_total < 0) {
        lon = value_string.length + pad_total;
        value_string = value_string.substr(0, lon);
    }
    return value_string;
}