// formStyle.js, produced by Philip Howard, GamingHeadlines.co.uk
// This JavaScript is open source and freely available to all those who wish to use it.
// A link back to GamingHeadlines.co.uk would be appreciated!

function toggleCheckbox(cbId,cbKey,ffId)
{
	if (cbKey==0||cbKey==32){
		var cbFF = document.getElementById(ffId);

		var cbFFValue = cbFF.checked;

		if(cbId.className.indexOf("CheckboxChecked")<0)
		{
			var checkBoxType = cbId.className.replace("CheckboxUnchecked","");
			cbFF.checked=true;cbId.className="CheckboxChecked"+checkBoxType;
		}
		else
		{
			var checkBoxType = cbId.className.replace("CheckboxChecked","");
			cbFF.checked=false;cbId.className="CheckboxUnchecked"+checkBoxType;
		}
		$(cbFF).change()
		return false;
	}
}

function InitialiseCheckboxes()
{
	var inputFields = document.getElementsByTagName("span");
	var checkboxIndex = 0;
	for (var inputIndex=0;inputIndex<inputFields.length;inputIndex++)
	{
		if (inputFields[inputIndex].className.indexOf("cbStyled")==0)
		{
			var styleType = "";
			//if (inputFields[inputIndex].getAttribute("name")!=null){styleType=inputFields[inputIndex].getAttribute("name");}
			styleType=inputFields[inputIndex].className.replace("cbStyled ","");
			if(styleType=="cbStyled"){styleType="";};
			inputFields[inputIndex].className="Checkbox"+styleType;

			var inputCurrent = inputFields[inputIndex].getElementsByTagName("input").item(0);
			if(inputCurrent.getAttribute("type")=="checkbox")
			{
				inputCurrent.className = "InputHidden";
				inputCurrent.setAttribute("id","StyledCheckbox"+checkboxIndex);

				if(navigator.appName.indexOf("Internet Explorer")>0||navigator.userAgent.indexOf("Netscape")>0)
				{
					//Internet Explorer
					var inputHTML = inputFields[inputIndex].innerHTML;
					var styledHTML = "<a"//href=\"#\""
					styledHTML+=" tabindex=\""+inputIndex+"\"";
					//styledHTML+=" name=\""+inputCurrent.getAttribute("name")+"\""

					if(inputCurrent.hasAttribute){if(inputCurrent.hasAttribute("title")){styledHTML+=" title=\""+inputCurrent.getAttribute("title")+"\"";}}

					if (inputCurrent.checked) {styledHTML+=" class=\"CheckboxChecked"+styleType+"\""}
					else {styledHTML+=" class=\"CheckboxUnchecked"+styleType+"\""}

					styledHTML+=" onClick=\"toggleCheckbox(this,'','StyledCheckbox"+checkboxIndex+"');return false;\""
					styledHTML+=" onKeyPress=\"return toggleCheckbox(this,event.keyCode,'StyledCheckbox"+checkboxIndex+"');\""

					if(navigator.userAgent.indexOf("Netscape")>0) {styledHTML+="><img src=\"formStyle.gif\" /></a>"}
					else {styledHTML+="></a>"}

					inputFields[inputIndex].innerHTML = inputHTML+styledHTML;
					inputFields[inputIndex].className = "Radiobox"+styleType;
				}
				else
				{
					var styledCheckbox = document.createElement("a");
					styledCheckbox.setAttribute("href","#");

					if(inputCurrent.hasAttribute){if(inputCurrent.hasAttribute("title")){styledCheckbox.setAttribute("title",inputCurrent.getAttribute("title"));}}

					styledCheckbox.setAttribute("onClick","toggleCheckbox(this,'','StyledCheckbox"+checkboxIndex+"');return false;");
					styledCheckbox.setAttribute("onKeyPress","return toggleCheckbox(this,event.keyCode,'StyledCheckbox"+checkboxIndex+"');");

					if (inputCurrent.checked) {styledCheckbox.className="CheckboxChecked"+styleType;}
					else {styledCheckbox.className="CheckboxUnchecked"+styleType;}
					inputFields[inputIndex].appendChild(styledCheckbox);
				}

				checkboxIndex++;
			}
		}
	}
}

function toggleRadiobox(rbObj,rbKey,rbGroup,rbId)
{
	if (rbKey==0||rbKey==32){
		var inputFields = document.getElementsByTagName("a");
		for (var inputIndex=0;inputIndex<inputFields.length;inputIndex++)
		{
			if (inputFields[inputIndex].getAttribute("name")==rbGroup){

				if(inputFields[inputIndex].className.indexOf("RadioboxChecked")<0)
				{var RadioBoxType = inputFields[inputIndex].className.replace("RadioboxUnchecked","");}
				else
				{var RadioBoxType = inputFields[inputIndex].className.replace("RadioboxChecked","");}

				inputFields[inputIndex].className="RadioboxUnchecked"+RadioBoxType;
			}
		}
		var inputFields = document.getElementsByTagName("input");
		for (var inputIndex=0;inputIndex<inputFields.length;inputIndex++)
		{
			if (inputFields[inputIndex].getAttribute("name")==rbGroup)
			{
				if (inputFields[inputIndex].getAttribute("id")==rbId)
				{
					if(rbObj.className.indexOf("RadioboxChecked")<0)
					{var RadioBoxType = rbObj.className.replace("RadioboxUnchecked","");}
					else
					{var RadioBoxType = rbObj.className.replace("RadioboxChecked","");}

					inputFields[inputIndex].checked = true;rbObj.className="RadioboxChecked"+RadioBoxType;}
					else
					{inputFields[inputIndex].checked = false;}
			}
		}
		return false;
	}
}

function InitialiseRadioboxes()
{
	var inputFields = document.getElementsByTagName("span");
	var radioboxIndex = 0;

	for (var inputIndex=0;inputIndex<inputFields.length;inputIndex++)
	{
		if (inputFields[inputIndex].className.indexOf("rbStyled")==0)
		{
			var styleType = "";
			styleType=inputFields[inputIndex].className.replace("rbStyled ","");
			if(styleType=="rbStyled"){styleType="";};
			//if (inputFields[inputIndex].getAttribute("name")!=null){styleType=inputFields[inputIndex].getAttribute("name");}
			inputFields[inputIndex].className="Radiobox"+styleType;

			var inputCurrent = inputFields[inputIndex].getElementsByTagName("input").item(0);
			if(inputCurrent.getAttribute("type")=="radio")
			{
				//inputCurrent.setAttribute("class","InputHidden");
				inputCurrent.className = "InputHidden";
				inputCurrent.setAttribute("id","StyledRadiobox"+radioboxIndex);

				if(navigator.appName.indexOf("Internet Explorer")>0||navigator.userAgent.indexOf("Netscape")>0)
				{
					//Internet Explorer
					var inputHTML = inputFields[inputIndex].innerHTML;
					var styledHTML = "<a"//href=\"#\""
					styledHTML+=" tabindex=\"1"+inputIndex+"\"";

					styledHTML+=" name=\""+inputCurrent.getAttribute("name")+"\""

					if(inputCurrent.hasAttribute){if(inputCurrent.hasAttribute("title")){styledHTML+=" title=\""+inputCurrent.getAttribute("title")+"\"";}}

					if (inputCurrent.checked)
					{styledHTML+=" class=\"RadioboxChecked"+styleType+"\""}
					else
					{styledHTML+=" class=\"RadioboxUnchecked"+styleType+"\""}

					styledHTML+=" onClick=\"toggleRadiobox(this,'','"+inputCurrent.getAttribute("name")+"','StyledRadiobox"+radioboxIndex+"');return false;\""
					styledHTML+=" onKeyPress=\"return toggleRadiobox(this,event.keyCode,'"+inputCurrent.getAttribute("name")+"','StyledRadiobox"+radioboxIndex+"');\""

					if(navigator.userAgent.indexOf("Netscape")>0)
					{styledHTML+="><img src=\"formStyle.gif\" /></a>"}
					else
					{styledHTML+="></a>"}

					inputFields[inputIndex].innerHTML = inputHTML+styledHTML;
					inputFields[inputIndex].className = "Radiobox"+styleType;
				}
				else
				{
					//Firefox, Opera, Netscape
					var styledRadiobox = document.createElement("a");
					styledRadiobox.setAttribute("href","#");
					styledRadiobox.setAttribute("name",inputCurrent.getAttribute("name"));

					if(inputCurrent.hasAttribute){if(inputCurrent.hasAttribute("title")){styledRadiobox.setAttribute("title",inputCurrent.getAttribute("title"));}}

					styledRadiobox.setAttribute("onClick","toggleRadiobox(this,'','"+inputCurrent.getAttribute("name")+"','StyledRadiobox"+radioboxIndex+"');return false;");
					styledRadiobox.setAttribute("onKeyPress","return toggleRadiobox(this,event.keyCode,'"+inputCurrent.getAttribute("name")+"','StyledRadiobox"+radioboxIndex+"');");

					if (inputCurrent.checked)
					{styledRadiobox.className="RadioboxChecked"+styleType;}
					else
					{styledRadiobox.className="RadioboxUnchecked"+styleType;}

					inputFields[inputIndex].appendChild(styledRadiobox);
				}

				radioboxIndex++;
			}
		}
	}
}

function checkImages() {
	if (document.getElementById('formStyleTestImage')) {
		var x = document.getElementById('formStyleTestImage').offsetWidth;
		if (x == '1'||x == '7') {
			document.getElementById('formStyleTestImage').style.display='none';
			return true;
		}else{
			return false;
		}
	}
}

function preloadImages()
{
	img1 = new Image();img1.src = "CheckboxUnchecked.gif";
	img1 = new Image();img1.src = "CheckboxChecked.gif";

	img2 = new Image();img1.src = "CheckboxUncheckedA.gif";
	img2 = new Image();img1.src = "CheckboxCheckedA.gif";

	img3 = new Image();img1.src = "CheckboxUncheckedB.gif";
	img3 = new Image();img1.src = "CheckboxCheckedB.gif";

	img4 = new Image();img1.src = "CheckboxUncheckedC.gif";
	img4 = new Image();img1.src = "CheckboxCheckedC.gif";

	img5 = new Image();img1.src = "RadioboxUnchecked.gif";
	img5 = new Image();img1.src = "RadioboxChecked.gif";
}

function Initialise()
{
	if(checkImages()){preloadImages();InitialiseCheckboxes();InitialiseRadioboxes();}
}

window.onload = Initialise;