// 
// Javascript to support the registration of or users email for the newsletter
// via AJAX.
//
//

// Email validatiom function
function validateEmail(addr,man,db) {
	if (addr == '') return true;
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
   		if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
    		if (db) alert('e-mailadres bevat ongeldige tekens');
			return false;
		}
	}
	for (i=0; i<addr.length; i++) {
		if (addr.charCodeAt(i)>127) {
			if (db) alert("e-mailadres bevat ongeldige tekens.");
			return false;
		}
	}

	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
		if (db) alert('het e-mailadres moet een @ bevatten');
		return false;
	}
	if (atPos == 0) {
		if (db) alert('het e-mailadres begint nooit met een @');
		return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
		if (db) alert('een e-mailadres kan slechts één @ bevatten');
		return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
		if (db) alert('een e-mailadres bevat altijd een punt in de domeinnaam');
		return false;
	}
	if (addr.indexOf('@.',0) != -1) {
		if (db) alert('na een @ kan nooit direct een punt komen');
		return false;
	}
	if (addr.indexOf('.@',0) != -1){
		if (db) alert('er kan nooit een punt direct vóór het @ staan');
		return false;
	}
	if (addr.indexOf('..',0) != -1) {
		if (db) alert('er kunnen nooit twee opeenvolgende punten in een e-mailadres staan');
		return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'nl' && 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('het e-mailadres bevat een foutive domeinnaam');
		return false;
	}
	return true;
}
	
function validEmail(jsEmail) {
	// check that the string isn't empty ..
	if (jsEmail.length<=0) {
		alert("U moet een geldig e-mailadres invoeren.");
		return false;
	}
		
	if (!validateEmail(jsEmail,0,true)) return false;
	// check if there is more than 1 @ ie more than 1 email address..
	if (jsEmail.lastIndexOf("@") != jsEmail.indexOf("@") ) {
		alert("U heeft meer dan één e-mailadres opgegeven.");
		return false;
	}
		
	// check for spaces in the email address ..
	if (jsEmail.indexOf(" ") >= 0) {
		alert("There seems to be spaces in your email address, can you please remove any spaces.");
		return false;
	}
		
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<jsEmail.length; i++) {
   		if ((invalidChars.indexOf(jsEmail.charAt(i),0) > -1) || (jsEmail.charCodeAt(i)>127)){
   			alert("Uw e-mailadres bevat ongeldige tekens ["+invalidChars+"], controleer uw e-mailadres.");
  			return false;
		}
	}
		
	return true;
}
