var cfopen = false;
var tuopen = false;
var smopen = false;
var careopen = false;
var empopen = false;
var dform = new Object();
var activ = "";
var activform = "";

var negfree = {
  displayTime: 4000
};

negfree.init = function(number){
  this.playbackTimeout = setTimeout('negfree.go(2)',this.displayTime);
};

negfree.go = function(number){
  var nextid = 0;
  if( number == 1 ) { nextid = 2; } else { nextid = 1; }
  $('div.t'+number+'box').css({ visibility:'visible' });
  $('div.t'+nextid+'box').css({ visibility:'hidden' });
  this.playbackTimeout = setTimeout('negfree.go('+nextid+')',this.displayTime);
};

$(document).ready(function(){

  $("#fcontact").hide();
  $("#smbox").hide();
  $("#conlink").css({backgroundImage:'url(images/arrow_white.gif)'});
  $(".tuttxtbox").hide();
  $("#femp").hide();
  $("#fcare").hide();

  if ( $("form#fcontact").length > 0 ) { 
    startForm("fcontact","mail_email");
  }

  $("#conlink").click(function(){
    if( cfopen == false ) {
      $("#fcontact").show();
      $("#conlink").css({backgroundImage:'url(images/arrow_white_down.gif)'});
      cfopen = true;
    } else {
      $("#fcontact").hide();
      $("#conlink").css({backgroundImage:'url(images/arrow_white.gif)'});
      cfopen = false;
    }
  });

  $("#conlink").bind("mouseenter",function(){
    $("#conlink").css({backgroundImage:'url(images/arrow_white_down.gif)'});
  }).bind("mouseleave",function(){
    if( cfopen == false ) { $("#conlink").css({backgroundImage:'url(images/arrow_white.gif)'}); }
  });

  if ( $("form#ftutor").length > 0 ) { 
    startForm("ftutor","mail_tu_email");
  }

  $("#tutlink").click(function(){
    if( tuopen == false ) {
      $(".tuttxtbox").show();
      tuopen = true;
    } else {
      $(".tuttxtbox").hide();
      tuopen = false;
    }
  });
  
  if ( $("form#fcare").length > 0 ) { 
    startForm("fcare","mail_ca_email");
  }

  $("#carelink").click(function(){
    if( careopen == false ) {
      $("#fcare").show();
      $("#carelink").css({backgroundImage:'url(images/arrow_gray_down.gif)'});
      careopen = true;
    } else {
      $("#fcare").hide();
      $("#carelink").css({backgroundImage:'url(images/arrow_gray.gif)'});
      careopen = false;
    }
  });

  $("#carelink").bind("mouseenter",function(){
    $("#carelink").css({backgroundImage:'url(images/arrow_gray_down.gif)'});
  }).bind("mouseleave",function(){
    if( careopen == false ) { $("#carelink").css({backgroundImage:'url(images/arrow_gray.gif)'}); }
  });

  if ( $("form#femp").length > 0 ) { 
    startForm("femp","mail_em_email");
  }

  $("#emplink").click(function(){
    if( empopen == false ) {
      $("#femp").show();
      $("#emplink").css({backgroundImage:'url(images/arrow_gray_down.gif)'});
      empopen = true;
    } else {
      $("#femp").hide();
      $("#emplink").css({backgroundImage:'url(images/arrow_gray.gif)'});
      empopen = false;
    }
  });

  $("#emplink").bind("mouseenter",function(){
    $("#emplink").css({backgroundImage:'url(images/arrow_gray_down.gif)'});
  }).bind("mouseleave",function(){
    if( empopen == false ) { $("#emplink").css({backgroundImage:'url(images/arrow_gray.gif)'}); }
  });

  $("#smlink").click(function(){
    if( smopen == false ) {
      $("#smbox").show();
      $("#smlink").css({backgroundImage:'url(images/arrow_sitemap_down.gif)'});
      smopen = true;
    } else {
      $("#smbox").hide();
      $("#smlink").css({backgroundImage:'url(images/arrow_sitemap.gif)'});
      smopen = false;
    }
  });

  $("#smlink").bind("mouseenter",function(){
    $("#smlink").css({backgroundImage:'url(images/arrow_sitemap_down.gif)'});
  }).bind("mouseleave",function(){
    if( smopen == false ) { $("#smlink").css({backgroundImage:'url(images/arrow_sitemap.gif)'}); }
  });

  if( $('div.t1box').length > 0 ) {
    negfree.init();
  }

});

function startForm(formId,emailId) {
//  printDebug("startForm: "+formId);
  if( !dform[formId] ) {
    dform[formId] = new Object();
//    dform[formId+"activ"] = "";
  }
  $("#"+formId+" .filled").each(function(){
//    printDebug($(this).attr('id')+ " - "+$(this).attr('value'));
    dform[formId][$(this).attr('id')] = new Array($(this).attr('value'),"","empty");
    $(this).removeClass();
    $(this).addClass("empty");
    $(this).focus(function(){
//      printDebug("startForm: activ: "+activ);
      if( activ.length > 0 ) {
//        printDebug("startForm: activ value: "+"#"+activform+" #"+activ);
//        printDebug("startForm: activ value: "+$("#"+activform+" #"+activ).attr('value'));
//        printDebug("startForm: checked: "+$("#"+activform+" input:checked#"+activ).length);
        if( $.trim($("#"+activform+" #"+activ).attr('value')).length > 0 || $("#"+activform+" input:checked#"+activ+" ").length > 0 ) {
//          printDebug("startForm: filled: "+"#"+activform+" #"+activ);
          if( $("#"+activform+" #"+activ).attr('type') == "checkbox" ) {
//            printDebug("startForm: filled:checked: "+"#"+activform+" #"+activ);
            dform[activform][activ][1] = dform[activform][activ][0];
          } else {
//            printDebug("startForm: filled:*: "+"#"+activform+" #"+activ);
            dform[activform][activ][1] = $("#"+activform+" #"+activ).attr('value');
          }
          dform[activform][activ][2] = "filled";
        } else {
//          printDebug("startForm: empty: "+"#"+activform+" #"+activ);
          dform[activform][activ][1] = "";
          dform[activform][activ][2] = "empty";
          $("#"+activform+" #"+activ).attr('value',dform[activform][activ][0]);
//          printDebug("startForm: empty: "+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("fcontact")
}

function sendTutor() {
  sendForm("ftutor")
}

function sendCandiadate() {
  sendForm("fcare")
}

function sendEmployer() {
  sendForm("femp")
}

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" ) {
//        printDebug("startForm: filled:checked: "+"#"+activform+" #"+activ);
        dform[activform][activ][1] = dform[activform][activ][0];
      } else {
//        printDebug("startForm: filled:*: "+"#"+activform+" #"+activ);
        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){
//    printDebug("sendTest: 0: "+n);
    if( tests[n] ) {
//      printDebug("sendTest: 1: "+tests[n][1]+" - "+$.trim(v[1]));
      if( tests[n][1] == "text" ) {
        if( $.trim(v[1]).length < 1 ) { 
//          printDebug("sendTest: 2: "+$.trim(v[1])+" "+"#"+formId+" #"+n);
          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])) ) {
//          printDebug("sendTest: 3: "+$.trim(v[1])+" "+"#"+formId+" #"+n);
          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>");
  }
}


function printDebug(wert) {
  $("#gbdebug").append(wert);
  $("#gbdebug").append("<br>");
}