function setCookie(name, value, seconds, path) {
	if (!seconds) seconds = 0;
	if (!path) path = "/";
	var expires = new Date();
	expires.setTime(expires.getTime()+seconds*1000);
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() + "; path="+path;
}


function getCookie(name) {
	var cookies = document.cookie;
	var startpos = cookies.indexOf(name);
	if (startpos != -1) {
		startpos += name.length+1;
		var endpos = cookies.indexOf(";",startpos);
		if (endpos == -1) endpos = cookies.length;
		return unescape(cookies.slice(startpos,endpos));
	}
	else
		return false;
}

function setTown(id) {
	setCookie('town', id, 60*60*3);
}

function logOut() {
	setCookie('user', 0, -1);
}

function genLocation(obj) {
	var url = window.location.href;
	if (typeof obj == 'object') {
		chr = (url.indexOf('?') != -1) ? '&' : '?';
		for (var param in obj) {
			var ins = param+'='+obj[param];
			var si = url.indexOf('&'+param+'=');
			var subchr = '&';
			if (si == -1) {
				si = url.indexOf('?'+param+'=');
				subchr = '?';
			}
			if (si != -1) {
				var ei = url.indexOf('&',si+1);
				if (ei == -1) ei = url.length;
				url = url.slice(0,si) + subchr + ins + url.slice(ei,url.length);
			}
			else {
				url+=chr+ins;
			}
		}
	}
	return url;
}

function checkForm(f) {
	var obligatory = $(".obligatory[name]",f).get();
	for (i=0;i<obligatory.length;i++) {
		if (obligatory[i].name == 'email') {
        	re = /^\w[\w\-\.]*@\w[\w\-]+\.([\w\-]+\.)*\w{1,4}$/;
			r = obligatory[i].value.match(re);
			if (r == null) {
				$(obligatory[i]).addClass('checkError');
				obligatory[i].focus();
    			return false;
			}
		}
		else if(!obligatory[i].value || obligatory[i].value == 0) {
        	$(obligatory[i]).addClass('checkError');
			obligatory[i].focus();
			return false;
		}
	}
	return true;
}

function selectAll(f,p) {
	$(":checkbox",f).attr("checked",p.checked);
}

function withChecked(f,a) {
	if ($(":checkbox[name^='check_']:checked",f).length) {
		return (a == "del")? confirm("Удалить выбранные?"):true;
	}
	return false;
}