// <summary>
// Displays a url in a popup window.
// </summary>
// <param name="url">string, url to display in the window.</param>
// <param name="handleName">string, a handle name to assign to the window.</param>
// <param name="height">int, height (in pixels) of the window.</param>
// <param name="width">int, width (in pixels) of the window.</param>
// <param name="appearance">string, window's options, i.e. scrollbars, etc.  If this value isn't specified, default options are used.</param>
function PopupWindow(url, handleName, height, width, appearance)
{
	// If "appearance" hasn't been defined, use the default one below...
	if (typeof(appearance) == "undefined")
	{
		appearance = "scrollbars=yes,status=no,toolbar=no,menubar=no";
	}

	// Pop up a window...
	var win = window.open(url, handleName, "width=" + width + ",height=" + height + "," + appearance);
	win.window.focus();
}

// AUTHOR:			Jason Noftall
// DATE:			Oct. 16, 2001
// DESCRIPTION:		Trims any leading/trailing spaces from the incoming string.
// PARAMETERS:		strIn = string that needs to be trimmed
// RETURNS:			String, input string less any leading/trailing spaces.
function trimString(strIn)
{
	var strOut = new String(strIn);	// Return value

	/* Remove trailing spaces. */
	while (strOut.charAt(strOut.length - 1) == ' ')
		strOut = new String(strOut.substring(0, strOut.length - 1));

	/* Remove leading spaces. */
	while (strOut.charAt(0) == ' ')
		strOut = new String(strOut.substring(1, strOut.length));

	return strOut;
}

// AUTHOR:			Jason Noftall
// DATE:			Aug. 13, 2003
// DESCRIPTION:		Validates specified text fields for content.
// PARAMETERS:		unknown, but at least a reference to a form (must be
// 					1st argument) and a reference to at least 1 text field.
// RETURNS:			<none>, submits the specified form if all specified text fields contain data.
function ValidateTextFields()
{
	var arglength = ValidateTextFields.arguments.length;
	if (arglength >= 2)
	{
		// We have at least a form and one text field...	
		var frm = ValidateTextFields.arguments[0];
		var content;

		for (var i = 1;i < arglength;i++)
		{
			// Check each of the text fields for content...
			content = trimString(ValidateTextFields.arguments[i].value);
			if (content.length == 0)
			{
				// Text field doesn't have content, alert the user and bail...
				ValidateTextFields.arguments[i].focus();
				alert("There is required data that is missing.  Please correct and retry.");
				return;
			}
		}
		
		frm.submit();
	}
}

function ValidateSendEmail(frm)
{
	var subject = frm.subject;
	var body = frm.body;

	if (trimString(subject.value).length == 0)
	{
		alert("Please specify a subject.");
		subject.focus();
		return;
	}
	else if (trimString(body.value).length == 0)
	{
		alert("Please enter some body text.");
		body.focus();
		return;
	}
	
	frm.submit();
}

/* Utility functions. */

function IsValidEmail(str)
{
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); 
}

function UrlEncode(str) {
    return escape(str).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

/* DRM-specific functions. */

// DESCRIPTION:		Validates DRM Add/Edit text fields for content.
// PARAMETERS:		1. frm = reference to the FRM form.
//					2. add = true for ADD; false for EDIT.
// RETURNS:			<none>, submits the specified form if the specified text fields contain data.
function ValidateAddObj(frm, add)
{
	var name = frm.objName;		   // Required
	var description = frm.ObjDesc; // Not required
	var width = frm.moviewidth;    // required for some types of files
	var height = frm.movieheight;  // required for some types of files
	var type = frm.type.value;     // determines whether width and height are required
	var file = frm.objFName;       // Required (only when "add" is true)

	if (trimString(name.value).length == 0)
	{
		alert("Please specify a name for your new file.");
		name.focus();
		return;
	}
	else if ((trimString(file.value).length == 0) && (add))
	{
		alert("Please specify a computer filename for uploading your new file.");
		file.focus();
		return;
	}
	else if( type == 2 || type == 3 || type == 5 || type == 6 )
	{
		if( (trimString(width.value).length == 0) || (trimString(height.value).length == 0) )
		{
			alert("Please specify the dimensions of your video.");
			width.focus();
			return;
		}
	}
	
	frm.submit();
}

// DESCRIPTION:		Validates a new category name and adds it if valid.
// PARAMETERS:		1. form = reference to the page form
//					2. from = source of the initial call to the DRM
// RETURNS:			<void>, the page is reloaded so that the category can be added to the DB.
function ValidateAddCat(form, from)
{
	var name = trimString(form.catName.value);	// the new category's name
	var desc = trimString(form.catDesc.value);	// the new category's description

	if (name.length == 0)
	{
		alert("Please specify a name for your new category.");
		name.focus();
		return;
	}

	document.location = 'Drm.aspx?action=catadd&from=' + from + '&catName=' + UrlEncode(name) + '&catDesc=' + UrlEncode(desc) + '&reload=true';
}
