var http = getHTTPObject();

var url = "generate_validation_email.php";
var params = "email=aaaaaa@yahoo.com";
//var params = emailAddress;
var debug = true;


function handler() {//Call a function when the state changes.
	if(http.readyState == 4 && http.status == 200) {
		//alert(http.responseText);
	}
}

function getMethod() {
	http.open("GET", url+"?"+params, true);
	http.onreadystatechange = handler;
	http.send(null);
}

function postMethod(validEmail) {
	
	//MA comment - not sure why the url variable wouldn't work here -- so just hardcoded it the validation email page
	http.open("POST", "generate_validation_email.php", true);
	var newparam = "email=" + validEmail;
	//Send the proper header infomation along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	http.onreadystatechange = handler;
	//alert(url);
	http.send(newparam);
}




function validateEmail(addr,man,db) {
	
pageTracker._trackPageview('/outgoing/Weekly-Email-Signup-ATTEMPT');

if (addr == '' && man) {
   if (db) alert('email address is mandatory');
   return false;
}
if (addr == '') return true;
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) > 0) {
   if (db) alert('email address should not contain a plus sign');
   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;
}



function processEmail(isValid,validEmail)
{

pageTracker._trackPageview('/outgoing/Weekly-Email-Signup-SUCCESS');
                                                                                                                                                                                   
if (isValid)
   {
    document.getElementById('btnJoinEmail').disabled = true;
    
    document.getElementById('emailsignup').innerHTML = '<font size=2> Verification e-mail will be sent to <B> ' + validEmail + '</B>. You must click the link in this e-mail to start receiving the Top 5 CD and Savings rates!';
    postMethod(validEmail);
   }

}



function getHTTPObject() {
	var http = false;
	//Use IE's ActiveX items to load the file.
	if(typeof ActiveXObject != 'undefined') {
		try {http = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) {
			try {http = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (E) {http = false;}
		}
	//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
	} else if (XMLHttpRequest) {
		try {http = new XMLHttpRequest();}
		catch (e) {http = false;}
	}
	return http;
}

