d = document;
w = window;

function in_array(val, arr){
	for (a = 0; a < arr.length; a++){
		if (arr[a] == val) return true;
	}
	return false;
}

function openAjax(responseDest, destType, openMethod, openURL){
	var xmlhttp;

	if (w.XMLHttpRequest) xmlhttp = new XMLHttpRequest(); //IE7+/C/FF/S/O
	else if (w.ActiveXObject) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE5-6
	else alert("Your browser does not support XMLHTTP!");

	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4){
			if (destType == 'innerHTML') d.getElementById(responseDest).innerHTML = xmlhttp.responseText;
			else if (destType == 'value') d.getElementById(responseDest).value = xmlhttp.responseText;
		}
	}

	xmlhttp.open(openMethod, openURL, true);
	xmlhttp.send(null);
}

function verify(){
	error = 0;

	exts = ['ac','ad','ae','aero','af','ag','ai','al','am','an','ao','aq','ar','arpa','as','asia','at','au','aw','ax','az','ba','bb','bd','be','bf','bg','bh','bi','biz','bj','bl','bm','bn','bo','br','bs','bt','bv','bw','by','bz','ca','cat','cc','cd','cf','cg','ch','ci','ck','cl','cm','cn','co','com','coop','cr','cs','cu','cv','cx','cy','cz','de','dj','dk','dm','do','dz','ec','edu','ee','eg','eh','er','es','et','eu','fi','fj','fk','fm','fo','fr','ga','gb','gd','ge','gf','gg','gh','gi','gl','gm','gn','gov','gp','gq','gr','gs','gt','gu','gw','gy','hk','hm','hn','hr','ht','hu','id','ie','il','im','in','info','int','io','iq','ir','is','it','je','jm','jo','jobs','jp','ke','kg','kh','ki','km','kn','kp','kr','kw','ky','kz','la','lb','lc','li','lk','lr','ls','lt','lu','lv','ly','ma','mc','md','me','mf','mg','mh','mil','mk','ml','mm','mn','mo','mobi','mp','mq','mr','ms','mt','mu','museum','mv','mw','mx','my','mz','na','name','nato','nc','ne','net','nf','ng','ni','nl','no','np','nr','nu','nz','om','org','pa','pe','pf','pg','ph','pk','pl','pm','pn','pr','pro','ps','pt','pw','py','qa','re','ro','rs','ru','rw','sa','sb','sc','sd','se','sg','sh','si','sj','sk','sl','sm','sn','so','sr','st','su','sv','sy','sz','tc','td','tel','tf','tg','th','tj','tk','tm','tn','to','tp','tr','travel','tt','tv','tw','tz','ua','ug','uk','um','us','uy','uz','va','vc','ve','vg','vi','vn','vu','wf','ws','ye','yt','yu','za','zm','zr','zw'];
	formEmail = d.getElementById('email').value;
	emailDB = d.getElementById('emaildb').value;
	splitAt = formEmail.split('@');
	domain = splitAt[splitAt.length - 1];
	splitDomainPeriod = splitAt[splitAt.length - 1].split('.');

	if (formEmail == ''){ //email address is blank
		alert('Please enter an email address.');
		error++;
	} else if (formEmail != ''){
		if (formEmail.length < 8 || formEmail.length > 256){ //email address total characters is less than 8 characters or greater than 256 characters
			alert('Your email address is not valid.');
			error++;
		} else if (splitAt.length > 2){ //more than two @ symbols
			alert('Your email address is not valid.');
			error++;
		} else if (splitAt[0].match(/^\"/) && splitAt[0].match(/\"$/)){ //local-part is a quoted-string
			alert('We do not allow e-mail addresses with quoted-strings for the local-part portion of the address.');
			error++;
		} else if (splitAt[0].length < 1 || splitAt[0].length > 64){ //local-part is less than 1 character or greater than 64 characters
			alert('Your email address is not valid.');
			error++;
		} else if (splitAt[0].match(/\.{2}/)){ //local-part contains consecutive periods
			alert('Your email address is not valid.');
			error++;
		} else if (splitAt[0].match(/^\./) || splitAt[0].match(/\.$/)){ //local-part starts or ends with a period
			alert('Your email address is not valid.');
			error++;
		} else if (splitAt[0].match(/[^a-z,0-9,\!,\#,\$,\%,\&,\',\*,\+,\-,\/,\=,\?,\^,\_,\`,\{,\|,\},\~,\.]/i)){ //local-part contains characters other than a-z A-Z 0-9 ! # $ % & ' * + - / = ? ^ _ ` { | } ~ .
			alert('Your email address is not valid.');
			error++;
		} else if (domain.match(/^\[/) && domain.match(/\]$/)){ //domain is ip address literal
			ip = splitAt[splitAt.length - 1].replace(/[\[|\]]/g, '');
			if (ip.indexOf('.') != -1) splitIpPeriod = ip.split('.');

			if (ip.indexOf('.') == -1){ //ip address has no periods
				alert('Your email address is not valid.');
				error++;
			} else if (ip.match(/\./g).length !== 3){ //ip address doesnt have 3 periods
				alert('Your email address is not valid.');
				error++;
			} else if (ip.match(/[^0-9\.]/)){ //ip address contains characters other than numbers or periods
				alert('Your email address is not valid.');
				error++;
			} else if(splitIpPeriod[0].length < 1 || splitIpPeriod[0].length > 3 || splitIpPeriod[1].length < 1 || splitIpPeriod[1].length > 3 || splitIpPeriod[2].length < 1 || splitIpPeriod[2].length > 3 || splitIpPeriod[3].length < 1 || splitIpPeriod[3].length > 3){ //an ip block is less than 1 character or greater than 3 characters
				alert('Your email address is not valid.');
				error++;
			} else query += '&email=' + formEmail;
		} else if (splitAt[splitAt.length - 1] < 6 || splitAt[splitAt.length - 1] > 255){ //domain is less than 6 characters or greater than 255 characters
			alert('Your email address is not valid.');
			error++;
		} else if (!in_array(splitDomainPeriod[splitDomainPeriod.length - 1], exts)){ //domain extension not in array
			alert('Your email address is not valid.');
			error++;
		} else if (emailDB == 1){
			alert('Email Address is already registered.');
			error++;
		}
	}

	if (error == 0) d.contactForm.submit();
}
