function fm_jSet_NoValidar(){
	document.form.fm_CalValidar.value='';
	return true;
}
function fm_jSet_CalValidar(){
	document.form.fm_CalValidar.value='-2';
	return true;
}
/* ==============
Tractament d'arxius
============== */
function fm_jFileSetSts(Id) {
	if (document.getElementById('fl_'+Id+'_v').value!=''){
		fm_jChgDisplay('file_'+Id+'_sp','none');
		fm_jChgDisplay('fl_'+Id+'_sp','inline');
	}else{
		fm_jChgDisplay('file_'+Id+'_sp','inline');
		fm_jChgDisplay('fl_'+Id+'_sp','none');}
}
function fm_jFileChange(Id) {
	fm_jChgDisplay('file_'+Id+'_sp','none');
	document.getElementById('fl_'+Id+'_v').value=document.getElementById('fl_'+Id).value;
	document.getElementById('fl_'+Id+'_info_sp').innerHTML=fl_From_js_Pendentenviar;
	fm_jChgDisplay('fl_'+Id+'_sp','inline');
}
function fm_jFileClear(Id) {
	fm_jChgDisplay('file_'+Id+'_sp','inline');
	document.getElementById('fl_'+Id+'_v').value='';
	document.getElementById('fl_'+Id+'_info_sp').innerHTML='';
	var Aux=document.getElementById('file_'+Id+'_sp').innerHTML;
	document.getElementById('file_'+Id+'_sp').innerHTML=Aux;
	fm_jChgDisplay('fl_'+Id+'_sp','none');
	fm_jChgDisplay('fl_'+Id+'_img','none');
}
function fm_jChgDisplay(Id,Display) {
	document.getElementById(Id).style.display=Display;
}

/* ==============
Llista de sellecció múltiple
============== */
function fm_jLlista_MoveById(Tipus,Camp,Origen,Desti,Tots){
	fm_jLlista_MoveByObj(Tipus,document.getElementById(Camp),document.getElementById(Origen),document.getElementById(Desti),Tots);
}
function fm_jLlista_MoveByObj(Tipus,Camp,Origen,Desti,Tots){
	var i;
	for (i = Origen.length - 1; i>=0; i--) {
		if (Origen.options[i].selected || Tots==true) {
			var elOptNew = document.createElement('option');
			elOptNew.text = Origen.options[i].text;
			elOptNew.value = Origen.options[i].value;
			elOptNew.selected = true;
			if (Tipus == '+') {
				if (Camp.value=='') {Camp.value=Origen.options[i].value;}
				else{Camp.value +=  (',' + Origen.options[i].value);}
			}
			else{
				var AuxCampV=',' + Camp.value + ',';
				AuxCampV=AuxCampV.replace(',' + Origen.options[i].value + ',',',');
				if (AuxCampV.length==1){Camp.value ='';}
				else{Camp.value = AuxCampV.substring(1,AuxCampV.length-1);}
			}
			try {
				Desti.add(elOptNew, null); // standards compliant; doesn't work in IE
			}
			catch(ex) {
				Desti.add(elOptNew); // IE only
			}
			Origen.remove(i);
	}	}
	for (i = Desti.length - 1; i>=0; i--) {
		if (Desti.options[i].value=='') {Desti.remove(i);}	
	}
	//Camp.value = Camp.value.replaceAll(',,',',')
}

function fm_jLlista_SetById(Camp,Origen,Desti){
	var valorCamp=document.getElementById(Camp).value;
	var Origenx =	document.getElementById(Origen);
	var Destix	=	document.getElementById(Desti);
	var llista=valorCamp.split(',');
	var Tret;
	for (var j=0 ; j<=Origenx.length - 1; j++) {
		Tret = false;
		for (var i = llista.length - 1; i>=0; i--) {
  			if (llista[i] != '' ) {
				if (llista[i] == Origenx.options[j].value) {
					var elOptNew = document.createElement('option');
					elOptNew.text = Origenx.options[j].text;
					elOptNew.value = Origenx.options[j].value;
					elOptNew.selected = true;
					try {
						Destix.add(elOptNew, null); // standards compliant; doesn't work in IE
					}
					catch(ex) {
						Destix.add(elOptNew); // IE only
					}
					Origenx.remove(j);
					Tret = true;
					break;
		}  	}	}
		if (Tret==true) {j--;}
	}
	for (i = Destix.length - 1; i>=0; i--) {
		if (Destix.options[i].value=='') {Destix.remove(i);}	
	}
}	

/* ==============
Validacions
============== */

function Val_EsData(valor){
	if (valor.length == 0) {return true;}

	var sep="/";
	var pos1=valor.indexOf(sep);
	if (pos1 <0) {
		sep="-";
		pos1=valor.indexOf(sep);
	}
	if (pos1 <1) {return false;}

	var pos2=valor.indexOf(sep,pos1+1);
	if (pos2 < pos1+1) {return false;}

	var dd = parseInt(valor.substring(0,pos1),10);// dia
	if (isNaN(dd)) {return false;}

	var mm = parseInt(valor.substring(pos1+1,pos2),10);// mes
	if (isNaN(mm)) {return false;}

	var aaaa= parseInt(valor.substring(pos2+1),10);// any
	if (isNaN(aaaa)) {return false;}

	if (aaaa < 100) {aaaa+=2000;}
	if (dd<1 || dd>31) {return false;}
	if (mm<1 || mm>12) {return false;}
	if (mm==4 || mm==6 || mm==9 || mm==11){
		if (dd==31) {return false;}
	}
	if (mm==2) {
		var DiesFebrer=(((aaaa % 4 == 0) && ( (!(aaaa % 100 == 0)) || (aaaa % 400 == 0))) ? 29 : 28 );
		if (mm>DiesFebrer) {return false;}
	}
	return true;
}
function Val_EsNumero(valor){
	if (valor.length == 0) {return true;}

	var TeDecimals=false;
	var inici=0;
	if (valor.charAt(0)=="-") {inici+=1;}
	var i;
	for (i = inici; i < valor.length; i++){
	   	var c = valor.charAt(i);
		if (((c < "0") || (c > "9"))) {
			if (c == ",") {
				TeDecimals=true;
			}else{
				if (TeDecimals) {return false;}
				if (c != ".") {return false;}
			}
		}
	}
    return true;
}
function Val_EsText(valor,longitut){
	if (longitut == 0) {return true;}
	if (valor.length == 0) {return true;}
	if (valor.length > longitut) {return false;}
	return true;
}

function Val_EsEmail(Text){
	if (Text.length == 0) {return true;}
	var aPos=Text.indexOf("@");
	var dotPos=Text.lastIndexOf(".");
	if (aPos > 1 && aPos + 1 < dotPos){
        if  (dotPos + 3 <= Text.length){return true;}
	}
	return false;
} 

function Val_Error(Camp,Missatge){
	alert(Missatge);
	if (Camp == ''){return false;}
	if (document.form[Camp].length){
		document.form[Camp][0].focus();
	}else{
		document.form[Camp].focus();
	}
	return false;
}

function Val_GetValor(nomCamp,esArray){	
	if (esArray) {
		var aux = document.form[nomCamp];
		if (aux.length) {
			for (var i=0; i<aux.length; i++) {
				if (aux[i].checked) {return aux[i].value}
			}
			return ''; 
		} else {
			return document.form[nomCamp].value;
		}
	}else{
		return document.form[nomCamp].value;
	}	
}

// ---------------------------
// Eines Java
// ---------------------------
String.prototype.replaceAll = function( 
	strTarget, // The substring you want to replace
	strSubString // The string you want to replace in.
	){
	var mosquis = new RegExp(strTarget, "g"); // Crea una expesió regular per fer un sustitució reiterada /g
	return( this.replace(mosquis,strSubString));
}