/*Andy Behrendt / Renke Kunst , object4.de - Germany*/
/*alle Rechte zur Nutzung der auf dieser Seite verwendeten Daten
und Grafiken liegen bei object4.de */
var baseUrl = "";
var defaultPic = "trans.gif";

function setBase(newUrl){
  baseUrl = newUrl;
}

function setDefaultPic(newPic){
  defaultPic = newPic;
}

function modifyMap() {
  var sel=document.getElementById("select_staff_country");
  if (sel==null)  return false;
  var si=sel.selectedIndex;
  if (sel.options[si].value=='') swapImage(-1);
  if (si<sel.options.length) {
    var hasChanged=false;
    for (i=0;i<countries.length;i++) {
      country=countries[i];
      if (country[0]==sel.options[si].text)   {
        swapImage(country[2]);
        hasChanged=true;
      }
    }
  }
}

function init(initCountry) {
  for (c=0;c<countries.length;c++) {
    var country=countries[c];
    if (initCountry==country[1]) activate(c);
  }
}
function activate(selCountry) {
  var country=countries[selCountry];
  if (country!=null) {
    swapImage(country[2]);
    builddropDown(country);
  }
  else{
  	swapImage(defaultPic)
  	};
}

function deactivate(selCountry) {
  var country=countries[selCountry];
  if (country!=null) {
    swapImage(defaultPic);
  }
}
function swapImage(imageName) {
  var cont=document.getElementById("swap_area");
  newImage = 'url('+baseUrl+imageName+')';
  if (cont!=null) {
    cont.style.backgroundImage = newImage;
  }
}

/* case-insensitive comparison of strings, respecting the sort order of the german umlauts */
function stringComparison(a, b)	{
	a = a.toLowerCase();
	a = a.replace(/ä/g,"a");
	a = a.replace(/ö/g,"o");
	a = a.replace(/ü/g,"u");
	a = a.replace(/ß/g,"s");

	b = b.toLowerCase();
	b = b.replace(/ä/g,"a");
	b = b.replace(/ö/g,"o");
	b = b.replace(/ü/g,"u");
	b = b.replace(/ß/g,"s");

	return(a==b)?0:(a>b)?1:-1;
}

function countryNameComparator(a, b) {
	var nameA = a[0];
	var nameB = b[0];
  return stringComparison(nameA, nameB);
}

function builddropDown(selCountry) {
  var sel=document.getElementById("select_staff_country");
  if(!sel) {
    return;
  }
  if(selCountry) {
    for(i = 0; i < sel.options.length; i++) {
      if(sel.options[i].value.indexOf(selCountry[1])!=-1) {
        sel.selectedIndex=i;
        return;
      }
    }
  } else {
    // CMSR299: Länder zuerst alphabetisch sortieren
    var sortedCountries = new Array();
    for (i = 0; i < countries.length; i++) {
    	sortedCountries[i] = countries[i];
    }
    sortedCountries.sort(countryNameComparator);
    
	  newo=new Option("Select a country ...","",false,false);
	  sel.options[0]=newo;
	  newo=new Option("_____________________","",false,false);
	  sel.options[1]=newo;     
    for(i = 0; i < sortedCountries.length; i++) {
      sel.options[i+2] = new Option(sortedCountries[i][0], sortedCountries[i][1], false, false);
    }
  }
}

function OLD_builddropDown(selCountry) {
  var pos=2,c=0;
  var sel=document.getElementById("select_staff_country");
  if (sel==null)  return false;
  for (c=1;c<sel.length;sel.options[c++]=null);
  newo=new Option("Select a country ...","",false,false);
  sel.options[0]=newo;
  newo=new Option("_____________________","",false,false);
  sel.options[1]=newo;
  for (c=0;c<countries.length;c++) {
    var country=countries[c];
    if (country!=null) {
      if (country==selCountry) {
        newo=new Option(country[0],''+country[1],true,true);
        sel.options[0]=newo;
      }
      else    {
        newo=new Option(country[0],''+country[1],false,false);
        sel.options[pos++]=newo;
      }
    }
  }
}

function checkFormAction() {
  var sel=document.getElementById("country_sel");
  var cf=document.getElementById("country_form");
  if (sel==null || cf==null) return false;
  for (var i=0;i<sel.length;i++) {
    opt=sel.options[i];
    if (opt.selected == true) cf.action=opt.value;
  }
}

function switchTo() {
  doFilter('Country','select_staff_country');
  document.forms.Country.submit();
}
