var messages = new Array(
	'Proszę podać poprawny adres email',
	'Podany adres email został dodany do naszej bazy.\nNa podany adres został wysłany email z aktywacją subskrybcji.',
	'Podany adres email istnieje już w naszej bazie subskrybentów',
	'Podany adres email zostanie usunięty z naszej bazy.\nNa podany adres został wysłany email z linkiem potwierdzającym usunięcie.',
	'Podany adresm email nie istnieje w naszej bazie subskrybentów.'
);

function popImage(image,title)
{
	title=title || 'Auto-Boss';
	tcp=window.open('','tc','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=100,height=100');
	tcp.document.open();
	tcp.document.write('<html><head><title>'+title+'</title><script>var __noframe=true;</script><script src="js/protoculous.js"></script><script src="js/common.js"></script></head><body onLoad="resizeWin(self,$(\'pic\').width,$(\'pic\').height)" style="padding: 0px; margin: 0px; background-color: white;"><img id="pic" src="'+image+'" onClick="self.close()" style="cursor: hand"></body></html>')
	tcp.document.close();
}

function resizeWin(win,w,h)
{
	iWidth = win.innerWidth?win.innerWidth:win.document.body.clientWidth;
	iHeight = win.innerHeight?win.innerHeight:win.document.body.clientHeight;
	iWidth = w - iWidth;
	iHeight = h - iHeight;
	win.resizeBy(iWidth, iHeight);
	win.focus();
}

var NewCar = {

	view: 'Info',

	selectView: function(view)
	{
		$('NewCar' + this.view).hide();
		$('NewCarTab' + this.view).removeClassName('selected');
		$('NewCarTab' + view).addClassName('selected');
		$('NewCar' + view).show();
		$('NewCarViewLabel').update($('NewCarTab' + view).innerHTML);
		this.view = view;
	}

}


var TopMenu = {

	visible: false,

	showList: function()
	{
		if (!this.visible) {
			var pos = Position.cumulativeOffset($('TopMenuSelector'));
			$('TopMenuPulldown').setStyle({
				left: pos[0],
				top: pos[1] + 21
			});
			$('TopMenuPulldown').show();
			this.visible = true;
			Event.observe('Container', 'mouseup', TopMenu.hideList);
		} else {
			TopMenu.hideList();
		}
	},

	hideList: function()
	{
		TopMenu.visible = false;
		$('TopMenuPulldown').hide();
		Event.stopObserving('Container', 'mouseup', TopMenu.hideList);
	}

}

var loadImage;

function setMainImage(src) {
	Element.show('LoadingImage')
	loadImage = new Image();
	loadImage.onload = function() {
		Element.hide('LoadingImage');
		$('MainImage').src = loadImage.src;
	}
	loadImage.src = src;
}

function isValidEmail(email)
{
	var splitted = email.match("^(.+)@(.+)$");
	if(splitted == null) return false;
	if(splitted[1] != null ) {
		var regexp_user=/^\"?[\w-_\.]*\"?$/;
		if(splitted[1].match(regexp_user) == null) return false;
	}
	if(splitted[2] != null) {
		var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		if(splitted[2].match(regexp_domain) == null) {
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if(splitted[2].match(regexp_ip) == null) return false;
		}
		return true;
	}
	return false;
}

function isEmpty(str,message)
{
	if (str.replace(/ /g,'')=='') {
		if (typeof(message)!='undefined') {
			alert(message);
		}
		return true;
	}
	return false;
}

function newsletterAdd(email)
{
	if (!isValidEmail(email)) {
		alert(messages[0]);
		return false;
	}

	$('sp').src="newsletter.php?do=add&email="+email+"&x="+Math.random();
}

function newsletterDelete(email)
{
	if (!isValidEmail(email)) {
		alert(messages[0]);
		return false;
	}
	$('sp').src="newsletter.php?do=delete&email="+email+"&x="+Math.random();
}

function notifyPopup(url)
{
	window.open(
		'powiadom.html?url=' + escape(url),
		'kontakt',
		'toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=1, width=480, height=340'
	);
}

function contactPopup(id, nazwa, oferta)
{
	window.open(
		'kontakt.html?id=' + id + '&name=' + escape(nazwa) + '&oferta=' + escape(oferta),
		'kontakt',
		'toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=1, width=480, height=370'
	);
}

function testDrivePopup()
{
	window.open(
		'jazdaprobna.html',
		'jazda',
		'toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1, width=500, height=450'
	);
}

function showPDF(id)
{
	window.open(
		'bielsko.php?p=SamochodyUzywane_Print&id=' + id,
		'pdf'
	);
}

function sellPopup()
{
	window.open(
		'sprzedaz.html',
		'jazda',
		'toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1, width=500, height=550'
	);
}

function getQueryParams()
{
	var qsParm = new Array();
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
		}
	}
	return qsParm;
}

if (typeof(__noframe)=='undefined') {
	document.write('<iframe id="sp" name="sp" src="" style="height:0px; width: 1px" scrolling="no" frameborder="0"></iframe>');
}