function submitenter(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13){
		heroLogin();
		return false;
	}else
		return true;
}

var xmlhttp = null;
var attempts = 0;
var em_username;
var valid = "New user";

function getFormElementsByTagType(formName, tagName, tagType) {
  this.$ = new Array();
  var el = null;
  var nameArr = document.getElementsByTagName(tagName);
  for (var i=0; i<nameArr.length -1; i++) {
	 el = nameArr[i];
	 if ('undefined' != typeof el.form) {
		if (('undefined'!=typeof el.form) && formName==el.form.id && 'undefined'!=typeof el.type) {
		  if ('object' == typeof tagType) {
			 for (var j=0; j<tagType.length; j++) {
				if (tagType[j] == el.type) {
				  this.$.push(el);
				}
			 }
		  } else {
			 if (tagType == el.type) {
				this.$.push(el);
			 }
		  } // if object
		}
	 }
  }
  return this.$;
}

var login_user = function() {
  var f = document.getElementById('loginForm');
  var args = '';
  var inputs = getFormElementsByTagType('loginForm', 'input', ['text', 'password']);
  for (var i=0; i<inputs.length; i++) {
	 args += (inputs[i].value && 0<inputs[i].value.length) ? '&'+inputs[i].name+'='+inputs[i].value : '';
  }
  if (0 < args.length) {
	 args = '?' + args.substr(1);
	 var url = '/check_user.php' + args;
	 url += '&location=' + encodeURIComponent(location.pathname);
	 xmlhttp = null;
	 try { // IE
		xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
	 } catch (err) {
		try {
		  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (err) {
		  xmlhttp = null;
		}
	 }
	 // FF
	 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	 }
	 if (xmlhttp) {
		xmlhttp.onreadystatechange = function() {
		  login_user_response();
		}
		try {
		  xmlhttp.open("GET",url,true);
		} catch(err) {
		  alert('ERROR: ' + err);
		}
		if (window.ActiveXObject) {
		  xmlhttp.send();
		} else if (window.XMLHttpRequest) {
		  xmlhttp.send(null);
		}
	 }
  } // if 0<args.length
}

var login_user_response = function() {
  var _response;
  if (4==xmlhttp.readyState && 200==xmlhttp.status) {
	 _response = new String(xmlhttp.responseText);
	 ++attempts;
	 rarr = _response.split('::');
	 if ('Password does not match' == rarr[0]) {
		if (3>attempts) {
		  var f = document.getElementById('loginForm');
		  f.log_password.value = '';
		  f.log_password.focus();
				alert("Wrong user name or password. Password is case sensitive. Please try again.");
		} else {
		  window.location.replace("./login_fail.php");
		  return false;
		}
	 } else {
 		username = rarr[1];
 		valid = rarr[0];
		switch(valid) {
		  case 'Old user':
			doMove(1);
			document.getElementById('login').innerHTML = "<div id='logged_in_user'>"+username+"</div>";
			if(String(window.location).split("/")[5]) document.getElementById('logged_in_user').style.display='none';
			//window.location.href = window.location;
			break;
		  case 'New user':
			alert("Wrong user name or password. Password is case sensitive. Please try again.");
			break;
		} // switch rarr0
	 } // if 'Password...'
  } //if 4
}

var logged_user = function() {
	if(valid == "Old user"){
		document.getElementById('login').innerHTML = "<div id='logged_in'><a id='logged_in_reg' href='javascript:window.location.href=\"register.php?prev_path=\"+window.location.href;'></a><a id='logged_in_out' href='javascript:document.cookie=\"GPLX_AJS=; path=/; domain=googolplex.cuna.org; expires=Thu, 01-Jan-1970 00:00:01 GMT\"; heroLogout();'></a></div><div id='logged_in_user'>"+username+"</div>";
	}
}

var logout_user = function() {
	document.getElementById('login').innerHTML = "";
}

var loggedout_user = function() {
	document.getElementById('login').innerHTML = '<div id="logged_out"><form id="loginForm" name="loginForm" action=""><input id="logged_out_user" type="text" id="username" name="log_username" /><input id="logged_out_pass" type="password" id="password" name="log_password" onKeyPress="return submitenter(this,event)"/><a id="logged_out_forgot" href="javascript:resetPW({CUID});"></a><a id="logged_out_go" href="javascript:heroLogin();"></a><a id="logged_out_reg" href="javascript:window.location.href=\'/{CUID}/ajsmall/register.php?prev_path=\'+window.location.href;"></a><input type=submit style="display:none;"/></form></div>';
}

var resetPW = function(cuid) {
  var emailexp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
  var email = prompt("Whoops, did you forget your password? Enter the e-mail address you used at registration. We'll send you a link where you can change your password.","")
  if (email) {
	 em_username = email;
	 if (emailexp.test(email)) {
		var url = '/'+cuid+'/include/check_username.php?email=' + email;
		url += '&location=' + encodeURIComponent(location.pathname);
		xmlhttp = null;
		try { // IE
		  xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
		} catch (err) {
		  try {
			 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		  } catch (err) {
			 xmlhttp = null;
		  }
		}
		// FF
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		  xmlhttp = new XMLHttpRequest();
		}
		if (xmlhttp) {
		  xmlhttp.onreadystatechange = function() {
			 resetPW_response();
		  }
		  try {
			 xmlhttp.open("GET",url,true);
		  } catch(err) {
			 alert('ERROR: ' + err);
		  }
		  if (window.ActiveXObject) {
			 xmlhttp.send();
		  } else if (window.XMLHttpRequest) {
			xmlhttp.send(null);
		  }
		}
	 } else {
		alert(email + " is not a valid email address. Please try again.")
	 }
  }
} // resetPW

var resetPW_response = function() {
  var _response;
  if (4==xmlhttp.readyState && 200==xmlhttp.status) {
	 var msg = '';
	 switch(xmlhttp.responseText) {
	  case '-1': case -1:
		 msg = "An error occurred when attempting to email. Please contact CUNA Web Services at (800) 356-9655, ext. 8083";
		 break;
	  case '0': case 0:
		 msg = "Username not found. Please try again.";
		 break;
	  case '1': case 1:
		 msg = "An email with information about resetting your password was sent to " + em_username;
		 break;
	 } // switch
	 alert(msg);
  } // if 4
}
