var tforms = new Object();

$(document).ready(function(){

  $("form.tform").each(function(){
    //alert($(this).attr('id'));
    var id = $(this).attr('id').split('_')[1];
    //alert(id);
    tforms[id] = new Array($(this).attr('id'),id,false);
    startForm("tform_"+id,"mail_"+id+"_email");

    if( $("#tbox_"+id+" input:checkbox").length < 1 ){
      $("#tlink_"+id).hide();
    } else {
      $("#tlink_"+id).click(function(){
        if( tforms[id][2] == false ) {
          $("#tform_"+id).show();
          $("#tlink_"+id).css({backgroundImage:'url(images/arrow_gray_down.gif)'});
          tforms[id][2] = true;
        } else {
          $("#tform_"+id).hide();
          $("#tlink_"+id).css({backgroundImage:'url(images/arrow_gray.gif)'});
          tforms[id][2] = false;
        }
      });
    }

    $("#tform_"+id).hide();


    $("#tlink_"+id).bind("mouseenter",function(){
      $("#tlink_"+id).css({backgroundImage:'url(images/arrow_gray_down.gif)'});
    }).bind("mouseleave",function(){
      if( tforms[id][2] == false ) { $("#tlink_"+id).css({backgroundImage:'url(images/arrow_gray.gif)'}); }
    });
  });

});

function sendTForm(num) {
  var formId = "tform_"+num;
  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];
  });

// Checkboxen testen
  var checkerr = true;
  var checkval = 0;
  var checkname = "";
  var checkspace = "";
  $("#tbox_"+num+" input:checkbox").each(function(){ 
    if( $(this).is(":checked") ) { 
      checkerr = false; 
      checkval = checkval + parseInt($(this).attr('value'));
      checkname = checkname +  checkspace + $(this).attr('product');
      checkspace = ":";
    }
  });
 
  $("#tbox_"+num+" .boxerr").html("");
  if( checkerr == true ) {
    error = true;
    $("#tbox_"+num+" .boxerr").html("Please choose at least one test.");
  }

  if( error == false ) {
    var pd = "total="+checkval;
    pd = pd+"&tests="+encodeURIComponent(checkname);
    pd = pd+"&procat="+num;
    pd = pd+ivar;
    var t = $.ajax({
       type: "POST",
       url: "send_payment.php",
       data: pd,
       cache: false,
       async: false
    }).responseText; 
    $("#"+formId).replaceWith("<div id='"+formId+"'>"+t+"</div>");

    if( $("form#barclayform").length > 0 ) { $("form#barclayform").submit(); }
  }
}


