
var temp = new Array();
var ok;
var msg;
var numRadioButtons = 0;

/*
alpha = does it only contain letters?
blank = is the text field blank?
country = if US, make state required
email = is it a valid email address?
lettersandnumbers = does it only contain numbers and letters?
numeric = does it only contain numbers?
phone = is it a valid phone number (US and int'l)?
password = is newpassword1 the same as newpassword2?
postalcode = is it a valid postalcode (US and int'l)?
pulldown = is something selected from the pulldown menu?
url = does it begin with http:// or https://?
*/


function validate(form,fields) {
	ok = 1; 
	msg = "The following fields need to be changed:\n";
	
	//sets formname equal to the name of the current form
	var formname = form.name;

	
	//check to see if there is a 'country' field in this form.  if so, check the value and if it is 'US' then create a variable called 'US'
   /* if ( eval( "document." + formname + ".country" ) )
    {
        if ( eval( "document." + formname + ".country[document." + formname + ".country.selectedIndex].value" ) == "US" )
        {
            
        }
    }*/
var US = 1;
    for (var i = 0; i < form.length; i++) 
    {

        var f = form.elements[i];
  
        for ( j = 0; j < fields.length; j++ ) 
        {
            temp = fields[j].split( '|' );
            if ( temp[0] == f.name ) 
            {
                if ( temp[2].indexOf( 'alpha', 0 ) != -1 ) 
                { 
                    if ( ( !isAlpha( f.value ) ) && ( !isBlank( f.value ) ) )
                        {
	                        ok = 0, msg += "    Your "+temp[1]+" should only contain letters\n";
                            alert(msg);
							eval("document." + formname + "." + f.name + ".focus()");
                            return false;
                        }
                }
                if ( temp[2].indexOf( 'blank', 0 ) != -1 ) 
                { 
                    if ( isBlank( f.value ) )  
                    {
                        ok = 0, msg += "    You didn\'t enter the "+temp[1]+"\n";						
						alert(msg);		
						eval("document." + formname + "." + f.name + ".focus()");	
                        return false;
                    }
                } 
                if ( temp[2].indexOf( 'country', 0 ) != -1 ) 
                {
                    if ( US )
                    {
                        if ( eval( "document." + formname + ".state[document." + formname + ".state.selectedIndex].value" ) == "" )
                        {					
                            ok = 0, msg += "    You didn\'t select a state\n";
                            alert(msg);
                            return false;
                        }
                    }
                }
                if ( temp[2].indexOf( 'email', 0 ) != -1 ) 
                {
                    if ( ( !verifyEmail( f.value ) ) && ( !isBlank( f.value ) ) )
                    {
                        ok = 0, msg += "    Your "+temp[1] + " is incorrectly formatted\n";
                        alert(msg);
                        eval("document." + formname + "." + f.name + ".focus()");
                        return false;
                    }
                }
                if ( temp[2].indexOf( 'lettersandnumbers', 0 ) != -1 ) 
                { 
                    if ( ( !isAlphaNumeric( f.value ) ) && ( !isBlank( f.value ) ) )
                    {
	                    ok = 0, msg += "    Your "+temp[1]+" should only contain letters and numbers\n";
                        alert(msg);
                        eval("document." + formname + "." + f.name + ".focus()");
                        return false;
	                }
                }
                if ( temp[2].indexOf( 'numeric', 0 ) != -1 ) 
                { 
                    if ( ( !isNumeric( f.value ) ) && ( !isBlank( f.value ) ) ) 
                    {
                        ok = 0, msg += "    Your "+temp[1]+" should only contain numbers\n";
						alert(msg);
                        eval("document." + formname + "." + f.name + ".focus()");
                        return false;
                    }
                }
				if ( temp[2].indexOf( 'password', 0 ) != -1 ) 
                { 
                    if ( eval("document." + formname + ".newpassword1.value") != eval("document." + formname + ".newpassword2.value") ) 
                    {
                        ok = 0, msg += "    Please make sure your new password is typed exactly the same\n";
						alert(msg);
                        eval("document." + formname + "." + f.name + ".focus()");
                        return false;
                    }
                }
                if ( temp[2].indexOf( 'phone', 0 ) != -1 ) 
                {
               
                    if ( US )
                    { 
                        if ( ( !verifyPhoneUS( f.value ) ) && ( !isBlank( f.value ) ) )
                        {
                            ok = 0, msg += "    You didn\'t enter a valid "+temp[1]+"\n";
							alert(msg);
                        	eval("document." + formname + "." + f.name + ".focus()");
                            return false;
                        }
                    } 
                    else 
                    {
                        if ( ( !verifyPhoneIntl( f.value ) ) && ( !isBlank( f.value ) ) )
                        {
                            ok = 0, msg += "    You didn\'t enter a valid "+temp[1]+"\n";
							alert(msg);
                            eval("document." + formname + "." + f.name + ".focus()");
                            return false;
                        }
                    }	  
                }
                if ( temp[2].indexOf( 'postalcode', 0 ) != -1 ) 
                {
                    if ( US )
                    {
                        if ( ( !verifyPostalcodeUS( f.value ) ) && ( !isBlank( f.value ) ) )
                        {
                            ok = 0, msg += "    You didn\'t enter a valid "+temp[1]+"\n";
							alert(msg);
                            eval("document." + formname + "." + f.name + ".focus()");
                            return false;
                        }
                    } 
                    else 
                    {
                        if ( ( !verifyPostalcodeIntl( f.value ) ) && ( !isBlank( f.value ) ) )
                        {
                            ok = 0, msg += "    You didn\'t enter a valid "+temp[1]+"\n";
							alert(msg);
                            eval("document." + formname + "." + f.name + ".focus()");
                            return false;
                        }
                    }
                }	
				if ( temp[2].indexOf( 'pulldown', 0 ) != -1 ) 
                {
                    var selectname = f.name;
                    var selectedone = eval( "document." + formname + "." + selectname + "[document." + formname + "." + selectname + ".selectedIndex].value" );	
				    //var selectedone = eval( formname + "." + selectname + ".selectedIndex" );	
                    if ( ( selectedone == "" ) || ( selectedone == -1 ) )
                    {
                        ok = 0, msg += "    You didn\'t select a "+temp[1]+"\n";
                        alert(msg);
                        return false;
                    }
                }
                if ( temp[2].indexOf( 'url', 0 ) != -1 ) 
                { 
                    if ( ( !verifyUrl( f.value ) ) && ( !isBlank( f.value ) ) )
                    {
                        ok = 0, msg += "    Your "+temp[1]+" should begin with 'http://' or 'https://'\n";
                        alert(msg);
                        eval("document." + formname + "." + f.name + ".focus()");
                        return false;
                    }
                }
            }
        }
    }
	if (numRadioButtons != 0)
    {
		validateRadio();
    }
    if ( ok )
    {
        form.submit();
    }
}







//---------------------------------------------------------------------------------
//
// Function: 	isAlpha
//
// Description: This function validates a string by verifying that it contains only 
//              characters that are (a-z). 
// 				
// Parameters:  string - String containing form field value to be verified.
//
// Returns:     true if string is valid, otherwise false.
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/9/01
//
// Updates:     2/9/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function isAlpha( string ) 
{
    if ( string.search( /^[a-z]*\s*\-*'*[a-z]*$/i) != -1 )
    {
        return true;
    }
	else
    {
	    return false;
    }
}







//---------------------------------------------------------------------------------
//
// Function: 	isAlphaNumeric
//
// Description: This function validates a string by verifying that it contains only 
//              characters that are (a-z) and/or numbers (0-9). 
// 				
// Parameters:  string - String containing form field value to be verified.
//
// Returns:     true if string is valid, otherwise false.
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/9/01
//
// Updates:     2/9/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function isAlphaNumeric( string ) 
{
    if ( string.search( /^\w*$/ ) != -1 )
    {
        return true;
    }
    else
    {
        return false;
    }
}







//---------------------------------------------------------------------------------
//
// Function: 	isBlank
//
// Description: This function checks to see whether the form field was left blank. 
// 				
// Parameters:  string - String containing form field value to be verified.
//
// Returns:     true if string is valid, otherwise false.
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/9/01
//
// Updates:     2/9/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function isBlank( string ) 
{
    for ( var i = 0; i < string.length; i++ ) 
    {
        var c = string.charAt( i );
        if ( c != " " && c != "\t" && c != null ) 
        {
            return false;
        }
    }
    return true;
}







//---------------------------------------------------------------------------------
//
// Function: 	verifyEmail
//
// Description: This function validates an email address by verifying that only 
//              only characters valid for an email address are present in the given
//              string.  
// 				
// Parameters:  string - String containing email address to be verified.
//
// Returns:     true if string is valid, otherwise false.
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/9/01
//
// Updates:     2/9/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function verifyEmail( string ) 
{
    if ( string.search( /^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1 )
    {
        return true;
    }
    else
    {
        return false;
    }
}







//---------------------------------------------------------------------------------
//
// Function: 	isNumeric
//
// Description: This function validates a string by verifying that only 
//              only numbers (0-9) are present.
// 				
// Parameters:  string - String containing form field value to be verified.
//
// Returns:     true if string is valid, otherwise false.
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/9/01
//
// Updates:     2/9/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function isNumeric( string ) 
{
    if ( string.search( /^[d]*$/ ) != -1 )
    {
        return true;
    }
    else
    {
        return false;
    }
}







//---------------------------------------------------------------------------------
//
// Function: 	verifyPhoneUS
//
// Description: This function validates a US phone/fax number by verifying that 
//              only characters valid for a phone number are present.  The number
//              must contain 10 numbers for the 3 digit area code and 7 digit number  
// 				
// Parameters:  string - String containing form field value to be verified.
//
// Returns:     true if string is valid, otherwise false.
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/9/01
//
// Updates:     2/9/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function verifyPhoneUS(string) 
{
    if ( string.search( /^\d?\-*\.*\s*\(*\d\d\d\)*\-*\.*\s*\d\d\d\-*\.*\s*\d\d\d\d$/ ) != -1 )
    {
        return true;
    }
    else
    {
        return false;
    }
}







//---------------------------------------------------------------------------------
//
// Function: 	verifyPhoneIntl
//
// Description: This function validates an international phone/fax number by verifying 
//              that only characters valid for a phone number are present.  It makes 
//              sure there are between 7 and 15 digits.  
// 				
// Parameters:  string - String containing form field value to be verified.
//
// Returns:     true if string is valid, otherwise false.
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/9/01
//
// Updates:     2/9/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function verifyPhoneIntl( string ) 
{
    var phonevalue = string;
    var regexp = /\s*\.*\+*\-*\(*\)*/g;
    var tempphone = string.replace( regexp, "" );

    if ( tempphone.search( /^\D+$/g ) != -1 )
    {
        return false;
	}
    if ( tempphone.search( /\d{7,15}/ ) != -1 )
    {
        return true;
    } 
    else 
    {
        return false;
    }
}	







//---------------------------------------------------------------------------------
//
// Function: 	verifyPostalcodeUS
//
// Description: This function validates a US postal code number by verifying 
//              that only characters valid for a postal code are present.  The 
//              postalcode may contain 5 or 9 digits.
// 				
// Parameters:  string - String containing form field value to be verified.
//
// Returns:     true if string is valid, otherwise false.
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/9/01
//
// Updates:     2/9/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function verifyPostalcodeUS( string ) 
{
	if ( string.search( /^\d{5}$|^\d{5}[\-|\s]?\d{4}$/ ) != -1 )
	{
        return true;
    } 
    else 
    {
        return false;
    }
}







//---------------------------------------------------------------------------------
//
// Function: 	verifyPostalcodeIntl
//
// Description: This function validates an intl postal code number by verifying 
//              that only characters valid for a postal code are present.  Intl 
//              postalcodes may contain letters.  This function checks for at least
//              3 characters.
// 				
// Parameters:  string - String containing form field value to be verified.
//
// Returns:     true if string is valid, otherwise false.
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/9/01
//
// Updates:     2/9/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function verifyPostalcodeIntl( string ) 
{
    var postalcodevalue = string;
    var regexp = /\s*\.*\+*\-*\(*\)*/g;
    var temppostalcode = string.replace( regexp, "" );

    if ( temppostalcode.search( /^\W*$/g ) != -1 )
    {
        return false;
    }
    if ( temppostalcode.search( /\w{3}/ ) != -1 )
	{
        return true;
    } 
    else 
    {
        return false;
    }
}







//---------------------------------------------------------------------------------
//
// Function: 	verifyUrl
//
// Description: This function checks to make sure beginning of the string begins
//              with "http://" or "https://".
// 				
// Parameters:  string - String containing form field value to be verified.
//
// Returns:     true if string is valid, otherwise false.
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/9/01
//
// Updates:     2/9/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function verifyUrl( string ) 
{
    if ( string.search( /^https?:\/\/.+$/ ) != -1 )
    {
        return true;
    } 
    else 
    {
        return false;
    }
}









//---------------------------------------------------------------------------------
//
// Function: 	clickRadio
//
// Description: This function changes the value of the current index to = " ";
// 				
// Parameters:  index - string is the name of the current radio button 
//
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/15/01
//
// Updates:     2/15/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function clickRadio( index ) 
{
     radioArray[index] = " ";
}


//---------------------------------------------------------------------------------
//
// Function: 	validateRadio
//
// Description: This function loops through the radioArray and checks to make sure 
//              each question has been answered
//
// Returns:     true if all answers have been answered, otherwise false
//
// Programmer:  Happy Tsugawa-Banta
// 
// Date: 		2/12/01
//
// Updates:     2/15/01 - added these comments (HT)
//
//--------------------------------------------------------------------------------

function validateRadio()
{
	for ( j = 0; j < radioArray.length; j++ ) 
    {
        temp = radioArray[j].split( '|' );
        if ( temp[0] != " " ) 
        {
            ok = 0, msg += "    You didn't select a radio button about "+temp[1] +"\n";
        }
    } 
}