/**********************************************************************
*	The Next Page, LLC
*	Function Library, Version 1.1
*	Copyright 1999, 2000. All Rights Reserved.
*	This source code may not be copied, modified, or redistributed
*	except as provided in the program that contains it and without
*	express written consent by The Next Page, LLC.
*	http://www.nxpage.com
***********************************************************************/
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
						I N D E X
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	function trimstring(instring)
*   function isEmpty(string)
*	function isWhitespace (string)
*   function isEmail (field,sMessage)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

/*
*********************************************************
	trimstring
*********************************************************
*/

function trimstring(instring){
a = instring.value
var outstring;
var startpos;
var endpos;
var ch;
//where do we start?
startpos = 0;
ch = instring.value.charAt(startpos);
while ((ch == " ") || (ch =="\b") ||  (ch =="\f") || (ch =="\n") ||  (ch == "\r") ||(ch == "\n") ){
    startpos++;
ch = instring.value.charAt(startpos);
}
// where do we end?
endpos = instring.value.length - 1;
ch = instring.value.charAt(endpos);
while ((ch == " ") || (ch =="\b") ||  (ch =="\f") || (ch =="\n") ||  (ch == "\r") ||(ch == "\n") ){
endpos--;
ch = instring.value.charAt(endpos);
}
//get the string
outstring = instring.value.substring(startpos, endpos + 1);
return outstring;
}//<====  end function  =====>


/*
*********************************************************
isEmpty
*********************************************************
*/

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


/*
*********************************************************
isWhitespace
 Returns true if string s is empty or 
 whitespace characters only.
*********************************************************
*/

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if ( c == ' ') return true;
    }

    // All characters are whitespace.
    return false;
}


/*
*********************************************************
isEmail

 Email address must be of form a@b.c -- in other words:
 * there must be at least one character before the @
 * there must be at least one character before and after the .
 * the characters @ and . are both required
 
*********************************************************
*/



function isEmail (field,sMessage)
{   
	s = trimstring(field);
	
    // is s whitespace?
    if (isWhitespace(s)) {
			alert(sMessage);field.focus();
			return false;
		}
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) 
		{
			alert(sMessage);field.focus();
			return false;
		}
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != "."))
    {
			alert(sMessage);field.focus();
			return false;
	}
    else return true;
}
