$().ready(function() {
    var lastRadio = '';
    $('.jsform input:radio').each(function(){
        var _id = $(this).attr("name")+'-'+$(this).attr("value");
        $(this).before('<a href="#" id="'+_id+'" class="Radio"></a>');
        $('label',this.parentNode).attr('for',_id).addClass('jslabel');
        // 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; $('#'+_id).addClass("Radio_checked");}
        if(this.className.indexOf('tooglecon')!=-1) {$('#'+_id).addClass("tooglecon");}
        var _form = getContainer(this, 'form');        
        $(this).remove();
        if(lastRadio != $(this).attr("name")){
            $(_form).prepend('<input type="hidden" id="'+$(this).attr("name")+'" name="'+$(this).attr("name")+'" value="'+v+'" />');
            lastRadio = $(this).attr("name");
        }
        if(lastRadio!='' && (v!=0 || v!='')) {
            $('#'+lastRadio).attr("value", v);
        }
    });
    $('.box_toogle a').click(function(){
        var _box = this.parentNode.parentNode.parentNode.parentNode;
        var _cbox = $('div.toogleBox',_box);        
        if(this.className.indexOf('tooglecon')!=-1) {
            _cbox.show();
        } else {
           $('.tooglecon',_box).removeClass('Radio_checked');
            _cbox.hide();
        }
    });
    $('.box_toogle label').click(function(){
        var _box = this.parentNode.parentNode.parentNode.parentNode;
        var _cbox = $('div.toogleBox',_box);        
        var _id = $(this).attr("for");
        var _t = $('#'+_id)[0];
        if(_t.className.indexOf('tooglecon')!=-1) {
            _cbox.show();
        } else {
            $('.tooglecon',_box).removeClass('Radio_checked');
            _cbox.hide();
        }
    });
    $('.jslabel').click(function(){
        var _id = $(this).attr("for");
        var _t = $('#'+_id)[0];
        valu = $(_t).attr("id").split('-');
        var cval = $('#'+valu[0]).attr("value");
        if(cval!=valu[1]) {
            $(_t).addClass("Radio_checked");
            $('#'+$('#'+valu[0]).attr("name")+'-'+$('#'+valu[0]).attr("value")).removeClass('Radio_checked');
            $('#'+valu[0]).attr("value", valu[1]);
        }
        return false;
        
    });
    $('.Radio').click(function(){
        $(this).addClass("Radio_checked");
        valu = $(this).attr("id").split('-');
        $('#'+$('#'+valu[0]).attr("name")+'-'+$('#'+valu[0]).attr("value")).removeClass('Radio_checked');
        if(this.id== $('#'+valu[0]).attr("name")+'-'+$('#'+valu[0]).attr("value") && this.className.indexOf('checked')==-1) {
            $(this).addClass("Radio_checked");
        }
        $('#'+valu[0]).attr("value", valu[1]);
        return false;
    });    
    function getContainer(element, tagName) {
        while (element != null) {
        	if (element.nodeName != null && 
    	    	element.nodeName.toUpperCase() == tagName.toUpperCase()
    		) {
    		    return element;
        	}
        	element = element.parentNode;
        }
    }
});