document.observe('dom:loaded', initId);

var globalId = '';
var globalBl = '';

/*
 *  inits of the standart id and country
 */
function initId(){
    /* init standart id and bl */
    var dd_head_id = $('dd_head_id');
    var dd_head_t = $('dd_head_t');
    var visible_container = $('visible_container');

    globalId = dd_head_id ? $('dd_head_id').innerHTML : '';
    globalBl = dd_head_t ? $('dd_head_t').innerHTML : '';

    if (visible_container) {
      visible_container.observe('mouseout', setBack);
    }
}

/*
 *  sets DropDown and Map back to default
 */
function setBack(){
    $('dd_head_t').innerHTML = globalBl;
    if(globalId != 0)
        $('ovl_'+globalId).show();
}

/*
 *  shows/hides th DropDown Box with countries
 */
function show_hide(){
    if($('ansp_container').style.display == 'none')
    {
        $('ansp_container').show();
        $('dd_head_arrow').setStyle({background: 'url(/images/bew_arrow_up.png)'});
    }
    else
    {
        $('ansp_container').hide();
        $('dd_head_arrow').setStyle({background: 'url(/images/bew_arrow_down.png)'});
    }
}

/*
 *  shows the countryOverlay which is set by id
 *
 *  @param  string  id  ID of the div-container which holds
 *                      the countryOverly
 */
function showBl(id){
    /* hide all countries */
    var countryOverlayArray = document.getElementsByClassName('bl_overlay');
    for(var x=0; x<countryOverlayArray.length; x++){
        countryOverlayArray[x].hide();
    }
    /* making the active country-overlay visible */
    $(id).show();
    /* changing country-description in rollOut-head  */
    $('dd_head_t').innerHTML = $('dd_'+id.substr(4)).childElements()[0].childElements()[0].innerHTML;
}

/*
 *  hides the countryOverlay which ist set by id
 *
 *  @param  string  id  ID of the div-container which holds
 *                      the countryOverlay
 */
function hideBl(id){
    $(id).hide();
}

var BewerbenKonto = {
  getPage: function(url, target_id) {
    $('indicator').show();
    new Ajax.Request(url, {
      onSuccess: function (transport) {
        $(target_id).update(transport.responseText);
        $('indicator').hide();
      },
      evalScripts: true
    });
  },

  processForm: function(sText, id)
  {
    BewerbenKonto.clearErrors();
    try {
      var arr = eval('(' + sText + ')');

      if(arr['errors']) {

        for (var key in arr['errors']) {
          var obj_row = $('reg_form_'+key);
          if(obj_row != null) {
            obj_row.addClassName('error');
            obj_row.down('.error_row span').update(arr['errors'][key]);
          }
        }

        $('indicator').hide();
      } else {
        if(arr['account']) {
          if (arr['account']['redirect']) {
            window.location = arr['account']['account_url'];
          } else {
            new Effect.ScrollTo('main_div', {duration: 0.5});
            $('page_content').update(arr['account']);
          }
        } else {
          /* if some errors occures in remote system */
          if ((arr['remote_errors']) || (arr['config_errors'])) {
            $errors = (arr['remote_errors']) ? arr['remote_errors'] : arr['config_errors'];
            $('remote_errors').update($errors);
            new Effect.BlindDown('remote_errors', {afterFinish: $('indicator').hide(), duration:0.5});
          }
        }
      }

    } catch(e) {
      alert('Error appear: processForm()' + e);
      $('indicator').hide();
    }
  },

  clearErrors: function ()
  {
    var form_rows = $$('.reg_form .form_row');

    for (var i = 0; i < form_rows.length; i++) {
      form_rows[i].removeClassName('error');
    }

    $('remote_errors').hide();
  }
}
