

var disable_wsfy_tease_div_display = false;

var title_text = {
	success:"We found <em><numfound></em> results for <em><fullname></em>",
	failure:"Register to find everyone named <b><fullname></b>"
};
var cities_text = {
	success:"<b>&raquo;</b> A few cities they live in:",
	failure:"",
	delimiter:", ",
	trail:"<i>and more...</i>",
	display:5
};
var states_text = {
	success:"<b>&raquo;</b> A few states they live in:",
	failure:"",
	delimiter:", ",
	trail:"<i>and more...</i>",
	display:12
};
var thumbnails_text = {
	success:"<b>&raquo;</b> Photos include:",
	failure:"",
	delimiter:"",
	trail:"",
	display:5
};

function loadTeaser(fn,ln)
	{
	if (fn && ln && fn != "null" && ln != "null")
		{
		ps_info = {};
		ps_info.first_name = (fn.substr(0,1)).toUpperCase()+fn.substr(1);
		ps_info.last_name = (ln.substr(0,1)).toUpperCase()+ln.substr(1);
		ps_info.full_name = ps_info.first_name+" "+ps_info.last_name;
		$.get("/PS/ps_api.php",{searchFirstName:fn,searchLastName:ln},handlePSRequest);
		
		var t_loading = document.getElementById("teaser_loading") || null;
		var t_loading_cities = document.getElementById("teaser_loading-cities") || null;
		var t_loading_states = document.getElementById("teaser_loading-states") || null;
		var t_loading_thumbs = document.getElementById("teaser_loading-thumbs") || null;
		if (t_loading) t_loading.style.display = "block";
		if (t_loading_cities) t_loading_cities.style.display = "block";
		if (t_loading_states) t_loading_states.style.display = "block";
		if (t_loading_thumbs) t_loading_thumbs.style.display = "block";
		}
	}
				
function handlePSRequest(data)
	{
	var t_title = document.getElementById("teaser_title");
	var t_states = document.getElementById("teaser_states");
	var t_cities = document.getElementById("teaser_cities");
	var t_thumbs = document.getElementById("teaser_thumbs");
	var t_loading = document.getElementById("teaser_loading") || null;
	var t_loading_cities = document.getElementById("teaser_loading-cities") || null;
	var t_loading_states = document.getElementById("teaser_loading-states") || null;
	var t_loading_thumbs = document.getElementById("teaser_loading-thumbs") || null;
	
	if (t_loading) t_loading.style.display = "none";
	if (t_loading_cities) t_loading_cities.style.display = "none";
	if (t_loading_states) t_loading_states.style.display = "none";
	if (t_loading_thumbs) t_loading_thumbs.style.display = "none";
	
	ps_info.result = eval("("+data+")");
	
	if (ps_info && ps_info.result && ps_info.result.count)
		{
		if (ps_info.result.count) t_title.innerHTML = insertData(title_text.success);
		else t_title.innerHTML = insertData(title_text.failure);

		if (psinfo_hasAtLeast(1,"cities"))
			{
			var cities_html = "<dt>"+cities_text.success+"</dt>";
			var cities_to_display = new Array();
			for (var i=0;cities_to_display.length<=cities_text.display&&i<ps_info.result.items.length;i++)
				{
				if (ps_info.result.items[i].city && !arrayContains(cities_to_display,ps_info.result.items[i].city)) cities_to_display[cities_to_display.length] = ps_info.result.items[i].city;
				}
			cities_to_display[cities_to_display.length] = cities_text.trail;
			cities_html += "<dd>";
			cities_html += cities_to_display.join(cities_text.delimiter+"</dd><dd>");
			cities_html += "</dd>";
			t_cities.innerHTML = insertData(cities_html);
			}
		else t_cities.innerHTML = "<dt>"+insertData(cities_text.failure)+"</dt>";

		if (psinfo_hasAtLeast(1,"states"))
			{
			var states_html = "<dt>"+states_text.success+"</dt>";
			var states_to_display = new Array();
			for (var i=0;states_to_display.length<=states_text.display&&i<ps_info.result.items.length;i++)
				{
				if (ps_info.result.items[i].state && !arrayContains(states_to_display,ps_info.result.items[i].state)) states_to_display[states_to_display.length] = ps_info.result.items[i].state;
				}
			states_to_display[states_to_display.length] = states_text.trail;
			states_html += "<dd>";
			states_html += states_to_display.join(states_text.delimiter+"</dd><dd>");
			states_html += "</dd>";
			t_states.innerHTML = insertData(states_html);
			}
		else t_states.innerHTML = "<dt>"+insertData(states_text.failure)+"</dt>";

		if (psinfo_hasAtLeast(1,"thumbnails or pictures"))
			{
			var thumbs_html = "<dt>"+thumbnails_text.success+"</dt>";
			var thumbs_to_display = new Array();
			for (var i=0;thumbs_to_display.length<=thumbnails_text.display&&i<ps_info.result.items.length;i++)
				{
				if (ps_info.result.items[i].thumb_url || ps_info.result.items[i].picture_url) thumbs_to_display[thumbs_to_display.length] = ps_info.result.items[i].thumb_url || ps_info.result.items[i].picture_url;
				}
			for (var i=0;i<thumbs_to_display.length;i++) thumbs_html += '<dd><img src="'+thumbs_to_display[i]+'" alt="'+ps_info.full_name+'" />'+thumbnails_text.delimiter+'</dd>';
			thumbs_html += thumbnails_text.trail;
			t_thumbs.innerHTML = insertData(thumbs_html);
			}
		else t_thumbs.innerHTML = "<dt>"+insertData(thumbnails_text.failure)+"</dt>";
		}
	
	}

function psinfo_hasAtLeast(num,ofwhat)
	{
	var num_has = 0;
	if (ps_info.result && ps_info.result.items)
		{
		for (var i=0;i<ps_info.result.items.length;i++)
			{
			if (ofwhat.indexOf("cities") != -1 && ps_info.result.items[i].city) {num_has++;continue;}
			if (ofwhat.indexOf("states") != -1 && ps_info.result.items[i].state) {num_has++;continue;}
			if (ofwhat.indexOf("locations") != -1 && ps_info.result.items[i].location) {num_has++;continue;}
			if (ofwhat.indexOf("thumbnails") != -1 && ps_info.result.items[i].thumb_url) {num_has++;continue;}
			if (ofwhat.indexOf("pictures") != -1 && ps_info.result.items[i].picture_url) {num_has++;continue;}
			//if (num_has >= num) return true;
			}
		}
	return num_has >= num;
	}

function insertData(into)
	{
	into = into.replace(/<numfound>/gi,ps_info.result.count || "");
	into = into.replace(/<firstname>/gi,ps_info.first_name || "");
	into = into.replace(/<lastname>/gi,ps_info.last_name || "");
	into = into.replace(/<fullname>/gi,ps_info.full_name || "");
	return into;
	}

function arrayContains(arr,what)
	{
	for (var i=0;i<arr.length;i++)
		{
		if (arr[i] === what) return true;
		}
	return false;	
	}

function launchItNow(fn,ln,a)
	{
	var ok_to_launch = true;
	
	var tform = document.getElementById("psForm");

	fn = fixName(fn);
	ln = fixName(ln);
	a = fixAge(a);
	
	if (fn && ln && !a) a = "33";
	
	if (fn) tform.searchFirstName.value = fn;
	else ok_to_launch = false;
	if (ln) tform.searchLastName.value = ln;
	else ok_to_launch = false;
	if (a) tform.searchAge.value = a;
	else ok_to_launch = false;

	if (ok_to_launch) submitPS(tform);
	else
		{
		if (!a) {try{tform.searchAge.focus()} catch(e){}};
		if (!ln) {try{tform.searchLastName.focus()} catch(e){}};
		if (!fn) {try{tform.searchFirstName.focus()} catch(e){}};	
		}
	}

function submitPS()
	{
	if (typeof(checkForZipFromIP) != "undefined") checkForZipFromIP();
	var tform = document.getElementById("psForm");
	if (psOnSubmit())
		{
		var fn = tform.searchFirstName.value;
		var ln = tform.searchLastName.value;
		var a = tform.searchAge.value || false;
		fn = fixName(fn);
		ln = fixName(ln);
		a = fixAge(a);
		loadTeaser(fn,ln);
		var rform = document.getElementById("regForm");
		if (rform.searchFirstName) rform.searchFirstName.value = fn;
		if (rform.searchLastName) rform.searchLastName.value = ln;
		if (rform.searchAge) rform.searchAge.value = a;

		document.getElementById("bText").style.display = "none";
		document.getElementById("psForm").style.display = "none";
		document.getElementById("regForm_d").style.display = "block";
		document.getElementById("teaseInfo").style.display = "block";
		document.getElementById("semTxt").style.marginTop = "0px";
		document.getElementById("regNow").innerHTML = 'Register Now to See Detailed Results for <b>' + fn + ' ' + ln + '</b>';
		
		fireRegOpenedPixel();
		}
	return false;
	}

function loadWSFYTeaser(fn,ln,a)
	{
	a = a || 37;
	if (fn && ln && a)
		{
		wsfy_info = {};
		wsfy_info.first_name = (fn.substr(0,1)).toUpperCase()+fn.substr(1);
		wsfy_info.last_name = (ln.substr(0,1)).toUpperCase()+ln.substr(1);
		wsfy_info.age = a;
		wsfy_info.full_name = wsfy_info.first_name+" "+wsfy_info.last_name;
		$.get("/WSFY/wsfy_api.php",{firstName:fn,lastName:ln,age:a},handleWSFYRequest);
		}
	}

function handleWSFYRequest(data)
	{
	var delay = 2000;
	var result_obj = eval("("+data+")");
	wsfy_info.result = (result_obj && result_obj.wsfy) ? result_obj.wsfy : null;
	var tc = (wsfy_info.result && wsfy_info.result.totalCount) ? wsfy_info.result.totalCount : 0;
	if (tc)
		{
		var tspot = document.getElementById("wsfy_teaser_spot");
		tspot.getElementsByTagName("h3")[0].innerHTML = 'Searching ...';
		tspot.getElementsByTagName("ul")[0].innerHTML = '<img src="http://a1.mylife.com/a/landa/i/ajax_loader-md_dots.gif" alt="loading..." />';
		if (!disable_wsfy_tease_div_display)
			{
			document.getElementById("pre_tease").style.display = "none";
			tspot.style.display = "block";
			}

		handleWSFYRequest_postdelay = function()
			{
			var tc = wsfy_info.result.totalCount;
			var tspot = document.getElementById("wsfy_teaser_spot");
			var title = tspot.getElementsByTagName("h3")[0];
			//var sub_title = tspot.getElementsByTagName("h2")[0];
			var list = tspot.getElementsByTagName("ul")[0];
			tspot.getElementsByTagName("h4")[0].innerHTML = 'Fill out this form and find out who they are!';
			list.innerHTML = "";
			title.innerHTML = "<strong>"+tc+"</strong> "+(tc>1?"people are":"person is")+" searching for &quot;"+wsfy_info.full_name+"&quot;";
			var li;
			if (wsfy_info.result.numFemales)
				{
				li = document.createElement("li");
				li.innerHTML = "<strong>"+wsfy_info.result.numFemales+"</strong> "+(wsfy_info.result.numFemales == 1 ? "is" : "are")+" female";
				list.appendChild(li);
				}
			if (wsfy_info.result.numMales)
				{
				li = document.createElement("li");
				li.innerHTML = "<strong>"+wsfy_info.result.numMales+"</strong> "+(wsfy_info.result.numMales == 1 ? "is" : "are")+" male";
				list.appendChild(li);
				}
			if (wsfy_info.result.numSearchedInLastWeek)
				{
				li = document.createElement("li");
				li.innerHTML = "<strong>"+wsfy_info.result.numSearchedInLastWeek+"</strong> "+(wsfy_info.result.numSearchedInLastWeek == 1 ? "has" : "have")+" searched in the last week";
				list.appendChild(li);
				}
			if (!disable_wsfy_tease_div_display)
				{
				document.getElementById("pre_tease").style.display = "none";
				tspot.style.display = "block";
				}
			}
		if (typeof(wsfy_teaser_timeout) != "undefined") clearTimeout(wsfy_teaser_timeout);
		wsfy_teaser_timeout = setTimeout("handleWSFYRequest_postdelay()",delay);
		}
	}

function checkForWSFYTease()
	{
	var rform = document.getElementById("regForm");
	var fn = rform.firstName.value || false;
	var ln = rform.lastName.value || false;
	var a = 37;
	if (fn && ln) loadWSFYTeaser(fn,ln,a);
	}

function preTeaser()
	{
	document.getElementById("pre_tease").style.display = "none";
	document.getElementById("pre_base").style.display = "none";
	document.getElementById("wsfy_teaser_spot").style.display = "block";
	document.getElementById("pre_baseGreen").style.display = "block";
	}

function changeZipCode()
	{
	document.getElementById("zip_replacement").style.display = "none";
	document.getElementById("zip_input").style.display = "block";
	var zip_field = document.getElementById("regForm").zip;
	zip_field.value = "";
	if (zip_field.focus) zip_field.focus();
	return false;
	}

function handleIPInfoRequest(data)
	{
	var ip_info = eval("("+data+")");
	if ($_GET["debug"]) alert(data);
	if (ip_info.postalCode)
		{
		document.getElementById("zip_input").style.display = "none";
		document.getElementById("zip_replace_num").innerHTML = ip_info.postalCode;
		document.getElementById("zip_replacement").style.display = "block";
		document.getElementById("regForm").zip.value = ip_info.postalCode;
		}
	else
		{
		var txt = "";
		for (var k in ip_info) txt += k+" = "+ip_info[k]+", &nbsp;";
		document.getElementById("zip_replacement").innerHTML = txt;
		}
	}

function checkForZipFromIP()
	{
	if (typeof(checkForZipFromIP_once) == "undefined")
		{
		var ip_to_send = $_GET["cip"] || "client";
		if (document.getElementById("zip_replacement")) $.get("/GeoIP/geoip_api.php",{ip:ip_to_send},handleIPInfoRequest);
		checkForZipFromIP_once = true;
		}
	}

$(function()
	{
	launchItNow($_GET["searchFirstName"] || "",$_GET["searchLastName"] || "",$_GET["searchAge"] || "");
	//$("#psForm [name='searchFirstName']").bind("keypress",checkForZipFromIP);
	if ($.browser.msie && $.browser.version == "6.0")
		{
		$(".regTxt").css("backgroundImage","url(http://a1.mylife.com/a/landa/i/vvgps_reg-head.gif)");
		$(".regTxtGreen").css("backgroundImage","url(http://a1.mylife.com/a/landa/i/vvgps_reg-head-green.gif)");
		$(".regBase").css("backgroundImage","url(http://a1.mylife.com/a/landa/i/vvgps_reg-base.gif)");
		$(".regBaseGreen").css("backgroundImage","url(http://a1.mylife.com/a/landa/i/vvgps_reg-base-green.gif)");
		$(".regBaseGreen").css("position","relative");
		}
	if ($_GET["schoolId"]) $("#regForm").prepend('<input type="hidden" name="schoolId" value="'+$_GET["schoolId"]+'" />');
	});