window.AJAX = function(url,options){
	
	if (!options) options = {};
	
	var data = options.data ? options.data : {};
	data.ajax = 1;
	
	options.url = url;
	options.data = data;
	
	var onSuccessFunc = options.onSuccess;
	
	options.onFailure = options.onFailure ? options.onFailure : function(){
		Messager.showError('Ошибка в канале связи!<br />повторите запрос позже.');
		newRequest.send(options.data);
	};
	
	options.evalScripts = false;
	options.evalResponse = false;
	
	options.onSuccess = function(rps, xml) {
		var scripts = {pre: [], post: []},
		text = rps.replace(/<script([^>]*)>([\s\S]*?)<\/script>/gi, function(){
			scripts[arguments[1].contains("_execpre") ? "pre" : "post"].include(arguments[2]);
			return "";
		});
		
		scripts.pre.each($exec);
		if ($type(onSuccessFunc) == "function") onSuccessFunc(text, xml);
		scripts.post.each(function(el){
			$exec(el);
		});
	};
	
	var newRequest = new Request(options);
	newRequest.success = newRequest.onSuccess.bind(newRequest);
	newRequest.send();	
};

/* action.method?var=1&var=2 */
window.Action = function(request,success,failure){
	var failure = failure ? failure : function(){};
	var request = request.split('?');
	AJAX('/',{
		data:'action='+request,
		onSuccess:function(ret){
			success && success(ret);
		},
		onFailure:function(ret){
			failure();
		}
	});
};

window.ajaxUploadForm = function(idform,onLoadFunc){
	var ret = '';
	var onLoadFunc = (onLoadFunc) ? onLoadFunc : (function(ret){});
	
	window.addEvent('domready', function() {
		if ($(idform))
		{
			var form = $(idform);
			form.adopt(new Element('input',{ type:'hidden', name:'ajax', value:'1' }));
			var frameid = 'frame'+Math.floor(Math.random()*11*Math.random()*11);
		
			var frame = new Element('iframe',{ 'name':frameid, 'id':frameid, 'styles':{ 'display':'none' } });
			frame.addEvent('load',function(){
				var body = $(frameid).contentWindow.document.body;
				var ret = $(body).get('text').trim();
				if (ret) onLoadFunc(ret,$(body).get('html'));
			});
			$(document.body).adopt(frame);
			
			$(form).set({
				'target':frameid
			});
		}
		else {
			alert('Форма с id:'+idform+' - не найдена!');			
		}
	});
};

window.htmlentities = function(string, quote_style, charset, double_encode) {
    
    var hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style),
        symbol = '';
        
    string = string == null ? '' : string + '';
 
    if (!hash_map) {
        return false;
    }
    
    if (quote_style && quote_style === 'ENT_QUOTES') {
        hash_map["'"] = '&#039;';
    }
    
    if (!!double_encode || double_encode == null) {
        for (symbol in hash_map) {
            if (hash_map.hasOwnProperty(symbol)) {
                string = string.split(symbol).join(hash_map[symbol]);
            }
        }
    } else {
        string = string.replace(/([\s\S]*?)(&(?:#\d+|#x[\da-f]+|[a-zA-Z][\da-z]*);|$)/g, function (ignore, text, entity) {
            for (symbol in hash_map) {
                if (hash_map.hasOwnProperty(symbol)) {
                    text = text.split(symbol).join(hash_map[symbol]);
                }
            }
            
            return text + entity;
        });
    }
 
    return string;
}
