/* ***************************************************************
 *
 * Copyright (c) TimeFrame, Inc. 2005, 2007
 * 
 * File: utils.js
 * Created: 
 * Description:
 *
 * This action looks up a customer in the customer table based
 * on a CustomerID in the request.
 * 
 * ***************************************************************
 * CHANGE HISTORY
 * ***************************************************************
 * DATE     USER       RECORD       DESCRIPTION
 * ---------------------------------------------------------------
 * 20051222 dewittsc   ------       Initial version.
 * 
 * *************************************************************** */

/* **********************************************************************
 * Validators
 * ********************************************************************** */

/**
 * Return <code>true</code> if <code>s</code> is a valid string of
 * at least length <code>min</code>.
 *
 * @param s     the string.
 * @param len   the required length.
 * @return <code>true</code> if <code>s</code> is a string of at least length
 *         <code>min</code>, otherwise <code>false</code>.
 */
 
function isStringOfLen( s, min )
{
	return ( s != null && s.length >= min );
}

/**
 * Return <code>true</code> if <code>s</code> is a valid number of
 * length <code>len</code>.
 *
 * @param s     the string.
 * @param len   the required length.
 * @return <code>true</code> if <code>s</code> is a number of length
 *         <code>len</code>, otherwise <code>false</code>.
 */
  
function isNumberOfLenRange( s, min, max )
{
	return ( s != null && s.length >= min && s.length <= max && !( isNaN( Number(s) ) ) && Number(s).valueOf() >= 0 );
}

/**
 * Return <code>true</code> if <code>s</code> is a valid number of
 * any length.
 *
 * @param s     the string.
 * @return <code>true</code> if <code>s</code> is a number, otherwise <code>false</code>.
 */
 
function isNumber( s )
{
	return ( s != null && s.length > 0 && !( isNaN( Number(s) ) ) && Number(s).valueOf() >= 0 );
}

/**
 * Return <code>true</code> if <code>c</code> is a valid digit character.
 *
 * @param c     the digit.
 * @return <code>true</code> if <code>s</code> is a number, otherwise <code>false</code>.
 */
 
function isDigit( c )
{
	return ( !( isNaN( Number(c) ) ) );
}

/**
 * Return <code>true</code> if <code>s</code> is a valid date string of
 * the form <code>MM/DD/YYYY</code>.
 *
 * @param s    the string.
 * @return <code>true</code> if <code>s</code> is s valid date string, otherwise <code>false</code>.
 */
 
function isDate( s )
{
	var dateFormat = /^([0-1][0-9])\/([0-3][0-9])\/([0-9]{4})$/i;
	var match = dateFormat.exec(s);
	var flag = false;

	if ( match != null )
	{
		var month = match[1];
		var day = match[2];
		
		if ( month == 9 || month == 4 || month == 6 || month == 11 )
		{
			flag = ( day > 0 && day < 31 );
		}
		else
		{
			flag = ( day > 0 && day < 32 );
		}
	}
	
	return flag;
}

/* **********************************************************************
 * Superproof Specific Utilities
 * ********************************************************************** */

function formatShipAddress( name, city, state, zip, email )
{
    var	out = '';
    
    out += name;
    out += '<br />';
    out += city;
    out += ', ';
    out += state;
    out += '<br />';
    out += zip;
    out += '<br />';
    out += '<a href="mailto:' + email + '">' + email + '</a>';
    
    return out;
}

function doCloseWindow()
{
    if ( window.close ) { window.close(); }
    else if ( self.close ) { self.close(); }
}