(function () {
	YAHOO.namespace ("tecnopyme.util"); 
	YAHOO.tecnopyme.util.purgeDOM = function(d) {
		var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
        		YAHOO.tecnopyme.util.purgeDOM(d.childNodes[i]);
        }
    }
	}
	YAHOO.tecnopyme.util.roundNumber = function  (num, dec) {
		var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
		return result;
	}
	YAHOO.tecnopyme.util.myAlert = function (str){
				var c = new YAHOO.tecnopyme.widget.SimpleDialogAlert("dialogcontainer", str);
				c.render(document.body);
				c.show();				
			}
	YAHOO.tecnopyme.util.myConfirm = function (str, handler, oScope){
				var c = new YAHOO.tecnopyme.widget.SimpleDialogYesNo("dialogcontainer", str);
				c.eventYes.subscribe(handler, oScope);
				c.render(document.body);
				c.show();				
	}
	YAHOO.tecnopyme.util.myConfirmCancel = function (str, handlerYes, handlerNo, oScope){
				var c = new YAHOO.tecnopyme.widget.SimpleDialogYesNoCancel("dialogcontainer", str);
				c.eventYes.subscribe(handlerYes, oScope);
				c.eventNo.subscribe(handlerNo, oScope);
				c.render(document.body);
				c.show();				
	}
		
	YAHOO.tecnopyme.util.wait = function (header){
			this.header = header || "Cargando";
			this.myWait = new YAHOO.widget.Panel("espere",
				{ width:'300px',  fixedcenter: true, constraintoviewport: true, underlay:"none", close:false, visible:false, draggable:true, modal:true
					}
				);

	}

	YAHOO.tecnopyme.util.wait.prototype.show = function (mensaje){
			var mensaje = "";
			if (arguments.length > 0)
				mensaje = texto;
			mensaje += " Por favor, espere";
			this.myWait.setHeader(this.header);
			this.myWait.setBody("<div class='wait'><img src='/css/ui/wait.gif'><div>" + mensaje + "</div></div>");
			this.myWait.render(document.body);			
			this.myWait.show();				
			
	}
	
	YAHOO.tecnopyme.util.wait.prototype.hide = function(){
		this.myWait.hide();
	}

	YAHOO.tecnopyme.util.validarEntero =  function (e) {	
    tecla = (document.all) ? e.keyCode : e.which;
	  if (tecla==35) return true;
	  if (tecla==36) return true;
	  if (tecla==37) return true;
	  if (tecla==39) return true;
		if (tecla==46) return true;
    if (tecla==8) return true; //Tecla de retroceso (para poder borrar)
    if (tecla==9) return true; //Tecla de tabulador
    if (tecla==0) return true; //Varias teclas en mozilla dan 0
    //patron =/[A-Za-z]/; // Solo acepta letras
    pattern = /^[0-9]*$/; // Sólo números
		te = String.fromCharCode(tecla);
    return pattern.test(te);  
	};

	YAHOO.tecnopyme.util.html_entity_decode = function(str) {
		var ta = document.getElementById('text_area_decode');
		if (!ta){
	  	 ta=document.createElement("textarea");
	  	 ta.id = 'text_area_decode';
	  	 ta.style.display = 'none';
	  	 document.body.appendChild(ta);
	  }	  
	  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	  return ta.innerHTML;  
	}
  
	YAHOO.tecnopyme.util.readCookie = function (name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
	}
	
	YAHOO.tecnopyme.util.createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	}
	
//inicio
	YAHOO.tecnopyme.util.insertString = function(id,string) {
		function insertAtcursor(myField, myValue) {
			//IE support
			if (document.selection) {
				myField.focus();
				sel = document.selection.createRange();
				sel.text = myValue;
			}else if ((myField.selectionStart) || (myField.selectionStart == '0')) {
				var startPos = myField.selectionStart;
				var endPos = myField.selectionEnd;
				myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
			} else {
				myField.value += myValue;
			}
			document.form1.text.focus();
		} 		
	 var el = YAHOO.util.Dom.get(id);
	 insertAtcursor(el, string);
}

//FIN
  //Configuraciones
  YAHOO.tecnopyme.util.DIR_IMAGES = "/images/";	
  YAHOO.tecnopyme.util.TAM_IMAGE = "100_";	
  //Crear la cookie para volver del carro
  if (document.location.href.indexOf("carro.php") == -1)
  	YAHOO.tecnopyme.util.createCookie("redir", document.location.href);
})();
