/*
 * THESE FUNCTIONS ARE USED IN 
 *  - comwonen_WoningBezit -> zoekform.tpl
 *  - comwonen_BuurtInformatie -> zoekform.tpl
 *  - comwonen_ActueelAanbod -> zoekform.tpl
 */

var gsModuleType; //global sModuleType

function evalJSONResponse( transport ) {
	  try {
	    return eval('(' + transport.responseText + ')');
	  } catch (e) {}
}

function getMunicipalityData( iMunicipalityID, sModuleType )
{
	gsModuleType = sModuleType;
	var sUrl = '/output/json/?function=getMunicipalityData&municipality=' + iMunicipalityID + '&moduleType=' + gsModuleType;

	doHideSelectNeighbourhoods();

	oSelect = document.getElementById( 'select_quarter' );
	oSelect.disabled = true;
	
	if( iMunicipalityID == -1 )
	{
		oSelect.parentNode.style.display = 'none';
		doneLoading();
		return false;
	}

	
	new Ajax.Request(sUrl, {
        method: 'post',
        asynchronous:false,
		evalScripts:false,
        onSuccess: function( transport ) {
			oJson = evalJSONResponse( transport );

			//Empty selectbox
			emptySelect( oSelect );
			oSelect.parentNode.style.display = 'none';

			if( oJson.Quarters.length > 0 )
			{
				//Add 'Maak uw keuze' option
				if( gsModuleType == 'buurtInfo' )
				{
					var oOption = document.createElement( 'option' );
					oOption.innerHTML = 'Maak uw keuze';
					oOption.value = -1;
					oSelect.appendChild( oOption );
				}
				
				//Loop through all quarters and add them to the option list
				for( var i = 0; i < oJson.Quarters.length; i++ )
				{
					var oOption = document.createElement( 'option' );
					oOption.innerHTML = oJson.Quarters[i].naam;
					if( oOption.innerHTML.substring( 0, 11 ) == 'Alle wijken' )
					{
						oOption.innerHTML = 'Alle deelgemeentes' + oOption.innerHTML.substring( 11 )
					}
					oOption.value = oJson.Quarters[i].ID;
	
					oSelect.appendChild( oOption );
				}
				//Add onchange function to display neighbourhoods
				oSelect.onchange = function() { 
					loadLocation( 'getQuarterData', this.options[this.options.selectedIndex].value, gsModuleType ); 
				};
				oSelect.disabled = false;
				oSelect.parentNode.style.display = 'block';
			}
		},
        onFailure: function(){
        	alert('Mislukt');
        }
	} );
	doneLoading();
}

function getQuarterData( iQuarterID, sModuleType )
{
	gsModuleType = sModuleType;
	doHideSelectNeighbourhoods();
	
	var sUrl = '/output/json/?function=getQuarterData&quarter=' + iQuarterID + '&moduleType=' + gsModuleType;

	oSelect = document.getElementById( 'select_neighbourhood' );
	oSelect.disabled = true;

	if( iQuarterID == -1 )
	{
		oSelect.parentNode.style.display = 'none';
		doneLoading();
		return false;
	}
	
	new Ajax.Request(sUrl, {
        method: 'post',
        asynchronous:false,
		evalScripts:false,
        onSuccess: function( transport ) {
			oJson = evalJSONResponse( transport );
		
			//Empty selectbox
			emptySelect( oSelect );
			oSelect.parentNode.style.display = 'none';
			
			if( oJson.Neighbourhoods.length )
			{
				//Add 'Maak uw keuze' option
				if( gsModuleType == 'buurtInfo' )
				{
					var oOption = document.createElement( 'option' );
					oOption.innerHTML = 'Maak uw keuze';
					oOption.value = -1;
					oSelect.appendChild( oOption );
				}
				
				//Loop through all neighbourhoods and add them to the option list
				for( var i = 0; i < oJson.Neighbourhoods.length; i++ )
				{
					var oOption = document.createElement( 'option' );
					oOption.innerHTML = oJson.Neighbourhoods[i].naam;
					if( oOption.innerHTML.substring( 0, 12 ) == 'Alle buurten' )
					{
						oOption.innerHTML = 'Alle wijken' + oOption.innerHTML.substring( 12 )
					}
					oOption.value = oJson.Neighbourhoods[i].ID;
	
					oSelect.appendChild( oOption );
				}
				//Add onchange function to display neighbourhoods
				oSelect.onchange = function() { 
					loadLocation( 'getNeighbourhoodData', this.options[this.options.selectedIndex].value, gsModuleType ); 
				};
				oSelect.disabled = false;
				oSelect.parentNode.style.display = 'block';
			}
		},
	    onFailure: function(){
	    	alert('Mislukt');
	    }
	} );
	doneLoading();
}

function getNeighbourhoodData( iNeighbourhoodID, sModuleType )
{
	gsModuleType = sModuleType;
	var sUrl = '/output/json/?function=getNeighbourhoodData&neighbourhood=' + iNeighbourhoodID + '&moduleType=' + gsModuleType;

	doShowHideSubmit();
	
	new Ajax.Request(sUrl, {
        method: 'post',
        asynchronous:false,
		evalScripts:false,
        onSuccess: function( transport ) {
			oJson = evalJSONResponse( transport );
		
			var oSelect = document.getElementById( 'select_neighbourhood' );

			doShowHideSubmit();
		},
	    onFailure: function(){
	    	alert('Mislukt');
	    }
	} );
	doneLoading();
}

function doHideSelectNeighbourhoods()
{
	//Hide 3rd select box if visible
	emptySelect( document.getElementById( 'select_neighbourhood' ) );
	document.getElementById( 'select_neighbourhood' ).parentNode.style.display = 'none'
	doHideSubmit();
}

function doHideSubmit()
{
	//Only in case of 'buurtInfo' the button can be hidden
	if( gsModuleType == 'buurtInfo' )
	{
		var oSubmit = document.getElementById( 'submit_row' );
		oSubmit.style.display = 'none';
	}
}

function doShowHideSubmit()
{
	//Only in case of 'buurtInfo' the button can be hidden
	if( gsModuleType == 'buurtInfo' )
	{
		var oSelectNeighbourhood = document.getElementById( 'select_neighbourhood' );
		var iNeighbourhoodID = oSelectNeighbourhood.options[oSelectNeighbourhood.options.selectedIndex].value
		var oSubmit = document.getElementById( 'submit_row' );

		if( oSelectNeighbourhood.parentNode.style.display == 'block' && iNeighbourhoodID > '-1'  )
		{
			oSubmit.style.display = 'block';
		}
		else
		{
			oSubmit.style.display = 'none';
		}
	}
}

function loadLocation( loadFunction, location, module )
{
	setTimeout ( 'showload()', 100 );
	setTimeout ( loadFunction + '( '+location+', "'+module+'" )', 400 );
}

function showload()
{
	document.getElementById('woningbezit_loading').style.display = 'block';
	document.getElementById('map24div').style.visibility = 'hidden';
}


function doneLoading()
{
	document.getElementById('map24div').style.visibility = 'visible';
	document.getElementById('woningbezit_loading').style.display = 'none';
}