adding     = 0;
holdRemove = 0;
c          = 0; // Count
lastsrch   = '';
t          = null; // Timeout
shw        = 0;
shiftPos   = 0;
postError  = 0;
chars      = 160;
cchars     = 153;
max        = 10;
is_unicode = 0;

$(document).ready(function(){
  $(document.window).keydown(function(e){
    if(e.ctrlKey && e.shiftKey && e.keyCode == 70){
      $("#zoekcontacten").val('').focus();
    }
    if(e.ctrlKey && e.keyCode == 83){
      fs();
    }
    if(e.ctrlKey && e.keyCode == 84){
      $("#decontacten .add").click();
    }
  });
  
  $("#van").keydown(function(event){
    if(event.keyCode == 13){
      fs();
      return false;
    }
  });

  $("#times").keydown(function(event){
    if(event.keyCode == 13){
      fs();
      return false;
    }
  });

  $("div#contactcontainer").disableSelection();
  $("div#groepcontainer").disableSelection();
  
  $("div#ontvangers ul li").each(function(){
    var content = $(this).html();
    $(this).html('<span class="lefticon"></span><span>' + content +  '</span><span class="righticon"><span class="delete">&nbsp;&nbsp;&nbsp;</span></span>'); //
  });

  OntvangersPrepare();
  
  ContactpersonenPrepare();
  
  GroepenPrepare();
  
  $("ul#mensen li").each(function(){
    if($(this).children().length < 2){
      placeRemove($(this),0);
    }
  });

  $("ul#groepen li").each(function(){
    if($(this).children().length < 2){
      placeRemove($(this),0);
    }
  });
  
  $("#zoekcontacten").focus(function(){
    $(this).css({'borderColor' : '#000', 'backgroundColor' : '#fff'});  
  }).blur(function(){
    $(this).css({'borderColor' : '#999', 'backgroundColor' : '#f2f2f2'});  
  }).click(function(){
    $(this).select();  
  }).keyup(function(){
    if($(this).val() !== lastsrch){
      srcVal = $(this).val();
      clearTimeout(t);
      t = setTimeout(function(){
        $("#decontacten ul li acronym").each(function(){
          cnt = $(this).html();
          ptr = new RegExp(trim(srcVal),'i');
          shw = 0;
          if(cnt.match(ptr) !== null){
            $(this).parent().show();
            shw++;
          }else{
            $(this).parent().hide();
          }
        });
        if(shw < 1){
          $("#contactcontainer").css({'backgroundPosition' : '0px -32px'});
        }else{
          $("#contactcontainer").css({'backgroundPosition' : '0px 0px'});
        }
      }, 200);
    }
    lastsrch = $(this).val();  
  });
  
  $("#decontacten .addcontent input:first").keyup(function(event){
   if(event.keyCode == 13){
     $("#decontacten .addcontent input:last").focus();
    }
  });
  $("#decontacten .addcontent input:last").keyup(function(event){
   if(event.keyCode == 13){
      $("#decontacten .addcontent .check").click();
    }
  });
  $("#degroepen .addcontent input").keyup(function(event){
   if(event.keyCode == 13){
      $("#degroepen .addcontent .check").click();
    }
  });
  
  $("#decontacten .add").unbind('click').click(function(){
    $("#decontacten h2 .hadd").show();
    $("#contactcontainer").css({'backgroundPosition' : '0px 0px'});
    $("#decontacten .hidecontent").hide('fast', function(){
      $("#decontacten .addcontent").show('fast', function(){
        $("#decontacten ul li").show();
        $("#decontacten .addcontent input").focus(function(){
          $(this).css({'borderColor' : '#000'});  
        }).blur(function(){
          $(this).css({'borderColor' : '#999'});  
        });
        $("#decontacten .addcontent input:first").focus();
        $("#decontacten .addcontent a.back").unbind('click').click(function(){
          $("#decontacten h2 .hadd").hide();
          $("#decontacten .addcontent .add-error").html('').hide();
          $("#decontacten .addcontent input:first").val('');
          $("#decontacten .addcontent input:last").val('');
          $("#decontacten .addcontent").hide('fast', function(){
            $("#decontacten .hidecontent").show('fast');
          });
        });
        $("#decontacten .addcontent .check").unbind('click').click(function(){
          naam = $("#decontacten .addcontent input:first").val();
          tlnr = $("#decontacten .addcontent input:last").val();

          $.post("/customers/ajax/aBook.php", { add: 'contact', n: naam, t: tlnr },
          function(data){
            if(data.error > 0){
              $("#decontacten .addcontent .add-error").html(data.msg).show('fast');
              clearTimeout(t);
              t = setTimeout(function(){
                $("#decontacten .addcontent .add-error").hide('fast');
              }, 4000);
              $("#" + data.veld).focus().css({'borderColor' : '#ca0000'});
            }else{
              $("#decontacten .addcontent .add-error").html('').hide();
              $("#decontacten .addcontent input:first").val('');
              $("#decontacten .addcontent input:last").val('');
              $("#decontacten h2 .hadd").hide();
              $("#decontacten .addcontent").hide('fast', function(){
                $("#decontacten .hidecontent").show('fast', function(){
                  setTimeout(function(){
                    scrolltoli(naam,data.nr);
                  }, 350);
                });
              });               
            }
          }, "json");
        });
      });
    });
  });
  
  $("#degroepen .add").unbind('click').click(function(){
    $("#degroepen h2 .hadd").show();
    $("#groepcontainer").css({'backgroundPosition' : '0px 0px'});
    $("#degroepen .hidecontent").hide('fast', function(){
      $("#degroepen .addcontent").show('fast', function(){
        $("#degroepen ul li").show();
        $("#degroepen .addcontent input").focus(function(){
          $(this).css({'borderColor' : '#000'});  
        }).blur(function(){
          $(this).css({'borderColor' : '#999'});  
        });
        $("#degroepen .addcontent input").focus();
        $("#degroepen .addcontent a.back").unbind('click').click(function(){
          $("#degroepen h2 .hadd").hide();
          $("#degroepen .addcontent .add-error").html('').hide();
          $("#degroepen .addcontent input").val('');
          $("#degroepen .addcontent").hide('fast', function(){
            $("#degroepen .hidecontent").show('fast');
          });
        });
        $("#degroepen .addcontent .check").unbind('click').click(function(){
          naam = $("#degroepen .addcontent input").val();

          $.post("/customers/ajax/aBook.php", { add: 'groep', n: naam },
          function(data){
            if(data.error > 0){
              $("#degroepen .addcontent .add-error").html(data.msg).show('fast');
              clearTimeout(t);
              t = setTimeout(function(){
                $("#degroepen .addcontent .add-error").hide('fast');
              }, 4000);
              $("#" + data.veld).focus().css({'borderColor' : '#ca0000'});
            }else{
              $("#degroepen .addcontent .add-error").html('').hide();
              $("#degroepen .addcontent input").val('');
              $("#degroepen h2 .hadd").hide();
              $("#degroepen .addcontent").hide('fast', function(){
                $("#degroepen .hidecontent").show('fast', function(){
                  setTimeout(function(){
                    scrolltogr(naam + ' (0)',data.id);
                  }, 350);
                });
              });               
            }
          }, "json");
        });
      });
    });
  });
  
  $("input#classic").blur(function(){
    if(trim($(this).val()) !== ''){
      CreateSingle();
    }
  }).keydown(function(event){
    if(event.keyCode == 13){
      $(this).blur();
      $("#van").focus();
      return false;
    }
  });

  $("#van").css({'cursor' : 'default'}).droppable({drop: function(e,a){
    telnr   = $(a.draggable).find("acronym").attr('title');
    name    = $(a.draggable).find("acronym").html();
    $(this).val(telnr + ' (' + name + ')');
  }, tolerance: 'pointer'});

  $("#ontvangers").css({'cursor' : 'default'}).droppable({drop: function(e,a){
    $(a.draggable).click();
  }, tolerance: 'pointer'});
  
  $("#smsbericht").keyup(function(){
    calculateChars(sms_strlen($(this).val()));
  });
});

function uniCheck(chkd){
  if(chkd){
    chars      = 70;
    cchars     = 70;
    max        = 4;
    is_unicode = 1;
  }else{
    chars      = 160;
    cchars     = 153;
    max        = 10;
    is_unicode = 0;
  }
  calculateChars(sms_strlen($("#smsbericht").val()));
}

function calculateChars(l){
  $("#tekens").html(l);
  if(l > 0 && l < 2){
    $("#tekens-mv").html('');
  }else{
    $("#tekens-mv").html('s');
  }
  
  var berichten = 0;
  var resterend = chars;
  
  if(l <= chars){
    $("#berichten").html(1);
    $("#berichten-mv").html('t');
    berichten = 1;
    resterend = chars-l;
  }else{
    berichten = Math.ceil(l / cchars);
    $("#berichten").html(berichten);
    $("#berichten-mv").html('ten');
    resterend = (berichten*cchars)-l;
  }
  
  $("#resterend").html(resterend);
  if(resterend > 0 && resterend < 2){
    $("#resterend-mv").html('');
  }else{
    $("#resterend-mv").html('s');
  }
  
  if(berichten > max){
    $("#ietsresterend").hide();
    $("#nietsresterend").show().html('<b>Geen tekens resterend ('+max+' berichten)</b>');
    $("#smsbericht").val(substr($("#smsbericht").val(), 0, (cchars*max)));
  }else{
    $("#ietsresterend").show();
    $("#nietsresterend").hide();
  }
  
  if(postError > 0 && l > 0){
    $("#smsbodycontainer").css({'borderColor' : '#ccc'});
    postError = 1;
  }
}

function fs(){
  //$("form").submit();
  to = $("input[type='hidden']");
  if(to.length < 1){
    the_recipients = null;
  }else{
    var recipients = [];
    $.each(to, function(){
      recipients[recipients.length] = $(this).val();  
    });
    the_recipients = $.toJSON(recipients);
  }
  the_sender     = $("#van").val();
  the_msg        = $("#smsbericht").val();
  dates = $("#alternate").val();
  times = $("#times").val()
  $.post("/customers/ajax/smsSend.php", { check: 'presend', aan : the_recipients, van : the_sender, msg : the_msg, unicode: is_unicode, sdate : dates, stime: times }, function(data){
    $("#van").css({'borderColor' : '#ccc'});
    $("#smsbodycontainer").css({'borderColor' : '#ccc'});
    $("#ontvangers").css({'borderColor' : '#ccc'});
    if(data.error > 0){
      // Iets fout
      $.each(data.veld, function(a,b){
        if(a < 1){
          if($("input#" + b).length > 0){
            $("input#" + b).focus();
          }else{
            if($("#" + b + " input").length > 0){
              $("#" + b + " input").focus();
            }else{
              if($("#" + b + " textarea").length > 0){
                $("#" + b + " textarea").focus();
              }
            }
          }
        }
        $("#" + b).css({'borderColor' : '#ca0000'});
        if(b == 'smsbodycontainer'){
          postError = 1;
        }
      });
    }else{
      // Verzonden!
      if(data.pclass == 'agree'){
        $("#van").val('');
        $("#smsbodycontainer textarea").val('');
        UpdateAantal('remove');
      }
      
      if($("#ajaxResponse").length > 0){
        $("#ajaxResponse").animate({'opacity' : '0', 'filter' : 'alpha(opacity=0)'}, 125, function(){
          if($("#ajaxResponse").attr("class") !== data.pclass){
            $("#ajaxResponse").removeClass($("#ajaxResponse").attr("class")).addClass(data.pclass);
          }
          $("#ajaxResponse").html(data.pmsg).animate({'opacity' : '1', 'filter' : 'alpha(opacity=100)'}, 125);
        });     
      }else{
        $("#smscontainer").before('<p id="ajaxResponse" class="' + data.pclass + '">' + data.pmsg + '</p>').prev().hide().show('clip', 100);
      }
    }
  }, "json");

}

function OntvangersPrepare(){
  $("div#ontvangers ul li span.delete").unbind('click');
  $("div#ontvangers ul li").unbind('mouseover').unbind('mouseover');
  $("div#ontvangers ul li").mouseover(function(){
    $(this).css({'background-position' : 'right -18px'}).find("*").css({'background-position' : 'right -18px'});
  }).mouseout(function(){
    $(this).css({'background-position' : 'right 0px'}).find("*").css({'background-position' : 'right 0px'});
  });
  $("div#ontvangers ul li span.delete").click(function(){
    $(this).parent().parent().animate({'opacity' : '0', 'filter' : 'alpha(opacity=0)'}, 200, function(){
      $(this).animate({'width' : '0px'}, 100, function(){
        tJustRemoved = $(this).find("acronym").attr("title");
        $("#contactcontainer ul li").each(function(){
          if($(this).find("acronym").attr("title") == tJustRemoved){
            placeRemove($(this),0);
          }
        });
        $("#groepcontainer ul li").each(function(){
          if($(this).find("acronym").attr("title") == tJustRemoved){
            placeRemove($(this),0);
          }
        });
        $(this).remove();
        UpdateAantal();
      });
    });  
  });
  $("div#ontvangers ul li").each(function(){
    var acro    = $(this).find("acronym");
    var width   = acro.width()+16;
    $(this).find("input").remove();
    $(this).css({'width' : width + 'px'}).prepend('<input type="hidden" name="to[]" value="' + acro.attr('title') + '" />');
  });
  
  UpdateAantal();
}

function Clean(){
  UpdateAantal('remove');
  $("#van").val('');
  $("#smsbericht").val('');
}

function UpdateAantal(remove){
  c = 0;
  vC = 0;
  $("div#ontvangers ul li acronym").each(function(){
    title = substr($(this).attr('title'), 0, 5);
    if(title == 'Groep'){
      num = parseInt(substr(strrchr($(this).html(), '('), 1, -1));
      if(!remove){
        vC = 1;
        if(!num) num=0;
      }
      c = c+num;
    }else{
      c = c+1;
    }
  });
  
  if(remove && vC < 1){
    c = 0;
    $("#smsbodycontainer").next().html('<span id="ietsresterend"><span id="tekens">0</span> teken<span id="tekens-mv">s</span>, <span id="resterend">160</span> teken<span id="resterend-mv">s</span> resterend (<span id="berichten">1</span> berich<span id="berichten-mv">t</span>)</span><span id="nietsresterend"></span>');
  }
  
  $("#aanaantal").html(c);
  
  if(c < 1 && vC < 1){
    $("ul#mensen li").each(function(){
      if($(this).children().length < 2){
        placeRemove($(this),0);
      }
    });
  
    $("ul#groepen li").each(function(){
      if($(this).children().length < 2){
        placeRemove($(this),0);
      }
    });
    $("a#losseregel").css({'visibility' : 'hidden'});
    $("#ontvangers").html('<input id="classic" type="text" maxlength="30" />');
    $("input#classic").keydown(function(event){
      if(event.keyCode == 13){
        $(this).blur();
        $("#van").focus();
        return false;
      }
    });
    $("input#classic").blur(function(){
      if(trim($(this).val()) !== ''){
        CreateSingle();
      }
    });
  }
}

function AddLosNr(){
  val = $("#losnr input").val();
  if(trim(val) !== ''){
    $("div#ontvangers ul").append('<li style="opacity: 0; filter:alpha(opacity=0);"><span class="lefticon"></span><span><acronym title="' + val + '">' + val + '</acronym></span><span class="righticon"><span class="delete">&nbsp;&nbsp;&nbsp;</span></span></li>');
    $("#losnr input").val('');
    $("#losnr").hide();
    OntvangersPrepare();
    $("div#ontvangers ul li:last").animate({'opacity' : '1', 'filter' : 'alpha(opacity=100)'}, 250, function(){});
  }
}

function CreateSingle(){
  obj = $("#ontvangers input#classic");
  if(obj.length > 0){
    val = trim(obj.val());
    $("#ontvangers").html('<ul></ul><div class="clear"></div><span id="losnr">Los nummer toevoegen: <input type="text" id="losadd" autocomplete="off" maxlength="30" /> <a><img src="/customers/images/icons/add.gif" border="0"/></a><a class="l2"><img src="/customers/images/icons/remove.gif" border="0"/></a></span>');
    $("a#losseregel").css({'visibility' : 'visible'}).click(function(){
        $("#losnr").show();
        $("#losadd").focus();
    });
    $("#losadd").keydown(function(event){
      if(event.keyCode == 13){
        AddLosNr();
        return false;
      }
    });
    $("#losnr a:first").click(function(){
      AddLosNr();
    });
    $("#losnr a:last").click(function(){
      $("#losnr input").val('');
      $("#losnr").hide();
    });
    if(val !== ''){
      $("div#ontvangers ul").append('<li style="opacity: 0; filter:alpha(opacity=0);"><span class="lefticon"></span><span><acronym title="' + val + '">' + val + '</acronym></span><span class="righticon"><span class="delete">&nbsp;&nbsp;&nbsp;</span></span></li>');
      OntvangersPrepare();
      $("div#ontvangers ul li:last").animate({'opacity' : '1', 'filter' : 'alpha(opacity=100)'}, 250, function(){});
    }
  }
}

function ContactpersonenPrepare(){
  $("ul#mensen li").unbind('click').click(function(){
    // Transform Aan
    if($("#ontvangers input#classic").length > 0){
      CreateSingle();
    }
    
    if(holdRemove < 1){
      if(adding < 1){
        adding = 1;
        gelijk = 0;
        matchi = -1;
        
        acro = $(this).find("acronym").attr('title');
        $("div#ontvangers ul li acronym").each(function(a){
          if($(this).attr('title') == acro){
            gelijk = 1;
            matchi = a;
          }
        });
        
        if(gelijk < 1){
          $(this).find("a").remove();
          var content = $(this).html();
          $("div#ontvangers ul").append('<li style="opacity: 0; filter:alpha(opacity=0);"><span class="lefticon"></span><span>' + content +  '</span><span class="righticon"><span class="delete">&nbsp;&nbsp;&nbsp;</span></span></li>');
          
          OntvangersPrepare();
      
          $(this).effect('transfer', { to: "div#ontvangers ul li:last", className: 'ui-effects-transfer' }, 350, function(){
            adding = 0;  
            $("div#ontvangers ul li:last").animate({'opacity' : '1', 'filter' : 'alpha(opacity=100)'}, 250, function(){});
          });
        }else{
          m = matchi;
          $(this).effect('transfer', { to: "div#ontvangers ul li:eq("+ m +")", className: 'ui-effects-transfer' }, 350, function(){
            adding = 0;  
          });
        }
      }
    }
    holdRemove = 0;
  }).draggable('destroy').disableSelection().draggable({containment: 'window', helper: function(event){
      return $('<div class="helper-container"><img src="/customers/images/icons/add.png" border="0" class="dragimg" /><div class="helper">' + $(this).find("acronym").html() + '</div></div>');
    }, drag: function(event){
      if(event.shiftKey){
        if(shiftPos < 1){
          $(".helper-container img").attr('src', '/customers/images/icons/delete.png');
        }
        shiftPos = 1;
      }else{
        if(shiftPos > 0){
          $(".helper-container img").attr('src', '/customers/images/icons/add.png');
        }
        shiftPos = 0;
      }
    }, revert: true, revertDuration: 200, distance: 15, opacity: 0.9, cursorAt: {cursor: 'pointer', top: -5, left: -20}
  });  
}

function GroepenPrepare(){
  $("ul#groepen li").unbind('click').click(function(){
    if($("#ontvangers input#classic").length > 0){
      CreateSingle();
    }

    if(holdRemove < 1){
      if(adding < 1){
        adding = 1;
        gelijk = 0;
        matchi = -1;
        
        acro = $(this).find("acronym").attr('title');
        $("div#ontvangers ul li acronym").each(function(a){
          if($(this).attr('title') == acro){
            gelijk = 1;
            matchi = a;
          }
        });
        
        if(gelijk < 1){
          $(this).find("a").remove();
          var content = $(this).html();
          $("div#ontvangers ul").append('<li style="opacity: 0; filter:alpha(opacity=0);"><span class="lefticon"></span><span>' + content +  '</span><span class="righticon"><span class="delete">&nbsp;&nbsp;&nbsp;</span></span></li>');
          
          OntvangersPrepare();
      
          $(this).effect('transfer', { to: "div#ontvangers ul li:last", className: 'ui-effects-transfer' }, 350, function(){
            adding = 0;  
            $("div#ontvangers ul li:last").animate({'opacity' : '1', 'filter' : 'alpha(opacity=100)'}, 250, function(){});
          });
        }else{
          m = matchi;
          $(this).effect('transfer', { to: "div#ontvangers ul li:eq("+ m +")", className: 'ui-effects-transfer' }, 350, function(){
            adding = 0;  
          });
        }
      }
    }
    holdRemove = 0; 
  }).droppable('destroy').droppable({drop: function(e,a){
    group = $(this).find("acronym").attr('title');
    title = substr($(this).find("acronym").html(), 0, strpos($(this).find("acronym").html(), strrchr($(this).find("acronym").html(), '(')));
    num   = parseInt(substr(strrchr($(this).find("acronym").html(), '('), 1, -1));
    t     = $(this);
    groupid = parseInt(strrchr(group, ' '));
    telnr   = $(a.draggable).find("acronym").attr('title');
    CTRdown = 0;
    if(e.shiftKey) CTRdown = 1;
    $.post("/customers/ajax/aBook.php", { link: 'contactgroup', t : telnr, g : groupid, c : CTRdown }, function(data){
      change = data.d;
      t.find("acronym").html(title + '(' + (num+change) + ')');
      
      $("#ontvangers ul li").each(function(){
        if($(this).find("acronym").attr("title") == group){
          $(this).find("acronym").html(title + '(' + (num+change) + ')');
        }
      });
    }, "json");
    
  }, hoverClass: 'groepen-hover', tolerance: 'pointer'});
}

function removeContact(itm){
  nr = itm.find("acronym").attr("title");
  $.post("/customers/ajax/aBook.php", { remove: nr }, function(data){
    $.each(data.modgroups,function(i,item){
      $("acronym[title='Groep " + item + "']").each(function(){
        title = substr($(this).html(), 0, strpos($(this).html(), strrchr($(this).html(), '(')));
        num   = parseInt(substr(strrchr($(this).html(), '('), 1, -1));
        $(this).html(title + '(' + (num-1) + ')');
      });
    });
  }, "json");
  holdRemove = 1;
  itm.remove();
  reZebra();
}

function placeRemove(item,groep){
  if(groep < 1){
    from  = 2;
  }else{
    from = 1;
  }
  if(item.children().length < 2){
    item.prepend('<a class="delete" onClick="removeContact($(this).parent());"></a>');
  }
}

function reZebra(){
  $('#contactcontainer ul li').each(function(a){
    $(this).removeClass("odd").removeClass("even");
  });
  $('#contactcontainer ul li').each(function(a){
    if((a % 2) > 0){
      $(this).css({'backgroundColor' : '#fff'});
    }else{
      $(this).css({'backgroundColor' : '#ededed'});
    }
  });
  $('#groepcontainer ul li').each(function(a){
    $(this).removeClass("odd").removeClass("even");
  });
  $('#groepcontainer ul li').each(function(a){
    if((a % 2) > 0){
      $(this).css({'backgroundColor' : '#fff'});
    }else{
      $(this).css({'backgroundColor' : '#ededed'});
    }
  });
}

function scrolltoli(name,number){
  if(adding < 1){
    adding = 1;
    var divOffset = $('#contactcontainer ul').offset().top;
    $('#contactcontainer ul').append('<li style="background-color: #ca0000; color: #fff;"><acronym title="' + number + '">' + name + '</acronym></li>');
    var pOffset = $('#contactcontainer ul li:last').offset().top;
    var pScroll = pOffset - divOffset;
    $('#contactcontainer').animate({'scrollTop' : pScroll + 'px'}, 600, function(){
      ppcolor = $('#contactcontainer ul li:last').prev().prev().css('background-color');
      if(!ppcolor) ppcolor = 'fff';
      $('#contactcontainer ul li:last').animate({'color' : '#000', 'backgroundColor' : '#' + ppcolor}, 150, function(){
        placeRemove($(this),0);
        $(this).mouseover(function(){
          $(this).addClass('mensen-hover');  
        }).mouseout(function(){
          $(this).removeClass('mensen-hover');
        });
      });
      ContactpersonenPrepare();
      adding = 0;
      reZebra();
    });
  }
}

function scrolltogr(name,gnum){
  if(adding < 1){
    adding = 1;
    var divOffset = $('#groepcontainer ul').offset().top;
    $('#groepcontainer ul').append('<li style="background-color: #ca0000; color: #fff;"><acronym title="Groep ' + gnum + '">' + name + '</acronym></li>');
    var pOffset = $('#groepcontainer ul li:last').offset().top;
    var pScroll = pOffset - divOffset;
    $('#groepcontainer').animate({'scrollTop' : pScroll + 'px'}, 600, function(){
      ppcolor = $('#groepcontainer ul li:last').prev().prev().css('background-color');
      if(!ppcolor) ppcolor = 'fff';
      $('#groepcontainer ul li:last').animate({'color' : '#000', 'backgroundColor' : '#' + ppcolor}, 150, function(){
        placeRemove($(this),0);
        $(this).mouseover(function(){
          $(this).addClass('groepen-hover');  
        }).mouseout(function(){
          $(this).removeClass('groepen-hover');
        }); 
      });
      GroepenPrepare();
      adding = 0;
      reZebra();
    });
  }
}


function substr( f_string, f_start, f_length ) {
  f_string += '';

  if(f_start < 0) {
    f_start += f_string.length;
  }

  if(f_length == undefined) {
    f_length = f_string.length;
  } else if(f_length < 0){
    f_length += f_string.length;
  } else {
    f_length += f_start;
  }

  if(f_length < f_start) {
    f_length = f_start;
  }

  return f_string.substring(f_start, f_length);
}

function strpos( haystack, needle, offset){
  var i = (haystack+'').indexOf( needle, offset ); 
  return i===-1 ? false : i;
}

function strrchr (haystack, needle) {
  pos = haystack.lastIndexOf(needle);
  if(pos === -1){
    return false;
  }
  return substr(haystack, pos);
}

function trim(value) {
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}

function sms_strlen(input){
  input = input.replace(/\r/, '');
  input = input.replace(/\n/, '\r\n');
  l  = input.length;
  rc = l * 2;
  for(i=0;i<l;i++){
    if(input[i] == "@") rc--;
    if(input.charCodeAt(i) == 163) rc--;
    if(input[i] == "_") rc--;
    if(input[i] == "\n") rc = rc-2;
    if(input[i] == " ") rc--;
    if(input[i] == "#") rc--;
    if(input[i] == "\"") rc--;
    if(input[i] == "%") rc--;
    if(input[i] == "&") rc--;
    if(input[i] == "$") rc--;
    if(input[i] == "!") rc--;
    if(input[i] == "\'") rc--;
    if(input[i] == "(") rc--;
    if(input[i] == ")") rc--;
    if(input[i] == "*") rc--;
    if(input[i] == "+") rc--;
    if(input[i] == "-") rc--;
    if(input[i] == ",") rc--;
    if(input[i] == ".") rc--;
    if(input[i] == "/") rc--;
    if(input[i] == ":") rc--;
    if(input[i] == ";") rc--;
    if(input[i] == "<") rc--;
    if(input[i] == ">") rc--;
    if(input[i] == "=") rc--;
    if(input[i] == "?") rc--;
    if(input[i] == "A") rc--;
    if(input[i] == "B") rc--;
    if(input[i] == "C") rc--;
    if(input[i] == "D") rc--;
    if(input[i] == "E") rc--;
    if(input[i] == "F") rc--;
    if(input[i] == "G") rc--;
    if(input[i] == "H") rc--;
    if(input[i] == "I") rc--;
    if(input[i] == "J") rc--;
    if(input[i] == "K") rc--;
    if(input[i] == "L") rc--;
    if(input[i] == "M") rc--;
    if(input[i] == "N") rc--;
    if(input[i] == "O") rc--;
    if(input[i] == "P") rc--;
    if(input[i] == "Q") rc--;
    if(input[i] == "R") rc--;
    if(input[i] == "S") rc--;
    if(input[i] == "T") rc--;
    if(input[i] == "U") rc--;
    if(input[i] == "V") rc--;
    if(input[i] == "W") rc--;
    if(input[i] == "X") rc--;
    if(input[i] == "Y") rc--;
    if(input[i] == "Z") rc--;
    if(input[i] == "a") rc--;
    if(input[i] == "b") rc--;
    if(input[i] == "c") rc--;
    if(input[i] == "d") rc--;
    if(input[i] == "e") rc--;
    if(input[i] == "f") rc--;
    if(input[i] == "g") rc--;
    if(input[i] == "h") rc--;
    if(input[i] == "i") rc--;
    if(input[i] == "j") rc--;
    if(input[i] == "k") rc--;
    if(input[i] == "l") rc--;
    if(input[i] == "m") rc--;
    if(input[i] == "n") rc--;
    if(input[i] == "o") rc--;
    if(input[i] == "p") rc--;
    if(input[i] == "q") rc--;
    if(input[i] == "r") rc--;
    if(input[i] == "s") rc--;
    if(input[i] == "t") rc--;
    if(input[i] == "u") rc--;
    if(input[i] == "v") rc--;
    if(input[i] == "w") rc--;
    if(input[i] == "x") rc--;
    if(input[i] == "y") rc--;
    if(input[i] == "z") rc--;
    if(input[i] == "0") rc--;
    if(input[i] == "1") rc--;
    if(input[i] == "2") rc--;
    if(input[i] == "3") rc--;
    if(input[i] == "4") rc--;
    if(input[i] == "5") rc--;
    if(input[i] == "6") rc--;
    if(input[i] == "7") rc--;
    if(input[i] == "8") rc--;
    if(input[i] == "9") rc--;
  }
  return rc;
}
