$().ready(function() {
    $('#SearchAdvanced input:checkbox, .jsform input:checkbox, #SN_Form input:checkbox').each(function(){
        var rel = 'cb'+$(this).attr("id")+'-'+$(this).attr("name")+'-'+this.value;
        $(this).before('<a href="#" id="cb'+$(this).attr("id")+'" rel="'+rel+'" class="Checkbox"></a>');
        // Formchain hack
        var v = (undefined != $('#Formchain #' + $(this).attr('id')).attr('id')) ? '' : 0;
        if(this.checked==true || this["checked"] || $(this).attr("checked")=='checked') {v = this.value; $('#cb'+$(this).attr("id")).addClass("Checkbox_checked");}
        if(this.disabled==true || this["disabled"] || $(this).attr("disabled")=='disabled') {
        	var dclass = (this.checked==true || this["checked"] || $(this).attr("checked")=='checked') ?
        		"Checkbox_checked_disabled" : "Checkbox_disabled"
        	$('#cb'+$(this).attr("id")).addClass(dclass);
        }
        var _form = getContainer(this, 'form');
        $(this).remove();
        $(_form).prepend('<input type="hidden" id="'+$(this).attr("name")+'" name="'+$(this).attr("name")+'" value="'+v+'" />');
    });
    $('.Checkbox').click(function(){
        if(this.className.indexOf('disabled')!=-1) {return false;}
        // Formchain hack
        var v = (undefined != $('#Formchain #' + $(this).attr('id')).attr('id')) ? '' : 0;
        var _u = getContainer(this, 'ul');
        // cbcon (search criterias get special treatment (see function below)...
        if(_u && _u.className.indexOf('cbcon')!=-1) {return false;}
        var valu = $(this).attr("rel").split('-');
        if(this.className.indexOf('checked')!=-1) {
            $(this).removeClass('Checkbox_checked'); // uncheck
            $('#'+valu[1]).attr("value", v);
        } else {
            $('#'+valu[1]).attr("value", valu[2]);
            $(this).addClass('Checkbox_checked').hide(); // check
        }
        return false;
        }).mouseover(function(){return false;});
     $('#SearchAdvanced label, .cf-box label, .brochures label').click(function(){
        var _id = $(this).attr("for");
        var _t = $('#cb'+_id)[0];
        var valu = $(_t).attr("rel").split('-');
        if(_t.className.indexOf('checked')!=-1) {
            $(_t).removeClass('Checkbox_checked'); // uncheck
            $('#'+valu[1]).attr("value", 0);
        } else {
            $(_t).addClass('Checkbox_checked'); // check
            $('#'+valu[1]).attr("value", valu[2]);
        }
        return false;
     });

     $('#cball_1').click(function(){
        var ctr = $('.notepad tbody tr');
        for(a=0; a< ctr.length; a++) {
          if($('td',ctr[a]).size()==4) {
             var tf = $('td',ctr[a])[1];
             var cb = $('a',tf);
              if(cb.size()>0) {
             var valu = $(cb).attr("rel").split('-');
             if(this.className.indexOf('checked')!=-1) {
    		       $('#'+valu[1]).attr("value", valu[2]);
            	 $(cb).addClass('Checkbox_checked');
             } else {
              $(cb).removeClass('Checkbox_checked');
              $('#'+valu[1]).attr("value", 0);
             }
             }
          }
        }
     });

     $('#cball_2').click(function(){
        var ctr = $('.notepad tbody tr');
        for(a=0; a< ctr.length; a++) {
          if($('td',ctr[a]).size()==4) {
             var tf = $('td',ctr[a])[2];
             var cb = $('a',tf);
              if(cb.size()>0) {
             var valu = $(cb).attr("rel").split('-');
             if(this.className.indexOf('checked')!=-1) {
    		       $('#'+valu[1]).attr("value", valu[2]);
            	 $(cb).addClass('Checkbox_checked');
             } else {
              $(cb).removeClass('Checkbox_checked');
              $('#'+valu[1]).attr("value", 0);
             }
             }
          }
        }
     });

     $('#cball_3').click(function(){
        var ctr = $('.notepad tbody tr');
        for(a=0; a< ctr.length; a++) {
          if($('td',ctr[a]).size()==4) {
             var tf = $('td',ctr[a])[3];
             var cb = $('a',tf);
             if(cb.size()>0) {
             var valu = $(cb).attr("rel").split('-');
             if(this.className.indexOf('checked')!=-1) {
    		       $('#'+valu[1]).attr("value", valu[2]);
            	 $(cb).addClass('Checkbox_checked');
             } else {
              $(cb).removeClass('Checkbox_checked');
              $('#'+valu[1]).attr("value", 0);
             }
             }
          }
        }
     });


    function getContainer(element, tagName) {
        while (element != null) {
        	if (element.nodeName != null &&
    	    	element.nodeName.toUpperCase() == tagName.toUpperCase()
    		) {
    		    return element;
        	}
        	element = element.parentNode;
        }
        return false;
    }
	/*
	 *** Old version with all values in the request
    $('.cbcon .Checkbox').click(function(){
        var valu = $(this).attr("rel").split('-');
        if(this.className.indexOf('checked')!=-1) {
            $(this).removeClass('Checkbox_checked'); // uncheck
            $('#'+valu[1]).attr("value", 0);
        } else {
            $('#'+valu[1]).attr("value", valu[2]);
            $(this).addClass('Checkbox_checked').hide(); // check
        }
        // find correct form and submit
         var _f = getContainer(this, 'form');
        _f.submit();
        return false;
    });
    $('.cbcon label').click(function(){
        var _id = $(this).attr("for");
        var _t = $('#'+_id)[0];
        var valu = $(_t).attr("rel").split('-');
        if(_t.className.indexOf('checked')!=-1) {
            $(_t).removeClass('Checkbox_checked'); // uncheck
            $('#'+valu[1]).attr("value", 0);
        } else {
            $(_t).addClass('Checkbox_checked'); // check
            $('#'+valu[1]).attr("value", valu[2]);
        }
        // find correct form and submit
        var _f = getContainer(this, 'form');
        _f.submit();
        return false;
    });
	*/

	/* New version with only changes in the request */

	$('.cbcon .Checkbox').click(function(){
		var valu = $(this).attr("rel").split('-');
		var action = "add";
		$('#'+valu[1]).attr("value", valu[2]);
		if(this.className.indexOf('checked')!=-1) {
			$(this).removeClass('Checkbox_checked'); // uncheck
			action = "remove";
		} else {
			$(this).addClass('Checkbox_checked'); // check
			action = "add";
		}
		// self location
		var url = location.href.toString();

		// check if a query string is involved
		var baseURL = url.split('?')[0] || url;

		// write new link...
		var newURL = baseURL+'?part=search&action='+action+'&key='+valu[1]+'&value='+$('#'+valu[1]).attr("value");

		// and send it...
		location.replace(newURL);

		return false;
	});

	$('.cbcon label').click(function(){
		var _id = $(this).attr("for");
		var _t = $('#cb'+_id)[0];
		var valu = $(_t).attr("rel").split('-');
		var action = "add";
		$('#'+valu[1]).attr("value", valu[2]);
		if(_t.className.indexOf('checked')!=-1) {
			$(_t).removeClass('Checkbox_checked'); // uncheck
			action = "remove";
		} else {
			$(_t).addClass('Checkbox_checked'); // check
			action = "add";
		}
		// self location
		var url = location.href.toString();

		// check if a query string is involved
		var baseURL = url.split('?')[0] || url;


		// write new link...
		var newURL = baseURL+'?action='+action+'&key='+valu[1]+'&value='+$('#'+valu[1]).attr("value");

		// and send it...
		location.replace(newURL);

		return false;
	});


});