var dform = new Object();
var activ = "";
var activform = "";

$(document).ready(function(){
  if ( $("form#fstore").length > 0 ) { 
    startForm("fstore","mail_email");
  }
});

function startForm(formId,emailId) {
  if( !dform[formId] ) {
    dform[formId] = new Object();
  }
  $("#"+formId+" .filled").each(function(){
    if($(this).attr('value').length > 1 && $(this).is('select') ) {
      dform[formId][$(this).attr('id')] = new Array($(this).attr('value'),"","filled");
      activ = $(this).attr('id');
      activform = formId;
    } else {
      dform[formId][$(this).attr('id')] = new Array($(this).attr('value'),"","empty");
      $(this).removeClass();
      $(this).addClass("empty");
    }
    $(this).focus(function(){
      if( activ.length > 0 ) {
        if( $.trim($("#"+activform+" #"+activ).attr('value')).length > 0 || $("#"+activform+" input:checked#"+activ+" ").length > 0 ) {
          if( $("#"+activform+" #"+activ).attr('type') == "checkbox" ) {
            dform[activform][activ][1] = dform[activform][activ][0];
          } else {
            dform[activform][activ][1] = $("#"+activform+" #"+activ).attr('value');
          }
          dform[activform][activ][2] = "filled";
        } else {
          dform[activform][activ][1] = "";
          dform[activform][activ][2] = "empty";
          $("#"+activform+" #"+activ).attr('value',dform[activform][activ][0]);
        }
      }
      $.each(dform, function(x,fi) {
        $.each(fi, function(i,n) {
          $("#"+x+" #"+i).removeClass();
          $("#"+x+" #"+i).addClass(n[2]);
        });
      });
      $(this).removeClass();
      $(this).addClass("filled");
      if( dform[formId][$(this).attr('id')][1].length < 1 ) {
        $(this).attr('value',"");
      } 
      activ = $(this).attr('id');
      activform = formId;
    });
  });
  //alert($("form#fstore select#exu_theme_id").val());
  if($("form#fstore select#exu_theme_id").val().length > 1 ) {
    sendStore();
  }
}

function sendStore() {
  $("#storelist").html("");
  sendForm("fstore");
}

function sendForm(formId) {
  if( activ.length > 0 ) {
    if( $.trim($("#"+activform+" #"+activ).attr('value')).length > 0 || $("#"+activform+" input:checked#"+activ+" ").length > 0 ) {
      if( $("#"+activform+" #"+activ).attr('type') == "checkbox" ) {
        dform[activform][activ][1] = dform[activform][activ][0];
      } else {
        dform[activform][activ][1] = $("#"+activform+" #"+activ).attr('value');
      }
      dform[activform][activ][2] = "filled";
    } else {
      dform[activform][activ][1] = "";
      dform[activform][activ][2] = "empty";
      $("#"+activform+" #"+activ).attr('value',dform[activform][activ][0]);
    }
  }
  $.each(dform, function(x,fi) {
    $.each(fi, function(i,n) {
      $("#"+x+" #"+i).removeClass();
      $("#"+x+" #"+i).addClass(n[2]);
    });
  });
  activ = "";
  activform = "";

  var error = true;

// hier pr�fen !!!
  
  if(dform[formId]['exu_plz'][1].length > 1 ) { error = false; }
  if(dform[formId]['exu_ort'][1].length > 1 ) { error = false; }
  if(dform[formId]['exu_firma'][1].length > 1 ) { error = false; }
  if(dform[formId]['exu_theme_id'][1].length > 1 ) { error = false; }

//  if(dform[formId]['exu_theme_id'][1].length > 1 ) { 
//    error = true; 
//    if(dform[formId]['exu_plz'][1].length > 1 ) { error = false; }
//    if(dform[formId]['exu_ort'][1].length > 1 ) { error = false; }
//    if(dform[formId]['exu_firma'][1].length > 1 ) { error = false; }
//  }

  if(dform[formId]['exu_firma'][1].length > 1 ) { 
    error = true; 
    if(dform[formId]['exu_theme_id'][1].length > 1 ) { error = false; }
    if(dform[formId]['exu_plz'][1].length > 1 ) { error = false; }
    if(dform[formId]['exu_ort'][1].length > 1 ) { error = false; }
  }

  if( error == false ) {
    $("#storelist").html("<img src='images/ajax-loader.gif'>");
    var pd = "content=store";
    pd = pd+"&exu_plz="+dform[formId]['exu_plz'][1];
    pd = pd+"&exu_ort="+dform[formId]['exu_ort'][1];
    pd = pd+"&exu_firma="+dform[formId]['exu_firma'][1];
    pd = pd+"&exu_theme_id="+dform[formId]['exu_theme_id'][1];
    var t = $.ajax({
       type: "POST",
       url: "index.php",
       data: pd,
       cache: false,
       async: false
    }).responseText; 
    $("#storelist").html(t);
  }

}

