/**********************************************************************************\
 * Set of common java helpers.
 * 
 * The MIT License
 * 
 * Copyright (c) 2008 Victor Denisenkov aka Mr.V!T
 * 
 * Permission  is hereby granted, free of charge, to any person obtaining a copy of
 * this  software  and  associated documentation files (the "Software"), to deal in
 * the  Software  without  restriction,  including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the  Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * 
 * The  above  copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR  A  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT  HOLDERS  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN  AN  ACTION  OF  CONTRACT,  TORT  OR  OTHERWISE,  ARISING  FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * @package vit-lib
 * @author Victor Denisenkov aka Mr.V!T
 * @version 1.0
\**********************************************************************************/

/*--------------------------------------------------------------------------------*\
|	toReal
|----------------------------------------------------------------------------------
| Ensures that number will be displayed as normal value (fix for JavaScript numbers)
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		[base]	int	- Base value to rounf value to.
|	Return:
|			string	- Human looking nuber roundes to sertain base.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
Number.prototype.Humanize = function (base) {
	
	if ( !base ) base = 10;
	
	var tmp = this.toFixed(base) + '';
	
	var res = tmp.rtrim('0.');
	if ( res == '' ) res = '0';
	
	return res;
} //FUNC toReal

/*--------------------------------------------------------------------------------*\
|	indexOf
|----------------------------------------------------------------------------------
| Fixes indexOf for IE6.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		obj		MIXED	- Item to search.
|		fromIndex	integer	- Search starting from index.
|	Return:
|				integer	- Item position in array, or -1 if not found.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
/**/
if ( !Array.prototype.indexOf )
Array.prototype.indexOf = function (obj, fromIndex) {
		
	if ( !fromIndex ) fromIndex = 0;
	
	for ( var i in this )
		if ( this[i] === obj ) return i;
		
	return -1;
} //FUNC indexOf
/**/

/*--------------------------------------------------------------------------------*\
|	escape
|----------------------------------------------------------------------------------
| Escapes string for using in REGex.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		NONE
|	Return:
|		NULL
\*--------------------------------------------------------------= by Mr.V!T =-----*/
String.prototype.escape = function() {
	return this.replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!<>\|\:])/g, '\\$1');
} //FUNC escape

/*--------------------------------------------------------------------------------*\
|	_trim
|----------------------------------------------------------------------------------
| Base function for trimming strings.
| ***TNX*** Ideas taken from phpjs.org
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		[charlist]	string	- Optional characteres to trim.
|		[left]		bool	- Trim left.
|		[right]		bool	- Trim right.
|	Return:
|				string	- Trimmed string.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
String.prototype._trim = function(charlist, left, right) {
	
	charlist += '';
	charlist = (charlist) ? charlist.escape() : ' \\s\u00A0';
	
	var str = this;
	
	if ( left ) str	= str.replace(new RegExp('^[' + charlist + ']+', 'g'), '');
	if ( right ) str = str.replace(new RegExp('[' + charlist + ']+$', 'g'), '');
	
	return str;
} //FUNC _trim

/*--------------------------------------------------------------------------------*\
|	ltrim
|----------------------------------------------------------------------------------
| Strips whitespace from the beginning of a string.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		[charlist]	string	- Optional characteres to trim.
|	Return:
|				string	- Trimmed string.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
String.prototype.ltrim = function(charlist) {
	return this._trim(charlist, 1, 0);
} //FUNC ltrim

/*--------------------------------------------------------------------------------*\
|	rtrim
|----------------------------------------------------------------------------------
| Removes trailing whitespace.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		[charlist]	string	- Optional characteres to trim.
|	Return:
|				string	- Trimmed string.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
String.prototype.rtrim = function(charlist) {
	return this._trim(charlist, 0, 1);
} //FUNC rtrim

/*--------------------------------------------------------------------------------*\
|	trim
|----------------------------------------------------------------------------------
| Removes trailing whitespace.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		[charlist]	string	- Optional characteres to trim.
|	Return:
|				string	- Trimmed string.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
String.prototype.trim = function(charlist) {
	return this._trim(charlist, 1, 1);
} //FUNC trim

/*--------------------------------------------------------------------------------*\
|	xtrim
|----------------------------------------------------------------------------------
| Very fast function for trimming strings.
| ***TNX*** Big thanx for Steve at steves_list (at) hotmail.com. 
| ***TNX*** http://blog.stevenlevithan.com/
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		NONE
|	Return:
|		NULL
\*--------------------------------------------------------------= by Mr.V!T =-----*/
String.prototype.xtrim = function() {
	var str = this;
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
} //FUNC xtrim

/*--------------------------------------------------------------------------------*\
|	compilePath
|----------------------------------------------------------------------------------
| Compile path from several pieces using '/' as glue.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		*	string	- One or several path pieces.
|	Return:
|			string	- Valid path.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function compilePath () {
	
	var len = arguments.length;
	if ( len == 1 ) return arguments[0];
	
	var res = (arguments[0]+'').rtrim('/');
	
	for (var i = 1; i < len; i++ ) {
		res += '/' + (arguments[i]+'').ltrim('/');
	} //FOR each argument
	
	return res;
} //FUNC compilePath

/*--------------------------------------------------------------------------------*\
|	arrayToPost
|----------------------------------------------------------------------------------
| Prepares multidimentional array to post in native PHP format.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		array	object	- Common JavaScript array object.
|	Return:
|			array	- Array in POSt format.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function arrayToPost (array, prefix) {
	
	var res = {};
	
	for ( i in array ) {
		
		var key = ( prefix )? prefix + '[' + i + ']' : i;
		
		if ( typeof(array[i]) == 'object' ) {

			var sub = arrayToPost(array[i], key);
			
			for ( j in sub ) res[j] = sub[j];
			
		} else { //IF sub array
			res[key] = array[i];
		} //IF other type
		
	} //FOR each array
	
	return res;
} //FUNC arrayToPost

/*--------------------------------------------------------------------------------*\
|	parseVariables
|----------------------------------------------------------------------------------
| Parses {varables} in a string.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		str		string	- String to parse.
|		vars		object	- Object with variables to pass.
|		[autoclean]	bool	- Clean unused vaiables automatically.
|	Return:
|				string	- Parsed string.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function parseVariables (str, vars, autoclean) {
	
	if ( autoclean == undefined ) autoclean = true;
	
	for ( i in vars ) {
		str = str.replace('{' + i + '}', vars[i]);
	} //FOR each var

	if ( autoclean )
		str = str.replace(/\{\w+\}/, '');
	
	return str;	
} //FUNC parseVariables

/*--------------------------------------------------------------------------------*\
|	isMail
|----------------------------------------------------------------------------------
| Simple function for checking if give string is correct mail.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		value	string	- Value to check.
|	Return:
|			bool	- Check status.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function isMail (value) {
	var filter = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9_\-]+\.)+[a-z]{2,6}$/i;
	return filter.test(value);
} //FUNC isMail

/*--------------------------------------------------------------------------------*\
|	jqmShowCSS
|----------------------------------------------------------------------------------
| Apllies given css to jQM window and shows it.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		selector	string	- Window to show.
|		css		string	- Object with css to apply.
|		effect		string	- Effect to use on displaying.
|	Return:
|				bool	- Alwyas false.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function jqmShowCSS (selector, css, effect) {
	
	jQuery(selector).css(css);

	if ( jqmVisible(selector) ) return false;

	if ( !isJQM(selector) ) {
		
		var opt = {};
		
		if ( effect == 'fade' )
			opt = {
				onShow: function(h) { 
					h.w.fadeIn(300);
				},
				onHide: function(h) { 
					h.o.remove();
					h.w.fadeOut(300);
				}
			}; //Object o
		
		jQuery(selector).jqm(opt);
		if ( jQuery().jqDrag ) jQuery(selector).jqDrag('.jqmHeader');
		if ( jQuery().jqResize ) jQuery(selector).jqResize('.jqmResize');
	} //IF element is not jqm window already
	
	jQuery(selector).jqmShow();

	return false;
	
} //FUNC jqmShowCSS

/*--------------------------------------------------------------------------------*\
|	jqmShowCentered
|----------------------------------------------------------------------------------
| Fixes coordinates of jQM window to match center.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		selector	string	- Window to show.
|		effect		string	- Effect to use on displaying.
|	Return:
|				bool	- Alwyas false.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function jqmShowCentered (selector, effect) {
/**/	
	var wnd_width	= jQuery(selector).width();
	var wnd_height	= jQuery(selector).height();
	
	var parent_window = screenSize();

	var wnd_left = ( parent_window.width - wnd_width ) / 2;
	var wnd_top = ( parent_window.height - wnd_height ) / 2;

	var new_css = {'top':wnd_top,'left':wnd_left};
/*/
	var new_css = {'top':0,'left':0, 'margin': 'auto auto'};
	
/**/	
	return jqmShowCSS(selector, new_css, effect);
} //FUNC jqmShowCentered

/*--------------------------------------------------------------------------------*\
|	jqmShowDocked
|----------------------------------------------------------------------------------
| Fixes coordinates of jQM window to dock to given element.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		selector	string	- Window to show.
|		dock_to		string	- Selector of element to dock to.
|		effect		string	- Effect to use on displaying.
|	Return:
|				bool	- Alwyas false.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function jqmShowDocked (selector, dock_to, effect) {
	
	var offset	= jQuery(dock_to).offset();
	if ( !offset.top ) return false;
	
	var outer	= jQuery(dock_to).outerHeight();
	
	var new_css = {
		'top'		: offset.top + outer + 3,
		'left'		: offset.left
	}; //New CSS
	
	return jqmShowCSS(selector, new_css, effect);
} //FUNC jqmShowDocked

/*--------------------------------------------------------------------------------*\
|	jqmHide
|----------------------------------------------------------------------------------
| Hides JQM window.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		selector	string	- Window to hide.
|		effect		string	- Effect to use on hiding.
|	Return:
|				bool	- Alwyas false.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function jqmHide (selector, effect) {
	jQuery(selector).jqmHide();
	return false;
} //FUNC jqmHide

/*--------------------------------------------------------------------------------*\
|	isJQM
|----------------------------------------------------------------------------------
| Check if element is already jqModal window.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		selector	string	- Element to check.
|	Return:
|		NULL
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function isJQM (selector) {
	
	var tmp = jQuery(selector).attr('class');
	if ( tmp.indexOf('jqmID') < 0 ) return false;
	
	return true;
	
} //FUNC isJQM

/*--------------------------------------------------------------------------------*\
|	jqmVisible
|----------------------------------------------------------------------------------
| Checks if jqm window is visible.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		selector	string	- Element to check.
|	Return:
|		NULL
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function jqmVisible (selector) {
	
	var disp = jQuery(selector).css('display');
	return (disp != 'none');
	
} //FUNC jqmVisible

function centerWindow (win) {
	var wnd_width	= jQuery('#'+win).width();
	var wnd_height	= jQuery('#'+win).height();
	
	var parent_window = screenSize();

	var wnd_left = ( parent_window.width - wnd_width ) / 2;
	var wnd_top = ( parent_window.height - wnd_height ) / 2;

	var new_css = {'top':wnd_top,'left':wnd_left};
	jQuery('#'+win).css(new_css);
} //FUNC

/*--------------------------------------------------------------------------------*\
|	FormToObj
|----------------------------------------------------------------------------------
| Converts form values to enum object.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		form	object(HTMLFormElement)
|				- Form to convert
|	Return:
|			objcect	- Form values.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function FormToObj (form) {
	
	var data = {};
	
	for ( i = 0; i < form.elements.length; i++ ) {
		
		el = form.elements.item(i);
		
		switch (el.type) {
			case 'checkbox'	:
				data[el.name] = (el.checked)? 1 : 0;
				continue;
			case 'select-one'	:
				data[el.name] = getValue_Select(el);
				continue;
			case 'select-multiple'	:
				data[el.name] = getValue_MutliSelect(el);
				continue;
			case 'radio'	:
				if (el.checked == false) continue;
			default	:
				data[el.name] = el.value;
		} //SWITCH
		
	} //FOR each element
	
	return data;
} //FUNC FormToObj

/*--------------------------------------------------------------------------------*\
|	FormFromObj
|----------------------------------------------------------------------------------
| Set's form's values from object fields.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		form	object(HTMLFormElement)
|				- Form setup.
|
|		data	object	- Values to set.
|	Return:
|		VOID
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function FormFromObj (form, data) {
	
	for ( i = 0; i < form.elements.length; i++ ) {
		
		el = form.elements.item(i);
		if ( !(el.name in data) ) continue;
		
		switch (el.type) {
			case 'checkbox'	:
				el.checked = ( data[el.name] )? true : false;
				continue;
			case 'radio'	: //TO-DO
				el.checked = ( data[el.name] == el.value )? true : false;
				continue;
			case 'select-one'	:
				setValue_Select(el, data[el.name]);
				continue;
			case 'select-multiple'	:
//				value	= getValue_MutliSelect(el);
				continue;
			default	:
				el.value = data[el.name];
		} //SWITCH
		
	} //FOR each element
	
} //FUNC FormFromObj

/*--------------------------------------------------------------------------------*\
|	getValue_Select
|----------------------------------------------------------------------------------
| Returns correct value of <select> based in setted or not value attribute. IE FIX.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		element	object	- DOM elemnt to get value.
|	Return:
|			MIXED
|			bool	- FALSE in case of error.
|			string	- Element value.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function getValue_Select (element) {
	
	if ( element.options.selectedIndex < 0 ) return false;
	
	var option	= element.options[element.options.selectedIndex];
	var value	= getValue_Option(option);
	//(option.attributes.value && option.attributes.value.specified)? option.value : option.text
	
	return value;
} //FUNC getValue_Select

/*--------------------------------------------------------------------------------*\
|	setValue_Select
|----------------------------------------------------------------------------------
| Correctly sets value to selector. Works in IE.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		element	object	- DOM elemnt to set value.
|		value	string	- Value to set.
|	Return:
|		VOID
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function setValue_Select (element, value) {
	
	if ( element.options.length <= 0 ) return res;
	
	for ( var i = 0; i < element.options.length; i++ ) {
		
		if ( getValue_Option(element.options[i]) == value)
			element.options[i].selected = true;
	} //FOR all managers
	
} //FUNC setValue_Select

/*--------------------------------------------------------------------------------*\
|	getValue_MutliSelect
|----------------------------------------------------------------------------------
| Returns values of <select> with multiple selection enabled as an array.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		element	object	- DOM elemnt to get value.
|	Return:
|			array	- Array with values. Can be emty.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function getValue_MutliSelect (element) {

	var res = [] ;
	
	if ( element.options.length <= 0 ) return res;

	for ( var i = 0; i < element.options.length; i++ ) {
		
		if ( !element.options[i].selected ) continue;
		
		res.push(getValue_Option(element.options[i]));
	} //FOR all managers

	return res;
} //FUNC getValue_MutliSelect

/*--------------------------------------------------------------------------------*\
|	getValue_Option
|----------------------------------------------------------------------------------
| Returns correct value of <option> based in setted or not value attribute. IE FIX.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		element	object	- DOM elemnt to get value.
|	Return:
|			string	- Element value.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function getValue_Option (element) {
	value	= (element.attributes.value && element.attributes.value.specified)? element.value : element.text;
	return value;
} //FUNC getValue_Option

function ObjToStr(obj, var_name, add_br){
	
	var_name	= (var_name)? var_name + '.' : '';
	add_br		= (add_br)? '<br \/>' : '';
	
	var result = '';
	for (var i in obj) 
		result += var_name + i + ' = ' + obj[i] + add_br + '\n';
		
	return result;
} //FUNC ObjToStr

function ToggleDisplay(elementID, state) {
	element = document.getElementById(elementID);
	
	if ( !element ) return;
	
	if (state == undefined) {
		state = ( element.style.display == 'none' );
	} //IF no state specified
	
	element.style.display = state ? 'inline' : 'none';
} //FUNC ToggleDisplay

function screenSize() {
    var w, h;
    w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
    h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
    return {width:w, height:h};
}

function selectRow(row, state) {
	var row_class =	jQuery('#'+row).attr('class');
	row_class = row_class.replace('_selected','');
	row_class += ( state )? '_selected' : '';
	jQuery('#'+row).attr('class', row_class);
} //FUNC selectRow

/*/
function insert_bar (bar_place) {
	jQuery('#'+bar_place).html('<IMG src="/images/processing/bar.gif" alt="Loading...">');
} //FUNC insert_bar

function insert_green_bar (bar_place) {
	jQuery('#'+bar_place).html('<IMG src="/images/processing/bar_green.gif" alt="Loading...">');
} //FUNC insert_bar
//*/


/*--------------------------------------------------------------------------------*\
|	getCookie
|----------------------------------------------------------------------------------
| Returns cookie with given name.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		name	string	- Cookie name to search.
|	Return:
|			string	- Cookie value.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function getCookie (name) {

	var cookieValue	= "";
	var cookieName	= name + "=";
	
	if ( document.cookie.length > 0 ) { 
	
		offset = document.cookie.indexOf(cookieName);
		if ( offset != -1 ) { 
		
			offset	+= cookieName.length;
			
			end	= document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			
			cookieValue = unescape(document.cookie.substring(offset, end))
		
		} //IF found smth
	
	} //IF there are cookies
	
	return cookieValue;
} //FUNC getCookie

/*--------------------------------------------------------------------------------*\
|	setCookie
|----------------------------------------------------------------------------------
| Sets cookie with given name.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		name	string	- Cookie name to set.
|		value	string	- Value of cookie.
|		[hours]	string	- Expiration date in hours.
|	Return:
|		VOID
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function setCookie(name, value, hours) {
	
	var expire	= "";
	
	if ( hours ) {
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	} //IF there is set expiration
	
	document.cookie = name + "=" + escape(value) + expire;
} //FUNC setCookie

function get_controller_views(views) {
		insert_bars(views);
		insert_views(views,0);
} //FUNC get_controller_views

function mark_checkboxes(container_form,val) {
	for (var i = 0; i < container_form.elements.length; i++) {
		el = container_form.elements[i];
		if (el.type=='checkbox')
			el.checked=val;
	}
} //FUNC mark_checkboxes

function status_string(message,style_class,padding) {
	if (message == '') return '';
	if (!style_class) style_class = 'status_error';
	if (!padding) padding = 0;
	return '<div class="'+style_class+'" style="padding:'+padding+'px">'+message+'</div>';
} //FUNC error_string

function ajax_pack_data(controller_url,place_to_put, data, ajax_options) {
	var res = [];
	res['controller']	= controller_url;
	res['place']		= place_to_put;
	if (!data) data	= {};
	res['data']		= data;
	if (!ajax_options) ajax_options	= {};
	res['options']		= ajax_options;
	return res;
} //FUNC pack_view

function ajax_insert_bars(views) {
	for (i=0; i < views.length; i++) {
		insert_bar(views[i]['place']);
	} //FOR each views
} //FUNC insert_bars

function ajax_insert_views(views,num) {
	if (num >= views.length) return false;

	var tdata = views[num]['data'];
	
	if (views[num]['options'].onLoad) {views[num]['options'].onLoad(tdata)};
	jQuery.ajax({
		type	: 'POST', dataType: 'html',
		url	: views[num]['controller'],
		data	: views[num]['data'],
		success	: function(obj){
				jQuery('#'+views[num]['place']).html(obj);
				ajax_insert_views(views,num+1);
				if (views[num]['options'].onSuccess) {views[num]['options'].onSuccess(tdata)};
			},//FUNC success
		error : function (XMLHttpRequest, textStatus, errorThrown) {
				jQuery('#'+views[num]['place']).html(status_string('This section is not avaible now (Possibly under construction). Try again later.'));
				ajax_insert_views(views,num+1);
			} //FUNC error
	}); //ajax
}//FUNC insert_views

function ajax_get_views(views) {
		ajax_insert_bars(views);
		ajax_insert_views(views,0);
} //FUNC get_controller_views

function ajax_get_view(controller_url,place_to_put,data, ajax_options) {
	ajax_get_views([ajax_pack_data(controller_url,place_to_put,data, ajax_options)]);
} //FUNC get_controller_view

function ajax_post (post_form,controller,status_place,settings) {
	insert_bar(status_place);
	var tt = document.getElementById(post_form);
	if (!tt) alert('no form');
//*/
	jQuery.ajaxUpload ({
		type		: 'POST', dataType : 'html',
	        url		: controller,
	        secureuri	: false,
	        uploadform	: document.getElementById(post_form),
	        success		: function (res, status) {
					data = res.split('<!-- CUT -->');
					jQuery('#'+status_place).html(data[0]);

					if ((data.length != 1) && (data[1] == 'success') ) {
						if (settings.success) settings.success(data);
					} else {
						if (settings.error) settings.error(data);
					} //IF returned status

				}, //FUNC sucess
		error		: function (res, status) {
					alert('error');
					jQuery('#'+status_place).html(status_string('Error: '+status));
					if (settings.error) settings.error();
				} //FUNC error

	}); //AJAX
//*/	
} //FUNC ajax_post
