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


function getPosition(elem) {
alert(":)");
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
	
    var l = 0;
    var t = 0;
	
    while (elem) {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return {"left":l, "top":t, "width": w, "height":h};
}


/*--------- Date functions --------------*/

Date.prototype.formatByType = function (type) {
	var month = this.getMonth() + 1;
	month = month > 9 ? month : '0' + month;
	
	var day = this.getDate();
	day = day > 9 ? day : '0' + day;
			
	var year = this.getFullYear();
	switch (type) {
		case 'euro':
			return month + '-' + day + '-' + year;
			break;
		case 'mysql':
			return year + '-' + month + '-' + day;
			break;
		default:
			return day + '-' + month + '-' + year;
	}
}

Date.create = function(st) {
	var pieces = st.split("-");
	var year   = parseInt(pieces[0], 10);
	var month  = parseInt(pieces[1], 10) - 1;
	var day    = parseInt(pieces[2], 10);
	return new Date(year, month, day);
}

Date.prototype.nextDate = function(){
	return new Date(this.getFullYear(), this.getMonth(), this.getDate() + 1);
}

Date.prototype.previousMonth = function(){
	return new Date(this.getFullYear(), this.getMonth() - 1, 1);
}

Date.prototype.nextMonth = function(){
	return new Date(this.getFullYear(), this.getMonth() + 1, 1);
}


/*---------   Convertors      ---------------*/

function ucFirst (str) {
   if (typeof str == 'string') {
		return  str.substr(0,1).toUpperCase() + str.substr(1,str.length);
	}
	
}

function toPersent (value) {

	if (value && parseFloat(value) && parseFloat(value) != 'NaN') {
			if (typeof value == 'string') {  
				value = parseFloat(value);
			}
			value = value * 100;
			
			return value.toString();// + '%';
	} else {
		return 0;
	}
}

function simple_clone (object) { 
	/* !NB this function does not work for nested object 
		(if some properties of object is also objects),
		and for object, if some properties is function
		but this one is faster
	*/
	var clone = {};
	for (var i in object) {
		if (typeof object[i] != 'function') {
			clone[i] = object[i];
		}
	}
	return clone;
}

/* ---- JSON functionality --------------*/

function jsonEncode(object){
	return Json.toString(object);
}

function jsonDecode(string){
	return Json.evaluate(string);
}

function json_client(params){
	var url = params.url;
	var data = params.data;
	var callback = params.callback;
	var lockAction = params.lockAction;
	var unlockAction = params.unlockAction;
	if (typeof lockAction == 'function'){
		lockAction();
	}
	var request = document.all && !window.opera ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
	request.onreadystatechange = function(){
		if (request.readyState == 4){
			if (request.status != 200){
				throw new Error('JSON client: HTTP error: ' + request.status);
			}
			try{
				var response = jsonDecode(request.responseText);
			}
			catch(error){
				throw new Error('JSON client: Cannot parse json response: ' + request.responseText);
			}
			if (typeof callback != 'function'){
				throw new Error('JSON cliend: Specified callback is not function');
			}
			callback(response);
			if (typeof unlockAction == 'function'){
				unlockAction();
			}
		}
	}
	request.open('post', url + '?r=' + Math.random());
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.setRequestHeader('X-Request', 'JSON');
	JSON_CLIENT_LOCKED = true;
	request.send(jsonEncode(data));
}
 	
/*------- Console.log fixer for non firefox browsers ------*/
 	
if(typeof console == 'undefined') {
	window.console = function() {
		this.log = function(str) {
			alert('console.log: ' + str);
		}
	};
}
else if (typeof console.log != 'function') {
	console.log = function(str) {
		alert('console.log: ' + str);
	}
}