function setCookie (name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function check_flat(inp, id) {

	koltso_flats = getCookie("koltso_flats");
	now = new Date();

	expires = new Date(now.getFullYear(), now.getMonth() + 3, now.getDay());

	if (inp.checked) {

		if(koltso_flats == null || koltso_flats == '') {
			setCookie("koltso_flats", id, expires, '/', 'koltso.ru', 0);
		} else {
			koltso_flats += ',' + id;
			setCookie("koltso_flats", koltso_flats, expires, '/', 'koltso.ru', 0);
		}

	} else {
		if(koltso_flats != null) {
			array_koltso_flats = koltso_flats.split(',');
			new_koltso_flats = '';
			for (i = 0; i < array_koltso_flats.length; i++){
				if(array_koltso_flats[i] != id) new_koltso_flats += array_koltso_flats[i] + ',';
			}
			new_koltso_flats = new_koltso_flats.substr(0, new_koltso_flats.length - 1);
			
			setCookie("koltso_flats", new_koltso_flats, expires, '/', 'koltso.ru', 0);
		}
	}
}


function check_flat_new(inp, id) {

	koltso_flats = getCookie("koltso_flats_new");
	now = new Date();

	expires = new Date(now.getFullYear(), now.getMonth() + 3, now.getDay());

	if (inp.checked) {

		if(koltso_flats == null || koltso_flats == '') {
			setCookie("koltso_flats_new", id, expires, '/', 'koltso.ru', 0);
		} else {
			koltso_flats += ',' + id;
			setCookie("koltso_flats_new", koltso_flats, expires, '/', 'koltso.ru', 0);
		}

	} else {
		if(koltso_flats != null) {
			array_koltso_flats = koltso_flats.split(',');
			new_koltso_flats = '';
			for (i = 0; i < array_koltso_flats.length; i++){
				if(array_koltso_flats[i] != id) new_koltso_flats += array_koltso_flats[i] + ',';
			}
			new_koltso_flats = new_koltso_flats.substr(0, new_koltso_flats.length - 1);
			
			setCookie("koltso_flats_new", new_koltso_flats, expires, '/', 'koltso.ru', 0);
		}
	}
}



function check_zhk(inp, id) {

	koltso_flats = getCookie("koltso_zhks");
	now = new Date();

	expires = new Date(now.getFullYear(), now.getMonth() + 3, now.getDay());

	if (inp.checked) {

		if(koltso_flats == null || koltso_flats == '') {
			setCookie("koltso_zhks", id, expires, '/', 'koltso.ru', 0);
		} else {
			koltso_flats += ',' + id;
			setCookie("koltso_zhks", koltso_flats, expires, '/', 'koltso.ru', 0);
		}

	} else {
		if(koltso_flats != null) {
			array_koltso_flats = koltso_flats.split(',');
			new_koltso_flats = '';
			for (i = 0; i < array_koltso_flats.length; i++){
				if(array_koltso_flats[i] != id) new_koltso_flats += array_koltso_flats[i] + ',';
			}
			new_koltso_flats = new_koltso_flats.substr(0, new_koltso_flats.length - 1);
			
			setCookie("koltso_zhks", new_koltso_flats, expires, '/', 'koltso.ru', 0);
		}
	}
}



function check_commercial_object(inp, deal, id) {

	if(deal == 'rent') cookie_name = "koltso_rent_commercial_objects";
	else cookie_name = "koltso_sale_commercial_objects";
	
	koltso_commercial_objects = getCookie(cookie_name);
	now = new Date();

	expires = new Date(now.getFullYear(), now.getMonth() + 3, now.getDay());

	if (inp.checked) {

		if(koltso_commercial_objects == null || koltso_commercial_objects == '') {
			setCookie(cookie_name, id, expires, '/', 'koltso.ru', 0);
		} else {
			koltso_commercial_objects += ',' + id;
			setCookie(cookie_name, koltso_commercial_objects, expires, '/', 'koltso.ru', 0);
		}

	} else {
		if(koltso_commercial_objects != null) {
			array_koltso_commercial_objects = koltso_commercial_objects.split(',');
			new_koltso_commercial_objects = '';
			for (i = 0; i < array_koltso_commercial_objects.length; i++){
				if(array_koltso_commercial_objects[i] != id) new_koltso_commercial_objects += array_koltso_commercial_objects[i] + ',';
			}
			new_koltso_commercial_objects = new_koltso_commercial_objects.substr(0, new_koltso_commercial_objects.length - 1);
			
			setCookie(cookie_name, new_koltso_commercial_objects, expires, '/', 'koltso.ru', 0);
		}
	}
}

function check_land(inp, id) {

	koltso_lands = getCookie("koltso_lands");
	now = new Date();

	expires = new Date(now.getFullYear(), now.getMonth() + 3, now.getDay());

	if (inp.checked) {

		if(koltso_lands == null || koltso_lands == '') {
			setCookie("koltso_lands", id, expires, '/', 'koltso.ru', 0);
		} else {
			koltso_lands += ',' + id;
			setCookie("koltso_lands", koltso_lands, expires, '/', 'koltso.ru', 0);
		}

	} else {
		
		if(koltso_lands != null) {
			
			array_koltso_lands = koltso_lands.split(',');
			
			new_koltso_lands = '';
			
			for (i = 0; i < array_koltso_lands.length; i++){
				if(array_koltso_lands[i] != id) new_koltso_lands += array_koltso_lands[i] + ',';
			}
			
			new_koltso_lands = new_koltso_lands.substr(0, new_koltso_lands.length - 1);
			
			setCookie("koltso_lands", new_koltso_lands, expires, '/', 'koltso.ru', 0);
			
		}
	}
}

function check_cottage(inp, id) {

	koltso_cottages = getCookie("koltso_cottages");
	now = new Date();

	expires = new Date(now.getFullYear(), now.getMonth() + 3, now.getDay());

	if (inp.checked) {

		if(koltso_cottages == null || koltso_cottages == '') {
			setCookie("koltso_cottages", id, expires, '/', 'koltso.ru', 0);
		} else {
			koltso_cottages += ',' + id;
			setCookie("koltso_cottages", koltso_cottages, expires, '/', 'koltso.ru', 0);
		}

	} else {
		
		if(koltso_cottages != null) {
			
			array_koltso_cottages = koltso_cottages.split(',');
			
			new_koltso_cottages = '';
			
			for (i = 0; i < array_koltso_cottages.length; i++){
				if(array_koltso_cottages[i] != id) new_koltso_cottages += array_koltso_cottages[i] + ',';
			}
			
			new_koltso_cottages = new_koltso_cottages.substr(0, new_koltso_cottages.length - 1);
			
			setCookie("koltso_cottages", new_koltso_cottages, expires, '/', 'koltso.ru', 0);
			
		}
	}
}

