//////// Utility Functions ////////

// -----------------------------------------------------------------
// Function    : IsEmailValid
// Language    : JavaScript
// Description : Checks if given email address is of valid syntax
// Copyright   : (c) 1998 Shawn Dorman
// http://www.goodnet.com/~sdorman/web/IsEmailValid.html
// -----------------------------------------------------------------
// Ver    Date    Description of modification
// --- ---------- --------------------------------------------------
// 1.0 09/04/1996 Original write
// 1.1 09/30/1998 CHG: Use standard header format
// -----------------------------------------------------------------
// Source: Webmonkey Code Library
// (http://www.hotwired.com/webmonkey/javascript/code_library/)
// -----------------------------------------------------------------

function IsEmailValid(FormName,ElemName)
{
var EmailOk  = true
var Temp     = document.forms[FormName].elements[ElemName]
var AtSym    = Temp.value.indexOf('@')
var Period   = Temp.value.lastIndexOf('.')
var Space    = Temp.value.indexOf(' ')
var Length   = Temp.value.length - 1   // Array is from 0 to length-1

if ((AtSym < 1) ||                     // '@' cannot be in first position
    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    (Period == Length ) ||             // Must be atleast one valid char after '.'
    (Space  != -1))                    // No empty spaces permitted
   {  
      return "+ E-mail address isn't valid.\n";
   }
   return "";
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

var whitespace = " \t\n\r";

function isSpace (c)
{
        if (whitespace.indexOf(c) == -1)
	{
		return false;
	}
	return true;
}

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 (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isInteger (s)

{   var i;

    // Search through string's characters one by one

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isUSPhoneNumber (s)
{   if (isEmpty(s)) 
	return "+ Phone Number is blank.\n";
    if (isInteger(s) && s.length == 10)
	return "";
    else
	return "+ Phone Number is invald.\n";
}

// must be all 0-9, -, ., (, ), or whitespace
function isLoosePhone (s)
{   if (isEmpty(s)) 
	return "+ Phone Number is blank.\n";

    var phoneChars = "-.()";

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is valid.
        var c = s.charAt(i);
        if (!isDigit(c) &&
		phoneChars.indexOf(c) == -1 &&
		!isSpace(c) ) 
		return "+ Phone Number contains invalid characters.\n";
    }

    return "";
}

function isZIPCode (s)
{
   if (isEmpty(s))
	return "+ Zip Code is empty.\n";
   else
   {
	if (s.length == 5)
	{
   		if (!isInteger(s))
			return "+ Zip Code is invalid.\n";
	}
        else
	{
		if     (s.length == 10)
		{
			if ((s.charAt(5) != '-') ||
			    (!isInteger(s.slice(0,4) + s.slice(6,9))))
				return "+ Zip Code is invalid.\n";
		}
		else
			return "+ Zip Code is wrong length.\n";
	}
   }
   return "";
}

// -----------------------------------------------------------------
// Function    : IsAlpha
// Language    : JavaScript
// Description : Returns true if string is all letters; pos. inc. spaces
// -----------------------------------------------------------------

function IsAlpha(name,testStr,spaceOK)
{
    // if empty
    if ( isWhitespace (testStr) )
    {
	// complain
	return ("+ "+ name + " is empty.\n");
    }

    // for each position in the string
    for (i = 0; i < testStr.length; i++)
    {
	// if it's not a letter
	if ( ! isLetter (testStr.charAt(i)) )
	{
	    // if it's not okay to have spaces
	    // or this is not a space
	    if ( !spaceOK || !isSpace (testStr.charAt(i)) )
	    {
		// complain
		return ("+ "+ name + " contains invalid characters.\n");
	    }
	}
    }

    return "";
}


//
// Name:		IsChecked
// Parameters:	formParam - form object containing boxes
//			checkParam - checkbox set name
// Description:	returns true if at least one box in the
//			set is checked
//

function isChecked (formParam,checkParam)
{
	// If the parameter types are correct
//	if ( formParam.type=="form" && checkParam.type=="string" )
	{
		// for element in the form
		for (i=0; i < formParam.length; i++)
		{
			// if the element is a checkbox AND
			// 			has the passed name AND
			//			is checked
			if ( formParam.elements[i].type == "checkbox" &&
				formParam.elements[i].name == checkParam &&
				formParam.elements[i].checked )
			{
				// return true
				return true;
			}
		}

		// if loop ends, return false
		return false;
	}

//	// If the parameter is the incorrect type, send alert
//	else
	{
//		return alert("IsChecked called with incorrect parameter type:\nformParam type: " + formParam.type + "\ncheckParam type: " + checkParam.type + "\n");
	}
}


