function trim(string){
	return string.replace(/(^\s+)|(\s+$)/g, "");
}

function strtr (str, from, to) {
    if (typeof from === 'object') {
    	var cmpStr = '';
    	for (var j=0; j < str.length; j++){
    		cmpStr += '0';
    	}
    	var offset = 0;
    	var find = -1;
    	var addStr = '';
        for (fr in from) {
        	offset = 0;
        	while ((find = str.indexOf(fr, offset)) != -1){
				if (parseInt(cmpStr.substr(find, fr.length)) != 0){
					offset = find + 1;
					continue;
				}
				for (var k =0 ; k < from[fr].length; k++){
					addStr += '1';
				}
				cmpStr = cmpStr.substr(0, find) + addStr + cmpStr.substr(find + fr.length, cmpStr.length - (find + fr.length));
				str = str.substr(0, find) + from[fr] + str.substr(find + fr.length, str.length - (find + fr.length));
				offset = find + from[fr].length + 1;
				addStr = '';
        	}
        }
        return str;
    }

	for(var i = 0; i < from.length; i++) {
		str = str.replace(new RegExp(from.charAt(i),'g'), to.charAt(i));
	}

    return str;
}

function text_found(mystring, findme){
	return mystring.indexOf(findme) > -1;
}

function authorization_exit() {
	ajax_setStyleDisplay('ajaxLoad','block');
	document.location.href = '/exit.ajax';
}

function getParameters(form) {
	if (!form)
		return "";
	var res = "";
	var fields = form.getElementsByTagName("input");
	for (var i = 0; i < fields.length; i++) {
		var f = fields[i];
		if (!f.name) continue;
		if ((f.type == "checkbox" || f.type == "radio") && !f.checked) continue;
		res += "&" + f.name + "=" + encodeURIComponent(f.value);
	}
	fields = form.getElementsByTagName("textarea");
	for (i = 0; i < fields.length; i++) {
		f = fields[i];
		if (f.name)
			res += "&" + f.name + "=" + encodeURIComponent(f.value);
	}

	fields = form.getElementsByTagName("select");
	for (i = 0; i < fields.length; i++) {
		f = fields[i];
		if (f.name)
			res += "&" + f.name + "=" + encodeURIComponent(f.options[f.selectedIndex].value);
	}
	return res.substring(1);
}

function getQueryString(name) {
	name = name || '';
	paramStr = window.location.search;
	if (name == '') {
		if (paramStr != '') {
			paramStr = str_replace('?', '', paramStr);
			return encodeURI(paramStr);
		}
	}
	else {
		if(paramStr.indexOf(name) == -1) return '';
		else {
			begin = paramStr.indexOf(name) + name.length + 1;
			end = paramStr.indexOf('&', begin);
			if(end == -1) end = paramStr.length;
			return unescape(paramStr.substring(begin, end));
		}
	}
	return '';
}

function is_array( mixed_var ) {	// Finds whether a variable is an array
	return ( mixed_var instanceof Array );
}

function elementShowHide(id) {
	id = ajax_getObject(id);
	if (id) {
		if (id.style.display == 'block') {
			id.style.display = 'none';
		}
		else {
			id.style.display = 'block';
		}
	}
}

function str_replace(search, replace, subject){
	var ra = replace instanceof Array,
		sa = subject instanceof Array,
		l = (search = [].concat(search)).length,
		replace = [].concat(replace),
		i = (subject = [].concat(subject)).length;
	while(j = 0, i--)
		while(subject[i] = subject[i].split(search[j]).join(ra ? replace[j] || "" : replace[0]), ++j < l);
	return sa ? subject : subject[0];
}

function onclick_arrow(list_array, id_row, count_rows_max, id_name, style_scroll, style_scroll_act) {

	var id_next = 0;
	var count_position = parseInt(count_rows_max);
	var id_move = Math.ceil(count_position/2);
	var id_not_move = Math.floor(count_position/2);
	var count_scroll = 0;
	var count_scroll_max = list_array['text'].length - id_not_move;
	var id_position_global = parseInt(id_row);

	id_position_global = id_position_global + 1;

	switch (true) {
		case (((id_position_global < id_move) || (id_position_global > count_scroll_max))&&(!update_block)): update_block = false; break;
		case ((id_position_global == 1) || (id_position_global == list_array['text'].length)): update_block = false; break;
		case ((id_position_global > id_move) && !update_block): update_block = true; break;
		case (!update_block && (id_position_global < count_scroll_max)): update_block = true; break;
	}

	if (list_array["text"].length <= count_position) {
		count_position = list_array["text"].length;
		update_block = false;
	}
	else {
		if (id_position_global < (id_move + 1)) {
			count_scroll = 0;
		}
		else if (id_position_global > (list_array['text'].length - id_move)) {
			count_scroll = list_array['text'].length - count_position;
		}
		else {
			count_scroll = id_position_global - id_move;
		}

		if (style_scroll == style_scroll_act) {
			update_block = true;
			if (id_position_global == 1) {
				count_scroll = 0;
			}
			else if (id_position_global == list_array['text'].length) {
				count_scroll = list_array['text'].length - count_position;
			}
			else {
				count_scroll = id_position_global - id_move;
			}
		}

		if (id_position_global == 1) {
			ajax_setInnerHTML("img_foto_small_left_" + id_name,arrow_left);
		}
		else if ((id_position_global < (id_move + 1)) && (style_scroll == style_scroll_act)) {
			ajax_setInnerHTML("img_foto_small_left_" + id_name,arrow_left);
		}
		else {
			id_next = id_position_global - 2;
			arrow_act = str_replace("onclick_replace", 'onclick_arrow(' + id_name + ', ' + id_next + ', ' + count_rows_max + ', \'' + id_name + '\', \'' + style_scroll + '\', \'' + style_scroll_act + '\'); ' + list_array['click_arrow'][id_position_global - 2], arrow_left_act);
			ajax_setInnerHTML("img_foto_small_left_" + id_name,arrow_act);
		}

		if (id_position_global == list_array['text'].length) {
			ajax_setInnerHTML("img_foto_small_right_" + id_name,arrow_right);
		}
		else if ((id_position_global > (list_array['text'].length - id_move)) && (style_scroll == style_scroll_act)) {
			ajax_setInnerHTML("img_foto_small_right_" + id_name,arrow_right);
		}
		else {
			id_next = id_position_global;
			arrow_act = str_replace("onclick_replace", 'onclick_arrow(' + id_name + ', ' + id_next + ', ' + count_rows_max + ', \'' + id_name + '\', \'' + style_scroll + '\', \'' + style_scroll_act + '\'); ' + list_array['click_arrow'][id_position_global], arrow_right_act);
			ajax_setInnerHTML("img_foto_small_right_" + id_name,arrow_act);
		}
	}
	for (i=0; i<count_position; i++){
		if (update_block) {
			ajax_setInnerHTML(id_name + "[" + i + "]",list_array['text'][count_scroll + i]);
		}
		if (i == (id_position_global - count_scroll - 1)) {
			ajax_setClassName(id_name + "[" + i + "]",style_scroll_act);
		}
		else {
			ajax_setClassName(id_name + "[" + i + "]",style_scroll);
		}
	}
}

function changeAreaFotos(first, second, table, blockFoto) {
	var ajaxFile = '/operationFotos/' + table + '/position/list.ajax';
	var param = 'first=' + first + '&second=' + second + '&blockFotos=' + blockFoto;
	ajax_update(ajaxFile, param, blockFoto);
}

function number_format( number, decimals, dec_point, thousands_sep ) {
	var i, j, kw, kd, km;
	if( isNaN(decimals = Math.abs(decimals)) ) decimals = 2;
	if( dec_point == undefined ) dec_point = ","
	if( thousands_sep == undefined ) thousands_sep = ".";
	i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
	if( (j = i.length) > 3 ){
		j = j % 3;
	} else{
		j = 0;
	}
	km = (j ? i.substr(0, j) + thousands_sep : "");
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");
	return km + kw + kd;
}

function offsetPosition(element) {
	var offsetLeft = 0, offsetTop = 0;
		do {
			offsetLeft += element.offsetLeft;
			offsetTop  += element.offsetTop;
		} while (element = element.offsetParent);
	return [offsetLeft, offsetTop];
}

function offsetXPosition(element) {
	var offsetLeft = 0;
		do {
			offsetLeft += element.offsetLeft;
		} while (element = element.offsetParent);
	return offsetLeft;
}

function offsetYPosition(element) {
	var offsetTop = 0;
		do {
			offsetTop  += element.offsetTop;
		} while (element = element.offsetParent);
	return offsetTop;
}

function setHtmlInBlocks(tegName, atributeName, atributeValue, value) {
	ajax_setHtmlInBlocks(tegName, atributeName, atributeValue, value);
	return false;
}

