var inputs;
var imgFalse = '/sites/default/themes/pf/images/tickbox-off.gif';
var imgTrue = '/sites/default/themes/pf/images/tickbox-on.gif';

//this function runs when the page is loaded, put all your other onload stuff in here too.
function init() {
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	if(!IE6) {
		replaceChecks();
	}
}

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) {
				img.src = imgTrue;
			} else {
				img.src = imgFalse;
			}

			//set image ID and onclick action
			img.id = 'checkImage'+i;
			img.className = 'checkbox';
			//set image 
			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';
		}
	}
}

//change the checkbox status and the replacement image
function checkChange(i) {
	if(inputs[i].checked) {
		if(i == 13) {
			for(i=3; i<=12; i++) {
				inputs[i].checked = '';
				document.getElementById('checkImage'+i).src=imgFalse;
			}
		} else if(i == 17) {
			for(i=14; i<=16; i++) {
				inputs[i].checked = '';
				document.getElementById('checkImage'+i).src=imgFalse;
			}
		}
		inputs[i].checked = '';
		document.getElementById('checkImage'+i).src=imgFalse;
			
		// Uncheck 'select all'
		if(i >= 3 && i <= 12) {
			inputs[13].checked = '';
			document.getElementById('checkImage13').src=imgFalse;
		}
		if(i >= 14 && i <= 16) {
			inputs[17].checked = '';
			document.getElementById('checkImage17').src=imgFalse;
		}
		
		
	} else {
		if(i == 13) {
			for(i=3; i<=12; i++) {
				inputs[i].checked = 'checked';
				document.getElementById('checkImage'+i).src=imgTrue;
			}
		} else if(i == 17) {
			for(i=14; i<=16; i++) {
				inputs[i].checked = 'checked';
				document.getElementById('checkImage'+i).src=imgTrue;
			}
		}
		inputs[i].checked = 'checked';
		document.getElementById('checkImage'+i).src=imgTrue;
		
	}
}


			/* Check submitted */
			function CheckMultiple1(frm, name) {
				for (var i=0; i < frm.length; i++)
				{
					fldObj = frm.elements[i];
					fldId = fldObj.id;
					if (fldId) {
						var fieldnamecheck=fldObj.id.indexOf(name);
						if (fieldnamecheck != -1) {
							if (fldObj.checked) {
								return true;
							}
						}
					}
				}
				return false;
			}
		function CheckForm1(f) {
			// Validate email
			var email_re = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
			if (!email_re.test(f.email.value)) {
				alert("Please enter your email address.");
				f.email.focus();
				return false;
			}
			
			// Intrests
			var intrests = new Array('CustomFields[12_1]_Film VFX',
								 'CustomFields[12_1]_Film Post',
								 'CustomFields[12_1]_Commercials',
								 'CustomFields[12_1]_Broadcast',
								 'CustomFields[12_1]_Music Video',
								 'CustomFields[12_1]_Corporate',
								 'CustomFields[12_1]_Archive',
								 'CustomFields[12_1]_Technologyy',
								 'CustomFields[12_1]_Digital',
								 'CustomFields[12_1]_Investor');
			var test=false;
			for(var x=0; x < intrests.length; x++) {
				if(document.getElementById(intrests[x]).checked) {
					test = true;
				}
			}
			if(!test) {
				alert('Please select at least one interest.');
				return false;
			}
			
			// Territories
			var territories = new Array('CustomFields[15_1]_North America',
								 'CustomFields[15_1]_UK',
								 'CustomFields[15_1]_India');
			var test=false;
			for(var x=0; x < territories.length; x++) {
				if(document.getElementById(territories[x]).checked) {
					test = true;
				}
			}
			if(!test) {
				alert('Please select at least one territory.');
				return false;
			}
			
			
			// T&Cs
			if(!document.getElementById('CustomFields[14_1]_Yes').checked) {
				alert('Please agree to the terms and conditions.');
				return false;
			}
			
			
			return true;
			
		}
		

window.onload = init;