// Create new XMLHttp object.
var XMLHttp = false;
            
// Valid for IE7 and all Firefox versions (and others).
try {
   XMLHttp = new XMLHttpRequest();
} catch ( tryMicrosoft ) {
   try {
      // Older then MS IE 7.
      XMLHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
   } catch ( otherMicrosoft ) {
      try {
         XMLHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
      } catch ( failed ) {
         XMLHttp = false;
      }
   }
}


function findRegions() {

   // Set the url.
   //XMLHttp.open("GET", "/search/ajaxGetRegions/sCountry/" + element.value);
	XMLHttp.open("GET", "/search/ajaxGetRegions/sCountry/" + document.getElementById('sCountry').value);

   // Onchange function.
   XMLHttp.onreadystatechange = function() {
 	
      // If the file is found.
      if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {

           // Retrieve the JSON object.
            var oRegions = eval('(' + XMLHttp.responseText + ')');

            // Retrieve regions dropdown.
            var eRegions = document.getElementById('sRegion');
            eRegions.options.length = 0;
            eRegions.options[0] = new Option('Alle regio\'s.', '-1');

            // Walk through all results and proces them.
            for (var i = 0; i < oRegions.length; i++) {

               // Add option to the regions select box.
               eRegions.options[i+1] = new Option(oRegions[i].name_filtered, oRegions[i].region_code);

            }

      // If the file was not found.
      } else {

         // Alert the user.
//         alert('File not found.');

      }

   }

   // Send the request.
   XMLHttp.send(null);

}


function findPlace(element) {

   // Set the url.
   XMLHttp.open("GET", "/search/ajaxGetPlacesJson/sRegion/" + element.value);

   // Onchange function.
   XMLHttp.onreadystatechange = function() {

      // If the file is found.
      if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {

            // Retrieve the JSON object.
            var oPlaces = eval('(' + XMLHttp.responseText + ')');

            // Retrieve regions dropdown.
            var ePlaces = document.getElementById('sPlace');
            ePlaces.options.length = 0;
            ePlaces.options[0] = new Option('Alle plaatsen.', '-1');

            // Walk through all results and proces them.
            for (var i = 0; i < oPlaces.length; i++) {

               // Add option to the regions select box.
               ePlaces.options[i+1] = new Option(oPlaces[i].place, oPlaces[i].place);

            }

      // If the file was not found.
      } else {

         // Alert the user.
//         alert('File not found.');

      }

   }

   // Send the request.
   XMLHttp.send(null);

}


function findDates() {

   // Set the url.
   XMLHttp.open("GET", "/search/ajaxGetDates/iMonth/" + document.getElementById('iMonth').value + "/iLength/" + document.getElementById('iLength').value);

   // Onchange function.
   XMLHttp.onreadystatechange = function() {

      // If the file is found.
      if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {

            // Retrieve the JSON object.
            var oDates = eval('(' + XMLHttp.responseText + ')');

            // Retrieve regions dropdown.
            var eDates = document.getElementById('sDate');
            eDates.options.length = 1;

            // Walk through all results and proces them.
            for (var i = 0; i < oDates.length; i++) {

               // Add option to the regions select box.
               eDates.options[i+1] = new Option(oDates[i].text.substr(0, 1).toUpperCase() + oDates[i].text.substr(1), oDates[i].date);

            }

      // If the file was not found.
      } else {

         // Alert the user.
//         alert('File not found.');

      }

   }

   // Send the request.
   XMLHttp.send(null);

}

function selectGroup() {

   if (document.getElementById('bGroup').checked) {
      if (document.getElementById('iPersons').value < 10) {
         document.getElementById('iPersons').value = 10;
      }
   }
}


/* Autocompleter for searching place name. */
function searchPlace(sKeyword) {

	// Retrieve places dropdown.
	var oDivPlaces = document.getElementById('divPlaceResults');

	// There must be at least two letters available.
	if (sKeyword.length > 1) {

		// Only search places if value is not numeric (else it probably will be an house code).
		if (!isNumeric(sKeyword)) {

			// Retrieve places from database through an ajax request (json response).
			XMLHttp.open("GET", "/search/ajax-get-places/sKeyword/" + escape(sKeyword));

			// Onchange function.
			XMLHttp.onreadystatechange = function() {

				// If the file is found.
				if (XMLHttp.readyState == 4 && XMLHttp.status == 200) {

						// Retrieve the JSON object.
						var oPlaces = eval('(' + XMLHttp.responseText + ')');

						// Clear list.
						oDivPlaces.innerHTML = "";

						// Empty html string.
						var sPlaces = "";

						// Walk through all results and proces them.
						for (var i = 0; i < oPlaces.length; i++) {

							// Add option to the places div.
							sPlaces = sPlaces + '<a href="/search/index/searchButton/1/sDate/-1/sPlace/' + oPlaces[i].place + '/">' + oPlaces[i].place + '</a>';

						}

						// Display places.
						oDivPlaces.innerHTML = sPlaces;

						// Display the div.
						oDivPlaces.style.display = "block";

				}

			}

			// Send the request.
			XMLHttp.send(null);

		} else {

			oDivPlaces.style.display = "none";

		}

	} else {

		oDivPlaces.style.display = "none";

	}

}



/* Check if a value is numeric. */
function isNumeric(sValue) {

	// Set regex.
	var sRegEx = /(^\d+$)/;

	// Check.
	if (sRegEx.test(sValue)) {
		return true;
	}

	// Return false by default.
	return false;

}