var convertedRadios = new Array();
var convertedCheckboxes = new Array();

function preloadFormImages() {
	var preloader = document.createElement('img');
	document.write('<div id="preloader-holder"></div>');
	var holder = document.getElementById('preloader-holder');
	
	holder.style.position = "absolute";
	holder.style.left = "-100px";
	holder.style.top = "-100px";
	
	preloader.onload = function() {
		this.onload = function() {
			destroyPreloader(holder, this);
		}
		this.src = checkboxPathChecked;
	}
	preloader.src = radioPathChecked;
	holder.appendChild(preloader);
}

function destroyPreloader(b,c) {
	c.parentNode.removeChild(c);
	b.parentNode.removeChild(b);
}

function convertRadio(obj) {
	obj.style.display = "none";
	
	var imgSibling = document.createElement('img');
	imgSibling.src = radioPath;
	imgSibling.style.marginTop = "2px";
	imgSibling.style.marginRight = "3px";
	imgSibling.style.marginLeft = "3px";
	imgSibling.style.cursor = "pointer";
	imgSibling.onclick = function() { changeRadioState(obj.name, obj.id) };
	
	var parent = obj.parentNode;
	parent.appendChild(imgSibling);
	
	var i = convertedRadios.length;
	convertedRadios[i] = new Array();
	convertedRadios[i]['img'] = imgSibling;
	convertedRadios[i]['obj'] = obj;
	convertedRadios[i]['name'] = obj.name;
}

function changeRadioState(group, id) {
	for (var i=0; i<convertedRadios.length; i++) {
		if (convertedRadios[i]['name']==group && convertedRadios[i]['obj'].id!=id) {
			convertedRadios[i]['obj'].checked = false;
			convertedRadios[i]['img'].src = radioPath;
		}
		if (convertedRadios[i]['name']==group && convertedRadios[i]['obj'].id==id) {
			convertedRadios[i]['obj'].checked = true;
			convertedRadios[i]['img'].src = radioPathChecked;
		}
	}
}

function changeRadioStateByLabel(label) {
	obj = document.getElementById(label.htmlFor);
	changeRadioState(obj.name, obj.id);
}

function convertCheckbox(obj) {
	obj.style.display = "none";
	
	var imgSibling = document.createElement('img');
	imgSibling.src = checkboxPath;
	imgSibling.style.marginTop = "2px";
	imgSibling.style.marginRight = "3px";
	imgSibling.style.marginLeft = "3px";
	imgSibling.style.cursor = "pointer";
	imgSibling.onclick = function() { changeCheckboxState(imgSibling, obj); };
	
	var parent = obj.parentNode;
	parent.appendChild(imgSibling);

	convertedCheckboxes[obj.id] = new Array();
	convertedCheckboxes[obj.id]['img']=imgSibling;
	convertedCheckboxes[obj.id]['state']=false;
}

function changeCheckboxState(img, obj) {
	convertedCheckboxes[obj.id]['state'] = !convertedCheckboxes[obj.id]['state'];
	obj.checked = convertedCheckboxes[obj.id]['state'];
	if (obj.checked) img.src = checkboxPathChecked;
	else img.src = checkboxPath;
}

function changeCheckboxStateByLabel(label) {
	if (label.htmlFor!="") {
		label.chid =  label.htmlFor;
		label.htmlFor = "";
	}
	obj = document.getElementById(label.chid);
	changeCheckboxState(convertedCheckboxes[obj.id]['img'], obj);
}

function prepareInput(obj) {
	obj.style.color = inputNotActiveColor;
	obj.isActive = false;
	obj.value = "wpisz";
	obj.onfocus = obj.onblur = function() { checkInput(this); };
}

function checkInput(obj) {
	if (obj.value=="wpisz" && !obj.isActive) {
		obj.value = "";
		obj.isActive = true;
		obj.style.color = inputActiveColor;
	}
	else if (obj.value=="" && obj.isActive) {
		obj.style.color = inputNotActiveColor;
		obj.isActive = false;
		obj.value = "wpisz";
	}
}
