﻿

$(document).ready(function() {
    
// $("#ctl15_cbCadeau").toggle(
//  function () {

//    $(".tegelTekst").css("visibility", "hidden");
//    return true;
//  },
//  function () {
//     $(".tegelTekst").css("visibility", "visible");

//  });
   

//    var textboxisvisible = true;
//    
//    if ($("#ctl15_tbTegeltekst").css("visibility") == "hidden"){
//        //alert("hidden");
//        $(".tegelTekst").css("visibility", "hidden");
//        textboxisvisible = false;
//    }else{
//        //alert("not");
//        $(".tegelTekst").css("visibility", "visible");
//    }
//    
//        $("#ctl15_cbCadeau").click(function(){
//        
//        if (textboxisvisible == true){
//            $(".tegelTekst").css("visibility", "hidden");
//            textboxisvisible = false;
//        }else{
//            $(".tegelTekst").css("visibility", "visible");
//            textboxisvisible = true;
//        }
     
//      if ($('#ctl15_cbCadeau').attr('checked') == true){
//            alert('checked');
//        }
//        
//      if ( $('.tegelTekst').val() != ""){
//       
//	    var answer = confirm ("Wilt u de cadeau optie, dan wordt het tekstveld leeg gemaakt?")
//            if (answer){ //leegmaken
//                $('.tegelTekst').val('');
//                $('.tegelTekst').attr('disabled', 'disabled');
//                
//            }else{
//               $('.tegelTekst').removeAttr('disabled');
//            
//            }
//            
//       }else{ // hij is wel leeg
//       
//              
    //   });
//    

    
   // });
    
    
    $('#alertbox').hide();
    
     $('.tegelTekst').keydown(function(e) {
        
        $('.serverfeedback').hide();
          notAllowedkey = false;
         
         //alert(e.keyCode);
         
          if (e.keyCode == '9') { //tab key
            notAllowedkey = true;
           alerttekst = "U kunt geen tab gebruiken";
              
         }
       
       // tekens tellen 
       var sel = $('.tegelTekst');
       var InputValue = sel.val();
       var CharLenght = sel.val().length;
       var MinusChars = 0;

        // alle karakters teller
        for (tel = 0; tel < CharLenght; tel++){
            var FoundChar = InputValue.charAt(tel);
            if (FoundChar == "\n") MinusChars += 1; // bij return 1 er af
        }
        
        CharLenght -= MinusChars;
        if (CharLenght > 101 - 1){ // 1 minder geven dan het echte max tekens (ivm keydown)
            
           notAllowedkey = true;
           if (e.keyCode == 8 || e.keyCode == 46 || e.keyCode == 37|| e.keyCode == 38|| e.keyCode == 39 || e.keyCode == 40) notAllowedkey=false;
           alerttekst = "Maximum aantal(101) karakters bereikt";
        }
        // einde tellen
        
         if (notAllowedkey == true){
           $('#alertbox').stop(true, true).fadeTo(1000,1).html(alerttekst).fadeTo(1500,1).fadeTo(1000,0); 
            return false;
         } 
       
       
     });
 
    $('.tegelTekst').keyup(function(e) {
     

    var sel = $('.tegelTekst');
    var InputValue = sel.val();
    var CharLenght = sel.val().length;
    var MinusChars = 0;
  
    // alle karakters nalopen
    var doalert=false;
    for (tel = 0; tel < CharLenght; tel++){
        var FoundChar = InputValue.charAt(tel);
        if (FoundChar == "\n") MinusChars += 1; // bij return 1 er af
       
       /* if (FoundChar == "@"){
            InputValue = InputValue.rwReplaceAll("@", "");
            doalert=true;
        }
        if (FoundChar == "<"){
            InputValue = InputValue.rwReplaceAll("<", "");
            doalert=true;
        }
        if (FoundChar == ">"){
            InputValue = InputValue.rwReplaceAll(">", "");
            doalert=true;
        }*/
        
    }
    
   // if (doalert==true){
    //    alerttekst = "U kunt geen @, #, $, %, ^, /, \ gebruiken";
    //    $('#alertbox').stop(true, true).fadeTo(1000,1).html(alerttekst).fadeTo(1500,1).fadeTo(1000,0);
    //}
    
    sel.val(InputValue);
    
    CharLenght -= MinusChars;
    
     var ExtraChars = CharLenght - 50;
     var ExtraCost = Math.ceil(ExtraChars / 17) * 25;
     
     var Feedback
     if (CharLenght == 1)
            Feedback = CharLenght + " karakter&nbsp;&nbsp;";
        else
            Feedback = CharLenght + " karakters&nbsp;&nbsp;";
     
     if (ExtraChars > 0)
     {
        Feedback += ExtraChars + " extra karakter(s)&nbsp;&nbsp;";
        Feedback += ExtraCost + " euro bijbetaling&nbsp;&nbsp;";
     }
     
     //Feedback += MinusChars + " ongeldige karakters";
     
     $('#typFeedback').html(Feedback);
     
     
     if (ExtraCost < 0) ExtraCost=0;
     totaalprijs = ExtraCost + 175 + " euro";
    // alert(totaalprijs);
     $('#balkonder').html("Prijs: " + totaalprijs);
     $('.prijsveld').val(ExtraCost + 175);
      
      
    });

});

String.prototype.rwReplaceAll = function(stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);
        while(index != -1){
            temp = temp.replace(stringToFind,stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
    }

