﻿// JavaScript functions for Common module user controls.

/* Log/LogManager.ascx */
function logManager_add_onClick(prefix)
{
	modalDialog_toggle(prefix + "_Dialog",true);
	var field = document.getElementById(prefix + "_LogEntryType");
	if (field)
		field.focus();
}

/* Tasks/TaskFields.ascx */
function taskFields_dueDate_onChange(prefix,value)
{
	var txfr = document.getElementById(prefix + "_TransferDueDateToRecurrence").value;
	if (txfr == "true")
		recurrenceFields_setStartDate(prefix + "_RecurrenceFields",value);
}

function taskFields_focus(prefix)
{
	var field = document.getElementById(prefix + "_TaskName");
	if (field)
		field.focus();
}

function taskFields_onLoad(prefix)
{
	taskFields_isRecurring_onClick(prefix);
}

function taskFields_isRecurring_onClick(prefix)
{
	var field = document.getElementById(prefix + "_IsRecurring");
	if (field)
	{
		recurrenceFields_toggleVisibility(prefix + "_RecurrenceFields",field.checked);
	}
}

/* Tasks/TasksManager.ascx */
function tasksManager_add_onClick(prefix)
{
	modalDialog_toggle(prefix + "_Dialog",true);
	taskFields_focus(prefix + "_TaskFields");
}

/* UniqueObjectSelector.ascx */
function uniqueObjectSelector_enableValidator(prefix,enable)
{
	var validator = $get(prefix + "_Validator");
	if (validator)
		ValidatorEnable(validator,enable);	
}

function uniqueObjectSelector_formatGoTo(prefix,url,pageID)
{
	var id = document.getElementById(prefix + "_ObjectID").value;
	if (id != "0")
		uniqueObjectSelector_goTo(url.replace("{0}",id),pageID);
	else
		alert("Please select a valid item before viewing or editing.\n\nTo select a valid item, type 4 or more characters in the box, then select the item from the search results that appear below the box.")
}
function uniqueObjectSelector_goTo(url,pageID)
{
	openDialogWindow(url,"UniqueObjectSelectorWindow" + pageID);
}

function uniqueObjectSelector_onClear(prefix)
{
	uniqueObjectSelector_onSelect(prefix,0,"");
}

function uniqueObjectSelector_onSelect(prefix, id, name)
{
	var idField = document.getElementById(prefix + "_ObjectID");
	var lastID = idField.value;

	idField.value = id;
	document.getElementById(prefix + "_ObjectName").value = unescape(name).replace("&amp;","&");
	
	//perform AutoPostBack when value changed, if requested
	var postBackField = document.getElementById(prefix + "_AutoPostBackI");
	if ((lastID != id) && id > 0 && postBackField.value == "true")
		window.setTimeout("__doPostBack('" + idField.name + "', '')",0);
}

function uniqueObjectSelector_onValidate(sender,args)
{
	//add 9 because we want "_Selector" included in the prefix
	var prefix = sender.id.substring(0,sender.id.lastIndexOf("_"));
	var id = parseInt(document.getElementById(prefix + "_ObjectID").value);
	var name = document.getElementById(prefix + "_ObjectName").value;
	
	//do base validation
	args.IsValid = (id > 0);
	
	//do extended validation for when a selection is not required
	if (sender.getAttribute("required") == "False")
		args.IsValid = ((args.IsValid) || (id == 0 && name.length == 0));
}