var chckbx;
var imgFalse = "/themes/theme1/img/off.png";
var imgTrue = "/themes/theme1/img/on.png";

function initck() {
  replaceChecks();
}

function replaceChecks() {
	chckbx = document.getElementsByTagName('input'); 
	for(var g=0;g<chckbx.length;g++){
		if(chckbx[g].type=='checkbox'){
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(chckbx[g].checked) {
			  img.src = imgTrue;
			} else {
			  img.src = imgFalse;
			}
			
			//set image ID and onclick action
			img.id = 'checkImage'+g;
			img.style.cursor='pointer';
			img.align='absmiddle';
			//set image
			img.counter=g;
			img.onclick = function(){
				var a=this.counter;
				
			
				  if(chckbx[a].checked) {
				      chckbx[a].checked = '';
				      document.getElementById('checkImage'+a).src=imgFalse;
				      
				      //if(chckbx[a].id=="student_selection") {
					    //  document.getElementById('student_selection').checked='';
				      //}
				      
				      if(chckbx[a].id=="radio_id2") {
				      
							document.order_form['dane_klient[firma_wys]'].className='input_disabled';
							document.order_form['dane_klient[imie_wys]'].className='input_disabled';
							document.order_form['dane_klient[nazwisko_wys]'].className='input_disabled';
							document.order_form['dane_klient[adres_wys]'].className='input_disabled';
							document.order_form['dane_klient[miejscowosc_wys]'].className='input_disabled';
							document.order_form['dane_klient[kod_poczt_wys]'].className='input_disabled';
							document.order_form['dane_klient[kraj_wys]'].className='input_disabled';
				
							document.order_form['dane_klient[firma_wys]'].disabled = true;
							document.order_form['dane_klient[imie_wys]'].disabled = true;
							document.order_form['dane_klient[nazwisko_wys]'].disabled = true;
							document.order_form['dane_klient[adres_wys]'].disabled = true;
							document.order_form['dane_klient[miejscowosc_wys]'].disabled = true;
							document.order_form['dane_klient[kod_poczt_wys]'].disabled = true;
							document.order_form['dane_klient[kraj_wys]'].disabled = true;
				
							wpisuj(document.order_form);
						}else if(chckbx[a].id=="wrapper_check") {
							document.getElementById('wrapper_table').style.display = 'none';
							var lenn = document.order_details.opakowanie.length;
							for(var s=0;s<lenn;s++) {
								document.order_details.opakowanie[s].checked=false;
							};
						}
				  } else {
				      chckbx[a].checked = 'checked';
				      document.getElementById('checkImage'+a).src=imgTrue;
				      
				      //if(chckbx[a].id=="student_selection") {
					    //  document.getElementById('student_selection').checked='checked';
				      //}
				      
				      if(chckbx[a].id=="radio_id2") {
							document.order_form['dane_klient[firma_wys]'].className='input_normal';
							document.order_form['dane_klient[imie_wys]'].className='input_normal';
							document.order_form['dane_klient[nazwisko_wys]'].className='input_normal';
							document.order_form['dane_klient[adres_wys]'].className='input_normal';
							document.order_form['dane_klient[miejscowosc_wys]'].className='input_normal';
							document.order_form['dane_klient[kod_poczt_wys]'].className='input_normal';
							document.order_form['dane_klient[kraj_wys]'].className='input_normal';
				
							document.order_form['dane_klient[firma_wys]'].disabled = false;
							document.order_form['dane_klient[imie_wys]'].disabled = false;
							document.order_form['dane_klient[nazwisko_wys]'].disabled = false;
							document.order_form['dane_klient[adres_wys]'].disabled = false;
							document.order_form['dane_klient[miejscowosc_wys]'].disabled = false;
							document.order_form['dane_klient[kod_poczt_wys]'].disabled = false;
							document.order_form['dane_klient[kraj_wys]'].disabled = false;
				      }else if(chckbx[a].id=="wrapper_check") {
							document.getElementById('wrapper_table').style.display = 'block';
					}
				      
				  }
			};
			
			//place image in front of the checkbox
			chckbx[g].parentNode.insertBefore(img, chckbx[g]);
			
			//hide the checkbox
			chckbx[g].style.display='none';
		}
	}
}

$(document).ready(function(){
	initck();
});
