﻿//************************************************************************
// This file contains JavaScript functions to validate common fields for
// ESA's website forms.
//
// ESA, Inc.
// P.O. Box 2110
// 15 82nd Drive, Suite 10
// Gladstone, OR  97027
//
// WRITTEN BY: Adam Szwarc
// DATE:       9/13/2006
//
// Copyright (c) 2006-2009 ESA, Inc.  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;
	
    if( strID == "request_state" )
	  {
				var state = document.getElementById( "request_state" );
				var index = state.selectedIndex;

				str = state.options[index].value;
		    
				if( index == 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;

				var strCountry = document.getElementById( "request_country" ).value;
				var bForeign = true;
		    
				if( strCountry == "United States" || strCountry == "Canada" || strCountry == "" || strCountry == null )
				{
						bForeign = false;
				}

				if( strCountry == "--Select--" )
				{
						str = "";
				}

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

function validate( source )
{
		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( "request_own" );

		if( source == 1 )
		{
				bInvalid |= validateString( "request_source" );

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

				if( index == 1 || index == 4 || index == 19 )
				{
						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 == 1 || index == 4 || index == 19 )
		{
				visib = "visible";
		}
		else
		{
				visib = "hidden";
		}

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