// Brings up a help window.
function help (url)
{
        var pad = screen.availWidth * 0.1;
        var width = screen.availWidth * 0.4
        var height = screen.availHeight - (pad * 2)
        var left = screen.availWidth - width - pad;
        var top = pad;
 
        var win = window.open (url, "help",
                "width=" + width + "," +
                "height=" + height + "," +
                "left=" + left + "," +
                "top=" + top + "," +
                "menubar=yes,scrollbars=yes");
 
        // Brings it to the front.
        if (win != null)
                win.focus();
}

function openWindow(url, height, width)
{
	var params;
	if( height )
		params = "height=" + height;

	if( width )
		params += ",width=" + width;

	params += ",scrollbars=yes";
	var newWindow = window.open(url, "", params);
}

// removes trailing spaces from string
function rtrim(string) 
{
	return string.replace(/\s+$/gi, "");
}

// removes leading spaces from string
function ltrim(string) 
{
	return string.replace(/^\s*/gi, "");
}

// removes trailing and leading spaces from string
function trim(string) 
{
	return ltrim(rtrim(string)); 
}

// Returns whether a date is valid.  Dates are mm/dd/yyyy.
function validDate (date)
{
	// Note: Unfortunately, many browsers do not support split.
	var i = 0;
	var l = 0;

	i = date.indexOf("/", i);
	var month = date.substr(0, i);

	l = i; i = date.indexOf("/", i+1);
	var day = date.substr(l+1, i-l-1);
	
	var year = date.substr(i+1);

	if (year == "") return false;
	if (day == "") return false;
	if (month == "") return false;

	if (isNaN(year)) return false;
	if (isNaN(day)) return false;
	if (isNaN(month)) return false;

	if (year < 1900 || year > 2100) return false;
	if (month < 1 || month > 12) return false;
	if (day < 1 || day > 31) return false;

	return true;
}



// Returns true if a GIF or a JPG.
function gifOrJpg (text)
{
	var extension = getExtension(text);

	if (extension == ".GIF" || extension == ".JPG")
		return true;

	return false;
}



// Returns the file part of a path (or last directory if no file is given).
function filePart (text)
{
	var cut_at = text.lastIndexOf('/');

	if (cut_at == -1)
		cut_at = text.lastIndexOf('\\');

	return text.substr(cut_at + 1);
}



// Returns an upper-cased extension.
function getExtension (text)
{
	return text.substr(text.lastIndexOf('.')).toUpperCase();
}


// checks required fields on a form
// you can pass in an arbitrary number of parameters
// parameters must be made up of a seqence of field and label.  
// Label will be displayed in the message box if the field is missing.
// e.g. checkRequiredFields(field1, "Description")
// if field1 is blank, a message box will display the following
// Description is required
// focus will then be set to that field
function checkRequiredFields()
{
	if( (arguments.length % 2) != 0 )
	{
		alert("Invalid parameter list [checkRequiredFields]");
		return false;
	}
	
	for( var i = 0; i < arguments.length; i += 2)
	{
		if( trim(arguments[i].value) == "" )
		{
			alert( arguments[i+1] + " is required." );
			arguments[i].focus();
			return false;
		}
	}
	
	return true;
}


// checks the length of fields
// you can pass in an arbitrary number of parameters
// parameters must be made up of a seqence of field, length, and label.  
// Label will be displayed in the message box if the field is too long.
// e.g. checkRequiredFields(field1, 25, "Description")
// if field1.length > 25 then , a message box will display the following
// Description is too long.
// focus will then be set to that field
function checkFieldLengths()
{
	if( (arguments.length % 3) != 0 )
	{
		alert("Invalid parameter list [checkFieldLengths]");
		return false;
	}

	for( var i = 0; i < arguments.length; i += 3)
	{
		if( arguments[i].value.length > arguments[i+1] )
		{
			alert( arguments[i+2] + " is too long." );
			arguments[i].focus();
			return false;
		}
	}
	
	return true;
}
