// Execute core app code
jQuery(function($)
{	
	// Initialize navigation
	if($.fn.superfish)
		$('ul.sf-menu').superfish();
	//
});

function optionsArrayToMap(s)
{
	var res = {};
	
	if(s && /\[(.+)\]/.exec(s))
	{
		var a = RegExp.$1.split(/,/);
		
		for(var i = 0; i < a.length - 1; i+=2)
			res[a[i]] = a[i+1];
	}
	
	return res;
}

function getAjaxData(ajaxdatastring, options)
{
	return eval('(' + ajaxdatastring + ')');
}

// Convert the generic AJAX response into a form compatible with AutoComplete
function getAutoCompleteArray(ajaxdatastring, options)
{
	var res = [];
	
	var resp = getAjaxData(ajaxdatastring);
	
	if(resp)
	{		
		var a = resp.Data;			
		
		for(var i = 0; i < a.length; i++)
		{
			var row = a[i];
			
			if(options.key)
				res.push({value:row[options.key], label:row[options.value]});
			else
				res.push(row[options.value]);
		}
	}
	
	return res;
}	

// ===========================================================================================================================================================================
cFront = typeof(cFront) != 'undefined' ? cFront : {};

cFront.Xml =
{
	defaultObjectName:'root',
	
	serialize:function(o, name, props)
	{ 
		var _o = o;
		var wrap = !props || !props.noWrap;
		
		if(wrap)
		{
			var _o = {};
			_o[name ? name : this.defaultObjectName] = o;
		}
		
		return this._serialize(_o); 
	},
	
	_serialize:function(o)
	{
		var s = '';
		
		if(o == null || typeof(o) == 'undefined')
			s = null;
		else
		{			
			var type = this.getType(o);
			
			if(type == 'array')
			{
				for(var i = 0; i < o.length; i++)
					s += this.genEl('el', this.getType(o[i]), this._serialize(o[i]));
			}
			else if(type == 'object')
			{				
				for(var el in o)
					if(typeof(o[el]) != 'function')
						s += this.genEl(el, this.getType(o[el]), this._serialize(o[el]));
			}
			else
				s += this.esc(o);
		}
		
		return s;
	},
	
	genEl:function(el, type, val)
	{
		var s = '';		
		var type_s = type == 'object' || type == 'array' ? ' type="' + type + '"' : '';			
		
		if(val == null || typeof(val) == 'undefined')
			s += '<' + el + ' isnull="1" />';
		else if(val == '')
			s += '<' + el + ' />';
		else
			s += '<' + el + type_s + '>' + val + '</' + el + '>';
			
		return s;
	},
	
	esc:function(s)
	{
		var _s = new String(s);
		
		return _s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
	},
	
	getType:function(o)
	{
		if(typeof(o) == 'array' || (typeof(o) == 'object' && typeof(o.push) == 'function' && typeof(o.join) == 'function'))
			return 'array';
		else
			return typeof(o);
	}
};

