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){
		login_user();
		return false;
	}else
		return true;
}


var xmlhttp = null;
var attempts = 0;
var em_username;
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 cbFunction = 'login_user_response';
          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.\n Please try again.");
            } else {
              // redirect to reset page, then home page; use replace so that back won't return to this page
              window.location.replace("./login_fail.php");
              return false;
            }
          } else {
	    username = rarr[1];
	    var CoolerColor = rarr[3];
	    var date = new Date();
	    date.setTime(date.getTime()+(365*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
	    document.cookie = "color="+CoolerColor+expires+"; path=/";
            if (formFunctions) { formFunctions.userFirstName = (rarr[1].substring(0,1).toUpperCase() + rarr[1].substr(1).toLowerCase()); }
            switch(rarr[0]) {
              case 'Old user':
		 message  = "<div id='logged_in'><span style='position:absolute; left:73px; top:30px; font-size:15px; color:#704113; font-weight:bold;'>HELLO, " + (rarr[1].toUpperCase()) + "!</span></div>";
		 document.getElementById('top').innerHTML = message;
		 if (formFunctions) { formFunctions.isValidUser = true; }
		 window.location.href = window.location;
		 break;
              case 'New user':
		alert("Wrong User Name or Password.\nPlease try again.");
		break;
            } // switch rarr0
          } // if 'Password...'
        } //if 4
      }

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 cbFunction = 'resetPW_response';
		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 e-mail with information about resetting your password was sent to " + em_username;
		 break;
	 } // switch
	 alert(msg);
  } // if 4
}
