function sprawdzEmail(email)
{
    return (email.indexOf(".") > 2 && email.indexOf("@") > 0);
}

function getElement(id)
{
	if (document.all)
		var element = document.all[id];
	else if (document.layers)
		var element = document.layers[id];
	else if (document.getElementById)
		var element = document.getElementById(id);
	else
		return null;

	return element;
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function popup(url, name, width, height)
{
	var x = (screen.availWidth - width)/2;
	var y = (screen.availHeight - height)/2;

	win = window.open(url, name, 'width='+width+',height='+height+',left='+x+',top='+y+',toolbar=0,menubar=0,scrollbars=0,resizable=0');

	if (win)
		win.focus();

	return win;
}

function toggle(e)
{
	if (e.style.display == 'none')
		e.style.display = 'block';
	else
		e.style.display = 'none';
}

function parseNIP(input)
{
	input.value = input.value.replace(/[^0-9\-]+/, '');
}

function parseREGON(input)
{
	input.value = input.value.replace(/[^0-9]+/, '');
}

function parseTelefon(input)
{
	input.value = input.value.replace(/[a-zA-Z]+/, '');
}

function potwierdzUsuniecie()
{
	return confirm('Czy na pewno chcesz usunąć wybrany rekord?');
}

/*
	AJAX: szukajka
*/

function pobierzRodzaje()
{
	advAJAX.get({
		url: "webservice",

		parameters :
		{
			"action" : "getTypes"
		},

		onSuccess : function(obj)
		{
			getElement('sRodzaje').innerHTML = obj.responseText;
		},

		onError : function(obj)
		{
			alert("Error: " + obj.status);
		}
	});
}

function pobierzWojewodztwa()
{
	advAJAX.get({
		url: "webservice",

		parameters :
		{
			"action" : "getStates",
			"country" : "Polska"
		},

		onSuccess : function(obj)
		{
			getElement('sWojewodztwa').innerHTML = obj.responseText;
		},

		onError : function(obj)
		{
			alert("Error: " + obj.status);
		}
	});

	pobierzMiasta();
}

function pobierzMiasta()
{
	advAJAX.get({
		url: "webservice",

		parameters :
		{
			"action" : "getCities",
			"country" : "Polska",
			"state" : getElement('sWojewodztwaSelect').value
		},

		onSuccess : function(obj)
		{
			getElement('sMiasta').innerHTML = obj.responseText;
		},

		onError : function(obj)
		{
			alert("Error: " + obj.status);
		}
	});

	pobierzDzielnice();
}

function pobierzDzielnice()
{
	advAJAX.get({
		url: "webservice",

		parameters :
		{
			"action" : "getDistricts",
			"country" : "Polska",
			"state" : getElement('sWojewodztwaSelect').value,
			"city" : getElement('sMiastaSelect').value
		},

		onSuccess : function(obj)
		{
			getElement('sDzielnice').innerHTML = obj.responseText;
		},

		onError : function(obj)
		{
			alert("Error: " + obj.status);
		}
	});
}

function pobierzParametryOfert()
{
	if (getElement('parametry'))
	{
		advAJAX.get({
			url: "webservice",

			parameters :
			{
				"action" : "getParameters",
				"id" : getElement('sRodzajeSelect').value
			},

			onSuccess : function(obj)
			{
				getElement('parametry').innerHTML = obj.responseText;
			},

			onError : function(obj)
			{
				alert("Error: " + obj.status);
			}
		});
	}
}

function pokazGieldeOgloszen()
{
    getElement('divGielda').style.display = 'block';
    getElement('divMapa').style.display = 'none';
}

function pokazMapeOgloszen()
{
    getElement('divGielda').style.display = 'none';
    getElement('divMapa').style.display = 'block';
}

