
var defEmailVal = 'putyouremail@here.com';
var emailRegex  = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;

// setup floating element
$(document).ready(function() {
	if($(window).height()>900) $("#give").hide();
	$('#email').attr('value',defEmailVal).removeAttr('disabled');
});
// reset floating element on browser change
$(window).resize(function() {
	if($(window).height()>900) $("#give").hide();
	$('#email').attr('value',defEmailVal).removeAttr('disabled');
});
// change opacity of floating icon while scrolling
$(window).scroll(function(){
	var $lev = 1-($(document).scrollTop()/80);
	if($lev<=1){$("#give").css({opacity: $lev}); }
});
// trim whitespace off ends of a string
function trim(s) { s = s.replace(/^\s+/, ''); for(var i=s.length-1; i>= 0; i--) { if(/\S/.test(s.charAt(i))) { s = s.substring(0, i+1); break; } } return s.replace(/^\s+|\s+$/, ''); }

// urlencode a string
function urlencode(str) { return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A'); }

// animation and form submit
function formSubmit(){
	var s = $('#submit'); var p=parseInt(s.attr('right')); var e=$('#email'); var v=trim(e.val());
	if(v!=defEmailVal && v!='' && emailRegex.test(v) && (isNaN(p) || p<332)) {
		s.animate({ right: "332px" }, 2000);
		e.val('').animate({ opacity: 0}, 500).delay(1000).animate({ opacity: 1}, 2000);
		$("#mail-status").animate({opacity: 0}, 500);
		emailSubmit(v);
		setTimeout("formSubmitted()", 1500);
	} else {
		$("#mail-status").html("please enter your <i>real</i> email address");
	}
}
// show success message when form submittend
function formSubmitted() { $('#email').attr({ value:'Thanks, we will be in touch!',disabled:'disabled' }); }
// pass email to server
function emailSubmit(email) {
	var xh = (window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
	xh.open('GET','http://graphux.com/signup.php?email='+urlencode(email),true);
	xh.send(null);
}

// clear input text on focus
function resetInput() {
	var e=$("#email"); var v = trim(e.val());
	if(v==defEmailVal) e.val('');
	else if(v=='') e.val(defEmailVal);
	$("#mail-status").text("we will not spam or do anything nefarious with your email, for real ").animate({opacity: 1}, 500);
}

