String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function ClassName(){
	this.getClassName=function (node){
		if (typeof node.tagName!="undefined"){
			if (node.className){
				return node.className;
			}
		}
		return false;
	}
	this.setClassName=function(){
		if (arguments[0].className) arguments[0].className+=" "+arguments[1];
		else arguments[0].className=arguments[1];
	}
	this.sobreEscriureClase=function (){
		arguments[0].className=arguments[1];
	}
}

function UtilsClassName(){
	heredaDe1=ClassName;
	heredaDe1();
	delete heredaDe1;
	this.eliminarClassName=function(node,classe){
		if (node.className){
			node.className=node.className.replace( new RegExp( classe, "gi" ), "" );
		}
	}
	return this;
}

function TagName(){
	this.getTagName=function (node){
		if (typeof node.tagName!="undefined")
			return node.tagName;
		return false;
	}
	this.setTagName=function (){}
}

function Atribut(){
	this.getAtributElement=function (node,nomAtribut){
		if (typeof node.tagName!="undefined"){
			var valorAtribut='';
			if (valorAtribut=node.getAttribute(nomAtribut)){
				return valorAtribut;
			}
		}
		return false;
	}
	this.setAtributElement=function(){
		arguments[0].setAttribute(arguments[1]);
	}
}

function ObjElement(){
	this.getAtributElement=function (node){
		return valorAtribut;
	}
	this.setAtributElement=function(){
		arguments[0]=arguments[1];
	}
}

function ObjValorAtribut(){
	var atributElement='';
	var valor='';
	var mirarNodesFills=false;
	var direccio='';
	var totsElsNodes=false;
	var exacte=false;
	this.getValorAtributElement=function (){
		return atributElement;
	}
	this.setValorAtributElement=function(argAtributElement){
		atributElement=argAtributElement;
	}
	this.getValor=function (){
		return valor;
	}
	this.setValor=function (argValor){
		valor=argValor;
	}
	this.getMirarNodesFills=function(){
		return mirarNodesFills;
	}
	this.setMirarNodesFills=function (argMirarNodesFills){
		mirarNodesFills=argMirarNodesFills;
	}
	this.getDireccio=function(){
		return direccio;
	}
	this.setDireccio=function (argDireccio){
		direccio=argDireccio;
	}
	this.getTotsElsNodes=function(){
		return totsElsNodes;
	}
	this.setTotsElsNodes=function (argTotsElsNodes){
		totsElsNodes=argTotsElsNodes;
	}
	this.getExacte=function(){
		return exacte;
	}
	this.setExacte=function (argExacte){
		exacte=argExacte;
	}
}

function ObjAtribut(){
	if (this.getValorAtributElement()=='className'){
		this.heredaDe=new ClassName();
		this.getAtributElement=this.heredaDe.getClassName;
		this.setAtributElement=this.heredaDe.setClassName;
	}
	else if (this.getValorAtributElement()=='tagName'){
		this.heredaDe=new TagName();
		this.getAtributElement=this.heredaDe.getTagName;
		this.setAtributElement=this.heredaDe.setTagName;
	}
	else if (typeof this.getValorAtributElement()=='string'){
		this.heredaDe=Atribut;
		this.heredaDe();	
	}
	else{
		this.heredaDe=ObjElement;
		this.heredaDe();
	}
	delete this.heredaDe;
}

function ContinuarAvallNodeFills(){
	this.continuar=function(node){
		if (node.childNodes) this.comencarMirarBuscarNode(node.firstChild);
		if (node.nextSibling) this.comencarMirarBuscarNode(node.nextSibling);
	}
}

function ContinuarAvall(){
	this.continuar=function(node){
		if (node.nextSibling) this.comencarMirarBuscarNode(node.nextSibling);
	}
}

function ContinuarAmuntMateixAlturaNode(){
	this.continuar=function(node){
		if (node.previousSibling) this.comencarMirarBuscarNode(node.previousSibling);
	}
}

function ContinuarAmunt(){
	this.continuar=function(node){
		if (node.parentNode) this.comencarMirarBuscarNode(node.parentNode);
	}
}

function ObjContinuar(){
	if (this.getDireccio()=='avall' && this.getMirarNodesFills())
		this.heredaDe=ContinuarAvallNodeFills;
	else if (this.getDireccio()=='avall' && !this.getMirarNodesFills())
		this.heredaDe=ContinuarAvall;
	else if (this.getDireccio()=='amunt' && this.getMirarNodesFills())
		this.heredaDe=ContinuarAmuntMateixAlturaNode;
	else
		this.heredaDe=ContinuarAmunt;
	this.heredaDe();
	delete this.heredaDe;
}

function ObjCondicioString(){
	this.condicio=function (node){
		var valorBuscat='';
		if (valorBuscat=this.getAtributElement(node,this.getValorAtributElement())){
			//DEBUG: alert(valorBuscat+'  '+valorBuscat.toUpperCase().indexOf(this.getValor().toUpperCase())+'   '+this.getValor().toUpperCase())
			if (valorBuscat.toUpperCase().indexOf(this.getValor().toUpperCase())!=-1){
				return true;
			}
		}
		return false;
	}	
}

function ObjCondicioStringExacte(){
	this.condicio=function (node){
		var valorBuscat='';
		if (valorBuscat=this.getAtributElement(node,this.getValorAtributElement())){
			if (valorBuscat.toUpperCase()==this.getValor().toUpperCase()){
				return true;
			}
		}
		return false;
	}	
}

function ObjCondicioElement(){
	this.condicio=function (node){
		if (node==this.getValor()){
			return true;
		}
		return false;
	}	
}

function ObjCondicio(){
	if (typeof this.getValor()!='string')
		this.heredaDe=ObjCondicioElement;
	else 	if (typeof this.getValor()=='string' && !this.getExacte())
		this.heredaDe=ObjCondicioString;
	else this.heredaDe=ObjCondicioStringExacte;
	this.heredaDe();
}

function Recursivitat(){
	this.mirarBuscarNode=function(node){
		this.nodeTrobat=false;
		this.comencarMirarBuscarNode(node);
		return this.nodeTrobat;
	}
	this.comencarMirarBuscarNode=function(node){
		var continuar=false;
		if (node){
			continuar=true;
			if (this.condicio(node)){
				this.nodeTrobat=true;
				this.node.push(node);
				if (!this.getTotsElsNodes()) continuar=false;
			}
		}
		if (continuar){
			this.continuar(node);
		}
		return this.nodeTrobat;
	}
}
function UtilsBuscarNode(node,argAtributElement,argValor,argMirarNodesFills,argDireccio,argTotsElsNodes,argExacte) {
	var objHerencia={herencia1:ObjValorAtribut,executaCodi:function (){this.setValor(argValor);this.setValorAtributElement(argAtributElement);this.setMirarNodesFills(argMirarNodesFills);this.setDireccio(argDireccio);this.setTotsElsNodes(argTotsElsNodes);this.setExacte(argExacte)},herencia2:ObjAtribut,herencia3:ObjContinuar,herencia4:ObjCondicio,herencia5:Recursivitat}
	for (i in objHerencia){
		this.heredaDe=objHerencia[i];
		this.heredaDe();
		}
	delete this.heredaDe;
	this.nodeTrobat=false;
	this.node=new Array();
	this.mirarBuscarNode(node);
}

function getElementsPerClassName(node,classe){
	var obj=new UtilsBuscarNode(node,'className',classe,true,'avall',true);
	if (obj.nodeTrobat) return obj.node;
	return obj.nodeTrobat;
}

function getElementPerClassName(node,classe){
	var obj=new UtilsBuscarNode(node,'className',classe,true,'avall',false);
	if (obj.nodeTrobat) return obj.node;
	return obj.nodeTrobat;
}


function getElementSiPertanyNodeClassName(node,classe){
	var obj=new UtilsBuscarNode(node,'className',classe,false,'amunt',false);
	if (obj.nodeTrobat) return obj.node[0];
	return obj.nodeTrobat;
}

function getElementsSiPertanyNodeClassNameTots(node,classe){
	var obj=new UtilsBuscarNode(node,'className',classe,false,'amunt',true);
	if (obj.nodeTrobat) return obj.node;
	return obj.nodeTrobat;
}

function getElementsPerTagName(node,tag){
	var obj=new UtilsBuscarNode(node,'tagName',tag,true,'avall',true);
	if (obj.nodeTrobat) return obj.node;
	return obj.nodeTrobat;
}

function getElementsPerTagNameExacte(node,tag){
	var obj=new UtilsBuscarNode(node,'tagName',tag,true,'avall',true,true);
	if (obj.nodeTrobat) return obj.node;
	return obj.nodeTrobat;
}

function getElementSiPertanyNodeTagName(node,tag){
	var obj=new UtilsBuscarNode(node,'tagName',tag,false,'amunt',false);
	if (obj.nodeTrobat) return obj.node[0];
	return obj.nodeTrobat;
}

function getElementsSiPertanyNodeTagNameTots(node,tag){
	var obj=new UtilsBuscarNode(node,'tagName',tag,false,'amunt',true);
	if (obj.nodeTrobat) return obj.node;
	return obj.nodeTrobat;
}

function getElementPerAtribut(node,nomAtribut,valorAtribut){
	var obj=new UtilsBuscarNode(node,nomAtribut,valorAtribut,true,'avall',true);
	if (obj.nodeTrobat) return obj.node;
	return obj.nodeTrobat;
}

function getElementSiPertanyNodeAtribut(node,nomAtribut,valorAtribut){
	var obj=new UtilsBuscarNode(node,nomAtribut,valorAtribut,false,'amunt',false);
	if (obj.nodeTrobat) return obj.node[0];
	return obj.nodeTrobat;
}

function getElementsSiPertanyNodeAtributTots(node,nomAtribut,valorAtribut){
	var obj=new UtilsBuscarNode(node,nomAtribut,valorAtribut,false,'amunt',true);
	if (obj.nodeTrobat) return obj.node;
	return obj.nodeTrobat;
}

function getElementPerTagNameMateixaAltura(node,tag){
	var obj=new	UtilsBuscarNode(node,'tagName',tag,true,'amunt',true);
	if (obj.nodeTrobat) return obj.node[0];
	return obj.nodeTrobat;
}

function getElementSiPertanyNode(node,nodeSiPertany){
	var obj=new UtilsBuscarNode(node,nodeSiPertany,node,false,'amunt',false);
	if (obj.nodeTrobat) return obj.node[0];
	return obj.nodeTrobat;
}

function pertanyNode(node,nodeSiPertany){
	var obj=new UtilsBuscarNode(node,nodeSiPertany,node,false,'amunt',false);
	return obj.nodeTrobat;
}

/*function init(){
	var obj=getElementsSiPertanyNodeTagNameTots(document.getElementById("trobat"),'div')
	alert(obj)
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(init);*/
