$(document).ready(function(){


	
	$("#codeEntry input").keyup(function(e){
		$("#msg").html(e.keyCode);
		if ((48 <= e.keyCode && e.keyCode <= 57)||(96 <= e.keyCode && e.keyCode <= 105)) { //0-9 was pressed
			$("#msg").html("");
			$(this).next().focus();
		}
		else{ //non numeric key was pressed
			$(this).val('');
			$("#msg").html("A non-numeric key was entered.");
		}
	});

	$("#btn_login a").click(function(){
		var form = $("#frm_login");
		var realm = TremorApp.realm;
		var user = $("#email2").val();
		var password = $("#pass2").val();
		var onSuccess = $("#tremorOnSuccess").val();
		var onFail = $("#tremorOnFail").val();
		var next = 'form.html';
		var params={user:user,password:password,realm:realm};
		var resp = Tremor.Ajax.Call("authentication","Login",params,
			{
				sync:false,
				secure:true,
				onSuccess: function(resp){
					var success = false;

					if (resp != null && resp.Auth != null){
						resp = resp.Auth;
						Tremor.Debug.Log("login request ok","security",4);
						Tremor.Debug.Log("login token " + resp.Token,"security",4);
						Tremor.SetAuthToken(resp.Token);
						Tremor.Debug.Log("login roles " + resp.Roles.join(','),"security",4);
						Tremor.SetAuthRoles(resp.Roles);
						
						if (resp.IsAuthenticated && Tremor.Security.HasRequiredRole(resp.Roles)){
							success = true;
							window.location=next;
						}
						else{
							$("#msg_login").html("We're sorry, your username and/or password is incorrect. Please try again. If you need help with your password, please click <a href='../member/forgotpassword.html' target='_blank'>forgot password</a>.");
						}
					}

					if (!success){
						try {
							if (onFail != null){
								eval(onFail);
							}
						} 
						catch(e){}

						var e = $('tremorLoginError');

						if (e) {
							Tremor.Element.Show(e);
						}
						//alert('fail');
						Tremor.Debug.Log("login request failed","security",3);
					}
				},
				onFail:function(resp){
					if (onFail != null){
						eval(onFail);
					}

					var e = $('tremorLoginError');

					if (e) {
						Tremor.Element.Show(e);
					}

					Tremor.Debug.Log("login request failed","security",3);     
				}
			});
	});
	
	$(".btn_noCode").click(function(){
		$("#ce1").val(8);
		$("#ce2").val(6);
		$("#ce3").val(7);
		$("#ce4").val(5);
		$("#ce5").val(3);
		$("#ce6").val(0);
		
		Tremor.Ajax.Call("tracking","track",{tremorPageName:'Fruitnaturals_userClickedNoCode'});
		urchinTracker('/fruitnaturals/userClickedNoCode');
		return false;
	});
	
	$("#btn_submit a").click(function(){
		var code = $("#ce1").val()+$("#ce2").val()+$("#ce3").val()+$("#ce4").val()+$("#ce5").val()+$("#ce6").val();
		$("#code").val(code);
		
		if(code.length !=6){
			$("#msg").html('The code must contain 6 digits.');
		}
		else{
			var programName = "FruitNaturals_VP_TP4";
			var resp = Tremor.Ajax.Call('ProgramCode','validateProgramCode', { programName:programName, code:code },
				{
					onSuccess:function(){
						handleGoodCode();
					}, 
					onFail:function(){
						$("#msg").html("We're sorry, the code was not entered correctly.<br />Please check your numbers and try again.");
					}
				},
				{sync:true});
			
		}
	});
});