
/*========================================================
Set focus to any field.
========================================================*/


function _set_focus ( _field ) { 
	_field.focus (  );
	_field.select (  );
}

/*========================================================
Trimming object. expr can be either string or array of strings.
========================================================*/
function trim_object(expr) {

	var new_expr;

	if (typeof(expr) == "string")
		new_expr = trim(expr);
	else {
		new_expr = trim_array(expr);
		for (var i=0; i < new_expr.length; i++) {
			new_expr[i] = trim (new_expr[i]);
		}
	}

	return new_expr;
}

/*========================================================
Trimming string. Removes spaces from begin and end of the string. Removes double spaces.
========================================================*/
function trim(str) {

	var new_str = "";
	var i = 0;

	while (str.charAt(i) == " ") {
		i++;
	}

	for (i; i< str.length; i++ ) {
		if ( str.charAt ( i) != " " ) {
			new_str += str.charAt ( i);
		}
		else {
	  	if (i<(str.length-1)) {
				if (str.charAt(i+1) != " ") {
					new_str += str.charAt ( i);
				}
			}
		}
	}

	return new_str;
}

/*========================================================
Trimming array. Removes empty elements from array.
========================================================*/
function trim_array(arr) {
	var new_arr = new Array();

	for (var i= 0, j = 0; i< arr.length; i++) {
		if (arr[i] != "") {
			new_arr[j] = arr[i];
			j++;
		}
	}

	return new_arr;
}

/*========================================================
Searches the string or array of strings from the left to the right and returns the part of the string from 0 to _pos. _expr can be either string or array of strings.
========================================================*/
function _left ( _expr, _pos ) { 
	var _new_expr;
	var _r_pos;

	if ( typeof ( _expr ) == "string" ) 
		_new_expr = _left_string ( _expr, _pos );
	else { 
		_new_expr = new Array (  );
		for ( _i = 0; _i < _expr.length; _i ++ ) { 
		_new_expr [ _i ] = _left_string ( _expr [ _i ], _pos ); 
		} 
	}

  return _new_expr; 
}

/*========================================================
Searches the string from the left to the right and returns the part of the string from 0 to _pos.
_pos can be either a number or string. If _pos is float number it rounded, if _pos is negative or greater than string length all string is returned.
========================================================*/
function _left_string ( _string, _pos ) { 
	var _new_string = "";
	var _r_pos;

	if ( !isNaN ( _pos ) ) { 
		_r_pos = Math.round ( _pos );
		if ( _r_pos >= 0 && _r_pos <= _string.length ) 
			_new_string = _string.substr ( 0, _r_pos )
		if ( _r_pos < 0 || _r_pos > _string.length ) 
			_new_string = _string 
	}
	else { 
		_r_pos = _string.indexOf ( _pos );
		if ( _r_pos != -1 ) 
			_new_string = _string.substr ( 0, _r_pos ) 
	}

	return _new_string; 
}

/*========================================================
Searches the string or array of strings from the left to the right and returns the part of the string from _pos to the end of the string. _expr can be either string or array of strings.
========================================================*/
function _right ( _expr, _pos ) { 
	var _new_expr;
	var _r_pos;

	if ( typeof ( _expr ) == "string" ) 
		_new_expr = _right_string ( _expr, _pos );
	else { 
		_new_expr = new Array (  );
		for ( _i = 0; _i < _expr.length; _i ++ ) { 
			_new_expr [ _i ] = _right_string ( _expr [ _i ], _pos );
		}
	}

	return _new_expr 
}

/*========================================================
Searches the string from the left to the right and returns the part of the string from _pos to the end of the string.
_pos can be either a number or string. If _pos is float number it rounded, if _pos is negative or greater than string length all string is returned.
========================================================*/
function _right_string ( _string, _pos ) { 
	var _new_string = "";
	var _r_pos;

	if ( !isNaN ( _pos ) ) { 
		_r_pos = Math.round ( _pos );
		
		if ( _r_pos >= 0 && _r_pos <= _string.length ) 
			_new_string = _string.substr ( _string.length - _r_pos )
			
		if ( _r_pos < 0 || _r_pos > _string.length ) 
			_new_string = _string 
	}
	else { 
		_r_pos = _string.indexOf ( _pos );
		
		if ( _r_pos != -1 ) 
			_new_string = _string.substr ( _r_pos + _pos.length ); 
	}

	return _new_string; 
}

/*========================================================
Searches the string or array of strings from the left to the right and returns the part of the string from _pos1 to _pos2. _expr can be either string or array of strings.
========================================================*/
function _middle ( _expr, _pos1, _pos2 ) { 
	var _new_expr;
	var _r_pos;

	if ( typeof ( _expr ) == "string" ) 
		_new_expr = _middle_string ( _expr, _pos1, _pos2 );
	else { 
		_new_expr = new Array (  );
		for ( _i = 0; _i < _expr.length; _i ++ ) { 
			_new_expr [ _i ] = _middle_string ( _expr [ _i ], _pos1, _pos2 );
		}
	}

	return _new_expr;
}

/*========================================================
Searches the string from the left to the right and returns the part of the string from _pos1 to _pos2.
_pos1, _pos2 can be either a number or string. If _pos1, _pos2 is float numbers they are rounded, if _pos1 is negative or greater than string length empty string is returned,
if _pos2 is negative then searches the string from _pos1 to the left and returns _pos2 numbers of characters,
if _pos2 is greater then string length then returns the string from _pos1 to the end of the string.
========================================================*/
function _middle_string ( _string, _pos1, _pos2 ) { 
	var _new_string = "";
	var _r_pos1;
	var _r_pos2;

	if ( !isNaN ( _pos1 ) && !isNaN ( _pos2 ) ) { 
		_r_pos1 = Math.round ( _pos1 );
		_r_pos2 = Math.round ( _pos2 );
		if ( _r_pos1 >= 0 && _r_pos1 <= _string.length ) _new_string = _r_pos2 >= 0 ? _string.substr ( _r_pos1, _r_pos2 ) : _right ( _left ( _string, _r_pos1 ), Math.abs ( _r_pos2 ) );
		if ( _r_pos1 < 0 || _r_pos1 > _string.length ) _new_string = "";
	}
	else { 
		if ( !isNaN ( _pos1 ) && isNaN ( _pos2 ) ) { 
			_r_pos1 = Math.round ( _pos1 );
			if ( _r_pos1 < 0 ) return "";
			_r_pos2 = _string.substr ( _r_pos1 ).indexOf ( _pos2 );
			if ( _r_pos2 != -1 ) 
				_new_string = _string.substr ( _r_pos1, _r_pos2 )
			else 
				_new_string = _string.substr ( _r_pos1 ) 
		}
		else { 
			if ( isNaN ( _pos1 ) && !isNaN ( _pos2 ) ) { 
				_r_pos1 = _string.indexOf ( _pos1 );
				_r_pos2 = Math.round ( _pos2 );
				if ( _r_pos1 != -1 ) _new_string = _r_pos2 >= 0 ? _string.substr ( _r_pos1 + 1, _r_pos2 ) : _right ( _left ( _string, _r_pos1 ), Math.abs ( _r_pos2 ) ) 
			}
			else { 
				if ( isNaN ( _pos1 ) && isNaN ( _pos2 ) ) { 
					_r_pos1 = _string.indexOf ( _pos1 );
					_r_pos2 = _string.substr ( _r_pos1 + 1 ).indexOf ( _pos2 );
				 	if ( _r_pos1 != -1 ) 
				 		_new_string = _r_pos2 != -1 ? _string.substr ( _r_pos1 + 1, _r_pos2 ) : _string.substr ( _r_pos1 + 1 ); 
				} 
			} 
		} 
	}

  return _new_string;
}

/*========================================================
Searches the string or array of strings from the right to the left and returns the part of the string from 0 to ( length - _pos ). _expr can be either string or array of strings.
========================================================*/
function _left_back ( _expr, _pos ) { 
	var _new_expr;
	var _r_pos;

	if ( typeof ( _expr ) == "string" ) 
		_new_expr = _left_back_string ( _expr, _pos );
	else { 
		_new_expr = new Array (  );
		for ( _i = 0; _i < _expr.length; _i ++ ) { 
			 _new_expr [ _i ] = _left_back_string ( _expr [ _i ], _pos ) } }

	return _new_expr 
}

/*========================================================
Searches the string from the right to the left and returns the part of the string from 0 to ( length - _pos ).
_pos can be either a number or string. If _pos is float number it rounded, if _pos is negative or greater than string length empty string is returned.
========================================================*/
function _left_back_string ( _string, _pos ) { 
   var _new_string = "";
   var _r_pos;

   if ( !isNaN ( _pos ) ) { 
      _r_pos = Math.round ( _pos );
      if ( _r_pos >= 0 && _r_pos <= _string.length ) _new_string = _string.substr ( 0, _string.length - _r_pos )
      if ( _r_pos < 0 || _r_pos > _string.length ) _new_string = "" }
   else { 
      _r_pos = _string.lastIndexOf ( _pos );
      if ( _r_pos != -1 ) _new_string = _string.substr ( 0, _r_pos ) }

   return _new_string 
}

/*========================================================
Searches the string or array of strings. _expr can be either string or array of strings.
========================================================*/
function _right_back ( _expr, _pos ) { 
	var _new_expr;
	var _r_pos;

	if ( typeof ( _expr ) == "string" ) 
		_new_expr = _right_back_string ( _expr, _pos );
	else { 
		_new_expr = new Array (  );
		for ( _i = 0; _i < _expr.length; _i ++ ) { 
			_new_expr [ _i ] = _right_back_string ( _expr [ _i ], _pos ); 
		} 
	}

	return _new_expr; 
}

/*========================================================
_pos can be either a number or string. If _pos is float number it rounded, if _pos is negative or greater than string length empty string is returned.
If _pos is a number then searches the string from the left to the right and returns the part of the string from _pos to the end of the string.
If _pos is a string then searches the string from the right to the left and returns the part of the string from _pos to the end of the string
========================================================*/
function _right_back_string ( _string, _pos ) { 
	var _new_string = "";
	var _r_pos;

	if ( !isNaN ( _pos ) ) { 
		_r_pos = Math.round ( _pos );
		if ( _r_pos >= 0 && _r_pos <= _string.length ) _new_string = _string.substr ( _r_pos )
		if ( _r_pos < 0 || _r_pos > _string.length ) _new_string = _string 
	}
	else { 
		_r_pos = _string.lastIndexOf ( _pos );
		if ( _r_pos != -1 ) _new_string = _string.substr ( _r_pos + _pos.length ) 
	}

	return _new_string; 
}

/*========================================================
Searches the string or array of strings from the rigth to the left and returns the part of the string from _pos1 to _pos2. _expr can be either string or array of strings.
========================================================*/
function _middle_back ( _expr, _pos1, _pos2 ) { 
	var _new_expr;
	var _r_pos;

	if ( typeof ( _expr ) == "string" ) 
		_new_expr = _middle_back_string ( _expr, _pos1, _pos2 );
	else { 
		_new_expr = new Array (  );
		for ( _i = 0; _i < _expr.length; _i ++ ) { 
			_new_expr [ _i ] = _middle_back_string ( _expr [ _i ], _pos1, _pos2 ); 
		} 
	}

	return _new_expr; 
}

/*========================================================
Searches the string from the right to the left and returns the part of the string from _pos1 to _pos2.
_pos1, _pos2 can be either a number or string. If _pos1, _pos2 are float numbers they are rounded, if _pos1 is negative or greater than string length empty string is returned,
if _pos2 is negative then searches the string from _pos1 to the left and returns _pos2 numbers of characters,
if _pos2 is greater then string length then returns the string from _pos1 to the end of the string.
========================================================*/
function _middle_back_string ( _string, _pos1, _pos2 ) { 
	var _new_string = "";
	var _r_pos1;
	var _r_pos2;

	if ( !isNaN ( _pos1 ) && !isNaN ( _pos2 ) ) { 
		_r_pos1 = Math.round ( _pos1 );
		_r_pos2 = Math.round ( _pos2 );
		if ( _r_pos1 >= 0 && _r_pos1 <= _string.length ) _new_string = _r_pos2 >= 0 ? _string.substr ( _string.length - _r_pos1 + 1, _r_pos2 ) : _right ( _left ( _string, _string.length - _r_pos1 + 1 ), Math.abs ( _r_pos2 ) )
		if ( _r_pos1 < 0 ) _new_string = ""
		if ( _r_pos1 > _string.length ) _new_string = _string.substr ( 1, _r_pos2 ) 
	}
	else { 
		if ( !isNaN ( _pos1 ) && isNaN ( _pos2 ) ) { 
			_r_pos1 = Math.round ( _pos1 );
			if ( _r_pos1 < 0 ) return ""
			if ( _r_pos1 > _string.length ) return _string.substr ( 0, 1 )
			_r_pos2 = _string.substr ( 0, _string.length - _r_pos1 + 1 ).lastIndexOf ( _pos2 );
			if ( _r_pos2 != -1 ) 
			_new_string = _right_back ( _left_back ( _string, _r_pos1 - 1 ), _r_pos2 + 1 )
			else _new_string = _string.substr ( 0, _string.length - _r_pos1 + 1 ) 
		}
		else { 
			if ( isNaN ( _pos1 ) && !isNaN ( _pos2 ) ) { 
				_r_pos1 = _string.lastIndexOf ( _pos1 );
				_r_pos2 = Math.round ( _pos2 );
				if ( _r_pos1 != -1 ) 
					_new_string = _r_pos2 >= 0 ? _string.substr ( _r_pos1 + 1, _r_pos2 ) : _right ( _left ( _string, _r_pos1 ), Math.abs ( _r_pos2 ) ); 
			}
			else { 
				if ( isNaN ( _pos1 ) && isNaN ( _pos2 ) ) { 
					_r_pos1 = _string.lastIndexOf ( _pos1 );
					if ( _r_pos1 == -1 ) return "";
					_r_pos2 = _string.substr ( 0, _r_pos1 ).lastIndexOf ( _pos2 );
					_new_string = _r_pos2 != -1 ? _right_back ( _left ( _string, _r_pos1 ), _r_pos2 + 1 ) : _string.substr ( 0, _r_pos1 ); 
				} 
			} 
		} 
	}

	return _new_string;
}

/*========================================================
Returns the _pos-th symbol from string or array of strings that is delimited by the defined separator character. _expr can be either a string or array of strings.
========================================================*/
function _word ( _expr, _sep, _pos ) { 
	var _new_expr;

	if ( typeof ( _expr ) == "string" ) 
		_new_expr = _word_string ( _expr, _sep, _pos );
	else { 
		_new_expr = new Array (  );
		for ( _i = 0; _i < _expr.length; _i ++ ) { 
			_new_expr [ _i ] = _word_string ( _expr [ _i ], _sep, _pos );
		} 
	}
	
	return _new_expr; 
}

/*========================================================
Returns the _pos-th symbol from string that is delimited by the defined separator character.
========================================================*/
function _word_string ( _string, _sep, _pos ) { 
	var _new_string;
	var _array = _string.split ( _sep );

	_new_string = _array [ _pos - 1 ];

	return _new_string; 
}

