$(document).ready(function() {
  var $paramPlzWk;
  var $page = "http://www.hr-online.de/website/specials/bundestagswahl2009/index.jsp";//hier evtl. absoluten Pfad eintragen!?
  var $plzFoundWk = false;
  $('#plzmessagewk').empty();
  $('#plzbuttonwk').click(function() {
    $paramPlzWk = document.PLZ_Suche_Wahlkreise.plzwk.value;
    if(checkInputWk($paramPlzWk)){
    $.get('xml/plz_wahlkreise.xml', function(data) {
    //$.get('/sd/plz_wahlkreise.xml', function(data) { 
      $(data).find('row').each(function() {
        var $row = $(this);
        $row.find('plz').each(function() {;
          if($(this).text()==$paramPlzWk){
            var $urlWk = $(this).parent().find('urlwahlkreise').text();
            $plzFoundWk = true;
            if(isNaN($urlWk)){
              window.location.href = $page + '?rubrik=47836&key=' + $urlWk;
            }else{
              window.location.href = $page + '?rubrik=' + $urlWk;
            }
          }
        });    
      });
      if(!$plzFoundWk){
        $('#plzmessagewk').empty();
        $('#plzmessagewk').append('Diese Postleitzahl ist leider ung&uuml;ltig!');
      }
    });
    }else{
      $('#plzmessagewk').empty();
      $('#plzmessagewk').append('Bitte &uuml;berpr&uuml;fen Sie Ihre Eingabe.');
    }
  });
});

function checkInputWk(plz){
  if(isNaN(plz)||plz==""){
    return false;
  }else{
    return true;
  }
}

function doEventDispatch(id) {
  var evt = null,
      elm = null;

  elm = document.getElementById(id);

  if(document.createEvent) {
    evt = document.createEvent('MouseEvents');
  }
  if(elm && elm.dispatchEvent && evt && evt.initMouseEvent) { // Firefox
    evt.initMouseEvent(
      'click',
      true,     // Click events bubble
      true,     // and they can be cancelled
      document.defaultView,  // Use the default view
      1,        // Just a single click
      0,        // Don't bother with co-ordinates
      0,
      0,
      0,
      false,    // Don't apply any key modifiers
      false,
      false,
      false,
      0,        // 0 - left, 1 - middle, 2 - right
      null);    // Click events don't have any targets other than
                // the recipient of the click
    elm.dispatchEvent(evt);
  } else { // IE
	  elm.click();
  }
}

