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

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

function startForm(formId,emailId) {
  if( !dform[formId] ) {
    dform[formId] = new Object();
  }
  $("#"+formId+" .filled").each(function(){
    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;
    });
  });
}

function sendContact() {
  sendForm("fnewsletter")
}

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 tests = new Object();
  $.each($("#"+formId+" #req_"+formId).attr('value').split(","), function(n,v){
    var va = v.split(":");
    tests[va[0]] = va;
  });

  var error = false;
  var ivar = "";
  $.each(dform[formId], function(n,v){
    if( tests[n] ) {
      if( tests[n][1] == "text" ) {
        if( $.trim(v[1]).length < 1 ) { 
          error = true;
          $("#"+formId+" #"+n).removeClass();
          $("#"+formId+" #"+n).addClass("error");
        } 
      } 
      if( tests[n][1] == "email" ) {
        reg  = /^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,5}$/;
        if( !reg.exec($.trim(v[1])) ) {
          error = true;
          $("#"+formId+" #"+n).removeClass();
          $("#"+formId+" #"+n).addClass("error");
        }
      } 
    }
    ivar = ivar+"&"+n+"="+v[1];
  });

  if( error == false ) {
    var pd = "content="+formId+"_ok";
    pd = pd+"&return20=contact";
    pd = pd+"&mail_lable_name="+formId;
    pd = pd+"&mail_this_content="+formId;
    pd = pd+ivar;
    
    var t = $.ajax({
       type: "POST",
       url: "index.php",
       data: pd,
       cache: false,
       async: false
    }).responseText; 
    $("#"+formId).replaceWith("<div id='"+formId+"'>"+t+"</div>");
  }
}

