/*----------------------------------------------------------------
This is a common javascript library for RTFC.coop.
Author: Patrick Buchanan
Date:	01/12/2002
Last Updated:	02/11/2002

----------------------------------------------------------------*/

/*------------------------------------------------------------------
FUNCTION:  	Opens a new window
RECEIVES:  	string url - url for new window
			string name - reference name of new window
			string widgets - window features to display (e.g. toolbar,etc.)
RETURNS:  	string
------------------------------------------------------------------*/
function openWindow (url,name,widgets) 
{
	popupWin = window.open (url,name,widgets);
	popupWin.focus();
}

/*------------------------------------------------------------------
FUNCTION:  	This function parses &-separated name=value argument pairs from
			the query string of the URL. It stores the name=value pairs in 
			properties of an object and returns that object
RECEIVES:  	nothing
RETURNS:  	object - array using the parameter name as the index and the value 
			as the parameter value
------------------------------------------------------------------*/
function setURLParameters() 
{
    var args = new Object();
    var query = location.search.substring(1);     // Get query string.
    var pairs = query.split("&");                 // Break at comma.
    for(var i = 0; i < pairs.length; i++) {
	var pos = pairs[i].indexOf('=');          // Look for "name=value".
	if (pos == -1) continue;                  // If not found, skip.
	var argname = pairs[i].substring(0,pos);  // Extract the name.
	var value = pairs[i].substring(pos+1);    // Extract the value.
	args[argname] = unescape(value);          // Store as a property.
	// In JavaScript 1.5, use decodeURIComponent() instead of escape()
    }
    return args;                                  // Return the object.
} // END function setURLParameters()

/*------------------------------------------------------------------
FUNCTION:  	Finds the URL parameter given using the array object built by
			getURLParameters();
RECEIVES:  	nothing
RETURNS:  	object - array using the parameter name as the index and the value 
			as the parameter value
------------------------------------------------------------------*/
function getURLParameter(arg)
{
	// build URL parameters array object
	var URLParameters = setURLParameters();
	
	// retreive parameter value from URLParameters array
	var argValue = URLParameters[arg];
	
	//return null if parameter doesn't exist
	if(argValue==null)
	{
		return null;
	} else { //return parameter value
		return argValue;
	} // if(argValue==null)

} // END function getURLParameter(arg)

/*------------------------------------------------------------------
FUNCTION:  	Removes trailing and leading spaces from a string.
RECEIVES:  	string
RETURNS:  	string
------------------------------------------------------------------*/
function trimSpaces(string) 
{
	while(string.charAt(string.length-1) == ' ') {
		string = string.substring(0,string.length-1);
	}
	while(string.charAt(0) == ' ') {
		string = string.substring(1,string.length);
	}
	return string
}

/***********************************************************
FUNCTION:  	Returns false if the string contains anything other than a positive integer (0-9)
RECEIVES:  	string
RETURNS:  	boolean
***********************************************************/
function isPosInteger(value)
{
	inputStr = value.toString()
	
	for ( var i = 0; i < inputStr.length; i++)
	{
		var x = inputStr.charAt(i);
		if (x < "0" || x > "9")
		{
			return false;
		}
	}
	
	return true;

	
	/* using regular expressions
	var reDigit = /^\d$/;
	if(reDigit.test(value))
	{
		return true;
	} else {
		return false;
	}
	*/
}

/***********************************************************
FUNCTION:  	Displays a message box.
RECEIVES:  	String
RETURNS:  	void
***********************************************************/
function showMessage(msg)
{
	alert(msg);
	return;
}

/***********************************************************
FUNCTION:  	Validates an e-mail address.  Checks if the email is empty, 
		if the @ symbol is present, and if the last 4 characters are 
		a valid domain.
RECEIVES:  	string
RETURNS:  	boolean
***********************************************************/
function isValidEmail(email) 
{
	//create array of valid internet domains
	var domains = new Array(".com", ".org", ".net", ".ws", ".gov", ".edu", ".mil", ".int", ".coop", ".tv", ".coop");
	
	if (email == null || email == "") 
	{
		return false;
	}
	if(email.indexOf("@") == -1)
	{
		return false;
	}
	domain = email.substring(email.lastIndexOf("."), email.length);
	for(x=0; x<domains.length; x++)
	{
		if(domain == domains[x])
		{
			return true;
		}
	}
	return false;
}



