﻿//************************************************************************
// This file contains JavaScript functions to validate common fields for
// ESA's website forms.
//
// ESA International, LLC
// P.O. Box 2110
// Clackamas, OR  97015
//
// WRITTEN BY: Adam Szwarc
// DATE:       9/13/2006
//
// Copyright (c) 2006-2012 ESA International, LLC.  All rights reserved.
//
//************************************************************************
function validateEmail( str )
{
	if( str == null || str == "" ) return false;
    if( str.length < 8 ) return false;
    if( str.indexOf("@") == -1 ) return false;
    if( str.indexOf(".") == -1 ) return false;
    
    return true;
}

function validatePhone( str, bForeign )
{
	if( str == null || str == "" ) return false;
	if( bForeign == true ) return true;
	
    if( str.length < 10 ) return false;
    
    var iDigits = 0;

    for (var i = 0; i < str.length; i++)
    {
        if( "0" <= str.charAt(i) && str.charAt(i) <= "9" )
        {
            iDigits++;	        
        }
    }

    if( iDigits < 10 ) return false;
    return true;
}

function formatPhone( str, bForeign )
{
    if( bForeign ) return str;
    if( str == null || str == "" ) return str;
    if( str.length < 10 ) return str;

    var i;
    var iDigits = 0;
    var digit = new Array();
    var ch;
    var strNew = "";

    for( i=0; i<str.length; i++ )
    {
        ch = str.charAt(i);
        
        if( "0" <= ch && ch <= "9" )
        {
            digit[iDigits] = ch;
            iDigits++;	        
        }
    }

    if( iDigits < 10 ) return str;

    strNew = "(" + digit[0] + digit[1] + digit[2] + ")" + digit[3] + digit[4] + digit[5] + "-" + digit[6] + digit[7] + digit[8] + digit[9];

    if( 10 < iDigits )
    {
        strNew += " ext ";
        
        for( i=10; i<iDigits; i++ )
	    {
            strNew += digit[i];
        }
    }
      
    return strNew;
}

function validateString( strID )
{
    var str;

    str = document.getElementById( strID ).value;

    if( strID == "request_state" )
    {
	    var state = document.getElementById( "request_state" );
    
	    if( state.selectedIndex == 0 ) str = "";
    }
    else if( strID == "request_source" )
	{
		var source = document.getElementById( "request_source" );
		var index  = source.selectedIndex;

		str = source.options[index].text;

		if( index == 0 )
		{
			str = "";
		}
    }
    else if( strID == "request_own" )
	{
	    var source = document.getElementById("request_own");
		var index  = source.selectedIndex;

		str = source.options[index].text;

		if( index == 0 )
		{
			str = "";
		}
    }
    else if( strID == "request_email" )
    {
	    str = document.getElementById( strID ).value;
	    if( !validateEmail( str ) )
	    {
		    str = "";
	    }
    }
    else if( strID == "request_telephone" )
    {
	    str = document.getElementById( strID ).value;

	    if( validatePhone( str, false ) == false )
	    {
		    document.getElementById( strID ).style.backgroundColor = "#fbfd9d";
		    return true;
	    }

	    document.getElementById( strID ).value = formatPhone( str, false );
    }
    else if( strID == "method" )
    {
	    var method = document.getElementById( "method" );

	    if( method.selectedIndex == 0 ) str = "";
    }
    else if( strID == "payment" )
    {
	    var payment = document.getElementById( "payment" );
    
	    if( payment.selectedIndex == 0 ) str = "";
    }

    if( str == null || str == "" )
    {
	    document.getElementById( strID ).style.backgroundColor = "#fbfd9d";
	    return true;
    }
    else
    {
	    document.getElementById( strID ).style.backgroundColor = "white";
	    return false;
    }
}

function validate()
{
	var bInvalid = false;

	bInvalid |= validateString( "request_name" );
	bInvalid |= validateString( "request_company" );
	bInvalid |= validateString( "request_addy" );
	bInvalid |= validateString( "request_city" );
	bInvalid |= validateString( "request_state" );
	bInvalid |= validateString( "request_zip" );
	bInvalid |= validateString( "request_email" );
	bInvalid |= validateString( "request_telephone" );
	bInvalid |= validateString( "qty" );
	bInvalid |= validateString( "method" );
	bInvalid |= validateString( "payment" );
	bInvalid |= validateString( "request_source" );
	bInvalid |= validateString( "request_own" );

	var index = document.getElementById( "request_source" ).selectedIndex;

	if (index == 3 || index == 13)
    {
		bInvalid |= validateString( "request_source_other" );
	}
	
	if( bInvalid == true )
    {
        alert( "Please check the entries highlighted in yellow.  We are unable to process your request because these entries are either blank or not entered correctly." );
	}
	
	return !bInvalid;
}

function OnChangeSource()
{
	var index = document.getElementById( "request_source" ).selectedIndex;

	if (index == 3 || index == 13)
    {
		visib = "visible";
	}
	else
	{
		visib = "hidden";
	}

	document.getElementById( "request_source_other" ).style.visibility = visib;
}

