
var runonlogin = null;
var globalslider = null;
var login_page=null;
var login_runner=null;

function login(page,runner,err)
{
	if(page==null || page=="null")
	{
		page=document.getElementById("currentpage").innerHTML;
	}
	
	runonlogin=runner;
	login_runner=runner;
	login_page = page;
	if(!err)
	err="";
	else
	err = "&amp;error="+encodeURIComponent(err);
	
	loadpopup("/loginform.jsp?page="+encodeURIComponent(page)+err);
}
function forgotpass(page)
{
	if(page==null || page=="null")
	{
		page=document.getElementById("currentpage").innerHTML;
	}
	loadpopup("/forgotpass.jsp?page="+encodeURIComponent(page));
}

function resetpassword(page)
{
	if(page==null || page=="null")
	{
		page=document.getElementById("currentpage").innerHTML;
	}
	loadpopup("/resetpassword.jsp?page="+encodeURIComponent(page)+"&email="+encodeURIComponent(document.getElementById("emailaddress").value));
}
function popupReport(subject,bug)
{
	loadpopup("/report.jsp?subject="+encodeURIComponent(subject)+"&bug="+encodeURIComponent(bug));

}
function reportBug()
{
var name = "";
var email = "";
if (document.getElementById("name") != null && document.getElementById("email") != null)
{
	name = "&name=" + encodeURIComponent(document.getElementById("name").value);
	email = "&email=" + encodeURIComponent(document.getElementById("email").value);
}

var ajax = loadAjax("/formvalidator.jsp?text=" + encodeURIComponent(document.getElementById("context").value) + "&sub=" + encodeURIComponent(document.getElementById("subject").value) + name + email);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4) {
			switch (ajax.responseText)
			{
				case '1': // both are no good
					ge('report-err').innerHTML = "<ul><li>There are invalid characters in your subject</li><li>There are invalid characters in your text</li></ul>";
					ge('report-err').style.display = "";
					break;
				case '2': // text is no good
					ge('report-err').innerHTML = "<ul><li>There are invalid characters in your text</li></ul>";
					ge('report-err').style.display = "";
					break;
				case '3': // sub is no good
					ge('report-err').innerHTML = "<ul><li>There are invalid characters in your subject</li></ul>";
					ge('report-err').style.display = "";
					break;
				default: // all good
					var browser = navigator.userAgent;
					loadpopup("thanksreport.jsp?subject="+encodeURIComponent(document.getElementById("subject").value)+"&context="+encodeURIComponent(document.getElementById("context").value) + "&browser=" + encodeURIComponent(browser) + name + email);				
			}
		}
	};
	ajax.send(null);
}

function finishReset()
{
	if(!hashpassword())
		return;
	var ajax = loadAjax("/finishreset.jsp?hash="+encodeURIComponent(document.getElementById("hash").value)
		+"&pword="+encodeURIComponent(document.getElementById("hashedpass").value));
	
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4) {
			var output = "";
			switch(ajax.responseText)
			{
				case '0':
					ge('errormsg').innerHTML = "Sorry, your password contains invalid characters";
					ge('errormsg').style.display = "";
					ge('newpassword').value = "";
					ge('confirm').value = "";
					break;
				case '1':
					ge('errormsg').innerHTML = "Sorry, your passwords did not match, please re-enter them";
					ge('errormsg').style.display = "";
					ge('newpassword').value = "";
					ge('confirm').value = "";
					break;
				case '2':
					ge('errormsg').innerHTML = "Sorry, your password must be at least six characters long";
					ge('errormsg').style.display = "";
					ge('newpassword').value = "";
					ge('confirm').value = "";
					break;
				case '3':
					ge('errormsg').innerHTML = "There was an error with your request, please try again";
					ge('errormsg').style.display = "";
					ge('newpassword').value = "";
					ge('confirm').value = "";
					break;
				case '4':
					ge('errormsg').innerHTML = "Sorry, this password request is no longer valid.  Reset must be completed within an hour, please try again";
					ge('errormsg').style.display = "";
					ge('newpassword').value = "";
					ge('confirm').value = "";
					break;
				default:
					ge('showimage').innerHTML = ajax.responseText;
			}
		}
	}
	ajax.send(null);
}
function checkkey(e)
{
	var key;
	var evnt = e || window.event;
	var key = evnt.keyCode;
	if(key==13)
	{
		submitLogin();
	}
}

/**
 * this one is for forget password reset requests
 */
function checkkey2(e)
{
	var key;
	var evnt = e || window.event;
	var key = evnt.keyCode;
	if(key == 13)
	{
		// do nothing
		void(0);
	}
}

/**
 * this is for submitting forgot password requests
 */
function checkkey2(e)
{
	var key;
	var evnt = e || window.event;
	var key = evnt.keyCode;
	if(key == 13)
	{
		resetpassword('null');
	}
}
function processlogin()
{
var d= new Date();
var email = document.getElementById("emailaddress");
var path = document.getElementById("path").value;
path = unescape(path);
var prehash = ge("password").value;
var pw = hex_md5(prehash);
var rm = document.getElementById("rm").checked;


// set the hidden hashpw input field
var hashedpw = ge('password');
hashedpw.value = pw;
var url = "/login.jsp?emailaddress=" + encodeURIComponent(email.value) + "&password=" + pw + "&rm=" + document.getElementById("rm").checked;

var ifInline;
if (path.indexOf("loader.jsp?page=ccc.jsp") >= 0)
{
	ifInline = true;
}
else
{
	ifInline = false;
}

if(path != "index.jsp")
{
	var ajax = loadAjax(url);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4) 
		{
			if(ajax.responseText.length > 0)
			{
				
				if (ifInline)
				{
					ge('errormsg').innerHTML = ajax.responseText;
					return;
				}
				else
				{
					login(login_page,login_runner,ajax.responseText);
					return;
				}
			}
			
			var tstat 	= 	document.getElementById("tstatus");
			var ac 		= 	document.getElementById("alertCounter");
			var ajax2;
			if (tstat != null)
			{
				ajax2 = loadAjax("loginstatus.jsp");
				ajax2.onreadystatechange = function() {
					if(ajax2.readyState == 4) {
						//update top bar of screen with login status
						tstat.innerHTML = ajax2.responseText;
						removeLoading();
					}
				}
				ajaxsend(ajax2);
			}
			else if (ac != null)
			{
				ajax2 = loadAjax("/alertCounter.jsp");
				ajax2.onreadystatechange = function() {
					if(ajax2.readyState == 4) {
						//update top bar of screen with login status
						$(ac).html(ajax2.responseText);
						removeLoading();
					}
				}
				ajaxsend(ajax2);
			}
			
			var ajax3 = loadAjax(path);
			ajax3.onreadystatechange = function() {
				if(ajax3.readyState == 4) {
					
					//location.href='/loader.jsp?page='+path; //goto the redirected page
					if (ifInline)
					{
						var rssid = ge('rssid').value;
						loadengagefeed(rssid);
					}
					else
					{
						location.href=path;
					}
				}
			}
			ajax3.send(null);
		}
	}
	
	if (ifInline)
	{
		ajax.send(null);
	}
	else
	{
		ajaxsend(ajax);
	}
	var lgn = createDivIfNExists("newpopup");
	lgn.style.display = "";
}
else
{
	var ajax=loadAjax(url);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4) 
		{
			if(ajax.responseText.length > 0)
			{
				login(login_page,login_runner,ajax.responseText);
				ge("loading").style.visibility="hidden";
				return;
			}
			// means login was successful, redirect to home page
			location.href="/"; //goto home page to redirect to index.		
		}
	}
	ajaxsend(ajax);
	var lgn=createDivIfNExists("newpopup");
	lgn.style.display = "";
}
}

function logout()
{
	var d = new Date();
	var ajax;
	
	try {
		ajax = new XMLHttpRequest();
	} 
	catch (e)
	{
		try 
		{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e)
		{
			try 
			{
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e)
			{
				div.innerHTML="Your Browser is not supported.  Please visit <a href='http://www.mozilla.org'>Mozilla</a> to download a compatable browser.";
				return;
			}
		}
	} 
	ajax.onreadystatechange = 
	function() 
	{
		if(ajax.readyState == 4) 
		{
			var ajax2;
			
			try 
			{
				ajax2 = new XMLHttpRequest();
			} 
			catch (e)
			{
				try 
				{
					ajax2 = new ActiveXObject("Msxml2.XMLHTTP");
				} 
				catch (e)
				{
					try 
					{
						ajax2 = new ActiveXObject("Microsoft.XMLHTTP");
					} 
					catch(e)
					{
						div.innerHTML="Your Browser is not supported.  Please visit <a href='http://www.mozilla.org'>Mozilla</a> to download a compatable browser.";
						return;
					}
				}
			} 
			var tstat 	= document.getElementById("tstatus");
			var ac 	= document.getElementById("alertCounter");
			var ajax2;
			if (tstat != null)
			{
				ajax2 = loadAjax("loginstatus.jsp");
				ajax2.onreadystatechange = function() {
					if(ajax2.readyState == 4) {
						//update top bar of screen with login status
						tstat.innerHTML = ajax2.responseText;
						removeLoading();
					}
				}
				ajaxsend(ajax2);
			}
			else if (ac != null)
			{
				ajax2 = loadAjax("/alertCounter.jsp");
				ajax2.onreadystatechange = function() {
					if(ajax2.readyState == 4) {
						//update top bar of screen with login status
						$(ac).html(ajax2.responseText);
						removeLoading();
					}
				}
				ajaxsend(ajax2);
			}
			ge("loading").style.visibility="hidden";
		}
	}
	
	//ajax.open("GET","logout.jsp?ptime="+d.getTime(),true);
	//ajaxsend(ajax);
	
	//took out the ajax for now because it wasn't working properly
	window.location = "logout.jsp?ptime="+d.getTime();
}

// focusEmailField() is called when the login form loads to place focus in the e-mail field.

function focusEmailField()
{
	ge('email').focus();
}


function rs3()
{
	//var page = ge("page").value;
	var page = "regstep3.jsp";
	var step = ge("step").value;
	var id = ge ("id").value;
	var pword = ge("pword").value;
	var invite = ge("invite").value;

	var location = ge("location").value;
	var country = ge("country").options[ge("country").selectedIndex].value;
	var gender = ge("gender").options[ge("gender").selectedIndex].value;
	var education = ge("education").options[ge("education").selectedIndex].value;
	var politics = ge("politics").options[ge("politics").selectedIndex].value;
	var interests = ge("interests").value;
	
	url = "/loader.jsp?" + 
		"page="+page+
		"&tb=0"+
		"&step="+step+
		"&id="+id+
		"&pword="+pword+
		"&invite="+invite+
		"&location="+location+
		"&country="+country+
		"&sex="+gender+
		"&education="+education+
		"&politics="+politics+
		"&interests="+interests;
	
	var ajax = loadAjax(url);
	ajax.onreadystatechange = function() 
		{
			if (ajax.readyState == 4) 
			{
				var div = document.getElementById("regUser");
				div.innerHTML = ajax.responseText;
				div.focus();
			}
		}
	ajax.send(null);
}

function submitLogin()
{
		ge('hp').value = hex_md5(ge('password').value);
		ge('login').submit();
}