String.prototype.trim = function() {
	return this.replace(/^\s*/,'').replace(/\s*$/,'');
}

var Ajax = {
	init:function() {
		var xmlhttp;
		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e) { 
			try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(e) { 
				try { xmlhttp = new XMLHttpRequest(); } 
				catch(e) { xmlhttp = false; }
			}
		}
		return xmlhttp;
	},
	send:function(url,options) {
		
		options = options || {};
		options.complete = options.complete || function() {};
		options.method = (options.method || "post").toUpperCase();
		
		// don't continue
		var xmlhttp = this.init();
		if(!xmlhttp) { return; }
		
		// set the readystatechange
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				options.complete(xmlhttp.responseText.trim());
			}
		}
		
		// was it a get or a post?
		if(options.method == "GET") {
			xmlhttp.open(options.method,url,true);
			xmlhttp.send(null);
		} else {
			var q = url.indexOf("?");
			xmlhttp.open(options.method,url.substring(0,q),true);
			xmlhttp.setRequestHeader("Method",options.method + " " + url.substring(0,q) + " HTTP/1.1");
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xmlhttp.send(url.substring(q + 1));
		}
		
	}
};

function getGoogleConversion() {
	return '';//"<img height=\"1\" width=\"1\" border=\"0\" src=\"http://www.googleadservices.com/pagead/conversion/1067342189/?label=YJY6CKf0ahDtsvn8Aw&amp;guid=ON&amp;script=0\" style=\"margin:0px; padding:0px;\" />";
}

function getCapterraConversion(){
  //return "<script type=\"text/javascript\">var capterra_vkey = \"2e2aecc993f7e2b1ed2f7a11408be38a\"; var capterra_vid = \"2076203\"; var capterra_prefix = ((\"https:\" == document.location.protocol) ? \"https://ct.capterra.com\" : \"http://ct.capterra.com\"); document.write(unescape(\"%3Cscript src='\" + capterra_prefix + \"/capterra_tracker.js?vid=\" + capterra_vid + \"&vkey=\" + capterra_vkey + \"' type='text/javascript'%3E%3C/script%3E\"));</script>"; 
  return "<img height=\"1\" width=\"1\" border=\"0\" src=\"http://ct.capterra.com/capterra_tracker.js?vid=2076203&vkey=2e2aecc993f7e2b1ed2f7a11408be38a\"/>";
}

function validate(what,value) {
	if(what == "email") {
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		return filter.test(value);
	} else if(what == "phone") {
		value = value.trim();
		return value.length > 7;
	}
}

function beforeDemo() {

	var form = document.forms["demo"];
	var firstname = form.firstname.value.trim();
	var lastname = form.lastname.value.trim();
	var email = form.email.value.trim();
	var honeypot = form.email_address.value.trim();
	var phone = form.phone.value.trim();
	var referrer = form.referrer.value.trim();

	if(honeypot != '') {
		return;
	}

	if(firstname == '' || lastname == '') {
		return;
	} 
	
	if(email == '' || !validate("email",email)) {
		return;
	}
	
	if(phone == '' || !validate("phone",phone)) {
		return;
	}
	
	Ajax.send("send_email.php?c=" + new Date().getTime() + "&firstname=" + firstname + "&lastname=" + lastname + "&email=" + email + "&phone=" + phone + "&referrer=" + referrer, {
		complete:function(html) {
			document.getElementById("form").innerHTML = "<div id=\"confirmation\"><p><strong>Thank you!</strong></p><p>&nbsp;</p><p>Access to your \"Free Demo\" has been sent to " + email + "</p></div>" + getGoogleConversion() + getCapterraConversion();
			setTimeout(function() {
				window.location.replace("http://www.demostorage.com");
			},2000);
		}
	});
	
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25869426-6']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

