$(document).ready(function() {login()});

// store fieldnames at one location in javascript
var NAMES = (function() {
  var pvt = {
    'EMAIL': 'e_11121',
    'COMBI': 'p_11121'
  };
  return {
    get: function(name) { return pvt[name]; }
  };
})();

var email;
var combination;
var errBox;
var isFailedLogin = false;

function login(){
	email = document.getElementById(NAMES.get('EMAIL'));
	combination = document.getElementById(NAMES.get('COMBI'));
	errBox = document.getElementById("errBox");
	if(document.getElementById("loginFailed"))
	{
		showError("fail","");
		combination.type = "password";
		isFailedLogin = true;
	}
	
	if(document.getElementById("sessionInvalid"))
	{
		showError("session","");
		isFailedLogin = false;
	}
	
	if(document.getElementById("notThisWave"))
	{
		showError("wave","");
		isFailedLogin = false;
	}
	
	if(!isFailedLogin)
	{
		email.value = "EMAIL";
		combination.value = "COMBINATION";
	}
}

function activateField(whichField){
	switch(whichField.id)
	{
		case NAMES.get('EMAIL'):
			if(document.getElementById(NAMES.get('EMAIL')).value == "EMAIL")
			{
				document.getElementById(NAMES.get('EMAIL')).value = "";
			}
			if(document.getElementById(NAMES.get('COMBI')).value == "")
			{
				try{
					document.getElementById(NAMES.get('COMBI')).type = "text";
					document.getElementById(NAMES.get('COMBI')).value = "COMBINATION";
				} catch(e){
//					var oldBox = document.getElementById("combination");
//					var holder = oldBox.parentNode;
//					holder.removeChild(oldBox);
//					
//					var newBox = document.createElement("INPUT");
//					newBox.type = "text";
//					newBox.name = "combination";
//					newBox.id = "combination";
//					newBox.value = "COMBINATION";
//					newBox.className = "loginBox";
//					newBox.onKeyDown = function(){checkForEnterKey(event)};
//					newBox.onFocus = function(){activateField('combination')};
//					holder.appendChild(newBox);
				}
			}
			break;
		case NAMES.get('COMBI'):
			if(document.getElementById(NAMES.get('COMBI')).type == "text")
			{
				try{
					document.getElementById(NAMES.get('COMBI')).type = "password";
					document.getElementById(NAMES.get('COMBI')).value = "";
				} catch(e){
					var oldBox = document.getElementById(whichField.id);
					var holder = oldBox.parentNode;
					holder.removeChild(oldBox);
					
					var newBox = document.createElement("INPUT");
					newBox.type = "password";
					newBox.name = NAMES.get('COMBI');
					newBox.id = NAMES.get('COMBI');
					newBox.className = "loginBox";
					newBox.onKeyDown = function(){checkForEnterKey(event)}
					newBox.onFocus = function(){activateField(newBox)}
					holder.appendChild(newBox);
				}
			}
			
			if(document.getElementById(NAMES.get('EMAIL')).value == "")
			{
				document.getElementById(NAMES.get('EMAIL')).value = "EMAIL";
			}
			break;
    default:
      break;
	}
}

function doLogin(){
	var greenLight = true;
	
	errBox.style.display = "none";
	
	if(email.value == "EMAIL")
	{
		greenLight = false;
	}
	
	if(greenLight)
	{
		if(email.value == "")
		{
			showError("enter","email");
			greenLight = false;
		}
	}
	
	if(greenLight)
	{
		if(combination.value == "")
		{
			showError("enter","combination");
			greenLight = false;
		}
	}
	
	if(greenLight)
	{
		document.getElementById("loginForm").submit();
	}
}

function showError(mode, errorType)
{
	switch(mode)
	{
		case "enter":
			errBox.innerHTML = "Please enter your " + errorType;
			break;
		case "fail":
			errBox.innerHTML = "Sorry, your login failed.";
			break;
		case "session":
			errBox.innerHTML = "Sorry, your session's expired.";
			break;
		case "wave":
			errBox.innerHTML = "Sorry, you're not in this redemption.";
			break;
		default:
			break;
	}
	$(errBox).fadeIn(500);
}

function checkForEnterKey(evt) {
	if(evt.keyCode == 13)
	{
		doLogin();
	}
}

