﻿// This file provides standard functions for retrieving entity details

var _CountryDialog;
// gets the required data and activates the resultsContainer
function ShowCountryPopupDialog(countryId, floatingDivTitle)
{
  
  if(!_CountryDialog)
  {
    _CountryDialog = createDialogMap(floatingDivTitle);
  }
 
  // set the container to the 'loading' state
  _CountryDialog.className = 'loading dialogMap';
  
  // call the server generated callback wrapper
  GetCountryPopup(countryId, _CountryDialog);
  
    var theTop;

  // get the body element
  if (document.documentElement && document.documentElement.scrollTop)
  {
	  theTop = document.documentElement.scrollTop;
  }
  else if (document.body)
  {
	  theTop = document.body.scrollTop
  }
   
  // scroll to the top of the page  
  _CountryDialog.style.top = theTop + 150 + "px";

  hideSelectMap();
}

function hideSelectMap()
{
  // get the body element
  if (document.documentElement && document.documentElement.scrollTop)
  {
    document.documentElement.className += ' hideSelect';
  }
  else if (document.body)
  {
    document.body.className += ' hideSelect';
  }
}

function showSelect()
{
  // get the body element
  if (document.documentElement && document.documentElement.scrollTop)
  {
    document.documentElement.className = document.documentElement.className.replace('hideSelect', '');
  }
  else if (document.body)
  {
    document.body.className = document.body.className.replace('hideSelect', '');
  }
}

function createDialogMap(floatingDivTitle)
{
  var dialogElement = document.createElement('div');
  dialogElement.id = 'dialogCountryPopup';
  dialogElement.className = 'off dialogMap';
  dialogElement.innerHTML = "<div class='dialogTitle' onmousedown='javascript:grab(this.parentNode);'><span><a onclick='this.parentNode.parentNode.parentNode.className=\"off\"; findContentDivMap(this.parentNode.parentNode.parentNode, \"dialogContentCountry\").innerHTML = \"\";showSelect();return false;' href='#' >sluit venster</a></span>"+ floatingDivTitle + "<br class='clearBoth'/></div><div class='errorContent' ></div><div class='loadingContent'>De gegevens worden opgehaald...</div><div class='dialogContentCountry' ></div>";
  document.body.appendChild(dialogElement);
  
  return dialogElement;
}

// this function is called when a callback returns
function HandleCountryPopupResponse(result, context)
{
  // context should be a dialog element
  var dialogContentCountry = findContentDivMap(context, 'dialogContentCountry');
  
  if(dialogContentCountry != null)
  {
		// Variables
		var resultEl;
		
		// Create an element and insert the result into it.
		resultEl = document.createElement('div');
		resultEl.innerHTML = result;
			
		// Check if the new element has children after the insert.
		if(resultEl.hasChildNodes())
		{
			// If the last child has an id that equals 'AccommodationOverviewMapDiv'
			// don't add that child to the dialog content just yet.
			var count = resultEl.childNodes.length -1;
			if(resultEl.lastChild.id == 'AccommodationOverviewMapDiv') {
				for( var i = 0; i < count; i++ )
				{
					dialogContentCountry.appendChild(resultEl.childNodes[0]);
				}
			}
			else
			{
				var count = resultEl.childNodes.length;
				// Add all the children to the content.
				for( var i = 0; i < count; i++ )
				{
					dialogContentCountry.appendChild(resultEl.childNodes[0]);
				}
			}
		}
  }
  SetDialogMapToOn();
 
}

function SetDialogMapToOn()
{
  if(!_CountryDialog)
  {
    _CountryDialog = document.getElementById('dialogCountryPopup');
  }
  _CountryDialog.className = 'on dialogMap';
  
  initTabs('tabsetDialog');
}

// this function is called when the callback raises
// an error
function HandleCountryPopupErrorResponse(result, context)
{
  // context should be a dialog element
  var dialogContentCountry = findContentDivMap(context, 'errorContent');
  if(dialogContentCountry)
  {
     dialogContentCountry.innerHTML = result;
  }
  
  // set the dialog state to 'on'
  context.className = 'error';
}

function findContentDivMap(dialog, className)
{
	// find the first div in a div (first tab in data div)
	var contentDiv = dialog.getElementsByTagName('DIV')[1];
	while(contentDiv != null)
	{
		if(contentDiv.className.indexOf(className) > -1)
		{
			return contentDiv;
		}
		else
		{
			contentDiv = contentDiv.nextSibling;
		}
	}
	return null;
}

