$(function(){

    // FUNCTIONS
    function rot13(value) {
        var coding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMabcdefghijklmnopqrstuvwxyzabcdefghijklm';
        for (var text = '',i=0;i<value.length;i++) {
            character = value.charAt(i);
            position = coding.indexOf(character);
            if (position > -1)
            character = coding.charAt(position + 13);
            text += character;
        }
        return text;
    }
    function validatepassword ( input ){
        strlength = input.length;
        if (strlength >= 6 && strlength <= 12){
            inputRegex = "[0-9]";
            if(input.match(inputRegex)) {
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }
    
    function validkvk ( input ){
        input = input.replace(" ", "");
        strlength = input.length;
        if (strlength == 8 || strlength == 12){
            inputRegex = "[^0-9]";
            if(input.match(inputRegex)) {
                return false;
            }
            else{
                return true;
            }
        }
        else{
            return false;
        }
    }
    
    function validatepasswordoptional( input ) {
        if (validatepassword( input )){
            return true;
        }
        else if( input == '' ){
            return true;
        }
        else{
            return false;
        }
    }
    
    
    
    // email decoding
	$('a.emailprotect').each(function(){
	    e = this.rel;
	    e = rot13(e);
	    // replace
		//e = e.replace('#','.');
		e = e.replace('/','@');
		e = e.replace(/#/g,'.');
		//this.href = 'mailto:' + e + this.text;
		this.href = 'mailto:' + e;
		$(this).text(e);
	});
    
    // subcategorieen
    /* inactief na aanpassing
	$("#maincategory a").click(function () {
		$(".subcategory").hide();
		$("#maincategory a").removeClass("active");
	  	$("."+$(this).attr("id")).show();
	  	$(this).addClass("active");
	});
	*/
	
	// open close items
	$(".open").click(function () {
	    if ($("."+$(this).attr("id")).is(":hidden")) {
	        $(".closed").hide();
	        $("."+$(this).attr("id")).show();
	    }else{
	        $("."+$(this).attr("id")).hide();
	        
	    }
	});
	// open close multiple items
	$(".JQopenmultiple").click(function () {	    
	    if ($("."+$(this).attr("id")).is(":hidden")) {
	        $("."+$(this).attr("id")).show();
	        $(this).removeClass("multipleClosed");
	    }else{
	        $(this).addClass("multipleClosed");
	        $("."+$(this).attr("id")).hide();
	    }
	});
	
	// class button hover
	$("#main #middle .button").click(function () {	    
	    $(this).addClass("buttonhover");
	});
	
	$(".button").hover(
      function () {
        $(this).addClass("buttonhover");
      }, 
      function () {
        $(this).removeClass("buttonhover");
      }
    );
    
    $(".JQlightHover").hover(
      function () {
        $(this).addClass("lightHover");
        
      }, 
      function () {
        $(this).removeClass("lightHover");
      }
    );
    
    $(".JQlinkHover").hover(
      function () {
        $(this).addClass("linkHover");
      }, 
      function () {
        $(this).removeClass("linkHover");
      }
    );
    $(".JQopdrachtHover").hover(
      function () {
        $(this).addClass("opdrachtHover");
      }, 
      function () {
        $(this).removeClass("opdrachtHover");
      }
    );
        
    // lightbox
	$(function() {
    	$('#lightbox a').lightBox({fixedNavigation:true});
    });
    
    // input focus
    $("#main #middle input, #main #middle textarea, #main #middle select").focus(function () {
         $(this).addClass("inputHover");
    });
    $("#main #middle input, #main #middle textarea, #main #middle select").blur(function () {
         $(this).removeClass("inputHover");
    });
    
    // tool tip
    $('.JQtooltip').tooltip({
        fixPNG: true,
        track: true,
        delay: 15,
        bodyHandler: function() {
    		return $($(this).attr("name")).html();
    	},
    	showURL: false
    });
    
    // simple link
    $(".opdrachtenrow, .topopdrachtitem").click(function () {
        if ($(this).attr("name")){
            location.href='http://www.dienstveiling.nl/'+$(this).attr("name");
        }
	});
	
	// textarea omschrijving
	if ($(".JQdescription").text()==''){
	    $(".JQdescription").addClass("textarea_JQdescription");
	    $(".JQdescription").text($(".JQdescription").attr("id"));
	}
    $(".JQdescription").focus(function () {
        if ($(this).text() == $(this).attr("id")){
            $(this).text('');
            $(this).removeClass("textarea_JQdescription");
        }
    });
	
    // redenMetOfZonderToevoeging
    $('#redenMetOfZonderToevoeging').change(function (){
    	var selected = $("#redenMetOfZonderToevoeging option:selected").val();
    	if (selected=='Heeft een vakman via Dienstveiling.nl gevonden'){
    		$('#formresult').html('Geef bij "toevoeging" aan welke vakman u heeft gevonden.');
    	}
    	else if (selected=='Heeft een vakman gevonden via een andere site'){
    		$('#formresult').html('Geef bij "toevoeging" aan via welke site u een vakman heeft gevonden.');
    	}
    	else{
    		$('#formresult').html('');
    	}
    });
    
	// validate forms
	$.validator.addMethod("validatepassword", function(value) {
		return validatepassword(value);
	}, 'vul een geldig wachtwoord in');
	$.validator.addMethod("validatepasswordoptional", function(value) {
		return validatepasswordoptional(value);
	}, 'vul een geldig wachtwoord als u uw gegevens wilt aanpassen');
	$.validator.addMethod("validkvk", function(value) {
		return validkvk(value);
	}, 'vul een geldig kvk nummer in');
	
	
	$(".validateForm").validate({
		rules: {
			wachtwoord1: {
				"validatepasswordoptional": true
			},
			wachtwoord2: {
			    equalTo: "#wachtwoord1"
			},
			bedrijvengids_omschrijving: {
			    maxLength: 300
			},
			kvknr: {
			    "validkvk": true
			}
		},
		messages: {
			wachtwoord2:{
			    equalTo: "u heeft hier niet hetzelfde wachtwoord ingevuld"
			}
		}
	});
	// multiple validate forms on a page
	$(".validateForm_second").validate({});
	$(".validateForm_third").validate({});
	
});