//global variables that can be used by ALL the functions on this page.
var globalCheckboxInputs = new Array();
var loaded = false;
var imgFalse = '../images/common/box_off.gif';
var imgTrue = '../images/common/box_on.gif';

function setImagesTo(offImage, onImage)
{
	imgFalse = offImage;
	imgTrue = onImage;
}

function replaceChecks() 
{
	//get all the input fields on the page
	var allInputs = document.getElementsByTagName('input');
	
	//cycle trough the input fields
	for (var i = 0; i < allInputs.length; i++) 
	{
		var currInput = allInputs[i];

		//check if the input is a checkbox
		if(currInput.getAttribute('type') == 'checkbox') 
		{
			//make sure this checkbox hasn't been already processed 
			if (document.getElementById('checkImage'+currInput.id) != null)
			{
				continue;
			}
			
			globalCheckboxInputs.push(currInput);
			
			//create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(currInput.checked) 
			{
				img.src = imgTrue;
			} 
			else 
			{
				img.src = imgFalse;
			}

			//set image ID and onclick action
			img.id = 'checkImage'+currInput.id//last;
			img.onclick = new Function('checkChange("'+currInput.id+'")');
			img.style.cursor='pointer';

			//place image in front of the checkbox
			currInput.parentNode.insertBefore(img, currInput);
			
			//hide the checkbox
			currInput.style.display='none';
		}
	}
	
	loaded = true;
}

function getCheckboxById(inputId)
{
	for (var i = 0; i < globalCheckboxInputs.length; i++)
	{
		if (globalCheckboxInputs[i].id == inputId)
		{
			return globalCheckboxInputs[i];
		}
	}
	
	return null;
}

//change the checkbox status and the replacement image
function checkChange(inputId) 
{
	var foundCheck = getCheckboxById(inputId);
	if (foundCheck != null)
	{
		if(foundCheck.checked) 
		{
			foundCheck.checked = '';
			document.getElementById('checkImage'+inputId).src = imgFalse;
			
			checkBoxOnChange(inputId, false)
		} 
		else 
		{
			foundCheck.checked = 'checked';
			document.getElementById('checkImage'+inputId).src = imgTrue;
			
			checkBoxOnChange(inputId, true)
		}
	}
}

function checkBoxChangeTo(inputId, newValue)
{
	var foundCheck = getCheckboxById(inputId);
	if (foundCheck != null)
	{
		if (loaded)
		{
			try
			{
				if (newValue)
				{
					foundCheck.checked = 'checked';
					document.getElementById('checkImage'+inputId).src = imgTrue;
				}
				else
				{
					foundCheck.checked = '';
					document.getElementById('checkImage'+inputId).src = imgFalse;
				}
			} catch (err) {}
		}
	}
}


/*------------------------------------------------------------------------------------*/

function checkBoxOnChange(inputIndex, newIsSelected)
{
	if (inputIndex == 'id_profilePicture')
	{
		document.submitform.hasProfilePicture.value = newIsSelected;
	}
	
	if (inputIndex == 'id_rememberMe')
	{
		var rememberSelectedInput = document.getElementById('id_rememberSelected');
		if (rememberSelectedInput != null)
		{
			if (newIsSelected)
			{
				rememberSelectedInput.value = 1;
			}
			else
			{
				rememberSelectedInput.value = 0;
			}
		}
	}
	
	if ((inputIndex == 'id_poll1') || (inputIndex == 'id_poll2') || (inputIndex == 'id_poll3'))
	{
		var pollValueInput = document.getElementById('id_pollValue');
		if (pollValueInput != null)
		{
			if (!newIsSelected)
			{
				pollValueInput.value = '';
				checkBoxChangeTo('id_poll1', false);
				checkBoxChangeTo('id_poll2', false);
				checkBoxChangeTo('id_poll3', false);
			}
			else if (inputIndex == 'id_poll1')
			{
				pollValueInput.value = 1;
				checkBoxChangeTo('id_poll2', false);
				checkBoxChangeTo('id_poll3', false);
			}
			else if (inputIndex == 'id_poll2')
			{
				pollValueInput.value = 2;
				checkBoxChangeTo('id_poll1', false);
				checkBoxChangeTo('id_poll3', false);
			}
			else if (inputIndex == 'id_poll3')
			{
				pollValueInput.value = 3;
				checkBoxChangeTo('id_poll1', false);
				checkBoxChangeTo('id_poll2', false);
			}
		}
	}
}