/* --[ prototype functions ]------------------------------------------------------------------- */
/** 
 * Check if the string is 'blank', meaning either empty or containing only whitespace.
 * (&lt;a href='http://prototypejs.org/api/string/blank/' target='_blank'&gt;http://prototypejs.org/api/string/blank/&lt;/a&gt;)
 * @return {boolean} true or false
 */
String.prototype.blank = function() { return /^\s*$/.test(this); }
/**
 * Check if the string is valid JSON by the use of regular expressions. This security method is called internally.
 * (&lt;a href='http://prototypejs.org/api/string/isJSON/' target='_blank'&gt;http://prototypejs.org/api/string/isJSON/&lt;/a&gt;)
 *
 * @return {String}
 */
String.prototype.isJSON = function() {
	var str = this;
	if (str.blank()) return false;
	var charAt0 = this.charAt(0);
	return charAt0 == '{' || charAt0 == '[';
//	str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
//	return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
};

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.filter) {
	Array.prototype.filter = function(fun /*, thisp*/) {
		var len = this.length;
		if (typeof fun != "function")
			throw new TypeError();

		var res = new Array();
		var thisp = arguments[1];
		for (var i = 0; i < len; i++) {
			if (i in this) {
				var val = this[i]; // in case fun mutates this
				if (fun.call(thisp, val, i, this))
					res.push(val);
			}
		}

		return res;
	};
}

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.map) {
	Array.prototype.map = function(fun /*, thisp*/) {
		var len = this.length;
		if (typeof fun != "function")
			throw new TypeError();

		var res = new Array(len);
		var thisp = arguments[1];
		for (var i = 0; i < len; i++) {
			if (i in this)
				res[i] = fun.call(thisp, this[i], i, this);
		}

		return res;
	};
}

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(elt /*, from*/) {
		var len = this.length;

		var from = Number(arguments[1]) || 0;
		from = (from < 0)
				 ? Math.ceil(from)
				 : Math.floor(from);
		if (from < 0)
			from += len;

		for (; from < len; from++) {
			if (from in this &&
					this[from] === elt)
				return from;
		}
		return -1;
	};
}

if (!Array.prototype.clean) {
	Array.prototype.clean = function(deleteValue) {
	  for (var i = 0; i < this.length; i++) {
	    if (this[i] == deleteValue) {         
	      this.splice(i, 1);
	      i--;
	    }
	  }
	  return this;
	};
}

if (!Array.prototype.max) {
	Array.prototype.max = function(){
	    return Math.max.apply( Math, this );
	};
}

if (!Array.prototype.min) {
	Array.prototype.min = function(){	
    	return Math.min.apply( Math, this );
	};
};

/*
Array.prototype.max = function() {
	var max = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
	return max;
}
Array.prototype.min = function() {
	var min = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
	return min;
}
*/

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

String.prototype.truncate = function(length, ending) {
	if (this.length <= length) {
		return this;
	} else {
		var truncate = this.substr(0, length - ending.length);
		return truncate + ending;
	}
}

String.prototype.lineSplit = function(lineLength, lineJoinStr) {
	txt = this.split(' ');
	var currentLineLength = 0;
	var ret = [];
	var lineNumber = 0;
	
	for (var i = 0; i < txt.length; i++) {
		var word = txt[i];
		var tmp1 = word.length;
		if (currentLineLength + word.length <= lineLength) {
			if (typeof ret[lineNumber] == 'undefined') {
				ret[lineNumber] = new Array();
			}
		ret[lineNumber].push(word);
		currentLineLength += word.length;
		} else {
		lineNumber++;
				
		if (word.length > lineLength) {
			word = word.truncate(lineLength, '...');
		}
					
		ret[lineNumber] = [word];
		currentLineLength = word.length;
		}
	}
	for (var i = 0; i < ret.length; i++) {
		if (typeof ret[i] != 'undefined') {
			ret[i] = ret[i].join(' ');
		}
	}
	return ret.join(lineJoinStr);
}

Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

// IE
if (typeof Array.prototype.map == 'undefined') {
	Array.prototype.map = function(fn, thisObj) {
		var scope = thisObj || window;
		var a = [];
		for ( var i=0, j=this.length; i < j; ++i ) {
			a.push(fn.call(scope, this[i], i, this));
		}
		return a;
	};
}
// IE
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

/*
Array.prototype.filter = function(fn, thisObj) {
	var scope = thisObj || window;
	var a = [];
	for ( var i=0, j=this.length; i < j; ++i ) {
		if ( !fn.call(scope, this[i], i, this) ) {
			continue;
		}
		a.push(this[i]);
	}
	return a;
};
*/
Array.prototype.foreach = function(fn, thisObj) {
	var scope = thisObj || window;
	for ( var i=0, j=this.length; i < j; ++i ) {
		fn.call(scope, i, this[i], this);
	}
};


// **************************************************************************
// Copyright 2007 - 2009 Tavs Dokkedahl
// Contact: http://www.jslab.dk/contact.php
//
// This file is part of the JSLab Standard Library (JSL) Program.
//
// JSL is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// JSL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// ***************************************************************************

// Return new array with duplicate values removed
/*
Array.prototype.unique = function() {
	var a = [];
	var l = this.length;
	for(var i=0; i<l; i++) {
		for(var j=i+1; j<l; j++) {
			// If this[i] is found later in the array
			if (this[i] === this[j])
			j = ++i;
		}
		a.push(this[i]);
	}
	return a;
};
*/


//use: var newObj = new cloneObject(oldObj);
function cloneObject(obj) {
	for (i in obj) {
		if (typeof obj[i] == 'object') {
			this[i] = new cloneObject(obj[i]);
		}
		else {
			this[i] = obj[i];
		}
	}
}

jQuery.fn.getSelectedText = function() {
	var a;
	this.each(function() {		
		if (this.tagName == 'SELECT') {
			var selectElement = this;
			a = selectElement.options[selectElement.options.selectedIndex].text;
		}			
	});
	return a;	
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Robert Nyman | http://robertnyman.com/ */
function removeHTMLTags(strInputCode){
	/* 
	This line is optional, it replaces escaped brackets with real ones, 
	i.e. < is replaced with < and > is replaced with >
	*/	
 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
	 	return (p1 == "lt")? "<" : ">";
	});
	var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
	return strTagStrippedText;
}