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 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 phone = form.phone.value.trim();

	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, {
		complete:function(html) {
			document.getElementById("form").innerHTML = "<div id=\"confirmation\"><p><strong>Thank you!</strong></p><p>&nbsp;</p><p>An agent will contact you shortly!</p></div>" + getGoogleConversion();
			setTimeout(function() {
				window.location.replace("http://www.mydebtpayoff.net");
			},1000);
		}
	});
	
}