Ext.namespace('CC');
Ext.namespace('CC.help');

CC.help.ajax = function(){
	
	
	return{
		running : [],
		
		result : {},
			
		showDebug : function(debugParam, urlParam){
			try {
				if ( document.getElementById('sfWebDebugConfig') ) {
					Ext.get('sfWebDebugConfig').update('URL: '+urlParam+'<br /><hr />'+debugParam);
				}
			} catch(e) {
			
			}
		},
		
		doAjax : function(param){
			//
			var canRun = null;
			if (typeof param.singleInstance == 'undefined' || param.singleInstance== false){
				canRun = true;
			}else{
				var fut = false;
				for (var i = 0; i < CC.help.ajax.running.length; i++){
					if (CC.help.ajax.running[i] == param.url){
						fut = true;
					}
				}
				if (fut == false){
					canRun = true;
					CC.help.ajax.running.push(param.url);
					setTimeout(function(){
						for (var i = 0; i < CC.help.ajax.running.length; i++){
							if (CC.help.ajax.running[i] == param.url){
								CC.help.ajax.running.splice(i, 1);
							}
						}
					}, 15000 );		
				}else{
					canRun = false;
				}
			}

			if (canRun == true){			
				Ext.Ajax.request({   
						timeout : 15000,
						params:param.params,
						url:param.url,
						success: function(response){
							try{
								for (var i = 0; i < CC.help.ajax.running.length; i++){
									if (CC.help.ajax.running[i] == param.url){
										CC.help.ajax.running.splice(i, 1);
									}
								}
							}catch(e){
							}
						
							var result;
							try 
							{ 
								result = Ext.util.JSON.decode( response.responseText );
								if ( result.success == 'session_timeout' ) {
									Ext.MessageBox.show(
										{
											title: CC.b.enter.config.msgError,
											msg: result.message,
											buttons: Ext.MessageBox.OK,
											fn: CC.f.mod.login.doLogout,
											icon: Ext.MessageBox.ERROR
										}
									);
								} else {
									if ( typeof result.error != 'undefined' && result.error != '' ) {
										var tempWindow = new Ext.Window({
											layout: 'fit',						
											height: 400,
											width: 800,
											modal: true,
											autoScroll : true,							
											title: 'PHP error',
											html : result.error
										});
										tempWindow.show();
									} else {
										try{
											param.success(result);
										}catch(e){
											Ext.MessageBox.alert( CC.b.enter.config.msgError, 'Error in the passed ajaxSuccess. '+ e.message );
										}
									}
									CC.help.ajax.showDebug(result.debug, param.url);
									CC.help.garbage.clean();
								}
							}
							catch ( e )
							{
								Ext.MessageBox.alert( CC.b.enter.config.msgError, 'Invalid server response. ' + e.message );
								return false;
							}
						}, 
						waitMsg: CC.b.enter.config.ajaxWait,
						method : typeof param.method != 'undefined' ? param.method : 'POST',
						failure: function(response){
							if ( document.getElementById("debugDiv") ) {
								Ext.get("debugDiv").update('<img src="'+CC_webroot+'/js/ext-2.2/resources/images/default/grid/done.gif" alt=""/>');
							}
							if (typeof param.failureMessage == 'undefined'){
								if(typeof CC.b.enter.config.ajaxError == 'undefined'){
									Ext.MessageBox.alert( CC.b.enter.config.msgError, "Couldn't connect to server, please try again!");
								}else{
									Ext.MessageBox.alert( CC.b.enter.config.msgError, CC.b.enter.config.ajaxError);
								}
							}          
						}                      
				});
			}
				
		}
	}
}();

Ext.override(Ext.data.Store, {
	toJSON: function(){
		var data = [];
		for(var i = 0, len = this.getCount(); i < len; i++){
			data.push(this.getAt(i).data);
		}
		return Ext.encode(data);
	}
});

//nemnagyonmegy ie7alatt, sot ha van benne akkor idonkent jol le is hal
Ext.Ajax.on('beforerequest', function(conn, options){
	if ( document.getElementById("debugDiv") ) {
		Ext.get("debugDiv").update('<img src="'+CC_webroot+'/js/ext-2.2/resources/images/default/grid/wait.gif" alt="" />');
	}
}, this);
        
Ext.Ajax.on('requestcomplete', function(conn, response, options){    
	if ( document.getElementById("debugDiv") ) {
			Ext.get("debugDiv").update('<img src="'+CC_webroot+'/js/ext-2.2/resources/images/default/grid/done.gif" alt="" />');
	}
}, this);