var checkboxHeight = "19";


var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				span[a].onmousedown = Custom.pushed;
				span[a].onmouseup = Custom.check;
				
				
				inputs[a].parentNode.parentNode.getElementsByTagName("input")[1].onkeyup = Custom.qty_update;
				document.onmouseup = Custom.clear;
				
				var divs = document.getElementsByTagName("h3");

				for(var i=0;i<divs.length;i++){
				
					var temp_price = document.createElement("div");
					temp_price.className = "temp_price";
					temp_price.id = "total_item_"+i;
					
				}
				
				document.getElementById("emag_form").appendChild(temp_price);
				
			}
		}
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	qty_update: function() {
		
		var product_node = this.parentNode.parentNode;
		
		var search_value = product_node.getElementsByTagName("div")[0].innerHTML;
			myForm=document.forms["order_form"];

			for(var i=0;i<myForm.length;i++){
			
				if(myForm.elements[i].type=="hidden"&&myForm.elements[i].value.search(search_value)!==-1){
					var search_element_id=myForm.elements[i].id;
				}
			}
			var search_element = document.getElementById(search_element_id);
			if (search_element != null)
			{
				document.getElementById("order_form").removeChild(search_element);
			}
		//===================================================	
		product_node.getElementsByTagName("span")[0].style.backgroundPosition = "0 -" + checkboxHeight + "px";
		product_node.getElementsByTagName("input")[2].checked = true;
		product_node.style.backgroundColor = "#e5f3fa";

		var inputs = document.getElementsByTagName("input");
			
		for(c = 0; c < inputs.length; c++) {
			if((inputs[c].type == "checkbox") && inputs[c].checked == true) {
				var form_input = document.createElement("input");
				form_input.type = "hidden";
				form_input.name = "prod_"+c;
				form_input.id = "prod_"+c;
				form_input.value = product_node.getElementsByTagName("a")[0].innerHTML +" | "+ product_node.getElementsByTagName("div")[0].innerHTML +" | "+ product_node.getElementsByTagName("input")[1].value;
			}
		}
		document.getElementById("order_form").appendChild(form_input);
		
		//===================================================
		
	},
	check: function() {
		
		var product_node = this.parentNode.parentNode;
		
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			product_node.style.backgroundColor = "#fff";
			
			product_node.getElementsByTagName("input")[1].value = "";
			
			var search_value = product_node.getElementsByTagName("a")[0].innerHTML +" | "+ product_node.getElementsByTagName("div")[0].innerHTML +" | "+ product_node.getElementsByTagName("input")[1].value;
			myForm=document.forms["order_form"];

			for(var i=0;i<myForm.length;i++){
			
				if(myForm.elements[i].type=="hidden"&&myForm.elements[i].value.search(search_value)!==-1){
					var rem_element_id=myForm.elements[i].id;
				}
			}
			var rem_element = document.getElementById(rem_element_id);
			
			document.getElementById("order_form").removeChild(rem_element);
			
			recalc();
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
			product_node.style.backgroundColor = "#e5f3fa";
			
						
			if (product_node.getElementsByTagName("input")[1].value=="")
			{
				product_node.getElementsByTagName("input")[1].value = "1";
			}
			
			
			var inputs = document.getElementsByTagName("input");
			
			for(a = 0; a < inputs.length; a++) {
				if((inputs[a].type == "checkbox") && inputs[a].checked == true) {
					var form_input = document.createElement("input");
					form_input.type = "hidden";
					form_input.name = "prod_"+a;
					form_input.id = "prod_"+a;
					form_input.value = product_node.getElementsByTagName("a")[0].innerHTML +" | "+ product_node.getElementsByTagName("div")[0].innerHTML +" | "+ product_node.getElementsByTagName("input")[1].value;
				}
			}
			document.getElementById("order_form").appendChild(form_input);
			recalc();
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	}
}
window.onload = Custom.init;
