var titleIn = location.href;
var ifs="";
var inFriendStuff = new Array("from_First_Name","from_Last_Name","from_Email");
			
if(titleIn.indexOf("?") > -1){
	var infoIn = titleIn.split("?");
	var fromInfo = infoIn[1].split("&");
	var baseURL = infoIn[0].substring(0,titleIn.indexOf("/",8));
}else{
	if(titleIn.indexOf("ekgleads") > -1){
		titleIn = document.scripts[2].src;
		if(titleIn == ""){
			titleIn = document.scripts[3].src;
		}
		baseURL = titleIn.substring(0,titleIn.indexOf("/",8));
	}

	if(typeof(window['First_Name']) != "undefined" && typeof(window['Last_Name']) != "undefined" && typeof(window['Email']) != "undefined"){
		var fromInfo = new Array();
		fromInfo[0] = "from_First_Name=" + First_Name;
		fromInfo[1] = "from_Last_Name=" + Last_Name;
		fromInfo[2] = "from_Email=" + Email;
	}else{
		var fromInfo = new Array(inFriendStuff.length);
	}
}


var URL = location.href.substr(location.href.indexOf("//")+2);
var baseURL = URL.substr(0,URL.indexOf("/"));

function doSTAF(){
	if(document.forms[0].First_Name.value == "" || document.forms[0].Last_Name.value == ""){
		alert("You must fill in your First and Last Name so your friend knows who sent them the link.");
		return false;
	}
	if(document.forms[0].Email.value == ""){
		alert("You must fill in your Email Address so your friend knows who sent them the link.");
		return false;
	}
	//alert(URL);
	openSTAF();
//end doSTAF
}


function openSTAF(){
	var sendString = "http://" + baseURL + "/sendtoafriend/default.asp?First_Name=" + document.forms[0].First_Name.value + "&Last_Name=" + document.forms[0].Last_Name.value + "&Email=" + document.forms[0].Email.value + "&Access_Code=" + document.forms[0].Access_code.value + "&URL=" + URL + "";
	var myWin = window.open(sendString,'Email_A_Friend','width=800,height=500,top=25,left=25');
	myWin.focus();
//end openSTAF
}


function populateSTAF(){
	if(titleIn.indexOf("?") == -1 && (typeof(window['from_First_Name']) == "undefined" && typeof(window['from_Last_Name']) == "undefined" && typeof(window['from_Email']) == "undefined")){
		ifs = '\n<table border=0 align="center" width="350">\n';
		for(i=0; i<inFriendStuff.length; i++){
			ifs += '<tr>\n<td align="right" width="175" class="body_text">Your ' + inFriendStuff[i].replace(/_/g," ").substr(4) + ':</td>\n<td align="left" width="175"><input type="text" name="' + inFriendStuff[i] + '"></td>\n</tr>\n';
		}
		ifs += '</table>\n';
	}else{
		for(i=0; i<inFriendStuff.length; i++){
			ifs += '<input type="hidden" name="' + inFriendStuff[i] + '">\n';
		}
		document.getElementById('inFriendInfo').style.height = "0px";
	}

	document.getElementById('inFriendInfo').innerHTML = ifs;
				
	if(titleIn.indexOf("?") > -1 || (typeof(window['First_Name']) != "undefined" && typeof(window['Last_Name']) != "undefined" && typeof(window['Email']) != "undefined")){
		for(f=0; f<fromInfo.length; f++){
			var nmvl = fromInfo[f].split("=");
			var nm = nmvl[0];
			var vl = nmvl[1];
//alert(nm + " : " + vl);
			if(nm.indexOf("from_") > -1 || nm == "confirm"){
				document.form[nm].value = vl;
			}else{
				document.form['from_' + nm].value = vl;
			}
		}
	}

//end populateSTAF
}


function stafCheck(){
	var tofrom = "";
	for(s=0; s<document.form.length; s++){
		if(document.form[s].type == "select-one" && document.form[s].selectedIndex == 0){
			if(document.form[s].name.indexOf("Access_Code") > -1){
				alert("Please select your Program.");
				document.form[document.form[s].name].focus();
				return false;
			}else{
				alert("Please select your " + tofrom + document.form[s].name.replace(/_/g," ").substr(document.form[s].name.indexOf("_")+1));
				document.form[document.form[s].name].focus();
				return false;
			}
		}
		if(document.form[s].type == "text" && document.form[s].value == ""){
			if(document.form[s].name.indexOf("to_") > -1){
				tofrom = "friend's ";
			}else{
				tofrom = "";
			}
			alert("Please fill in your " + tofrom + document.form[s].name.replace(/_/g," ").substr(document.form[s].name.indexOf("_")+1));
			document.form[document.form[s].name].focus();
			return false;
		}
		if(document.form[s].name.indexOf("Email") > -1 && document.form[s].value != ""){
			var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
			var returnval = emailfilter.test(document.form[s].value);
			if (returnval == false){
				alert("The Email address you entered is invalid.\nPlease enter a valid Email address.")
				document.form[document.form[s].name].focus();
				return false;
			}
		}
	}
//end stafCheck
}