/* Created by Cap Gemini / Finn Rasmussen */

// Used together with the Guide Dk Map
var ok     = false;
var uuids  = new Array();
var images = new Array();
var selectedImage = 0;
var selectedRegionIndex = 0;
var selectedCityIndex = 0;
   
// Check browserversion
function checkBrowser(){
 this.ver = navigator.appVersion;
 this.dom = document.getElementById?1:0;
 this.ie5 = (this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
 this.ie4 = (document.all && !this.dom)?1:0;
 this.ie6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? 1 : 0;
 this.ns5 = (this.dom && parseInt(this.ver) >= 5) ?1:0;
 this.ns4 = (document.layers && !this.dom)?1:0;
 this.bw  = (this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns5);
 this.ie  = (this.ie6 || this.ie4 || this.ie5 || this.ns5);
 return this;
}
var browserIs = new checkBrowser();

// TODO
// Get rid of document.layers[0]... urghhh....

function updateImage(theForm, theMap) {
 if (ok) { 
    selectedImage = document.getElementById(theForm).selectedIndex;
    if (browserIs.ie) {
      eval('document.'+theMap+'.src = images['+selectedImage+'].src');
    } else {
      if (browserIs.ns4) {
        eval('document.layers[0].document.'+theMap+'.src = images['+selectedImage+'].src');
      } else {
        document.getElementById(theMap).src=images[selectedImage].src
      }
    }
    updateCityNames(selectedImage, theForm.name); // Assume that 'selectedImage' match the regionUuids[i]
  } else {
    alert( 'Images are Not fully loaded, try again' );
  }
}

function clickImage(i, theForm) {
  
  if (browserIs.ie) {
    document.getElementById(theForm).selectedIndex = i;
    //eval('document.'+theForm+'.region.selectedIndex='+i);
  } else {
    if (browserIs.ns4) {
      eval('document.layers[0].document.'+theForm+'.region.selectedIndex='+i);
    } else {
      document.getElementById(theForm).selectedIndex = i;
	  // Unknown browser, ignore
    }
  }
  selectedImage = i;
  updateCityNames(selectedImage, theForm); // if defined in clubdkmap
}

// Reset all
function onReset(i, theMap, theForm) {
  selectedImage = i;
  swapImage(selectedImage, theMap);
  updateCityNames(i, theForm.name); // if defined in clubdkmap
}

function onLoadReset(i, theMap, theForm) {
  selectedImage = i;
  swapImage(selectedImage, theMap);
  
  var myForm = null;
  if (browserIs.ie) {
    myForm = document.forms[theForm.name];
  } else {
    if (browserIs.ns4) {
      myForm = document.layers[0].document.forms[theForm.name];
    } else {
      // Unknown browser
    }
  }
  if (myForm == null || typeof myForm == 'undefined' || typeof myForm.cityId == 'undefined') {
    return false;
  } // else, valid cityId

   var region = parseInt(i)-1; // Start from regionId = 1, RegionId=0 is reserved for the default map
   var myCityId = '';
   var citselIndex = theForm.cityId.selectedIndex;
   var theSelectedCityIndex = 0;
   myForm.cityId.options.length = 0; // Remove all cities
   myForm.cityId.options[0] = new Option(selectCityText, '');  // re-populate first element in the dropdownbox
   // Lookup the new cities, matching the regionId
   if (i != '' && citiesPerRegion[region] != null) {
	  if(typeof selectedCityIndex !="undefined"){
	    if(citselIndex==0){
	      theSelectedCityIndex = selectedCityIndex;
		}else{
	      theSelectedCityIndex = citselIndex;
		}
	  }
      for (var cityid = 0; cityid < citiesPerRegion[region].length; cityid++) {
         myCityId = citiesPerRegion[region][cityid]; // For this regionId, lookup the cityId in question
         var newCity = new Option(cityNames[myCityId], cityUuids[myCityId] ); // Prepare the option element
         myForm.cityId.options[cityid+1] = newCity; // re-populate the dropdownbox, remember already added elelement 0
      }
   } else { // Update all cities
      // Lookup all cities, because a reset situation has occured
      for (var cityid=0;cityid <cityNames.length;cityid++) {
         var newCity = new Option(cityNames[cityid], cityUuids[cityid] );
         myForm.cityId.options[cityid+1] = newCity; // re-populate the dropdownbox, remember already added elelement 0
      }
   }
   myForm.cityId.options[theSelectedCityIndex].selected = true;
  
}

function onSubmit(theForm) {
  return true;
}

function swapImage(i, theMap) {
  if (browserIs.ie) {
    eval('document.'+theMap+'.src=images['+i+'].src');
  } else {
    if (browserIs.ns4) {
      eval('document.layers[0].document.'+theMap+'.src=images['+i+'].src');
    } else {
      document.getElementById(theMap).src=images[i].src
	  // Unknown browser, ignore
    }
  }
}

function overImage(i, theMap) {
  if (ok) {
    if (i > 0 ) { // MouseOver event
      swapImage(i, theMap);
    } else { // MouseOut event
      if (selectedImage > 0) {
        swapImage(selectedImage, theMap);
      } else {
        swapImage(i, theMap); // Skip, no image selected yet
      }
    }
  } else {
    // Just ignore
  }
}

// *****************************************************************************************
// Description: 
// The function will clear all cities in the drop down box, named 'cityId' and
// will re-populate the dropdown with new city enties, depending on the regionId selected.
// If regionId is empty or not in the idRegionCity Array, then re-populate with all cities
//
// Parameters: 
//    regionId : The region number(1-11), which have been selected by click or by change or reset
//               regionId = 0 is reserved for the default map index
//    theForm  : The name of the form object
// *****************************************************************************************
function updateCityNames(regionId, theForm) {
  var myForm = null;
  if (browserIs.ie) {
    myForm = document.forms[theForm];
  } else {
    if (browserIs.ns4) {
      myForm = document.layers[0].document.forms[theForm];
    } else {
      // Unknown browser
    }
  }
  if (myForm == null || typeof myForm == 'undefined' || typeof myForm.cityId == 'undefined') {
    return false;
  } // else, valid cityId

   var region = parseInt(regionId)-1; // Start from regionId = 1, RegionId=0 is reserved for the default map
   var myCityId = '';
   var theSelectedCityIndex = 0;
   myForm.cityId.options.length = 0; // Remove all cities
   myForm.cityId.options[0] = new Option(selectCityText, '');  // re-populate first element in the dropdownbox
   // Lookup the new cities, matching the regionId
   if (regionId != '' && citiesPerRegion[region] != null) {
      for (var cityid = 0; cityid < citiesPerRegion[region].length; cityid++) {
         myCityId = citiesPerRegion[region][cityid]; // For this regionId, lookup the cityId in question
		 if(typeof selectedCityIndex !="undefined" && (myCityId+1)==selectedCityIndex){
		   theSelectedCityIndex = cityid+1;
		 }
         var newCity = new Option(cityNames[myCityId], cityUuids[myCityId] ); // Prepare the option element
         myForm.cityId.options[cityid+1] = newCity; // re-populate the dropdownbox, remember already added elelement 0
      }
   } else { // Update all cities
      // Lookup all cities, because a reset situation has occured
      for (var cityid=0;cityid <cityNames.length;cityid++) {
         var newCity = new Option(cityNames[cityid], cityUuids[cityid] );
         myForm.cityId.options[cityid+1] = newCity; // re-populate the dropdownbox, remember already added elelement 0
      }
   }
   myForm.cityId.options[theSelectedCityIndex].selected = true;
}

if (document.images)  {
  images[0] = new Image(); images[0].src = "App_Themes/visitdenmark/images/map/guidedk_kort.gif"; uuids[0] = "";
  images[1] = new Image(); images[1].src = "App_Themes/visitdenmark/images/map/guidedk_kortbor.gif"; uuids[1] = "1:74:91f7baf8ec2ddf279271d";
  images[2] = new Image(); images[2].src = "App_Themes/visitdenmark/images/map/guidedk_kortkbh.gif"; uuids[2] = "1:74:91f7baf8ec2ddf1c28146";
  images[3] = new Image(); images[3].src = "App_Themes/visitdenmark/images/map/guidedk_kortsj.gif"; uuids[3] = "1:74:91f7baf8ec2ddf281f75a";
  images[4] = new Image(); images[4].src = "App_Themes/visitdenmark/images/map/guidedk_kortfyn.gif"; uuids[4] = "1:74:91f7baf8ec2ddf2243472";
  images[5] = new Image(); images[5].src = "App_Themes/visitdenmark/images/map/guidedk_kortnju.gif"; uuids[5] = "1:74:91f7baf8ec2ddf257f5f6";
  images[6] = new Image(); images[6].src = "App_Themes/visitdenmark/images/map/guidedk_kortwju.gif"; uuids[6] = "1:74:91f7baf8ec2ddf1ea928a";
  images[7] = new Image(); images[7].src = "App_Themes/visitdenmark/images/map/guidedk_korteju.gif"; uuids[7] = "1:74:91f7baf8ec2ddf199823";
  images[8] = new Image(); images[8].src = "App_Themes/visitdenmark/images/map/guidedk_kortsej.gif"; uuids[8] = "1:74:91f7baf8ec2ddf204f369";
  ok = true;
} else {
  ok = false;
}