﻿// JavaScript Document
var oUtils = new Utils();
oUtils._construct();
function Utils(){
	var self = this;
	self.firefox = document.getElementById&&!document.all;
	self.Arreglo = null;
	self._construct = function(){
		self.Arreglo = new self._arrayfuncs();
	}
	self.AgregarEventos =  function(obj,eventType,functionName,suffix)
	{ 
	  if(!suffix)suffix = '';
	  if(obj.attachEvent){ 
	    obj['e'+eventType+functionName+suffix] = functionName; 
	    obj[eventType+functionName+suffix] = function(){obj['e'+eventType+functionName+suffix]( window.event );} 
	    obj.attachEvent( 'on'+eventType, obj[eventType+functionName+suffix] ); 
	  } else 
	    obj.addEventListener(eventType,functionName,false); 	    
	}
	self.QuitarEventos = function(obj,eventType,functionName,suffix){
	   if (obj.detachEvent) { //Para IE 
         obj['e'+eventType+functionName+suffix] = functionName;
         obj[eventType+functionName+suffix] = function(){obj['e'+eventType+functionName+suffix]( window.event );} 
         document.detachEvent('on'+eventType, obj[eventType+functionName+suffix]); 
       } 
       else { //Para IE4 
         obj.removeEventListener(eventType,functionName,true); 
       } 

	}
	self.mouseCoords = function(ev){
		if(ev.pageX || ev.pageY){
			return {x:ev.pageX, y:ev.pageY};
		}
		return {
			x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y:ev.clientY + document.body.scrollTop  - document.body.clientTop
		};
	}
	self._xml = function(){
		var fn = this;
		fn._str2xml = function(strXML){
			if (window.ActiveXObject)
			{
				var doc=new ActiveXObject("Microsoft.XMLDOM");
				doc.async="false";
				doc.loadXML(strXML);
			}
			// code for Mozilla, Firefox, Opera, etc.
			else
			{
				var parser=new DOMParser();
				var doc=parser.parseFromString(strXML,"text/xml");
			}// documentElement always represents the root node
			return doc;
		}
		fn._xml2string = function(xmlDom){
			var strs = null;
			var doc = xmlDom.documentElement;
			if(doc.xml == undefined){
				strs = (new XMLSerializer()).serializeToString(xmlDom);
			}else strs = doc.xml;
			return strs;
			
		}
		fn._nodevalue = function(nodo){
			valor = (nodo.childNodes.length > 0)?nodo.firstChild.nodeValue:"";
			return valor;
		}
		fn._subnodevalue = function(nodo,nombre){
			var sbnodolist = nodo.getElementsByTagName(nombre);
			var valor = "";
			if(sbnodolist.length<=0){
				valor ="";
			}
			else{
				var sbnodo = sbnodolist[0];
				valor = fn._nodevalue(sbnodo);
			}
			return valor;
			
		}
	}

	self.Convert2XML = function(strXML){
		if (window.ActiveXObject)
		{
			var doc=new ActiveXObject("Microsoft.XMLDOM");
			doc.async="false";
			doc.loadXML(strXML);
		}
		// code for Mozilla, Firefox, Opera, etc.
		else
		{
			var parser=new DOMParser();
			var doc=parser.parseFromString(strXML,"text/xml");
		}// documentElement always represents the root node
		xmldoc = doc.documentElement;
		return xmldoc;
	}
	self.MyNavigatorSize = function(){
    	var bodyWidth = document.documentElement.clientWidth;
    	var bodyHeight = document.documentElement.clientHeight;
    	
		var bodyWidth, bodyHeight; 
		if (window.innerHeight){ // all except Explorer 
		 
		   bodyWidth = window.innerWidth; 
		   bodyHeight = window.innerHeight; 
		}  else if (document.documentElement && document.documentElement.clientHeight) {
		   // Explorer 6 Strict Mode 		 
		   bodyWidth = document.documentElement.clientWidth; 
		   bodyHeight = document.documentElement.clientHeight;

		} else if (document.body) {// other Explorers 		 
		   bodyWidth = document.body.clientWidth; 
		   bodyHeight = document.body.clientHeight; 

		}
		propiedades = new Object();
		propiedades.w = bodyWidth;
		propiedades.h = bodyHeight;
		return propiedades;		
	}
	self.getTopPos = function(inputObj)
	{		
		var returnValue = inputObj.offsetTop;
		while((inputObj = inputObj.offsetParent) != null){
			if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
		}
		return returnValue;
	}
	
	self.getLeftPos = function(inputObj)
	{
		var returnValue = inputObj.offsetLeft;
		while((inputObj = inputObj.offsetParent) != null){
			if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
		}
		return returnValue;
	}
	self.LimpiaFormularioEspacios = function(texto){
		texto = texto.replace(/^\s+/g, "");
		texto = texto.replace(/\s+$/g, "");
		texto = texto.replace(/\s{2,}/gi," ");
		return texto;
	}
	self._hoy = function(){
		miFecha = new Date();
		dia = miFecha.getDate();
		mes = miFecha.getMonth()+1;
		anio = miFecha.getFullYear();
		var fecha = dia + "/" + mes + "/" + anio;
		return fecha;
	}
	self.PalabraLimpia = function(palabra){
		palabra = palabra.toLowerCase();
		palabra = self.CleanHTML(palabra);
		palabra = self.LimpiaFormularioEspacios(palabra);
		palabra = self.CleanAcentos(palabra);
		return palabra;
	}
	self.CleanAcentos = function(palabra){
		palabra = palabra.replace(/[á]/gi,"a");
		palabra = palabra.replace(/[é]/gi,"e");
		palabra = palabra.replace(/[í]/gi,"i");
		palabra = palabra.replace(/[ó]/gi,"o");
		palabra = palabra.replace(/[ú]/gi,"u");
		return palabra;
	}
	self._arrayfuncs = function(){
		var fn = this;
		fn._removeItem = function(arreglo,indice,num){
			if(num ==null || num ==undefined)num=1;
			arreglo.splice(indice,num);
			return arreglo;
		}
		fn.in_array = function(arreglo,needle) {
			for(var i=0; i < arreglo.length; i++) if(arreglo[i] === needle) return true;
			return false;
		}

	}

	self.aleatorio = function(inferior,superior){ 
    numPosibilidades = superior - inferior 
    aleat = Math.random() * numPosibilidades 
    aleat = Math.round(aleat) 
    return parseInt(inferior) + aleat 
	}
	self.CleanHTML = function(palabra){
		palabra = palabra.replace(/<\/?[^>]*>/gi,"");
		return palabra;
	}
	self.escapa = function(cadena){ return escape(cadena).replace(/\+/g, '%2B').replace(/\//g, '%2F');}
	
}