﻿var httpRequest;
var objName = 'ctl00_MPBISSContent_txtHelysegnev';

function GetSettlement(zipObject)
{
    if (!RegularExpressionValidateControl(zipObject.id, '^[1-9]{1}[0-9]{3}$', true)
        || !RequiredFieldValidateControl(zipObject.id, zipObject.title)) return;
    blockValidatorsOfControl(document.getElementById(objName));

    if(window.ActiveXObject)
    {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)
    {
        httpRequest = new XMLHttpRequest();
    }

    httpRequest.open("GET", 'gate.aspx?zipCode=' + zipObject.value + '&function=cities', true);
    httpRequest.onreadystatechange = function()
    {
        processCityRequest();
    };
    httpRequest.send(null);
}

function processCityRequest()
{
	if(httpRequest.readyState == 4)
	{
		if(httpRequest.status == 200)
		{
		    var objCity = document.getElementById(objName);
            citySelector(httpRequest.responseText, objName);
		}
	}
}

var citySelectorDnotclose = false;
var citySelectorOpen = false;

function citySelector( response, cityID )
{
	cityArray = response.parseJSON();
	
	if(cityArray.length > 1)
	{
	    var posOffset=0;
        try
	    {
	        if (document.getElementById('pageId').value == 'tab2')
	            posOffset = -5;
                //document.getElementById(cityID).parentNode.style.position = 'relative';
	    }
	    catch (e) {}

		ul =  document.createElement('ul');
		ul.style.width = document.getElementById(cityID).offsetWidth+"px";
		ul.style.listStyleType = "none";
		ul.style.zIndex = "100";
		ul.style.margin = "0px";
		ul.style.padding = "0px";
		ul.style.position = "absolute";
		ul.style.borderColor = '#D7EBD7';
		ul.style.borderStyle = 'solid';
		ul.style.borderWidth = '1px';
		ul.style.borderBottomWidth = '3px';		
		ul.style.backgroundColor = '#FFFFFF';

		ul.style.left = eval(document.getElementById(cityID).offsetLeft+posOffset)+"px";
		ul.style.top = eval(document.getElementById(cityID).offsetTop+document.getElementById(cityID).offsetHeight+1)+"px";
		ul.id = "cityRichSelector";
		
		ul.selected = 0;
		
		ul.selectNext = function()
		{
    		if( this.selected+1 < this.childNodes.length )
			{
				this.childNodes[this.selected].unselected();
				++this.selected;
				this.childNodes[this.selected].selected();
			}
		};
		ul.selectPrevious = function()
		{
			if( (this.selected-1) >= 0 )
			{
				this.childNodes[this.selected].unselected();
				--this.selected;
				this.childNodes[this.selected].selected();
			}
		};		
		ul.selectFirst = function()
		{
			this.childNodes[0].selected();
		};
		
		ul.selectSubmit =  function()
		{
			setCityValue(cityID, this.childNodes[this.selected].innerHTML);
			document.getElementById('cityRichSelector').parentNode.removeChild( document.getElementById('cityRichSelector') );
			citySelectorOpen = false;			
		};
		
		ul.onmouseover = function()
		{
			citySelectorDnotclose = true;
		};
		ul.onmouseout = function()
		{
			citySelectorDnotclose = false;
		};		
		
		for(var i = 0; i<cityArray.length; i++)
		{
			li =  document.createElement('li');
			li.style.padding = '1px';
			li.innerHTML = cityArray[i];
			li.style.color = '#004719';
			li.style.cursor = 'pointer';
			li.style.width = '100%';
			li.onmouseover = function(){  this.selected();  }
			li.onmouseout = function(){  this.unselected();  }
			
			li.selected = function(){ this.parentNode.childNodes[this.parentNode.selected].unselected(); this.style.backgroundColor = '#EEF8EE'; }
			li.unselected = function(){  this.style.backgroundColor = '#FFFFFF'; }
			
			li.onclick = function ()
			{
				setCityValue(cityID, this.innerHTML);
				document.getElementById('cityRichSelector').parentNode.removeChild( document.getElementById('cityRichSelector') );
				citySelectorOpen = false;
			}
			ul.appendChild(li);
		}

		if (document.getElementById(cityID).nextSibling)
			document.getElementById(cityID).parentNode.insertBefore(ul, document.getElementById(cityID).nextSibling);
		else
			document.getElementById(cityID).parentNode.appendChild(ul);

		document.getElementById('cityRichSelector').selectFirst();
	
		citySelectorOpen = true;
	
		document.getElementById(cityID).onblur = function()
		{
			if(!citySelectorDnotclose && document.getElementById('cityRichSelector')) document.getElementById('cityRichSelector').parentNode.removeChild( document.getElementById('cityRichSelector') );
			citySelectorOpen = false;
            unblockValidatorsOfControl(document.getElementById(cityID));
            executeValidatorsOfControl(document.getElementById(cityID));
		}

		document.getElementsByTagName('body')[0].onclick = function()
		{
			if(!citySelectorDnotclose && document.getElementById('cityRichSelector') && !citySelectorOpen) document.getElementById('cityRichSelector').parentNode.removeChild( document.getElementById('cityRichSelector') );
			citySelectorOpen = false;
            unblockValidatorsOfControl(document.getElementById(cityID));
            executeValidatorsOfControl(document.getElementById(cityID));			
		}

		if ( document.attachEvent )
		{
			document.getElementsByTagName('body')[0].onkeydown = function(e)
			{
			    if (!citySelectorOpen) return true;
				if(e == undefined ) e = event;
				
				// 40 le 38 fel 13 enter 32 space
				if( e.keyCode == 40 )
				{
					document.getElementById('cityRichSelector').selectNext();
				}
				else if ( e.keyCode == 38 )
				{
					document.getElementById('cityRichSelector').selectPrevious();
				}
				else if ( e.keyCode == 13 || e.keyCode == 32)
				{
					document.getElementById('cityRichSelector').selectSubmit();
					return false; // ne submitoljunk enter megnyomására
				}
			}
		}
	}
	else if ( cityArray.length == 1 )
	{
	    setCityValue(cityID, cityArray[0]);
	}
}

function setCityValue(cityID, value)
{
    var objCity=document.getElementById(cityID);
    objCity.value = value;
    unblockValidatorsOfControl(objCity);
    executeValidatorsOfControl(objCity);
}


//function allwaysUsedChanged(obj,id)
function isInhabitedChanged(obj,id)
{
var select2 = document.getElementById(id);
var select3 = document.getElementById('ctl00_MPBISSContent_ddlPackage');
//if (obj.value > 0) {
if (obj.value == 'true') {
    select2.disabled = true;
    select2.selectedIndex = 1;
    select3.options[0].disabled = false;
    select3.options[3].disabled = false;
} else {
    select2.disabled = false;
    select3.options[0].disabled = true;
    select3.options[3].disabled = true;
    select3.selectedIndex = 1;
    //select3.options[1].selected = true;
}

}
