// JavaScript Document

/*
* Creada por Fernando Soto
*/

function isNoEmpty( Var ){
	
	if( typeof( Var ) == 'undefined' || Var == '' ){
		
		return null;
		
	}
		
	return true;
			
}

String.prototype.mysqlToDateFormat = function(mask) {
	
	var monthNames = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];

	 
	  var tmDate 	= this.replace('-/ / /','-');
	  var tmDate 	= tmDate.split('-');	  
	  var MontCheck =  tmDate[1].split(''); 
	 
	  var Year  = tmDate[0];
	  var month = MontCheck[0] == 0 ?  MontCheck[1] : tmDate[1];
	  var day   = tmDate[2];
	  
	
	
	  var dateFormat = day + ' ' + monthNames[ month - 1] + ' ' + Year;
	  
	 return dateFormat;
	 
};

var LibraryOnDemand = Class.create({
								   
	 initialize: function( instanceName) {
			
			this.instanceName = instanceName;			
			
			
	 },
	 noCacheJs : function(){
		
		var ODate 	 = new Date() 
		var OHour 	 = ODate.getHours();
		var OMinutes = ODate.getMinutes();
		
		return String(OHour +''+ OMinutes);
		 
	 },
	 
	 load : function (component,id,options){
		
		this.SWTime 		= this.noCacheJs();
			
		this.HandonComplete = this.onComplete.bindAsEventListener(this);			
		var fileType 		= component.substring(component.lastIndexOf('.'));
		var head     		= document.getElementsByTagName("head")[0];
		this.options 		= options;	
		this.remove			= typeof(this.options.remove) != 'undefined' ? this.options.remove : null;
		
		done         		= false;
		
			if (fileType === ".js") {
				
				if( this.remove ){
						
						if( $(id) ){
							
							$(id).remove();
						
						}
				}
				
				var file = component +'?'+this.SWTime;
				
				var fileRef = document.createElement('script');
				fileRef.setAttribute("type", "text/javascript");
				fileRef.setAttribute("src",  file);
				fileRef.setAttribute("id", id);
				
				fileRef.onload = fileRef.onreadystatechange = this.HandonComplete;

			}
			
			if (typeof fileRef != "undefined") {
				head.appendChild(fileRef);
			}
			
			idBferoeLoader = id;
	},
	
	onComplete : function ( request ){
		
		if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete") ) {
			
			done = true;									
			window.setTimeout( this.instanceName + ".notify('onCompletedLoad',this )",100);
			
		}
		
	},
	
	notify :function( event_name ,event ){
		
		try{
			if(this.options[event_name])
				return [this.options[event_name].apply(this.options[event_name],$A(arguments).slice(1),event)];
		}catch(e){
			if(e != $break)
				throw e;
			else
				return false;
		}
	 }
  
	
});

var CSefMxm = Class.create({
 
 initialize: function( target, options ) {
   	
	//Check for options
	this.oinit = typeof(arguments[0]) == 'object' ? arguments[0] : arguments[1];	
   
    this.Key 			  = null;    
	this.DataReservation  = null;
   	this.HandshowLoad 	  = this.showLoad.bindAsEventListener(this);
	this.HandshowResponse = this.showResponse.bindAsEventListener(this);
	this.Handfalla 		  = this.showFail.bindAsEventListener(this);
	this.Title			  = ''; 
	this.Target			  = null;
	this.Server			  = null; 
	this.ORequest		  = null;
	
	IndexLoader	 		 = typeof(IndexLoader) != 'undefined' ? IndexLoader :  0;
	
	this.init( this.oinit );
	
	
  },
  
  // Just for use in child classes
  init : function(){
	
  },


 trace : function ( str ) {
	
	if( this.Mode = 'Debug') {
	
     if( typeof(console) != 'undefined' ){
			
			console.debug( str );
		
		}else{
		
			alert( str );
		
		}
		
	}
},

  msg : function( msg ){
		
		this.trace( msg );
  },
  
  
   noCacheJs : function(){
		
		var ODate 	 = new Date() 
		var OHour 	 = ODate.getHours();
		var OMinutes = ODate.getMinutes();
		
		return String(OHour +''+ OMinutes);
		 
	 },
	 
	 
	 /*
	*  function : loadJs 
	*  description: carga un js desde otro servicio
	*
	*/
	loadJs : function ( ){
	
		IndexLoader++;
		this.iter++;
		this.HandExecuter = this.executer.bindAsEventListener(this);
		this.HandLoader   = this.createLoader.bindAsEventListener(this);
		this.TUrl = this.Url + '?' + this.noCacheJs();
		
		new Ajax.Request(this.TUrl,{
			method : 'get',	
			onComplete : this.HandExecuter,
			onCreate : this.HandLoader
		
		})
		
	},
	
  
  initEvent : function(){
	  
	  Event.observe(arguments[0],arguments[1],arguments[2]); 
	  
  },
  
   notify :function( event_name ,event ){
		
		try{
			if(this.options[event_name])
				return [this.options[event_name].apply(this.options[event_name],$A(arguments).slice(1),event)];
		}catch(e){
			if(e != $break)
				throw e;
			else
				return false;
		}
	 },
  
  /* ---------- Ajax Section ------------*/
  
  	showLoad : function(){
		
	},
		
	 showResponse : function(t){
	
	},
			
	showFail : function (){
		
	
	},
  
    request : function(){
	  
		new Ajax.Request(this.Url,this.ORequest);
			
	},
	
	updater : function(){
	  
		new Ajax.Updater(this.Target,this.Url,this.ORequest);
			
	},
	
	///////////////////////////////////////////////////////////////////
	
	
	createLoader : function(){
		
			//document.body.style.position = 'relative';		
			this.divLoaderContainer = document.createElement('div');					
			Element.extend(this.divLoaderContainer);
			this.divLoaderContainer.id = 'Loader_'+ IndexLoader;
			document.body.appendChild(this.divLoaderContainer);
			this.divLoaderContainer.hide();
			
			var Scroll = document.viewport.getScrollOffsets();
			
			this.divLoaderContainer.setStyle({
								  
								  top:Scroll.top,
								  right:0,
								  position:'absolute',
								  zIndex:'15000',
								  backgroundColor:'#000',
								  color:'#fff',
								  padding:'5px',
								  font:'11px Arial'
								  
								 });
			
			var str  = ' <img src="http://i2.esmas.com/deportes30/img/loadinfo.net.gif" width="13" height="13">';
				str += ' Actualizando..';
			this.divLoaderContainer.update(str)
		
		
		this.divLoaderContainer.show();
	
		
	},
	
	
	removeLoader : function(){
		
		setTimeout(this.removeClearDiv.bind(this),40);
	},
	
	removeClearDiv : function(){
		
		this.divLoaderContainer.fade({
									 
				afterFinish :function(effect){   
					
					effect.element.remove();
					
				 }
				
				
		});
		//this.divLoaderContainer.remove();
		
	},
	
	//////////////////////////////////////////////////////////////////////
	
	DefineData : function( request ){
	
		if( typeof(request) != 'undefined') {
		
			if( window.eval ){
		
				return window.eval(request.responseText);
		  
			}else{
				
				return eval(request.responseText); 
			
			}
		
		}
		
	}	 
  
 })
