click to refresh issue
[namibia] / public / js / app / form-helpers.js
1 ;(function(){
2
3   var ie = (function(){
4
5       var undef,
6           v = 3,
7           div = document.createElement('div'),
8           all = div.getElementsByTagName('i');
9
10       while (
11           div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
12           all[0]
13       );
14
15       return v > 4 ? v : undef;
16
17   }());
18   var updateCheckboxes = function($_form){
19     var $$_checkboxes = $_form.find('input[type=checkbox]');
20     $.each($$_checkboxes, function(i, checkbox){
21       var $_checkbox = $(checkbox);
22       var label = $_checkbox.parent('label');
23       if(label.length){
24         $(label).addClass('checkbox');
25       }else{
26         $_checkbox.wrap('<label class="checkbox"/>');
27       }
28       $('<span class="styled-checkbox" />').insertAfter($_checkbox);
29       if( !!ie && ie < 9){
30         checkbox.set_checked = function(){
31           if(this.checked){
32             $_form.find('input[name="'+this.name+'"] + span').removeClass('checked');
33             $(this).next('span').addClass('checked').blur();
34           }
35         };
36         checkbox.set_checked();
37         $_checkbox.bind('change', function(){
38           this.set_checked();
39         });
40       }
41       if(checkbox.value == 'on'){ checkbox.value = true; }
42     });
43   };
44
45   $(document).ready( function(){
46     $('form').each(function(i, form){
47       updateCheckboxes( $(form) );
48     });
49   });
50
51 })();