function showLogin(redirectUrl) {
	/*
	var browserWidth = document.viewport.getWidth();
	var leftPos = ((browserWidth - 970)/2) + 970 - 410 - 6;
	
	loginWindow = new UI.Window({    
	    id:			"popup",
	    theme:		"login_popup",
	    width:		410,
	    height:		160,                
	    minWidth:		410,
	    minHeight:		120,	
	    shadow:		true,
	    resizable:		false,
	    shadow: true}).show();
	    loginWindow.setContent($('login_content').innerHTML);
	    
	    loginWindow.setPosition(10,leftPos);
	    */
	showBox("login_popup");
	$("login_popup").scrollTo();
	
	if (redirectUrl) {
		document.navLogin.redirectUrl.value = redirectUrl;
	}
}

function hideLogin() {
	//loginWindow.destroy();
	hideBox('login_popup');
}

function showLoginLoading(formName) {
    hideBox("login_box_" + formName);
    showBox("login_box_loading_" + formName);
}

function hideLoginLoading(formName) {
	hideBox("login_box_loading_" + formName);
	showBox("login_box_" + formName);
}

function ajaxLogin(formName, callback) {

    showLoginLoading(formName);
	
	var theForm;
    var forms = document.forms;
    for (var i = 0; i < forms.length; i++) {
    	if (forms[i].name == formName) {
    		theForm = forms[i];
    		break;
    	}
    }
    
    var userName = theForm.userName.value;
    var userPassword = theForm.userPassword.value;
    var rememberMe = theForm.rememberMe.value;
    var redirectUrl = theForm.redirectUrl.value;

    var url = "/ajax/login";
    new Ajax.Request(url, {
		method: "post",
        parameters: {	userName: userName,
                     	userPassword: userPassword,
                     	rememberMe: rememberMe,
                     	redirectUrl: redirectUrl
                     },
		onSuccess: function(transport) {
			var jo = transport.responseJSON;
			if (jo.status == "ok") {


                var loginType = 'foo';
                if (jo.fbConnected) {
                    // make the GA calls
                    trackThis({type:'event', label:'Login|CS Account FB Connected'});
                } else {
                    trackThis({type:'event', label:'Login|CS Account'});
                }


				if (!jo.redirectUrl && callback) {
					callback.callback();
				} else if (jo.redirectUrl) {
					window.location.replace(jo.redirectUrl);
				}
			} else {
				
				hideLoginLoading(formName);
				
				var li = $$("form[name='" + formName + "'] ul li.error_fields")[0];
				li.update("<div class='exclamation'></div>" + jo.msg);
				li.up("ul").show();
				
				//commented the alert cause it looks like its working to me
				//alert(jo.msg + " - we're showing this since the validation error is not displaying.");
				
			}
		},
		onFailure: function(transport) {
			hideLoginLoading(formName);
			showAjaxError();
		}
	});

}

// enter key form submission for all login forms
function trapLoginKeyPress() {
	
	var theForm;
    var forms = document.forms;
    for (var i = 0; i < forms.length; i++) {
    	var elements = forms[i].elements;
    	for (var j = 0; j < elements.length; j++) {
    		var field = elements[j];
    		if (field.name == "userName" || field.name == "userPassword") {
    		    Event.observe($(field), "keypress", function(event) {
    			    if (event.keyCode == Event.KEY_RETURN && !event.altKey && !event.shiftKey) {
    			    	var element = Event.element(event);
    			    	if (element.form.loginJs.value) {
    			    		eval(element.form.loginJs.value);
    			    	}
    			        Event.stop(event);
    			    }
    			});
    			
    		}
    	}
    	
    }
}

function checkUser(box) {

	if (box.value && box.value.length > 0) {
		new Ajax.Request("/ajax/check-user?user=" + box.value, {
			method: "get",
			onSuccess: function(transport) {

				if ($("error_userName")) {
					hideBox("error_userName");
				}

				var jo = transport.responseJSON;
				if (jo.status == "ok") {

					hideBox("user_name_failure");
					showBox("user_name_success");

					if ($("exclaim_userName")) {
						hideBox("exclaim_userName");
					}
					
				} else {
					hideBox("user_name_success");
					showBox("user_name_failure");
				}
				
			}
		});
	}
}

document.observe("dom:loaded", function() {
	trapLoginKeyPress();
});

// -----------------------
// generic login callbacks
//-----------------------
function RefreshPageCallback() {}

RefreshPageCallback.prototype.callback = function() {
	window.location.reload();
}

function RedirectPageCallback(val) {
    this.val = val;
}

RedirectPageCallback.prototype.callback = function() {
    if (typeof this.val != undefined && this.val != null) {
        window.location.replace(this.val);
    } else {
    	window.location.reload();
    }
}

/**
 * Function wrapper for showLogin implemented
 * to separate the call OMGPop makes from the
 * call tied to our site. Will provide us the
 * opportunity to change the call completely or
 * augment it by calling other functions around it.
 */
function OMGPop_ShowLogin() {
    showLogin();
}