dojo.declare("dojom.data.Manager", null, 
{
	log: null,
	
	constructor: function ()
	{
		this.log = LOG.addClass('Data Manager')
	},
	
	/*=====
	dojo.__xhrArgs = function(kwArgs){
		//	summary:
		//		In addition to the properties listed for the dojo.__ioArgs type,
		//		the following properties are allowed for dojo.xhr* methods.
		//	handleAs: 
		//		String. Acceptable values are:
		//			"text" (default)
		//			"json"
		//			"json-comment-optional"
		//			"json-comment-filtered"
		//			"javascript"
		//			"xml"
		//	sync:
		//		Boolean. false is default. Indicates whether the request should
		//		be a synchronous (blocking) request.
		//	headers:
		//		Object. Additional HTTP headers to send in the request.
	}
	=====*/
	getData: function(/*dojo.__xhrArgs*/options)
	{
		try{
			// load data using dojo API
			dojo.xhrGet({ 
					
				url: options['url'],
				handleAs: options['handleAs'],
				load: options['load'],	
				//sync: true,	        
				error: options['error']
			});		
		}catch(ex){this.log.error('getData.'+ex)}
	},
	
	
	
	sendData: function(/*dojo.__xhrArgs*/options)
	{
		try{
			dojo.xhrPost({
				url: options['url'],
				handleAs: options['handleAs'],
				content: options['content'],
				load: options['load'],		        
				error: options['error']
			})
		}catch(ex){this.log.error('setData'+ex)}
	},
	
	getJsonFromUrl: function(url,onLoad,funcname)
	{
		var obj = this;
		try{
			this.getData({url:url,handleAs:'json',
									load:function(data_, ioArgs){
										try{
											if(data_['result']=='failure')
											{																									
												if(data_['location'] == 'frontend')	
												{			
													obj.log.error('getJsonFromUrl /url: '+url+'/.'+data_['description']);
													obj.log.msg('Error in frontend. Reload page please','error');
												}
												else if(data_['location'] == 'backend')
												{
													obj.log.error('getJsonFromUrl /url: '+url+'/.'+data_['description']);
													obj.log.msg(data_['description'],'error');
												}
												else 
													obj.log.msg(data_['description'],'warning');
												
											}
											else onLoad(data_);
										}catch(ex){obj.log.error('getJsonFromUrl /onLoad:'+funcname+'/.'+ex)}
									},
									error:function(data_){
										obj.log.fatal('getJsonFromUrl /url: '+url+'/.'+data_);
										obj.log.msg('Service is not accessible. Try again later.','error');
									}
								});
		}catch(ex){obj.log.error('getJsonFromUrl.'+ex)}
    },
    
    sendDataToUrl: function(url,content,onLoad)
    {
    	var obj = this;
    	try{
			this.sendData({url:url,handleAs:'json',
				content: content,
				load:function(data_, ioArgs){
					try{
						if(data_['result']=='failure')
											{																									
							if(data_['location'] == 'frontend')	
							{			
								obj.log.error('sendDataToUrl /url: '+url+'/.'+data_['description']);
								obj.log.msg('Error in frontend. Reload page please','error');
							}
							else if(data_['location'] == 'backend')
							{
								obj.log.error('sendDataToUrl /url: '+url+'/.'+data_['description']);
								obj.log.msg(data_['description'],'error');
							}
							else 
								obj.log.msg(data_['description'],'warning');
							
						}
						else onLoad(data_);
					}catch(ex){obj.log.error('sendDataToUrl /onLoad:'+funcname+'/.'+ex)}
				},
				error:function(data_){
					obj.log.fatal('sendDataToUrl /url: '+url+', content: '+content+'/.'+data_);
					obj.log.msg('Service is not accessible. Try again later.','error');
				}
			});
		}catch(ex){obj.log.error('sendDataToUrl.'+ex)}
    },
    
    writeToFile: function (filename, text) 
    {    	
		try {
			var fso, s;
		    fso = new ActiveXObject("Scripting.FileSystemObject");
		    fso.CreateTextFile(filename);
		    s = fso.GetFile(filename);
		    s.OpenAsTextStream(ForWriting, TristateFalse);
		    s.Write(text);
		    s.Close();
		  }
		catch(ex){this.log.error('writeToFile.'+ex)}
	}
});

