/*#############################################################
Name: Niceforms
Version: 1.0
Author: Lucian Slatineanu
URL: http://www.badboy.ro/

Feel free to use and modify but please provide credits.
#############################################################*/

//Global Variables
var niceforms = document.getElementsByTagName('form'); var inputs = new Array(); var labels = new Array(); var checkboxes = new Array(); var checkboxLabels = new Array(); var isMac = new RegExp('(^|)'+'Apple'+'(|$)'); var texts = new Array();


//Initialization function - if you have any other 'onload' functions, add them here
function init() {
	if(!document.getElementById) {return false;}
	getElements();
	separateElements();
	replaceCheckboxes();
	smartTexts();
	if(document.getElementById('firstname')) {
		document.getElementById('firstname').focus();
	}
}


//getting all the required elements
function getElements() {
	var re = new RegExp('(^| )'+'niceform'+'( |$)');
	for (var nf = 0; nf < document.getElementsByTagName('form').length; nf++) {
		if(re.test(niceforms[nf].className)) {
			for(var nfi = 0; nfi < document.forms[nf].getElementsByTagName('input').length; nfi++) {inputs.push(document.forms[nf].getElementsByTagName('input')[nfi]);}
			for(var nfl = 0; nfl < document.forms[nf].getElementsByTagName('label').length; nfl++) {labels.push(document.forms[nf].getElementsByTagName('label')[nfl]);}
		}
	}
}
//separating all the elements in their respective arrays
function separateElements() {
	var r = 0; var c = 0; var t = 0; var rl = 0; var cl = 0; var tl = 0; var b = 0;
	for (var q = 0; q < inputs.length; q++) {
		if(inputs[q].type == 'checkbox') {
			checkboxes[c] = inputs[q]; ++c;
			for(var w = 0; w < labels.length; w++) {if(labels[w].htmlFor == inputs[q].id) {if(inputs[q].checked) {labels[w].className = "chosen";} checkboxLabels[cl] = labels[w]; ++cl;}}
		}
		if((inputs[q].type == "text") || (inputs[q].type == "password")) {texts[t] = inputs[q]; ++t;}
	}
}
function replaceCheckboxes() {
	for (var q = 0; q < checkboxes.length; q++) {
		//move checkboxes out of the way
		checkboxes[q].className = "outtaHere";
		//create div
		var checkboxArea = document.createElement('div');
		if(checkboxes[q].checked) {checkboxArea.className = "checkboxAreaChecked";} else {checkboxArea.className = "checkboxArea";}
		checkboxArea.style.left = findPosX(checkboxes[q]) + 'px';
		checkboxArea.style.top = findPosY(checkboxes[q]) + 'px';
		checkboxArea.style.margin = "1px";
		checkboxArea.id = "myCheckbox" + q;
		//insert div
		checkboxes[q].parentNode.insertBefore(checkboxArea, checkboxes[q]);
		//asign actions
		checkboxArea.onclick = new Function('checkCheckbox('+q+')');
	}
	return true;
}
function checkCheckbox(who) {
	var cls = checkboxes[who].checked;
	if(who == 4) {
		for(i=0;i<4;i++) {
				if(checkboxes[i].checked == cls) { checkCheckbox(i); }
			}	
	}
	var what = document.getElementById('myCheckbox'+who);
	if(cls == false) {
		what.className = "checkboxAreaChecked";
		checkboxes[who].checked = true;
		return;
	}
	if(cls == true) {
		what.className = "checkboxArea";
		checkboxes[who].checked = false;
		return;
	}
}
function smartTexts() {
	for(var q = 0; q < texts.length; q++) {
		texts[q].onfocus = function() {
			if(this.className == "small" || this.className == "") {
				this.className += 'focused';
			}else{
				this.className += ' focused';
			}
		}
		texts[q].onblur = function() {
			if(this.className == "smallfocused" || this.className == "focused") {
				this.className = this.className.replace(/focused/, '');
			}else{
				this.className = this.className.replace(/ focused/, '');
			}
		}
	}
}
//Useful functions
function findPosY(obj) {
	var posTop = 0;
	while (obj.offsetParent) {posTop += obj.offsetTop; obj = obj.offsetParent;}
	return posTop;
}
function findPosX(obj) {
	var posLeft = 0;
	while (obj.offsetParent) {posLeft += obj.offsetLeft; obj = obj.offsetParent;}
	return posLeft;
}

window.onload = init;