//global variables that can be used by ALL the function son this page. 
var inputs; 
var imgCheckboxFalse = '/images/mkt/checkbox_unchecked.gif'; 
var imgCheckboxTrue = '/images/mkt/checkbox_checked.gif'; 
var imgCheckboxFalseDisabled = '/images/mkt/checkbox_unchecked_disabled.gif'; 
var imgCheckboxTrueDisabled = '/images/mkt/checkbox_checked_disabled.gif'; 
var imgRadioFalse = '/images/mkt/radio_unchecked.gif'; 
var imgRadioTrue = '/images/mkt/radio_checked.gif'; 
var imgRadioFalseDisabled = '/images/mkt/radio_unchecked_disabled.gif'; 
var imgRadioTrueDisabled = '/images/mkt/radio_checked_disabled.gif'; 

//this function runs when the page is loaded, put all your other onload stuff in here too. 
function init() { 
    replaceChecks(); 
    replaceRadios();
} 

function replaceChecks() { 

    //get all the input fields on the page 
    inputs = document.getElementsByTagName('input'); 

    //cycle trough the input fields 
    for(var i=0; i < inputs.length; i++) { 

	//check if the input is a checkbox 
	if(inputs[i].getAttribute('type') == 'checkbox') { 

	    //create a new image 
	    var img = document.createElement('img'); 

	    //check if the checkbox is checked 
	    if(inputs[i].checked) { 
				if(inputs[i].disabled)
				{
					img.src = imgCheckboxTrueDisabled; 
				} else {
					img.src = imgCheckboxTrue; 
				}
	    } else { 
		if(inputs[i].disabled)
				{
					img.src = imgCheckboxFalseDisabled; 
				} else {
					img.src = imgCheckboxFalse; 
				}
	    } 

	    //set image ID and onclick action 
	    img.id = 'checkImage'+i; 
	    //set image 
	    if(!inputs[i].disabled) img.onclick = new Function('checkChange('+i+')'); 
	    //place image in front of the checkbox 
	    inputs[i].parentNode.insertBefore(img, inputs[i]); 

	    //hide the checkbox 
	    inputs[i].style.display='none'; 
	} 
    } 
} 

function replaceRadios() { 

    //get all the input fields on the page 
    inputs = document.getElementsByTagName('input'); 

    //cycle trough the input fields 
    for(var i=0; i < inputs.length; i++) { 

	//check if the input is a checkbox 
	if(inputs[i].getAttribute('type') == 'radio') { 

	    //create a new image 
	    var img = document.createElement('img'); 

	    //check if the checkbox is checked 
	    if(inputs[i].checked) { 
				if(inputs[i].disabled)
				{
					img.src = imgRadioTrueDisabled; 
				} else {
					img.src = imgRadioTrue; 
				}
	    } else { 
		if(inputs[i].disabled)
				{
					img.src = imgRadioFalseDisabled; 
				} else {
					img.src = imgRadioFalse; 
				}
	    } 

	    //set image ID and onclick action 
	    img.id = 'radioImage'+inputs[i].id; 

	    //set image click event if button not disabled
	    if(!inputs[i].disabled) img.onclick = new Function('radioChange('+i+')'); 
	    //place image in front of the checkbox 
	    inputs[i].parentNode.insertBefore(img, inputs[i]); 

	    //hide the checkbox 
	    inputs[i].style.display='none'; 
	} 
    } 
} 

//change the checkbox status and the replacement image 
function checkChange(i) { 
    if(inputs[i].checked) { 
	inputs[i].checked = ''; 
	document.getElementById('checkImage'+i).src=imgCheckboxFalse; 
	// This code dynamically resets the variable for ActionCode with multiple values based on what gets unchecked 
		if(inputs[i].value == 'Interest 1') {
			document.RegForm.actionCode1.value = '';
		}
		else if(inputs[i].value == 'Interest 2') {
			document.RegForm.actionCode2.value = '';
		}
		else if(inputs[i].value == 'Interest 3') {
			document.RegForm.actionCode3.value = '';
		}
		else if(inputs[i].value == 'Interest 4') {
			document.RegForm.actionCode4.value = '';
		}
		else if(inputs[i].value == 'Interest 5') {
			document.RegForm.actionCode5.value = '';
		}
		else if(inputs[i].value == 'Interest6') {
			document.RegForm.actionCode6.value = '';
		}
		else if(inputs[i].value == 'Interest 7') {
			document.RegForm.actionCode7.value = '';
		}
		else if(inputs[i].value == 'Interest 8') {
			document.RegForm.actionCode8.value = '';
		}
		else if(inputs[i].value == 'Interest 9') {
			document.RegForm.actionCode9.value = '';
		}
		else if(inputs[i].value == 'Interest 10') {
			document.RegForm.actionCode10.value = '';
		} 
		else if(inputs[i].value == 'Interest 11') {
			document.RegForm.actionCode11.value = '';
		} 
		else if(inputs[i].value == 'Interest 12') {
			document.RegForm.actionCode12.value = '';
		} 
		else if(inputs[i].value == 'Interest 13') {
			document.RegForm.actionCode13.value = '';
		} 
		else if(inputs[i].value == 'Interest 14') {
			document.RegForm.actionCode14.value = '';
		} 
		else if(inputs[i].value == 'Interest 15') {
			document.RegForm.actionCode15.value = '';
		} 
    } else { 
	inputs[i].checked = 'checked'; 
	document.getElementById('checkImage'+i).src=imgCheckboxTrue; 
	// This code dynamically sets the variable for ActionCode with multiple values based on what gets checked 
		if(inputs[i].value == 'Interest 1') {
			document.RegForm.actionCode1.value = document.RegForm.interest1code.value;
		}
		else if(inputs[i].value == 'Interest 2') {
			document.RegForm.actionCode2.value = document.RegForm.interest2code.value;
		}
		else if(inputs[i].value == 'Interest 3') {
			document.RegForm.actionCode3.value = document.RegForm.interest3code.value;
		}
		else if(inputs[i].value == 'Interest 4') {
			document.RegForm.actionCode4.value = document.RegForm.interest4code.value;
		}
		else if(inputs[i].value == 'Interest 5') {
			document.RegForm.actionCode5.value = document.RegForm.interest5code.value;
		}
		else if(inputs[i].value == 'Interest 6') {
			document.RegForm.actionCode6.value = document.RegForm.interest6code.value;
		}
		else if(inputs[i].value == 'Interest 7') {
			document.RegForm.actionCode7.value = document.RegForm.interest7code.value;
		}
		else if(inputs[i].value == 'Interest 8') {
			document.RegForm.actionCode8.value = document.RegForm.interest8code.value;
		}
		else if(inputs[i].value == 'Interest 9') {
			document.RegForm.actionCode9.value = document.RegForm.interest9code.value;
		}
		else if(inputs[i].value == 'Interest 10') {
			document.RegForm.actionCode10.value = document.RegForm.interest10code.value; 
		}
		else if(inputs[i].value == 'Interest 11') {
			document.RegForm.actionCode11.value = document.RegForm.interest11code.value; 
		}
		else if(inputs[i].value == 'Interest 12') {
			document.RegForm.actionCode12.value = document.RegForm.interest12code.value; 
		}
		else if(inputs[i].value == 'Interest 13') {
			document.RegForm.actionCode13.value = document.RegForm.interest13code.value; 
		}
		else if(inputs[i].value == 'Interest 14') {
			document.RegForm.actionCode14.value = document.RegForm.interest14code.value; 
		}
		else if(inputs[i].value == 'Interest 15') {
			document.RegForm.actionCode15.value = document.RegForm.interest15code.value; 
		}
		
    } 
	document.RegForm.ActionCode.value = document.RegForm.actionCode1.value + document.RegForm.actionCode2.value + document.RegForm.actionCode3.value + document.RegForm.actionCode4.value + document.RegForm.actionCode5.value + document.RegForm.actionCode6.value + document.RegForm.actionCode7.value + document.RegForm.actionCode8.value + document.RegForm.actionCode9.value + document.RegForm.actionCode10.value + document.RegForm.actionCode11.value + document.RegForm.actionCode12.value + document.RegForm.actionCode13.value + document.RegForm.actionCode14.value + document.RegForm.actionCode15.value;
	
	// Set this value so that you can make sure at least one event was selected when the user attempts to submit the form
	document.RegForm.Interest.value = document.RegForm.ActionCode.value;
	
	// The following is for error checking the above script with an alert window to show the variables being set
	//alert('ActionCode = ' + document.RegForm.ActionCode.value );
} 

//change the checkbox status and the replacement image of checked and all in same group
function radioChange(i) { 
	var radios=new Array();
	var tmpradios;

	// load all the inputs into tmp array
	tmpradios = document.getElementsByTagName('input'); 

	for(var j=0; j < tmpradios.length; j++) { 
		// Add only the radios in this group to the array
		if(tmpradios[j].getAttribute('name') == inputs[i].getAttribute('name'))
		{
			radios.push(tmpradios[j]);
		}
	}

    if(inputs[i].checked) { 
		// already checked so do nothing as radio does not uncheck like a checkbox
    } else { 
		// make all other group items unchecked
	for(var j=0; j < radios.length; j++) { 
			document.getElementById('radioImage'+radios[j].id).src=imgRadioFalse; 
			document.getElementById('radioImage'+radios[j].id).checked=''; 
		}
		// make the selected item checked
	inputs[i].checked = 'checked'; 
	document.getElementById('radioImage'+inputs[i].id).src = imgRadioTrue;       
    } 
} 

window.onload = init;
