function detect_os(){
	var v_sis="";
	if (navigator.platform.toUpperCase().indexOf("WIN")==-1){
		v_sis="mac";
	}else{
		v_sis="win";
	}
	return v_sis;					
}
var ns4=(document.layers)?true:false;
var ie4=(document.all)?true:false;
var dom=(!document.all && document.getElementById) ? true : false;
var v_plat=detect_os();

function canvi(celda,actiu){
	nom_cel="opcio"+celda;
	if (actiu=="S"){
		color='#EBD816';
	}else{
		color='#25753F';
	}
	if (!ns4){
		if (!dom){
			//explorer
			if (ie4){
				document.all[nom_cel].style.background=color;
			}
		}else{
			//ns6
			document.getElementById(nom_cel).style.background=color;
		}
	}
	return;
}

function val_fecha(v_dat){
		
	var mesos=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	var fecha=v_dat
				
	var dia="";
	var mes="";
	var ano="";
	
	var dia=fecha.substr(0,2);
	var se1=fecha.substr(2,1);
	var mes=fecha.substr(3,2);
	var se2=fecha.substr(5,1);
	var ano=fecha.substr(6,4);
								
	var ok=true;
	
	if((se1!="/") || (se2!="/")){
		ok=false;
	}
			
	if ( isNaN(dia) || isNaN(mes) || isNaN(ano) ) {
		ok=false;
	}
	
	n_dia=dia;
	n_mes=mes-1;
	n_ano=ano;
									
	if ((n_ano%4)==0){
		mesos[1]=29;
	}
	
	if ((n_dia<1) || (n_dia>mesos[n_mes])){
		ok=false;
	}
	
	if ((n_mes+1<1) || (n_mes+1>12)){
		ok=false;
	}
	
	if ( (n_ano==0) || (n_ano=="") ){
		ok=false;
	}
	
	return ok;
	
}

function test_mail(camp){
	camp.toLowerCase();
	sw_pas=true;
	if (camp==""){
		sw_pas=false;
	}else{
		if ((camp.indexOf("@")==-1) || (camp.indexOf(" ")!=-1) || (camp.indexOf(".")==-1)){
			sw_pas=false;
		}else{
			if (camp.length<6){
				sw_pas=false;
			}
		}
	}
	return sw_pas;
}

var v_fin;

//obri popup
function obrir_popup(url,f_amp,f_alt,nom_finestra){
	ancho=screen.width;				//ample pantalla
	alto=screen.height;				//alt pantalla
	v_top=(alto-f_alt)/2;	
	v_left=(ancho-f_amp)/2;
	
	if (typeof(v_fin)!="undefined"){
		v_fin.close();
	
	}
	
	v_fin=window.open(url,nom_finestra,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width="+f_amp+",height="+f_alt+",top="+v_top+",left="+v_left);
}

function setPointer(theRow, thePointerColor){
    if (typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined') {
        return false;
    }
	var row_cells_cnt = theRow.cells.length-1;
    for (var c = 0; c < row_cells_cnt; c++) {
        theRow.cells[c].bgColor = thePointerColor;
    }
	
	return true;
}

//ordena un select
function ordena_select(nom_form,nom_camp){
	//cream l'objecte del select
	var obj=eval("document."+nom_form+"."+nom_camp);
	//agafem la informació
	var llista=new Array();
	for (i=0;i<obj.length;i++){
		llista[llista.length]=obj.options[i].text+"||"+obj.options[i].value+'||'+obj.options[i].className;
	}
	//ordenam
	llista.sort();
	//inicialitzam el select
	borra_select(nom_form,nom_camp);
	//tornam a crear el select
	for (i=0;i<llista.length;i++){
		aux=llista[i].split("||");
		valor=aux[1];
		texte=aux[0];
		classe = aux[2]
		ele = new Option(texte,valor)
		ele.className = classe;
		obj.options[i] = ele;
	}
}

//borrar capsa select
function borra_select(nom_form,nom_camp){
	//inicialitzam el select de seleccionats
	var obj_sele=eval("document."+nom_form+"."+nom_camp);
	var num=obj_sele.length;
	for(i=0;i<num;i++){
		obj_sele.options[0]=null;
	}	
}

function marca_select(n_form,n_camp,valor){
	var obj_sele=eval("document."+n_form+"."+n_camp);
    for(i=0;i<obj_sele.length;i++){
    	if (obj_sele.options[i].value==valor){
        	obj_sele.options[i].selected=true;
		}else{
			obj_sele.options[i].selected=false;
		}
	}	
    return true;
}

//actualitzam select (llista -> array d'objectes Option (text,valor))
function update_select(nom_form,nom_camp,llista,inicial){

	var obj_sele=eval("document."+nom_form+"."+nom_camp);
	
	borra_select(nom_form,nom_camp);
	
	if (typeof(inicial)!='undefined'){
		obj_sele.options[0] = new Option(inicial,'');
	}
	/*
	for (i=0;i<llista.length;i++){
		obj_sele.options[obj_sele.options.length]= llista[i];	
	}
	*/
	for (cod in llista){
		
		//obj_sele.options[obj_sele.options.length]= llista[cod];	
		ele = new Option( llista[cod].text , llista[cod].value);
		ele.className = llista[cod].className;
		obj_sele.options[obj_sele.options.length]= ele;	
		
	}

}

// passam una data en format cadena dd/mm/aaaa
// i retorna un objecte data de javascript
function obj_data(str_data){
	var arr_aux=str_data.split("/");
	this.obj_data=new Date(arr_aux[2],parseInt(arr_aux[1],10)-1,arr_aux[0]);
	return this.obj_data;
}

//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)
}

//
function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
    
        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
		
    return value_string
	
}

Date.prototype.dma = dma;

function dma(){

	var dia=this.getDate();
	if (dia<10){
		dia="0"+dia;
	}
	
	var mes=this.getMonth()+1;
	if (mes<10){
		mes="0"+mes;
	}
	
	var ano=this.getFullYear();

	return dia+"/"+mes+"/"+ano;
	
}


//*********************************************
function punt(nom){
    this.nom=nom;
    this.x=0;
    this.y=0;
    this.getX=getX;
    this.getY=getY;
    this.getOffsetLeft=getOffsetLeft;
    this.getOffsetTop=getOffsetTop;
    //if (!is.ie){
	if (!document.all){
        //ns4 i ns6+ i altres
        this.objSp = document.images[this.nom];
        this.x = this.objSp.x;
        this.y = this.objSp.y;
    }else{
        //ie
        this.objSp = eval("document.images['"+this.nom+"']");
        this.x = this.getOffsetLeft(this.objSp);
        this.y = this.getOffsetTop(this.objSp);
    }
}
function getX(){
    return this.x;
}
function getY(){
    return this.y;
}
//*********************
//funcions per explorer
function getOffsetLeft (el) {
    var ol = el.offsetLeft;
    while ((el = el.offsetParent) != null){
        ol += el.offsetLeft;
    }
	return ol;
}
function getOffsetTop (el) {
    var ot = el.offsetTop;
    while((el = el.offsetParent) != null){
        ot += el.offsetTop;
    }
    return ot;
}
//*********************
//*********************************************

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/*extenem l'array amb el mčtode unshift*/

Array.prototype.unshift = unshift
function unshift() {

    // Store the total number of Value arguments
    var total_values = arguments.length
    
    // Make sure at least one Value argument was passed
    if (total_values > 0) {
    
        // First, shift the existing elements up
        for (counter = this.length + total_values - 1; counter >= total_values; counter--) {
            this[counter] = this[counter - total_values]
        }
        
        // Now insert the new values at the beginning
        for (counter = 0; counter < total_values; counter++) {
            this[counter] = arguments[counter]
        }
    }
	
}


