// 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,checkedtrue,checkedfalse)
	{

	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("Checkbox","");
	                                 cbFF.checked=checkedtrue;cbId.className="CheckboxChecked"+checkBoxType;}
	                         else
	                         {
	                                 var checkBoxType = cbId.className.replace("CheckboxChecked","");
	                                 cbFF.checked=checkedfalse;cbId.className="Checkbox"+checkBoxType;}

                         /*
                         if (cbId.className.indexOf("Checkbox")<0)
	                         {
	                                 var checkBoxType = cbId.className.replace("CheckboxChecked","");
	                                 cbFF.checked=false;cbId.className="Checkbox"+checkBoxType;}
	                         else
	                         {
	                                 var checkBoxType = cbId.className.replace("Checkbox","");
	                                 cbFF.checked=true;cbId.className="CheckboxChecked"+checkBoxType;}

                                          */

	return false;
		}
	}

	function InitialiseCheckboxes()
	{
		var checkedtrue;
		var checkedfalse;
		var inputFields = document.getElementsByTagName("span");
		var checkboxIndex = 0;
		for (var inputIndex=0;inputIndex<inputFields.length;inputIndex++)
			{
				if (inputFields[inputIndex].className=="cbStyled" || inputFields[inputIndex].className=="cbStyled2" )
					{
                                         	if (inputFields[inputIndex].className=="cbStyled") {
                                                         checkedtrue = true;
							checkedfalse = false;
                                                 }
                                                 else if (inputFields[inputIndex].className=="cbStyled2") {
                                                 	checkedtrue = false;
							checkedfalse = true;
                                                 }
						var styleType = "";
						if (inputFields[inputIndex].getAttribute("name")!=null){styleType=inputFields[inputIndex].getAttribute("name");}

						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)
							{
								//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 (inputFields[inputIndex].className=="cbStyled") {
                                                                 	if (inputCurrent.checked)
                                                                 		{styledHTML+=" class=\"CheckboxChecked"+styleType+"\""}
									else 	{styledHTML+=" class=\"Checkbox"+styleType+"\""}
                                                                 }
                                                                 else if (inputFields[inputIndex].className=="cbStyled2")
                                                                 	if (inputCurrent.checked)
                                                                 		{styledHTML+=" class=\"Checkbox"+styleType+"\""}
									else 	{styledHTML+=" class=\"CheckboxChecked"+styleType+"\""}
                                                                 }
                                                                 */

                                                                 if (inputCurrent.checked) {
                                                                        	if (inputFields[inputIndex].className=="cbStyled")
								       	{styledHTML+=" class=\"CheckboxChecked"+styleType+"\""}
								       	else if (inputFields[inputIndex].className=="cbStyled2")
								       	{styledHTML+=" class=\"Checkbox"+styleType+"\""}
                                                                 }
                                                                 else {
                                                                       	if (inputFields[inputIndex].className=="cbStyled")
								       	{styledHTML+=" class=\"Checkbox"+styleType+"\""}
								       	else if (inputFields[inputIndex].className=="cbStyled2")
								       	{styledHTML+=" class=\"CheckboxChecked"+styleType+"\""}
                                                                 }


                                                                 /*
								if (inputCurrent.checked)
									{styledHTML+=" class=\"CheckboxChecked"+styleType+"\""}
									else
									{styledHTML+=" class=\"Checkbox"+styleType+"\""}
                                                                 */

								styledHTML+=" onClick=\"toggleCheckbox(this,'','StyledCheckbox"+checkboxIndex+"','" + checkedtrue + "','" + checkedfalse + "');return false;\""
								styledHTML+=" onKeyPress=\"return toggleCheckbox(this,event.keyCode,'StyledCheckbox"+checkboxIndex+"','" + checkedtrue + "','" + checkedfalse + "');\""

								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+"','"+checkedtrue+"','"+checkedfalse+"');return false;");
								styledCheckbox.setAttribute("onKeyPress","return toggleCheckbox(this,event.keyCode,'StyledCheckbox"+checkboxIndex+"','"+checkedtrue+"','"+checkedfalse+"');");


                                                                 if (inputCurrent.checked) {
                                                                        	if (inputFields[inputIndex].className=="cbStyled")
								       	{styledCheckbox.className="CheckboxChecked"+styleType;}
								       	else if (inputFields[inputIndex].className=="cbStyled2")
								       	{styledCheckbox.className="Checkbox"+styleType;}
                                                                 }
                                                                 else {
                                                                       	if (inputFields[inputIndex].className=="cbStyled")
								       	{styledCheckbox.className="Checkbox"+styleType;}
								       	else if (inputFields[inputIndex].className=="cbStyled2")
								       	{styledCheckbox.className="CheckboxChecked"+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("Radiobox","");}
									else
									{var RadioBoxType = inputFields[inputIndex].className.replace("RadioboxChecked","");}

					inputFields[inputIndex].className="Radiobox"+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("Radiobox","");}
									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=="rbStyled")
					{
						var styleType = "";
						if (inputFields[inputIndex].getAttribute("name")!=null){styleType=inputFields[inputIndex].getAttribute("name");}

						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)
							{
								//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=\"Radiobox"+styleType+"\""}

								styledHTML+=" onClick=\"toggleRadiobox(this,'','"+inputCurrent.getAttribute("name")+"','StyledRadiobox"+radioboxIndex+"');return false;\""
								styledHTML+=" onKeyPress=\"return toggleRadiobox(this,event.keyCode,'"+inputCurrent.getAttribute("name")+"','StyledRadiobox"+radioboxIndex+"');\""

								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="Radiobox"+styleType;}

								inputFields[inputIndex].appendChild(styledRadiobox);
							}

							radioboxIndex++;
						}
					}
			}
	}

	function checkImages() {
	  if (document.getElementById) {
		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 = "./images/CheckboxUnchecked.gif";
		img1 = new Image();img1.src = "./images/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()
		{
	                // make sure the browser has images turned on. If they are, so we will
	                // fire when this small test image loads. otherwise, the user will get the hard-coded checkboxes
	                formStyleTestImage = document.body.appendChild(document.createElement("img"));

                         // MSIE will cache the test image, causing it to not fire the onload event the next time the
	                // page is hit. The parameter on the end will prevent this.
	                //formStyleTestImage.src = "formStyle.gif?" + new Date().valueOf();
	                formStyleTestImage.src = "./images/formStyle.gif";
	                formStyleTestImage.id = "formStyleTestImage";
	                //formStyleTestImage.onload = checkImages();

			if(checkImages()){preloadImages();InitialiseCheckboxes();InitialiseRadioboxes();}
		}
/*
	window.onload = Initialise;
*/

function checkAll() {
	var boxes = document.getElementsByName('attachment[]');

         var checkall = false;
         for (var i=0; i<boxes.length; i++) {
         	if (i == 0 && boxes[0].checked == false)
                 	checkall = true;
                 if (checkall)
                 	boxes[i].checked = true;
                 else
                 	boxes[i].checked = false;
         }
}


