	
function fillCities()
{
	$("#loading").css('visibility','visible');
	
//	var domain=window.location.href.split(/\/+/g)[1];
//	var  = 'http://' + domain + '/';

	var ajaxUrl = SITE_URL + '?page=ajax';
	var regionIds = new Array;
	$("#regions :selected").each(function(i, selected){
		regionIds.push($(selected).val());
	});

	$.post(ajaxUrl, {"regionIds[]": regionIds}, function(response) {
		$("#loading").css('visibility','hidden')
		$("#cities").html(response);
	});
}

function checkPrice()
{
	var selected = $("#type").val();
	if (selected == "targs") {
		var priceFrom = $("#priceFrom").val();
		var priceTo = $("#priceTo").val();
		if (priceFrom == "") {
			priceFrom = 0;
		}
		if (priceTo == "") {
			priceTo = 0;
		} 
		priceFrom = parseInt(priceFrom, 10);
		priceTo = parseInt(priceTo, 10);
		if ( (priceFrom != "" && priceFrom != null && isNaN(priceFrom)) ||
			(priceTo != "" && priceTo != null && isNaN(priceTo)) ) {
			alert("Цената трябва да е число!");
			return false;
		} else if ( (priceFrom != "" && priceFrom != null && !isNaN(priceFrom)) &&
					(priceTo != "" && priceTo != null && !isNaN(priceTo)) &&
					(priceFrom > priceTo)) {
			alert("Крайната цена трябва да е по-голяма от началната!");
			return false;
		}
	}
	return true;
}

function switchType()
{
	var selected = $("#type").val();
	if (selected == "targs") {
		$("#targs").show();
		$("#publications").hide();
	} else {
		$("#targs").hide();
		$("#publications").show();
	}
}

function customDateParse(string)
{
	var date = string.match(/(\d+)/g);
	var dateString = date[1]+"/"+date[0]+"/"+date[2];
	return new Date(dateString);
}

function getTargDates(cal)
{
	var dateFromField = document.getElementById("targDateFrom");
	var dateToField = document.getElementById("targDateTo");
	var doClose;
	doClose = compareDates(dateFromField, dateToField, cal);
	if (doClose){ 
		calendar.hide(); 
	}
}

function getPublicationDates(cal)
{
	var dateFromField = document.getElementById("publicationDateFrom");
	var dateToField = document.getElementById("publicationDateTo");
	var doClose;
	doClose = compareDates(dateFromField, dateToField, cal);
	if (doClose){ 
		calendar.hide(); 
	}
}

function compareDates(dateFromField, dateToField, cal)
{
	if (dateFromField.value != "" && dateFromField.value != null && dateToField.value != "" && dateToField.value != null){
		var dateFrom = customDateParse(dateFromField.value);
		var dateTo = customDateParse(dateToField.value);
		
		if (dateFrom > dateTo) {
			alert("Крайната дата трябва да е по-голяма от началната!");

			var field = dateFromField;
	        if (field == cal.params.inputField) {
	        	time = dateTo.getTime() - Date.DAY;
	        } else {
	        	field = dateToField;
	        	time = dateFrom.getTime() + Date.DAY;
	        }

	        var date = new Date(time);
			field.value = date.print("%d.%m.%Y");
	        //field.value = "";
			return false
		}
	}
	return true
}

function CalendarsInit()
{
	Calendar.setup({
	    inputField      :    "targDateFrom", // id of the input field
	    ifFormat        :    "%d.%m.%Y", // format of the input field, , default %Y-%m-%d %H:%M
		firstDay		:    1, //default 0 (Sunday)
	    showsTime       :    false, //default true
	    timeFormat      :    "24",
	    onClose        :    getTargDates
	});

	Calendar.setup({
	    inputField      :    "targDateTo", // id of the input field
	    ifFormat        :    "%d.%m.%Y", // format of the input field, default %Y-%m-%d %H:%M
		firstDay		:    1, //default 0 (Sunday)
	    showsTime       :    false, //default true
	    timeFormat      :    "24",
	    onClose        :    getTargDates
	});
	
		Calendar.setup({
	    inputField      :    "publicationDateFrom", // id of the input field
	    ifFormat        :    "%d.%m.%Y", // format of the input field, , default %Y-%m-%d %H:%M
		firstDay		:    1, //default 0 (Sunday)
	    showsTime       :    false, //default true
	    timeFormat      :    "24",
	    onClose        :    getPublicationDates
	});

	Calendar.setup({
	    inputField      :    "publicationDateTo", // id of the input field
	    ifFormat        :    "%d.%m.%Y", // format of the input field, default %Y-%m-%d %H:%M
		firstDay		:    1, //default 0 (Sunday)
	    showsTime       :    false, //default true
	    timeFormat      :    "24",
	    onClose         :    getPublicationDates
	});
}
