/*****************************************************************************
SHAJAX - General Utility Functions
Copyright (C) 2006  Daniel Dalgo

This library is free software; you can redistribute it and/or modify it under 
the terms of the GNU Lesser General Public License as published by the 
Free Software Foundation; either version 2.1 of the License, or (at your option) 
any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY 
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along 
with this library; if not, write to the Free Software Foundation, Inc., 59 
Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*****************************************************************************
*****************************************************************************
*****************************  CONTACT **************************************
							   -------
PLEASE, CONTACT US AT:
www.7bits.com.ec
ddalgo@7bits.com.ec
Quito, Ecuador
*****************************************************************************
***************************  EXTRA FILES ************************************
							 -----------
Also, please refer to the helper files annexed
- "updates.txt" 
- "bugs.txt"
for info about new features or bugs in this library, and also use it 
for post your own information. Contact us using the media referred above, in
the CONTACT section.

All the files referred here, in the EXTRA FILES section, and the new info
posted in those files are also covered for the license here mentioned.
*****************************************************************************/

// JavaScript

var Util = {
	/** Función encargada de devolver un elemento de acuerdo al id que se esté
	 * buscando, dentro del documento DOM o del elemento dado (la función reconoce
	 * automáticamente si es documento DOM o si es elemento).
	 * El valor de "ID" es un patrón de "Regular Expression", pero pasado a su formato
	 * de cadenas (es decir no en formato Perl-like)
	 */ 
	getElementoPorID	:	function(documentDOM_oElemento, ID){
								/** Validaciones
								 */
								if(typeof documentDOM_oElemento=="undefined" || documentDOM_oElemento==null){
									return null;
								}
								if(typeof ID=="undefined" || ID==null){
									return null;
								}
								
								if(documentDOM_oElemento.documentElement){
									return Util._getElementoPorAtributoRecursivo(documentDOM_oElemento.documentElement, "id", ID);
								} else {
									return Util._getElementoPorAtributoRecursivo(documentDOM_oElemento, "id", ID);
								}
							},

	/** Función encargada de devolver un elemento que tenga un atributo especificado con
	 * en valor indicado, dentro del documento DOM o del elemento dado (la función reconoce
	 * automáticamente si es documento DOM o si es elemento)
	 * El valor de "valor" es un patrón de "Regular Expression", pero pasado a su formato
	 * de cadenas (es decir no en formato Perl-like)
	 */
	getElementoPorAtributo	:	function(documentDOM_oElemento, atributo, valor){
									/** Validaciones
									 */
									if(typeof documentDOM_oElemento=="undefined" || documentDOM_oElemento==null){
										return null;
									}
									if(typeof atributo=="undefined" || atributo==null){
										return null;
									}
									if(typeof valor=="undefined" || valor==null){
										return null;
									}
									
									/** Se verifica y corrige el nombre de atributo, de ser necesario (por browser)
									 */
									atributo=Util._verificarAtributo(atributo);
									
									if(documentDOM_oElemento.documentElement){
										return Util._getElementoPorAtributoRecursivo(documentDOM_oElemento.documentElement, atributo, valor);
									} else {
										return Util._getElementoPorAtributoRecursivo(documentDOM_oElemento, atributo, valor);
									}
								},

	/** Función privada encargada de hacer el recorrido recursivo por todos los hijos
	 * del elemento especificado en busca de un elemento con atributo y valor específicados
	 * El valor de "valor" es un patrón de "Regular Expression", pero pasado a su formato
	 * de cadenas (es decir no en formato Perl-like)
	 */
	_getElementoPorAtributoRecursivo:	function(oElement, atributo, valor){
											if(typeof oElement.getAttribute!="undefined"){
												if(oElement.getAttribute(atributo)!=null && Util._isCumplePatron(oElement.getAttribute(atributo), valor)){
													return oElement;
												}
											}

											for(var i=0; i<oElement.childNodes.length; i++){
												if(oElement.childNodes[i].nodeType==1){
													var oElementTemp=Util._getElementoPorAtributoRecursivo(oElement.childNodes[i], atributo, valor);
								
													if(oElementTemp!=null){
														return oElementTemp;
													}
												}
											}
								
											return null;
										},
	/** Función encargada de devolver un CONJUNTO de elementos de acuerdo al id que se esté
	 * buscando, dentro del documento DOM o del elemento dado (la función reconoce
	 * automáticamente si es documento DOM o si es elemento)
	 * El valor de "ID" es un patrón de "Regular Expression", pero pasado a su formato
	 * de cadenas (es decir no en formato Perl-like)
	 */ 
	getElementosPorID	:	function(documentDOM_oElemento, ID){
								/** Validaciones
								 */
								if(typeof documentDOM_oElemento=="undefined" || documentDOM_oElemento==null){
									return null;
								}
								if(typeof ID=="undefined" || ID==null){
									return null;
								}
								
								if(documentDOM_oElemento.documentElement){
									return Util._getElementosPorAtributoRecursivo(documentDOM_oElemento.documentElement, "id", ID);
								} else {
									return Util._getElementosPorAtributoRecursivo(documentDOM_oElemento, "id", ID);
								}
							},

	/** Función encargada de devolver un CONJUNTO de elementos que tenga un atributo especificado con
	 * en valor indicado, dentro del documento DOM o del elemento dado (la función reconoce
	 * automáticamente si es documento DOM o si es elemento)
	 * El valor de "valor" es un patrón de "Regular Expression", pero pasado a su formato
	 * de cadenas (es decir no en formato Perl-like)
	 */
	getElementosPorAtributo	:	function(documentDOM_oElemento, atributo, valor){
									/** Validaciones
									 */
									if(typeof documentDOM_oElemento=="undefined" || documentDOM_oElemento==null){
										return null;
									}
									if(typeof atributo=="undefined" || atributo==null){
										return null;
									}
									if(typeof valor=="undefined" || valor==null){
										return null;
									}
									
									/** Se verifica y corrige el nombre de atributo, de ser necesario (por browser)
									 */
									atributo=Util._verificarAtributo(atributo);
									
									if(documentDOM_oElemento.documentElement){
										return Util._getElementosPorAtributoRecursivo(documentDOM_oElemento.documentElement, atributo, valor);
									} else {
										return Util._getElementosPorAtributoRecursivo(documentDOM_oElemento, atributo, valor);
									}
								},

	/** Función privada encargada de hacer el recorrido recursivo por todos los hijos
	 * del elemento especificado en busca de un elementos con un atributo y valor iguales 
	 * a los específicados
	 * El valor de "valor" es un patrón de "Regular Expression", pero pasado a su formato
	 * de cadenas (es decir no en formato Perl-like)
	 */
	_getElementosPorAtributoRecursivo:	function(oElement, atributo, valor){
											/** Crea un arreglo en el cual se almacenarán los elementos
											 * a medida que se vayan obteniendo
											 */
											var arregloElementos=new Array();
											
											/** Si el elemento es cabe dentro de los requerimientos, se
											 * lo añade al arreglo
											 */
											if(typeof oElement.getAttribute!="undefined"){
												if(oElement.getAttribute(atributo)!=null && Util._isCumplePatron(oElement.getAttribute(atributo), valor)){
													arregloElementos[arregloElementos.length]=oElement;
												}
											}

											/** Se recorre a través de los elementos hijos
											 */
											for(var i=0; i<oElement.childNodes.length; i++){
												if(oElement.childNodes[i].nodeType==1){
													var arregloElementosTemp=Util._getElementosPorAtributoRecursivo(oElement.childNodes[i], atributo, valor);
								
													/** Anexa los elementos devueltos en el arreglo temporal, al
													 * arreglo principal
													 */
													for(var j=0; j<arregloElementosTemp.length; j++){
														arregloElementos[arregloElementos.length]=arregloElementosTemp[j];
													}
												}
											}
								
											/** Se devuelve el arreglo de elementos
											 */
											return arregloElementos;
										},
	/** Función encarga de hacer una verificación acerca de que nombre de atributo se
	 * está utilizando, para evitar posibles problemas de reconocimiento de nombres de atributo 
	 * por parte de ciertos browsers
	 */
	_verificarAtributo:	function(atributo){
							switch(atributo){
								case "class":
												var oElementoPrueba=document.createElement("DIV");
												oElementoPrueba.className="prueba";
								
												if(oElementoPrueba.getAttribute("class")!="prueba"){
													/** Soluciona el bug de ie con el atributo class
													 */
													atributo="className";
												}
								
												break;
							}
							
							return atributo;
						},
										
	/** Función privada encargada de indicar si el valor cumple con el patrón enviado.
	 * El valor de "patron" es un patrón de "Regular Expression", pero pasado a su formato
	 * de cadenas (es decir no en formato Perl-like)
	 */
	_isCumplePatron:	function(valor, patron){
							if(typeof valor=="undefined" || valor==null){
								return null;
							}

							if(typeof patron=="undefined" || patron==null){
								return null;
							}
							
							var re=new RegExp(patron);
							var blRespuesta=false;
							
							blRespuesta=re.test(valor);
							
							return blRespuesta;
						}
}

