var ApplicateNewsLetter = Class.create();

ApplicateNewsLetter.prototype = {
    	initialize: function() {
    	this.myPostUrl = '_ajaxApplicateNewsLetter.php';
    },
    bindMe: function(element) {
		Event.observe(element, 'click', this.tellMe.bindAsEventListener(this), false);
    },
    tellMe: function() {
		if (this.checkMe()) {
		    this.animateMe();
		    this.submitMe();  
		} else {
			$('errorMessageNewsLetter').style.display='block';
			$('errorMessageNewsLetter').style.color='red';
			$('errorMessageNewsLetter').innerHTML=noValidEmailAddress;
		}
    },
    checkMe: function() {
		var addr = $('emailadresNieuwsbrief').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() {
		$('errorMessageNewsLetter').innerHTML='';
	    $('throbber').style.display='block';
		$('divYesButton').style.display='none';
    },
    submitMe: function() {
		new Ajax.Request(this.myPostUrl, { parameters: Form.serialize('applicationForm'), 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';
		
		$('errorMessageNewsLetter').style.color="green";
		$('errorMessageNewsLetter').innerHTML='Uw emailadres is opgeslagen';
    	//new Effect.Appear('errorMessageNewsLetter', { delay: 0.2, duration: 3.5, from: 1, to: 0, scope: 'global'});
    	//$('errorMessageNewsLetter').style.display='none';
    },
    warnMe: function(theRequest) {
		if (theRequest) {
			$('errorMessageNewsLetter').style.color="red";
		    if (theRequest.getResponseHeader('X-B4A-responseError')) {
				$('errorMessageNewsLetter').innerHTML=theRequest.getResponseHeader('X-B4A-responseError');
		    } else {
				$('errorMessageNewsLetter').innerHTML=theRequest.status + ' -- ' + theRequest.statusText;
		    }
		}
		$('throbber').style.display='none';
    	$('divYesButton').style.display='block';
		
    }
}

function applicateNewsLetterWindowOnLoad() {
	var myApplicateNewsLetter = new ApplicateNewsLetter();
	myApplicateNewsLetter.bindMe('divYesButton');
}

Event.observe(window, 'load', applicateNewsLetterWindowOnLoad, false);




