var isIE = document.all && navigator.userAgent.match(/MSIE/);
var isIE6 = document.all && navigator.userAgent.match(/MSIE [56]/);
var xmlHttpReq;
var uid = "";
var signupId = "";

/*
 * Checks for a referring signupid (uid is only made from a signup right now)
 */
function getSignupId()
{
	var signupIdArray = window.location.search.match(/\?signupid=([^&]+)/);
	
	if (signupIdArray)
		signupId = signupIdArray[1];
	else
		signupId = "";
}

/*
 * Creates the XMLHTTP Object
 */
function ddCreateXMLHttpRequest()
{
	if (isIE6)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else
		return new XMLHttpRequest();
}

/*
 * Function validates email address format
 */
function ddValidEmail(pString)
{
	// More forgiving, allows student.level@major.university.edu, and checks this for the absence of ; [ ] * < > " and '
	return (/^([\w|\-]+)(\.[\w|\-]+)*@([\w|\-]+)(\.[\w|\-]+)+$/.test(pString) && !(/[;\[\]\*<>"']/.test(pString)));
}

/*
 * Validates and sends TAF email
 */
function sendMail()
{
	var showError = false;
	
	if (!ddValidEmail(document.taf.taf4.value) && document.taf.taf4.value != "")
	{
		showError = true;
		document.taf.taf4.focus();
	}
	if (!ddValidEmail(document.taf.taf3.value) && document.taf.taf3.value != "")
	{
		showError = true;
		document.taf.taf3.focus();
	}
	if (!ddValidEmail(document.taf.taf2.value) && document.taf.taf2.value != "")
	{
		showError = true;
		document.taf.taf2.focus();
	}
	if ((!ddValidEmail(document.taf.taf1.value) && document.taf.taf1.value != "")
	   || (document.taf.taf1.value == "" && document.taf.taf2.value == "" && document.taf.taf3.value == "" && document.taf.taf4.value == ""))
	{
		showError = true;
		document.taf.taf1.focus();
	}

	if (showError)
		alert("Please enter a valid Email.");
	else
	{
		// send the email
		xmlHttpReq = ddCreateXMLHttpRequest();
		xmlHttpReq.open("post", "kiterunnerfunction.php", true);
		xmlHttpReq.onreadystatechange = function() { };
		xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		xmlHttpReq.send("taf1=" + escape(document.taf.taf1.value) + "&taf2=" + escape(document.taf.taf2.value)
		 + "&taf3=" + escape(document.taf.taf3.value) + "&taf4=" + escape(document.taf.taf4.value)
		 + (uid.length > 0 ? "&uid=" + uid : (signupId.length > 0 ? "&signupid=" + signupId : "")));
		
		document.getElementById("taf").style.display = "none";
		document.getElementById("tafthankyou").style.display = "";
	}
}
		
/*
 * Re-shows the TAF form, and clears it
 */
function showSendMail()
{
	document.taf.reset();
	document.getElementById("tafthankyou").style.display = "none";
	document.getElementById("taf").style.display = "";
}

