<?php
// -------------------
// Includes
// -------------------
require_once('config.php');


// -------------------
// Functions
// -------------------

// Validate Email Address
// Function courtesy of ILoveJackDaniels
// http://www.ilovejackdaniels.com/php/email-address-validation/
// (just for the record ...I am partial to a JD and Coke now and then!)
function CheckEmailAddress($sEmail) {
    if (!
ereg("^[^@]{1,64}@[^@]{1,255}$"$sEmail)) {
        return 
false;
    }

    
$aEmailArray explode("@"$sEmail);
    
$aLocalArray explode("."$aEmailArray[0]);
    for (
$i 0$i sizeof($aLocalArray); $i++) {
        if (!
ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$"$aLocalArray[$i])) {
            return 
false;
        }
    }
    
    if (!
ereg("^\[?[0-9\.]+\]?$"$aEmailArray[1])) {
        
$aDomainArray explode("."$aEmailArray[1]);
        if (
sizeof($aDomainArray) < 2) {
            return 
false;
        }
        for (
$i 0$i sizeof($aDomainArray); $i++) {
            if (!
ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$"$aDomainArray[$i])) {
                return 
false;
            }
        }
    }
    return 
true;
}

// -------------------
// Main
// -------------------

// If the POST variable 'ValidateEmail' is set
// (i.e. if the form has been submitted)
if(isset($_POST['ValidateEmail'])){
    
// Check the email address by running the function 'CheckEmailAddress'
    
if(CheckEmailAddress($_POST['EmailAddress'])){
        
// If it returns true, let the user know
        
echo '<p>This email address is <span class="valid">VALID</span>.</p>';
        
// Then EXIT!
        // This is very important in AJAX applications, as we
        // don't want to process the entire page. We can exit here.
        
exit;
    }else{
        
// Otherwise, let the user know it's invalid.
        
echo '<p>This email address is <span class="invalid">INVALID</span>.</p>';
        
// Then exit.
        
exit;
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Validation Example with PHP and AJAX</title>

<style type="text/css" media="screen">
<!--
@import url("default.css");
-->
</style>
<!-- Include Prototype JavaScript file -->
<script type="text/javascript" language="javascript" src="<?= PROTO_INC_URL?>"></script>

<!-- Custom JavaScript functions -->
<script type="text/javascript" language="javascript">
// Clears the textbox when it's clicked on
function ClearBox(eTheBox) {
    if (eTheBox.defaultValue==eTheBox.value){
        eTheBox.value = '';
    }
    
    if (eTheBox.style) {
        eTheBox.style.cssText = '';
    }
}

// Sends the AJAX call to validate the form.
function ValidateEmail(){
    // Invoke the Prototype AJAX class that handles
    // AJAX interactions within the prototype.js file.
    // See http://www.prototypejs.org/api/ajax/updater for more info.
    new Ajax.Updater('validation_results', '<?= $_SERVER['PHP_SELF']; ?>', 
                    {parameters:Form.serialize($('EmailValidator')),
                     asynchronous:true, 
                     evalScripts:true});
}
</script>
</head>

<body>
<div id="content">
  <!-- The div for the results to fill. -->
  <div id="validation_results"><p>&nbsp;</p></div>
  
  <form id="EmailValidator"
          name="EmailValidator"
        method="post"
        action=""
        onsubmit="return false;">
        
  <input name="EmailAddress"
           type="text"
         id="EmailAddress"
         value="Enter Email Address"
         size="40"
         maxlength="60"
         onfocus="ClearBox(this);"
         style="color:#CCCCCC;"
         onkeypress="{if (event.keyCode==13)ValidateEmail();}" />
  
  <br />
  <br />
  
  <input type="image"
           src="images/validate_button.jpg"
         name="ValidateEmail"
         id="ValidateEmail"
         value="Validate Email"
         onclick="ValidateEmail();" />
  
  </form>
</div>
</body>
</html>