/**
 * This function sets the loginId to the same as email address if the login id was 
 * not filled out.
 */
function setLoginId() 
{
	if(document.registrationForm.loginId.value=="")
	{
		document.registrationForm.loginId.value = document.registrationForm.email.value;
	}
}

//Holds a collection of values- these can be anything
var values = new Array();
var canSubmit = false;

/**
 * Adds a specified value to the values collection, as well as managing the buttons disabled status
 */
function selectValueClicked(value)
{
	if (document.getElementById("selectVal_" + value).checked){
		addValueToSet(value);
	}
	else{
		removeValueFromSet(value);
	}
	//button permission logic
	if (values.length > 0){
		if (document.getElementById("hasNotSelected").value == "false"){
			document.getElementById("startTagging").disabled = false;
			canSubmit = true;
		}
		canSubmit = true;
		//document.getElementById("startTagging").disabled = false;
		//document.getElementById("continueSelecting").disabled = false;	
	}
	else{

		if (document.getElementById("hasNotSelected").value == "true"){
			//document.getElementById("startTagging").disabled = true;
		}
		canSubmit = false;
		//document.getElementById("continueSelecting").disabled = true;
	}
}


/**
 * Allows a user to click an image and check the corresponding select control
 */
function checkForImage(value)
{
	result = document.getElementById("selectVal_" + value).checked;
	document.getElementById("selectVal_" + value).checked = !result;
	selectValueClicked(value);
}

/**
 * Adds a value to the values colleciton
 */ 
function addValueToSet(value)
{
	if (findValueIndex(value) == -1){
		values.push(value);
	}
	else{
		throw("value already exists within the current values");
	}
}

/**
 * Removes a specified value from the values collection
 */ 
function removeValueFromSet(value)
{
	index = findValueIndex(value);
	if (index == -1){
		throw("Could not find value in the current values");
	}
	else{
		values.splice(index, 1);
	}
}


/**
 * Finds the index of a given value in the values array. Returns the index integer if found, -1 otherwise.
 */ 
function findValueIndex(value)
{
	for (i=0; i < values.length; i++){
		if (value == values[i]){
			return i;
		}
	}
	return -1;
}

/**
 * Stores a string representation of the values in a hidden field and sets the action of the form based on the continueTagging property
 */
function buildValuesAndSetAction(continueTagging, controller)
{
	output = new String();
	for (i=0; i < values.length; i++){
		output += values[i];
		if (i + 1 < values.length){
			output += "_";
		}
	}
	document.getElementById("values").value = output;
	
	if (continueTagging){
		document.getElementById("task").value = controller + "_viewHubPage";
	}
	else {
		document.getElementById("task").value = controller + "_createImageSet";
	}
//james's dirty, dirty IE 7 hack
	if(canSubmit){
		document.getElementById("buildValues").submit();
	}

}

/**
 * Confirms if a user wants to refresh the current page, displaying a confirm message and then setting the task param with the supplied target value
 */
function confirmRefresh(msg, controller)
{
        result = confirm(msg);
        if (result == true){
                document.getElementById("task").value = controller + "_startHubPage"; 
                document.getElementById("buildValues").submit();
        }
}

