function randomString(string_length, chars) {
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
} //FUNC randomString

function randomString_AlNum(string_length) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	return randomString(string_length, chars);
} //FUNC randomString

function randomString_CapsNum(string_length) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
	return randomString(string_length, chars);
} //FUNC randomString

function ajax_checkbox_switch (name,state) {
	if (state) {
		jQuery('#'+name+'_proc').hide();
		jQuery('#'+name+'_check').show();
	} else {
		jQuery('#'+name+'_check').hide();
		jQuery('#'+name+'_proc').show();
	} //IF state = show
} //FUNC ajax_checkbox_switch

function toggleTab(tab) {
	if (!offTab) offTab='';

	if (tab==offTab) return;

	ontab='tab_'+tab;
	ctab='clicktab_'+tab;
	uftab='tab_'+offTab;
	ufctab='clicktab_'+offTab;

	$(uftab).style.display='none';
	$(ontab).style.display='block';
	$(ctab).className='selectedTab';
	$(ufctab).className='clickTab';

	offTab=tab;

} //FUNC toggleTab

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 insert_bar (bar_place,bar_name) {
	if (!bar_name) bar_name = 'processing_bar.gif';
	jQuery('#'+bar_place).html('<IMG src="/ecomm/appimages/processing/'+bar_name+'" alt="Processing...">');
} //FUNC insert_bar

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);
//*/
	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

/*--------------------------------------------------------------------------------*\
|	showConfirmation
|----------------------------------------------------------------------------------
| Displays simple confirmation window with custom text and two links 'Yes'/'No'.
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|	Params:
|		text	string	- Text to display in confirmation.
|		url	string	- Url for 'Yes' link ('No' link just closes window).
|		[title]	string	- Window title to display.
|	Return:
|			bool	- Alyways FALSE.
\*--------------------------------------------------------------= by Mr.V!T =-----*/
function showConfirmation (text, url, title) {
	
	if ( !title ) title = '&nbsp;';
	
	jQuery('#confirmation_content').html(text);
	jQuery('#confirmation_header').html(title);

	jQuery('#confirmation_yes_link').attr('href', url);

	jqmShowCentered('#confirmation_window');
	
	return false;
} //FUNC showConfirmation
