var request = false;
var dots = 0;
var msgTimer;
var loginForm;

var b_loggedIn = false;

//on the registration page, force postback
var isRegPage = false;

function doHdrLogin() {
	doLogin('hdr');
}
/*
1) find all the controls (buttons) to see if the same name is there more than once
2) have a counter, add one each log in/out.  Append that to the button's id
*/

function doLogin(frm) {
	loginForm = frm;
	var username = document.getElementById(frm + '_username').value;
	var password = document.getElementById(frm + '_password').value;
	var Reginfo = document.getElementById('Reginfo') ? document.getElementById('Reginfo').value : 0;
	var valid = true;
	//validation
	if (frm == 'logpop') {
		if (username == '') {
			document.getElementById('spnLogPopUsername').className = 'form_error';
			valid = false
		} else {
			document.getElementById('spnLogPopUsername').className = 'small';
		}
		if (password == '') {
			document.getElementById('spnLogPopPassword').className = 'form_error';
			valid = false
		} else {
			document.getElementById('spnLogPopPassword').className = 'small';
		}
	} else {
		if (username == '' || password == '') {
			document.getElementById('hdr_message').innerHTML = 'Please enter your email address and password above.';
		}
	}
	if (valid) {
		document.getElementById(loginForm + '_loggingin').innerHTML = 'logging in';
		msgTimer = setTimeout('UpdateMsg()', 1);
		var request = getRequest();
		if (!request) {
			//force postback somehow
		} else {
			//var url = '/ajax/CesarUser.aspx?action=login&username=' + username + '&password=' + password
			var url = '/ajax/CesarUser.aspx?action=login&username=' + username + '&password=' + password + '&Reginfo=' + Reginfo
			request.open('GET', url, true);
			request.onreadystatechange = showLoginResults;
			request.send(null);
		}
	}
}

function UpdateMsg() {
	document.getElementById(loginForm + '_loggingin').innerHTML += '.';
	dots++;
	if (dots == 5) {
		clearTimeout(msgTimer);
	}
}

function showLoginResults() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			if (isRegPage) {
				//force postback
				window.location.href = window.location.href; //+ "?r=1"
			} else {
				//alert(request.responseText);
				clearTimeout(msgTimer);
				document.getElementById('hdr_message').innerHTML = '';
				var arrResponse = request.responseText.split('|');
				if (arrResponse[0] > 0) {
					b_loggedIn = true;
					var arrCookie = arrResponse[3].split('&');
					
					for (var i = 0; i < arrCookie.length; i++) {
						document.cookie = arrCookie[i] + '; path=/';
						
					}
					if (document.getElementById('LogInGoTo').value != '') {
						window.location.href = document.getElementById('LogInGoTo').value;
					}
					showLoggedInHeader(request.responseText);
				} else {
					switch (arrResponse[0]) {
						case '-2':
							document.getElementById(loginForm + '_message').innerHTML = 'The email address you supplied could not be found.';
							document.getElementById(loginForm + '_loggingin').innerHTML = '';
							break
						case '-3':
							document.getElementById(loginForm + '_message').innerHTML = 'The password you supplied was not correct.';
							document.getElementById(loginForm + '_loggingin').innerHTML = '';
							break
						default:
							document.getElementById(loginForm + '_message').innerHTML = 'There was a problem trying to log you in.';
							document.getElementById(loginForm + '_loggingin').innerHTML = '';
							break
					}
				}
			}
		}
	}
}

function showLoggedInHeader(params) {
	var arrResponse = params.split('|');
	var greeting = arrResponse[2];
	var div = document.createElement('div');
	div.className = 'small';
	div.style.textAlign = 'right';
	div.innerHTML = 'hello ' + greeting + '&nbsp;&nbsp;&nbsp;<a href="/logout.aspx">Not you?</a><br><a href="/register_thanks.aspx">manage my profile</a><br>';
	
	var btn = document.createElement('input');
	btn.setAttribute('type', 'button');
	btn.setAttribute('id', 'btnLogOut');
	btn.className = 'logoutbtn';
	//btn.setAttribute('class', 'logoutbtn');
	btn.setAttribute('value', 'log out >');
	btn.onclick = Function('doLogout();');
	div.appendChild(btn);
	document.getElementById('hdr_results').innerHTML = '';
	document.getElementById('hdr_results').appendChild(div);
	if (arrResponse[1] == '0') {
		showWelcome();
	}

}

// TMR 081202 Refactored to be used by the new forgot password page.
function callForgotPW( email, callback )
{
		var request = getRequest();
		if (!request) {
			//force postback somehow
		} else {
			var url = '/ajax/CesarUser.aspx?action=forgotpw&email=' + email;
			request.open('GET', url, true);
			request.onreadystatechange = callback;
			request.send(null);
		}

}

function doForgotPWPage() {
	var email = document.getElementById('forgotpw_email_page').value;
	if (email == '') {
		document.getElementById('spnForgotPWEmail_page').className = 'form_error';
	} else {
		callForgotPW( email, showForgotPWPageResults );
	}
}


function showForgotPWPageResults() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var result = request.responseText;
			switch (result) {
				case '0':
					document.getElementById('forgotpw_message_page').innerHTML = 'Thank you.<br><br>Your password has been sent to your e-mail account.';	
					break
				case '-2':
					document.getElementById('forgotpw_message_page').innerHTML = 'We could not find your e-mail address.  Please make sure you entered it correctly.';
					break
				case '-1':
					document.getElementById('forgotpw_message_page').innerHTML = 'There was a problem sending your password.  Please try again.';
					break
			}
		}
	}
}


function doForgotPW() {
	var email = document.getElementById('forgotpw_email').value;
	if (email == '') {
		document.getElementById('spnForgotPWEmail').className = 'form_error';
	} else {
		callForgotPW( email, showForgotPWResults );
	}
}


function showForgotPWResults() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var result = request.responseText;
			switch (result) {
				case '0':
					document.getElementById('forgotpw_message').innerHTML = 'Thank you.<br><br>Your password has been sent to your e-mail account.';	
					break
				case '-2':
					document.getElementById('forgotpw_message').innerHTML = 'We could not find your e-mail address.  Please make sure you entered it correctly.';
					break
				case '-1':
					document.getElementById('forgotpw_message').innerHTML = 'There was a problem sending your password.  Please try again.';
					break
			}
		}
	}
}

function doLogout() {
	/*
	var request = getRequest();
	if (!request) {
		document.location.href = '/logout.aspx';
	} else {
		request.open('GET', '/ajax/CesarUser.aspx?action=logout', true);
		request.onreadystatechange = showLogout;
		request.send(null);
	}
	*/
	document.location.href = "/logout.aspx";
}

/*	
function showLogout() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			//delete cookie
			document.cookie = 'UserID=0; path=/';
			document.cookie = 'Greeting=; path=/';
			document.cookie = 'FollowingPromoPath=0; path=/';
			document.cookie = 'PreviousPage=; path=/';
			document.cookie = 'LoggedIn=false; path=/';
			document.cookie = 'RedirectURL=; path=/';
			document.cookie = 'Name=; path=/';
			document.cookie = 'FirstName=; path=/';
			document.cookie = 'Email=; path=/';
			document.cookie = 'UserGuid=; path=/';
			if (bNeedLogin) {
				window.location.href = '/default.aspx';
			} else {
				document.getElementById('hdr_results').removeChild(document.getElementById('hdr_results').children[0]);
				//document.getElementById('btnLogOut').removeNode;
				var div = document.createElement('div');
				//div.style.textAlign = 'right';
				//div.style.paddingTop = '7px';
				var table = document.createElement('table');
				table.setAttribute('width', '260');
				table.setAttribute('cellpadding', '0');
				table.setAttribute('cellspacing', '0');
				table.setAttribute('border', '0');
				var tr = document.createElement('tr');
				var td = document.createElement('td');
				td.setAttribute('width', '57');
				td.className = 'small';
				//td.setAttribute('class', 'small');
				td.setAttribute('align', 'center');
				td.innerHTML = 'sign up';
				tr.appendChild(td);
				
				td = document.createElement('td');
				td.setAttribute('width', '76');
				var txt = document.createElement('input');
				txt.setAttribute('type', 'text');
				txt.setAttribute('id', 'hdr_username');
				txt.setAttribute('name', 'hdr_username');
				txt.className = 'login';
				//txt.setAttribute('class', 'login');
				td.appendChild(txt);
				tr.appendChild(td);
				
				td = document.createElement('td');
				td.setAttribute('width', '78');
				txt = document.createElement('input');
				txt.setAttribute('type', 'password');
				txt.setAttribute('id', 'hdr_password');
				txt.setAttribute('name', 'hdr_password');
				txt.className = 'login';
				//txt.setAttribute('class', 'login');
				td.appendChild(txt);
				tr.appendChild(td);
				
				td = document.createElement('td');
				td.setAttribute('width', '49');
				var btn = document.createElement('input');
				btn.setAttribute('type', 'button');
				btn.setAttribute('value', 'login ');
				btn.setAttribute('id', 'btnLogIn');
				btn.className = 'loginbtn';
				//btn.setAttribute('class', 'loginbtn');
				//btn.onclick = new Function('doLogin(\'hdr\');');
				btn.onclick =  Function('doHdrLogin();');
				//btn.onclick = new function() {alert('ouch!');};	
				td.appendChild(btn);
				tr.appendChild(td);
				table.appendChild(tr);
				
				tr = document.createElement('tr');
				td = document.createElement('td');
				td.setAttribute('colspan', '4');
				td.className = 'register';
				//td.setAttribute('class', 'register');
				//td.setAttribute('style', 'padding-top:8px;text-align:right;');
				td.style.paddingTop = "8px";
				td.style.textAlign = "right";
				td.innerHTML = 'not registered? <a href="/register.aspx" class="register">click here</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;forgot <a href="/forgot_password.aspx" class="register">password</a>?';
				tr.appendChild(td);
				table.appendChild(tr);
				
				div.appendChild(table);
				
				var span = document.createElement('span');
				span.setAttribute('class', 'form_error');
				span.setAttribute('id', 'hdr_message');
				span.setAttribute('name', 'hdr_message');
				div.appendChild(span);
				
				//alert(div.outerHTML);
				//document.getElementById('hdr_results').removeChild(document.getElementById('ctlHeader_pnlLoggedInYes'));
				//document.getElementById('hdr_results').innerHTML = div.outerHTML;
				alert(document.getElementById('hdr_results').outerHTML);
				document.getElementById('hdr_results').appendChild(div);
				alert(document.getElementById('hdr_results').outerHTML);
				//document.getElementById('hdr_results').replaceChild(div, document.getElementById('ctlHeader_pnlLoggedInYes'));
				//document.write (div.outerHTML);
			}
		}
	}
}
*/
			
			
		
function getRequest() {
	try 
		{request = new XMLHttpRequest();} 
	catch (trymicrosoft) {
		try 
			{request = new ActiveXObject("Msxml2.XMLHTTP");} 
		catch (othermicrosoft) {
			try 
				{request = new ActiveXObject("Microsoft.XMLHTTP");} 
			catch (failed) 
				{request = false;}
		}
	}
	return request
}	

function loginAndGo(href) {
	if (b_loggedIn) {
		window.location.href = href;
	} else {
		document.getElementById("LogInGoTo").value = href;
		showLogin();
	}
}