var Soon = Class.create();

Soon.prototype = {
    	initialize: function() {
    },
    bindMe: function(element) {
		Event.observe(element, 'click', this.tellMe.bindAsEventListener(this), false);
    },
    tellMe: function() {
	   $('errorName').style.display='none';
	   $('firstName').className='';
	   $('errorEmail').style.display='none';
	   $('emailAddress').className='';
	   
	   if (!this.checkName()){
	    	$('errorName').style.display='block';
			$('errorName').innerHTML='Please fill in your first name';
	    	$('firstName').className='error';
	    } 
	    if (!this.checkEmail()){
	    	$('errorEmail').style.display='block';
			$('errorEmail').innerHTML='Please fill in an email address';
	    	$('emailAddress').className='error';
	    } 
	  	if (this.checkName() && this.checkEmail()) {
	  		this.animateMe();
	  		this.submitMe();
	  	}
    },
    checkName: function() {
    	if ($('firstName').value != "") { return true; }
    	return false;
    },
    checkEmail: function() {
    	var addr = $('emailAddress').value;
        
		var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
        for (i=0; i<invalidChars.length; i++) {
           if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
              return false;
           }
        }
        for (i=0; i<addr.length; i++) {
           if (addr.charCodeAt(i)>127) {
              return false;
           }
        }
        var atPos = addr.indexOf('@',0);
        if (atPos == -1) {
           return false;
        }
        if (atPos == 0) {
           return false;
        }
        if (addr.indexOf('@', atPos + 1) > - 1) {
           return false;
        }
        if (addr.indexOf('.', atPos) == -1) {
           return false;
        }
        if (addr.indexOf('@.',0) != -1) {
           return false;
        }
        if (addr.indexOf('.@',0) != -1){
           return false;
        }
        if (addr.indexOf('..',0) != -1) {
           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') {
           return false;
        }
		return true;
    },
    animateMe: function() {
	    $('throbber').style.display='block';
		$('divYesButton').style.display='none';
    },
    submitMe: function() {
    	$('generalError').style.display="none";
    	new Ajax.Request('../buzzMe.php', { parameters: Form.serialize('landingForm'), method: 'post', onSuccess: this.thankMe, onFailure: this.warnMe });
    },
    thankMe: function(theRequest) {
    	if (theRequest.status != 200) {
    		this.warnMe(theRequest);
    		return;
    	}
		$('throbber').style.display='none';
		$('divYesButton').style.display='block';
		
		$('before').style.display='none';
		new Effect.Appear('after');
		
    },
    warnMe: function(theRequest) {
    	$('generalError').style.display="block";
		if (theRequest) {
		    if (theRequest.getResponseHeader('X-B4A-responseError')) {
				$('generalError').innerHTML=theRequest.getResponseHeader('X-B4A-responseError');
		    } else {
				$('generalError').innerHTML=theRequest.status + ' -- ' + theRequest.statusText;
		    }
		}
		$('throbber').style.display='none';
    	$('divYesButton').style.display='block';
    }
}

function soonWindowOnLoad() {
	var mySoon = new Soon();
	mySoon.bindMe('divYesButton');
}

Event.observe(window, 'load', soonWindowOnLoad, false);




