function Redirect(url)
{
	window.location = url;
}

function SubmitForm(name)
{
	document.forms[name].submit();
}

function ToggleCheckbox(id)
{
	document.getElementById(id).checked == true ? document.getElementById(id).checked = false : document.getElementById(id).checked = true;
}

// Load page of address "url" and copy it's output into element of id/name "id"
function RequestPage(url, id)
{
	if (window.XMLHttpRequest)
	{	// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{	// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	url = encodeURI(url);
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	
	if (id != -1)
		document.getElementById(id).innerHTML=xmlhttp.responseText;
	else
		return xmlhttp.responseText;
}

// Zaznaczenie wszystkich wiadomosci
function MessagesCheckAll(fields)
{
	if (typeof(fields) == "undefined")
		return false;
		
	if (fields.length)
	{
		for (var x=0; x < fields.length; x++)
			fields[x].checked = true;
	}
	else
	{
		fields.checked = true;
	}
}

// Odznaczenie wszystkich wiadomosci
function MessagesUnCheckAll(fields)
{
	if (typeof(fields) == "undefined")
		return false;
		
	if (fields.length)
	{
		for (var x=0; x < fields.length; x++)
			fields[x].checked = false;
	}
	else
	{
		fields.checked = false;
	}
}

// Usuniecie zaznaczonych wiadomosci
function MessagesDelete(fields, url)
{
	if (typeof(fields) == "undefined")
		return false;
		
	var ids = '';
	if (fields.length)
	{
		for (var x=0; x < fields.length; x++)
			fields[x].checked == true ? ids += '|'+fields[x].id : false;
	}
	else
	{
		fields.checked == true ? ids += '|'+fields.id : false;
	}
	ids = ids.substr(1, 300);
	Redirect(url+'&ms='+ids);
}

// Scale image to it's parent
function Scale(el, x, y) {
	// 85x64
	var w = $(el).width();
	var h = $(el).height();

	if (w > x || h > y) {
		var k = w/x;
		var l = h/y;

		if (k > l) {
			w = x;
			h = h/k;
		} else {
			h = y;
			w = w/l;
		}
		$(el).attr('width', w);
		$(el).attr('height', h);
		var margin = (y - h)/2;
		$(el).css('margin-top', margin);
	}
}

function ScaleObject(object, toW, toH) {
	var diffW = 1;
	var diffH = 1;
	var diff = 1;
	
	if (object.w > toW) {
		diffW = object.w / toW;
	}
	
	if (object.h > toH) {
		diffH = object.w / toW;
	}
	if (diffW > diffH) {
		diff = diffW;
	} else {
		diff = diffH;
	}
	object.setW( object.w * diff );
	object.setH( object.h * diff );
}

// Scale image to it's parent
// if type is true it will scale image even if it's not bigger then parent
// if force is true image is scaled to exact x and y
function Scale(el, x, y, type, force) {
	// 85x64
	if (type == undefined) type = false;
	if (force == undefined) force = false;
	var w = $(el).width();
	var h = $(el).height();
	var change = false;

	if (((w > x || h > y) || type) && !force) {
		var k = w/x;
		var l = h/y;

		if (k > l) {
			w = x;
			h = Math.round(h/k);
		} else {
			h = y;
			w = Math.round(w/l);
		}
		change = true;
	}
	
	if (!change && !force) return;
	
	if (force) {
		$(el).attr('width', x);
		$(el).attr('height', y);
	} else {
		$(el).attr('width', w);
		$(el).attr('height', h);
		var margin_top = (y - h)/2;
		var margin_left = (x - w)/2;
		$(el).css('margin-top', margin_top);
		$(el).css('margin-left', margin_left);
		$(el).css('margin-right', margin_left);
	}
}

//Fit image to it's parent
function Fit(el, x, y, type, h, w) {
	// 85x64
	if (w == undefined) {
		w = $(el).width();
	}
	if (h == undefined) {
		h = $(el).height();
	}

	var margin_top = 0;
	var margin_left = 0;

	if (type == "normal") {
		margin_top = (y - h)/2;
		margin_left = (x - w)/2;
	} else if (type == "down") {
		margin_top = (y - h);
		margin_left = (x - w)/2;
	} else if (type == "top") {
		margin_top = 0;
		margin_left = (x - w)/2;
	} else if (type == "left") {
		margin_top = (y - h)/2;
		margin_left = 0;
	} else if (type == "right") {
		margin_top = (y - h)/2;
		margin_left = (x - w);
	} else if (type == "hor_center") {
		margin_left = (x - w)/2;
		$(el).css('margin-left', margin_left);
		return;
	}
	$(el).css('margin-top', margin_top);
	$(el).css('margin-left', margin_left);
}

// Scale image
function Scale2(object, width, height, toW, toH, type) {
	var diffW = 1;
	var diffH = 1;
	var diff = 1;
	if (width > toW) {
		diffW = toW / width;
	}
	if (height > toH) {
		diffH = toH / height;
	}
	if (diffH > diffW) {
		diff = diffW;
	} else {
		diff = diffH;
	}
	height = height * diff;
	width = width * diff;
	object.style.height = height + 'px';
	object.style.width = width + 'px';

	if (type == 'def_img') {
		object.style.marginTop = '100px';
		object.style.marginLeft = (toW - width) / 2 + 'px';
	} else {
		object.style.marginTop = (toH - height) / 2 + 'px';
		object.style.marginLeft = (toW - width) / 2 + 'px';
	}
}

function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
 }

function $_GET(q) { 
	var url = window.location.href;
	var pos = url.indexOf('?')+1;
	var vars = new Array();
	var splited = url.substr(pos, url.length-pos).split('&');
	for (var it in splited) {
		var temp = splited[it].split('=');
		vars[temp[0]] = temp[1];
	}
	return vars[q];
}

function $_URL(index) { 
	var url = window.location.href;
	var pos = url.lastIndexOf('/')+1;
	
	var vars = new Array();
	var splited = url.substr(pos, url.length-pos).split(',');
	
	for (var i = 0; i < splited.length; i++) {
		vars[i] = splited[i];
	}

	return vars[index];
}

function addClass(el, cl) {
	el.className += ' '+cl;
	el.className = el.className.replace(/ +/g, ' ');
}

function removeClass(el, cl) {
	el.className = el.className.replace(cl, '');
}

function stopPropagationEvent(e) {
	if ('bubbles' in e) { // all browsers except IE before version 9
		if (e.bubbles) e.stopPropagation();
	} else { // Internet Explorer before version 9
		e.cancelBubble = true;
	}
}



