/*********************** checkbox ***************************/
function ctoggle(box)
{
	if(box != undefined)
		box.checked = !box.checked;
}

function crestyle(cspan)
{
	if(cspan.childNodes[0].checked)
	{
		cspan.style.backgroundImage = "url('https://www.meinnotizbuch.de/out/meinnotizbuch/src/bg/bg-checkbox-checked.png')";
	}
	else
	{
		cspan.style.backgroundImage = "url('https://www.meinnotizbuch.de/out/meinnotizbuch/src/bg/bg-checkbox.png')";
	}
}

function setCheckBoxTo(cspan, cbool)
{
	cspan.childNodes[0].checked = cbool;
}

function togglecheckbox(cspan)
{
	ctoggle(cspan.childNodes[0]);
	crestyle(cspan);
}

function ctogglethis()
{
	var thespan = this.className == "checkbox" ? this : event.srcElement;
    // alert(this.className + "\n" + event.srcElement.className + "\n" + thespan.className);
	ctoggle(thespan.childNodes[0]);
	crestyle(thespan);

	var i=0;

	if(thespan.id == "setallto")
	{
		for(i=0;i<checkSpans.length - 1;i++)
		{
			setCheckBoxTo(checkSpans[i], thespan.childNodes[0].checked)
			crestyle(checkSpans[i]);
		}
	}

	// Shipping Adress != Delivery Adress
	if(thespan.id == "changeShippingAdress")
	{
		var form = document.getElementById("shippingAddress");
		if(thespan.childNodes[0].checked == false)
			form.style.display = "block";
		else
			form.style.display = "none";
	}
}

/********************** radiobutton *************************/

function rtoggle(button)
{
	// debug(button + ", " + button.checked);
	button.checked = true;
}

function rrestyle(rspan)
{
	if(rspan.childNodes[1].checked == true)
	{
		rspan.style.backgroundImage = 'url(/out/meinnotizbuch/src/bg/bg-checkbox-checked.png)';
	}
	else
	{
		rspan.style.backgroundImage = 'url(/out/meinnotizbuch/src/bg/bg-checkbox.png)';
	}
}

function display(rspan, deflist)
{

	if(rspan.childNodes[1].checked == true)
	{
		deflist.style.display = 'block';
	}
	else
	{
		deflist.style.display = 'none';
	}
}

function setRadioButtonTo(rspan, rbool)
{
	rspan.childNodes[0].checked = rbool;
}

function rtoggleall()
{
	var thespan = this.className == "checkbox" ? this : event.srcElement;
	var i=0;
	// rtoggle(thespan.childNodes[1]);
	// thespan.childNodes[1].checked = true;

	// rtoggle(thespan.childNodes[1]);
	for(i=0;i<radioSpans.length;i++)
	{
		if(radioSpans[i].childNodes[1].id == thespan.childNodes[1].id)
		{
			// sdebug(radioSpans[i].className);
			// document.getElementById(radioSpans[i].className.split(" ")[1]).checked = true;

			radioSpans[i].childNodes[1].checked = true;
		}
		else
		{
			// document.getElementById(radioSpans[i].className.split(" ")[1]).checked = false;
		 	radioSpans[i].childNodes[1].checked = false;
	 	}
	}

	/*selectPayment(thespan.childNodes[1].id);*/

	for(i=0;i<radioSpans.length;i++)
	{
		rrestyle(radioSpans[i]);
		display(radioSpans[i], defLists[i]);
	}


}

/************************ misc ******************************/
function getdeflists()
{
	defLists = document.getElementsByTagName("dd");
}

function initcheckboxes()
{
	checkSpans = Array();
	radioSpans = Array();
	//var spans = document.getElementsByTagName("span");
	var spans = $("span");

	//alert(spans.length);

	var i=0;
	var j=0;

	for(i=0;i<spans.length;i++)
	{
		if(spans[i].className == "checkbox")
		{
			try
			{
				if(spans[i].childNodes[0].type == "checkbox")
				{
					checkSpans.push(spans[i]);

					if( !spans[i].addEventListener && !spans[i].attachEvent )
						spans[i].onclick = ctogglethis;
					else if(spans[i].addEventListener)
						spans[i].addEventListener("click",ctogglethis, true);
					else if(spans[i].attachEvent)
						spans[i].attachEvent("onclick", ctogglethis);

					crestyle(spans[i]);
				}


				/*if(spans[i].childNodes[1].type == "radio")
				{
					radioSpans.push(spans[i]);
					try			{	spans[i].addEventListener("click", rtoggleall, true);	}
					catch(e)	{	spans[i].attachEvent("onclick", rtoggleall);			}

					if(spans[i].childNodes[1].checked)	//(j++ == 1)
					{
						spans[i].childNodes[1].checked = true;
					}
					else
					{
						spans[i].childNodes[1].checked = false;
					}
					rrestyle(spans[i]);
					display(defLists[j]);
				}*/
			}
			catch(e)
			{
			/*
				checkSpans.push(spans[i]);
				spans[i].addEventListener("click",ctogglethis, true);
				crestyle(spans[i]);
			*/
			}


		}

	}

}

function debug(astring)
{
	var myconsole = document.getElementById("myconsole");
	if(!myconsole)
	{
		myconsole = document.createElement("div");
		with(myconsole.style)
		{
			padding = "5px";
			fontSize = "12px";
			height = "200px";
			width = "100%";
			position = "fixed";
			zIndex = 50000;
			bottom = "0px";
			left = "0px";
			backgroundColor = "#abcdef";
			color = "#543210;"
			overflow = "scroll";

		}
		myconsole.id = "myconsole";

		document.body.appendChild(myconsole);
	}

	var msg = document.createElement("p");
	var themsg = document.createTextNode(astring);
	msg.appendChild(themsg);
	myconsole.appendChild(msg);
	myconsole.appendChild(document.createElement("hr"));
}


/********************************
 *** globale Variablen        ***
 ********************************/
var checkSpans;
var radioSpans;
var defLists;

/********************************
 *** main routine             ***
 ********************************/
getdeflists();
initcheckboxes();

try
{
	if(window.addEventListener)
		window.addEventListener('DOMMouseScroll', wheel, false);
	else if( window.attachEvent )
		window.attachEvent("DOMMouseScroll", wheel);
	else
		window.onmousewheel = wheel;
}
catch(e)
{
	window.onmousewheel = wheel;
}

function wheel(event)
{
	if(event.axis == 1)
		event.preventDefault();
}

