/**
 * Trim special chars from string
 * @returns trimmed string
 * @type String
 */

String.prototype.trim = function() {
    return this.replace(/^[\r\n\t\s]+|[\r\n\t\s]+$/g, "");
};


/**
 * Substitute non-latin chars to it's latin representation
 * @returns substituted string
 * @type String
 */

String.prototype.onlyLatinChars = function() {
	var chars = {'ā' : 'a','ē' : 'e','ī' : 'i','ū' : 'u','õ' : 'o','ŗ' : 'r','š' : 's','ģ' : 'g','ķ' : 'k','ļ' : 'l','ž' : 'z','č' : 'c','ņ' : 'n'};
	var str = this;
	var r = new RegExp();
	for ( i in chars ) {
		r = new RegExp( i, 'g' );
		str = str.replace( r, chars[i] );
		r = new RegExp( i.toUpperCase(), 'g' );
		str = str.replace( r, chars[i].toUpperCase() );
	}
	return str;
};


/**
 * Separates string by commas, return array
 * @returns array of values
 * @type Array
 */
 
String.prototype.splitIntoTerms = function() {
    return this.split( /\s*,\s*/ );
};


/**
 * @returns last comma separated value in string
 * @type String
 */
 
String.prototype.lastTerm = function() {
    return this.splitIntoTerms().pop();
};


/**
 * Check if value exists in array
 * @param {String} value Niddle
 * @returns true if value is find in array, else false
 * @type Boolean
 */

Array.prototype.has = function(value) {
    for (var i in this) {
        if (this[i] == value) return true;
    }
    return false;
};

/**
 * Removes dublicates from array
 * @param {Boolean} strict Also compare variable type
 * @returns unicue array
 * @type Array
 */

Array.prototype.unique = function(strict) {
    strict = typeof strict == 'undefined' ? false : true;
    var checker = {};
    var out = [];
    var hash = '';
    for (var i = 0, j = this.length; i < j ; i++) {
        hash = this[i] + (strict ? typeof this[i] : '');
        if (!checker[hash]) {
            out.push(this[i]);
            checker[hash] = true;
        }
    }
    return out || this;
};
