﻿function BindDistrict(e, collection) {
	$.ajax({
		type: "POST",
		url: "/WebServices/CascadingDropDownList.asmx/DistrictGetList",
		contentType: "application/json; charset=utf-8",
		data: '{"regionNameId":"' + collection.parent.value + '"}',
		dataType: "json",
		success: function(dataObj) {
		Bind(collection.current, dataObj.d, collection.selected, collection.isOnLoad, collection.defaultText);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{ alert(errorThrown); }
	});
}

function BindDistrictFilter(e, collection) {
	$.ajax({
		type: "POST",
		url: "/WebServices/CascadingDropDownList.asmx/DistrictGetList",
		contentType: "application/json; charset=utf-8",
		data: '{"regionNameId":"' + collection.parent.value + '"}',
		dataType: "json",
		success: function(dataObj) {
			var dList = $('input[name="districtIds"]');
			var sdList = $('input[name="subdistrictIds"]');
			Bind(collection.current, dataObj.d, collection.selected, collection.isOnLoad, collection.defaultText);
			if (collection.parent.value == "kiev") {
				collection.current.style.display = "none";
				$('div[id="chooseDistrict"]').show();
			}
			else {
				collection.current.style.display = "block";
				dList[0].value = sdList[0].value = "";
				$('div[id="chooseDistrict"]').hide();
			}
			$('a[class="chooseDistrict"]')[0].innerHTML = GetCaptionForDistrictFilter(dList[0].value);

		},
		error: function(XMLHttpRequest, textStatus, errorThrown)
		{ alert(errorThrown); }
	});
}


function BindDistrictByRegionId(e, collection)
{
    $.ajax({
        type: "POST",
        url: "/WebServices/CascadingDropDownList.asmx/DistrictGetListByRegioId",
        contentType: "application/json; charset=utf-8",
        data: '{"regionId":"' + collection.parent.value + '"}',
        dataType: "json",
        success: function(dataObj)
        {
        	Bind(collection.current, dataObj.d, collection.selected, collection.isOnLoad, collection.defaultText);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        { alert(errorThrown); }
    });
}

function BindSubDistrict(e, collection)
{
	var selected  = collection.parent.value == '' ? collection.parentValue : collection.parent.value;
    $.ajax({
        type: "POST",
        url: "/WebServices/CascadingDropDownList.asmx/SubDistrictGetList",
        contentType: "application/json; charset=utf-8",
        data: '{"districtId":"' + selected + '"}',
        dataType: "json",
        success: function(dataObj)
        {
        	Bind(collection.current, dataObj.d, collection.selected, collection.isOnLoad, collection.defaultText);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            //alert(errorThrown); 
        }
    });
}

function BindStreet(e, collection)
{
	var selected = collection.parent.value == '' ? collection.parentValue : collection.parent.value;
	$.ajax({
		type: "POST",
		url: "/WebServices/CascadingDropDownList.asmx/StreetGetList",
		contentType: "application/json; charset=utf-8",
		data: '{"districtId":"' + selected + '"}',
		dataType: "json",
		success: function(dataObj) {
			var data = Bind(collection.current, dataObj.d, collection.selected, collection.isOnLoad, collection.defaultText);
			AutoComplete("RealtyEditLocation_StreetName", data, collection.current, collection.isOnLoad, collection.parent.value);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			//alert(textStatus);            
		}
	});
}
function BindBuildingNumber(e, collection) {
	var selected = collection.parent.value == '' ? collection.parentValue : collection.parent.value;
	$.ajax({
		type: "POST",
		url: "/WebServices/CascadingDropDownList.asmx/BuildingNumberGetList",
		contentType: "application/json; charset=utf-8",
		data: '{"streetId":"' + selected + '"}',
		dataType: "json",
		success: function(dataObj) {
		Bind(collection.current, dataObj.d, collection.selected, collection.isOnLoad, collection.defaultText);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			//alert(textStatus);            
		}
	});
}
function BindBuildingMaterials(e, collection)
{

    $.ajax({
        type: "POST",
        url: "/WebServices/CascadingDropDownList.asmx/BuildingMaterialGetList",
        contentType: "application/json; charset=utf-8",
        data: '{"buildingTypeId":"' + collection.parent.value + '"}',
        dataType: "json",
        success: function(dataObj)
        {
        	Bind(collection.current, dataObj.d, collection.selected, collection.isOnLoad, collection.defaultText);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert(errorThrown); 
        }
    });
}

function Bind(districtList, districtValues, selectedValue, isOnLoad, defaultText)
{

	var arr = new Array();
	districtList.options.length = 0;
	newOption = new Option(defaultText, "0");
	districtList.options.add(newOption);
	districtList.options[0].selected = true;
	for (var i = 1; i <= districtValues.length; i++) 
	{
		newOption = new Option(districtValues[i - 1].Name, districtValues[i - 1].Id);
		arr[i - 1] = districtValues[i - 1].Name;
		districtList.options.add(newOption);
		if (districtList.options[i].value == selectedValue) {
			districtList.options[i].selected = true;
		}

	}
	if (!isOnLoad || (selectedValue == "" || selectedValue == 0)) {
		DoListClick(districtList);
	}
	return arr;

}

function DoListClick(list) {


	if (list.dispatchEvent != undefined) {
		var mine = document.createEvent("MouseEvents");
		mine.initEvent("change", true, true);
		list.dispatchEvent(mine);
	}
	else {
		var evt = document.createEventObject();
		list.fireEvent('onchange', evt);
	}
		
}
function GetSelectedIndex(list, text) {
	for (var i = 0; i < list.length; i++) {
		if (list[i].text == text)
			return i;
	}
	return 0;
}
function AutoComplete(inputId, data, hiddenList, isOnLoad, parentId) {

	//var transaction = GetQueryParam("transaction", window.location);
	//var isSnimautPokupaut = (transaction.indexOf("snimajut") > -1) || (transaction.indexOf("pokupajut") > -1)
	//if (hiddenList.length < 2 && parentId == "0" && !isSnimautPokupaut) {
	if (hiddenList.length < 2 && parentId == "0") {
		$("#" + inputId).attr("disabled", "disabled");
		$("#" + inputId).attr("value", "");
		return;
	}
	$("#" + inputId).removeAttr('disabled');
	if (isOnLoad == "true") { 
		if (hiddenList.value != 0)
			$("#" + inputId).attr("value", hiddenList[hiddenList.selectedIndex].text);
	}
	else
		$("#" + inputId).attr("value", "");
	$("#" + inputId).autocomplete(data, {
		minChars: 1,
		max: 200
	});
	$("#" + inputId).result(function(event, data, formatted) {
	hiddenList.selectedIndex = GetSelectedIndex(hiddenList, formatted);
	DoListClick(hiddenList);
	});
	$("#" + inputId).blur(function() {
	hiddenList.selectedIndex = GetSelectedIndex(hiddenList, $("#" + inputId).val());
	DoListClick(hiddenList);
	});
}

	