﻿// EmailForm.js
// Todd Reeves - 10/10/2006

var sEmailText = "  : e-mail :";
var sSubscribeURL = "Subscribe/Subscribe.aspx";
var sThankYouURL = "Subscribe/ThankYouJS.aspx";
var xmlhttp = null;

function EmailFocusIn(txtEmail)
{
    setStyle(txtEmail, "background-color: #FFFFFF;");
    
    if(txtEmail.value.indexOf(": e-mail :") != -1)
        txtEmail.value = "";
}

function EmailFocusOut(txtEmail)
{
    setStyle(txtEmail, "background-color: #FFFFCD;");
    
    if(txtEmail.value == "")
        txtEmail.value = sEmailText;
}


function EmailSubscribe()
{
    var result;
    var body = null;
    var inputText = document.getElementById('txtEmail');
    var value = inputText.value;
    
    if(value && value != '' && value != sEmailText)
    {
        if(validateEmail(value, 1, 1))
        {
            body = inputText.name + "=" + value;

            xmlhttp = getXMLHttpObject();
            xmlhttp.open('POST', sSubscribeURL, false);
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttp.send(body);
            processStatusChange();
        }
    }
    
   
    else
        alert("Please enter a valid email address.");  
    
    return;
}

function processStatusChange() 
{
    // only if "complete"
    if (xmlhttp.readyState == 4)
    {
        // only if "OK"
        if (xmlhttp.status == 200)
        {
            var inputText = document.getElementById('txtEmail');
            var newWindow = window.open(sThankYouURL + '?sub=' + inputText.value, "ThankYouWindow", 'width=500,height=245,resizable=0') 
            inputText.value = sEmailText;
        }
        
        else
            alert("There was a problem processing your subscription:\n" + xmlhttp.statusText);
    }
}


// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateEmail(addr,man,db)
{
    if (addr == '' && man) {
       if (db) alert('Email address is mandatory');
       return false;
    }
    var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
    for (i=0; i<invalidChars.length; i++) {
       if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
          if (db) alert('Email address contains invalid characters');
          return false;
       }
    }
    for (i=0; i<addr.length; i++) {
       if (addr.charCodeAt(i)>127) {
          if (db) alert("Email address contains non ascii characters.");
          return false;
       }
    }

    var atPos = addr.indexOf('@',0);
    if (atPos == -1) {
       if (db) alert('Email address must contain an @');
       return false;
    }
    if (atPos == 0) {
       if (db) alert('Email address must not start with @');
       return false;
    }
    if (addr.indexOf('@', atPos + 1) > - 1) {
       if (db) alert('Email address must contain only one @');
       return false;
    }
    if (addr.indexOf('.', atPos) == -1) {
       if (db) alert('Email address must contain a period in the domain name');
       return false;
    }
    if (addr.indexOf('@.',0) != -1) {
       if (db) alert('Period must not immediately follow @ in email address');
       return false;
    }
    if (addr.indexOf('.@',0) != -1){
       if (db) alert('Period must not immediately precede @ in email address');
       return false;
    }
    if (addr.indexOf('..',0) != -1) {
       if (db) alert('Two periods must not be adjacent in email address');
       return false;
    }
    var suffix = addr.substring(addr.lastIndexOf('.')+1);
    if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
       if (db) alert('Invalid primary domain in email address');
       return false;
    }
    return true;

}