
function setFormReason(prefix, reason, subreason) {
	// Set form's selected reason and subreason.
	var reasonElement = _gel(prefix + 'reason');
	if (reasonElement) {
		reasonElement.value = reason;
	}
	var subreasonElement = _gel(prefix + 'sub_reason');
	if (subreasonElement) {
		subreasonElement.value = subreason;
	}
}

function onFlagVideoCheckboxClicked() {
	onFlagCheckboxClicked('', 'selectedFlagReason');
}

function onFlagAnnoCheckboxClicked() {
	onFlagCheckboxClicked('anno_', 'selectedAnnoFlagReason');
}

function onFlagCheckboxClicked(annoTag, dropDownId) {
	var selCheckbox = _gel('flag_' + annoTag + 'checkbox');
	if (!selCheckbox) {
		// Error.
		return;
	}
	if (selCheckbox.checked) {
		// Nothing to do.
		return;
	}
	
	// User has unchecked the box.
	var dropDown = _gel(dropDownId);
	if (dropDown) {
		dropDown.innerHTML = '- ' + MSG_FlagDefault + ' -';
		clearMenuSelectionStyles(dropDown);
	}
	closeAllFlagMoreInfo('flag_' + annoTag);	
}

function flagReasonSelection(mainMenuId, subMenuId, newParentSelId, newChildSelId, reason, subreason) {
	// Set the corresponding checkbox.
	var selCheckbox = _gel('flag_checkbox');
	if (selCheckbox) {
		selCheckbox.checked = true;
	}

	// Set the menu and submenu items correctly.
	flagMenuSelection('selectedFlagReason', mainMenuId, subMenuId, newParentSelId, newChildSelId);

	// Close additional elements.
	closeAllFlagMoreInfo('flag_');

	// Set form's selected reason and subreason.
	setFormReason('flag_', reason, subreason);
}

function flagAnnoReasonSelection(mainMenuId, subMenuId, newParentSelId, newChildSelId, reason, subreason) {
	// Set the corresponding checkbox.
	var selCheckbox = _gel('flag_anno_checkbox');
	if (selCheckbox) {
		selCheckbox.checked = true;
	}

	// Set the menu and submenu items correctly.
	flagMenuSelection('selectedAnnoFlagReason', mainMenuId, subMenuId, newParentSelId, newChildSelId);

	// Close additional elements.
	closeAllFlagMoreInfo('flag_anno_');

	// Set form's selected reason and subreason.
	setFormReason('flag_anno_', reason, subreason);
}

function clearMenuSelectionStyles(dropDown) {
	if (dropDown) {
		// Clear styles previously selected.
		var oldParentSelId = dropDown.currentParentSelId;
		var oldChildSelId = dropDown.currentChildSelId;
		if (oldParentSelId) {
			clearSelectionStyles(_gel(oldParentSelId));
		}
		if (oldChildSelId) {
			clearSelectionStyles(_gel(oldChildSelId));
		}
	}
}

function flagMenuSelection(dropDownId, mainMenuId, subMenuId, newParentSelId, newChildSelId) {
	var dropDown = _gel(dropDownId);
	if (!dropDown) {
		// Error.
		return;
	}

	// Clear styles previously selected.
	clearMenuSelectionStyles(dropDown);

	// Deal with new selections
	var newParentElement = null;
	var newChildElement = null;

	// Highlight background of new selection.
	if (newParentSelId) {
		newParentElement = _gel(newParentSelId);
		setSelectionStyles(newParentElement);
	}
	if (newChildSelId) {
		newChildElement = _gel(newChildSelId);
		setSelectionStyles(newChildElement);
	}

	// User's selection is the child element if exists. Otherwise it's the
	// parent element.
	var newSelElement = null;
	if (newChildElement) {
		newSelElement = newChildElement;
	} else {
		if (newParentSelId) {
			newSelElement = newParentElement;
		}
	}

	// Reflect the selection in drop down text.
	if (dropDown) {
		if (newSelElement) {
			dropDown.innerHTML = newSelElement.innerHTML;
		}
		dropDown.currentParentSelId = newParentSelId;
		dropDown.currentChildSelId = newChildSelId;
	}

	// Hide the menus.
	setVisible(subMenuId, false);
	setVisible(mainMenuId, false);
}


function closeAllFlagMoreInfo(prefix) {
	var divs = ['MoreInfo1', 'MoreInfo2', 'MoreInfo3', 'MoreInfo4', 'MoreInfo5', 'MoreInfo6', 'Error'];
	for (var i = 0; i < divs.length; i++) {
		var theDiv = _gel(prefix + divs[i]);
		if (theDiv) {
			theDiv.style.display ='none';
		}
	}
}

function resetFlagFormValues(elForm) {
	var formElement = elForm;
	if (formElement) {
		formElement.flag_reason.value = '';
		formElement.flag_sub_reason.value = '';
		formElement.flag_t_secs.value = '';
		formElement.flag_t_mins.value = '';
		formElement.flag_desc.value = '';
		formElement.flag_protected_group.value = '';
	}
}

function resetFlagAnnoFormValues(elForm) {
	var formElement = elForm;
	if (formElement) {
		formElement.flag_anno_reason.value = '';
		formElement.flag_anno_sub_reason.value = '';
		formElement.flag_anno_t_secs.value = '';
		formElement.flag_anno_t_mins.value = '';
		formElement.flag_anno_desc.value = '';
		formElement.flag_anno_protected_group.value = '';
	}
}

function isHcHv(reason, subreason) {
	if ((reason == 'R') &&
		(subreason == '10')) {
		return true;
	}
	return false;	
}

function isVcAf(reason, subreason) {
	if ((reason == 'G') &&
		(subreason == '5')) {
		return true;
	}
	return false;	
}

function isScSu(reason, subreason) {
	if ((reason == 'P') &&
		(subreason == '3')) {
		return true;
	}
	return false;	
}

function toggleReportConcernResult(selReason, selSubReason, selAnnoReason, selAnnoSubReason) {
	// Decide how to toggle display after post
	var report = '';
	if (selReason != '') {
		if (isHcHv(selReason, selSubReason)) {
			report = 'reportConcernResult3';
		} else if (isVcAf(selReason, selSubReason) ||
             isScSu(selReason, selSubReason)) {
			report = 'reportConcernResult2';
		} else {
			report = 'reportConcernResult1';
		}
	} else {
		if (isHcHv(selAnnoReason, selAnnoSubReason)) {
			report = 'reportConcernResult3';
		} else if (isVcAf(selAnnoReason, selAnnoSubReason) ||
             isScSu(selAnnoReason, selAnnoSubReason)) {
			report = 'reportConcernResult2';
		} else {
			report = 'reportConcernResult1';
		}
	}
	toggleDisplay(report);
}

function setExtraFlaggingData(annoTag, selReason, selSubReason) {
	var flagTMins = _gel('flag' + annoTag + '_t_mins');
	var tMins = _gel('t_mins' + annoTag);
	if (flagTMins && tMins) { 
		flagTMins.value = tMins.value;
	}
	var flagTSecs = _gel('flag' + annoTag + '_t_secs');
	var tSecs = _gel('t_secs' + annoTag);
	if (flagTSecs && tSecs) { 
		flagTSecs.value = tSecs.value;
	}
	var flagDesc = _gel('flag' + annoTag + '_desc');
	var desc = _gel('desc' + annoTag);
	if (flagDesc && tSecs) { 
		flagDesc.value = desc.value;
	}
	var flagProtectedGroup = _gel('flag' + annoTag + '_protected_group');
	if (flagProtectedGroup) {
		flagProtectedGroup.value = '';
	}
	// Verify that hc_hv has a group.
	if (isHcHv(selReason, selSubReason)) {
		var group = _gel('protected_group' + annoTag);
		if (group) {
			if (group.options[group.selectedIndex].value == '') {
				toggleDisplay('flag' + annoTag + '_Error');
				return false;
			}
			if (flagProtectedGroup) {
				flagProtectedGroup.value = group.options[group.selectedIndex].value;
			}
		}
	}
	return true;
}

function processFlagForm(elForm, opt_forChannel) {
	var formElement = elForm;
	if (formElement) {
		var selCheckbox = _gel('flag_checkbox');
		var selAnnoCheckbox = _gel('flag_anno_checkbox');

		// Either flag video or flag annotations must be on.
		if (!selCheckbox.checked) {
			resetFlagFormValues(elForm);
		}

		if (!selAnnoCheckbox.checked) {
			resetFlagAnnoFormValues(elForm);
		}

		var selReason = formElement.flag_reason.value;
		var selSubReason = formElement.flag_sub_reason.value;
		var selAnnoReason = formElement.flag_anno_reason.value;
		var selAnnoSubReason = formElement.flag_anno_sub_reason.value;

		if ((selReason == '') && (selAnnoReason == '')) {
			toggleDisplay('flag_Error');
			return;
		}

		// Set extra flagging data values.
		if (selReason != '') {
			if (!setExtraFlaggingData('', selReason, selSubReason)) {
				return;
			}
		}
		if (selAnnoReason != '') {
      		if (!setExtraFlaggingData('_anno', selAnnoReason, selAnnoSubReason)) {
				return;
			}
		}

		// Alter display after post
		toggleReportConcernResult(selReason, selSubReason, selAnnoReason, selAnnoSubReason);
		toggleDisplay('inappropriateVidDiv');

		var callback = function() { if (opt_forChannel) {playnav.closePopup();} else {hideDiv('inappropriateVidDiv');} };
		ajaxRequest(elForm.action, { postBody: extractFormData(elForm), onComplete: callback, onException: callback });

		_gel('selectedFlagReason').innerHTML = '- ' + MSG_FlagDefault + ' -';
		clearMenuSelectionStyles(_gel('selectedFlagReason'));
		closeAllFlagMoreInfo('flag_');

		_gel('selectedAnnoFlagReason').innerHTML = '- ' + MSG_FlagDefault + ' -';
		clearMenuSelectionStyles(_gel('selectedAnnoFlagReason'));
		closeAllFlagMoreInfo('flag_anno_');

		addClass(_gel('watch-tab-flag'), 'disabled');
	}
}

function flagError(elName, errorText) {
	if (elName) {
		_gel(elName).innerHTML = errorText;
		toggleDisplay(elName);
	}
}
function clearSelectionStyles(elName) {
	if (elName) {
		elName.style.backgroundColor = '';
		elName.style.color = '';
	}
}

function setSelectionStyles(elName) {
	if (elName) {
		elName.style.backgroundColor = '#6681ba';
		elName.style.color = '#fff';
	}
}

function stripNonNumber(val) {
	return val.replace(/[^\d]/g, '')
}


