var memLoginBox = dojo.byId("memLoginBox"); // div containing the login box
var memToolsBox = dojo.byId("memToolsBox"); //div containing the member tools box (initially not being displayed)
var dummy = dojo.byId("rightColumn_dummyDiv"); // the dummy div on top of the locate and attorney div that grows in order to make for logout box
var rightTop = dojo.byId("rightTop"); // the div whose class will change to show the yellow top
var logoutBox = dojo.byId("logoutBox"); //the div containing the logout box and "welcome so and so"
var myHome = dojo.byId("myHome"); // the link that will get highlighted yellow after successful login
var memLoginLoadingBox = dojo.byId("memLoginLoadingBox"); //div containing the loading symbol
var memNameLink = dojo.byId("memNameLink"); // the link containing the name of the member that is logged in
var memLogoutButton = dojo.byId("memLogoutButton"); //the link for logging oot


//Inputs----------------------------------|
var memLoginEmail = dojo.byId("memLoginEmail");
var memLoginPassword = dojo.byId("memLoginPassword");
var memSignInButton = dojo.byId("memSignInButton");
//----------------------------------------|

//THIS METHOD RUNS WHEN THE USER CLICKS SIGN IN; IT PERFORMS ERROR CHECKING FOR BLANK FIELDS
function signIn(){
	
	var errBox = dojo.byId("memLoginErrorBox"); //error ul
	var emailErr = dojo.byId("memLoginEmailError"); //email error li
	var passwordErr =  dojo.byId("memLoginPasswordError"); //password error li
	var emailPasswordErr =  dojo.byId("memLoginEmailPasswordError"); //email/pass error li
	var expiredErr =  dojo.byId("memLoginExpiredError"); //email/pass error li
	
	//hide all errors to begin with----------|
	errBox.style.display = "none";
	passwordErr.style.display = "none";
	emailErr.style.display = "none";
	emailPasswordErr.style.display = "none";
	expiredErr.style.display = "none";
	//---------------------------------------|
	
	var cont = true; //if no errors
	if(dojo.string.trim(memLoginEmail.value) == ""){ // if no value was entered in email field
		errBox.style.display = "block";
		emailErr.style.display = "block";
		cont = false; //we have error!
	}
	if(dojo.string.trim(memLoginPassword.value) == ""){//if no value was entered in the pass. field
		errBox.style.display = "block";
		passwordErr.style.display = "block";
		cont = false; //we have error!
	}
	
	if(cont){ // if no errors send the request to the server
		dojo.html.setOpacity(memLoginLoadingBox, 1); //we set opacity to 100% since it might have been set to 0% if someone logged out and then logged back in
		memLoginLoadingBox.style.display = "block"; //show the loading symbol
		memLoginBox.style.display = "none"; //hide the login box
		dojo.io.bind({ //send msg to server
			url: "ajax/ajax_validateMemLogin.cfm",
			handle: 	function(type, data, evt){
							rcvMemSignInResponse(type, data, evt);
						},
			mimetype: "text/plain",
			useCache: false,
			content: { //send email and password inputs to server
				email: dojo.string.trim(memLoginEmail.value),
				password: dojo.string.trim(memLoginPassword.value)
			},
			method: "POST"
		});
	}
}
//RUNS WHEN THE RESPONSE FROM THE LOGIN REQUEST RETURNS
function rcvMemSignInResponse(type, data, evt){
	if(data == "error"){ //if incorrect email/password error occurred
		memLoginLoadingBox.style.display = "none"; // hide the loading symbol
		memLoginBox.style.display = "block"; //show the login box again
		//SHOW THE ERROR---------------------------------------------------|
		dojo.byId("memLoginErrorBox").style.display = "block";
		dojo.byId("memLoginEmailPasswordError").style.display = "block";
		//-----------------------------------------------------------------|
	}
	else if(data == "error2"){ //if member is expired error occurred
		memLoginLoadingBox.style.display = "none";
		memLoginBox.style.display = "block";
		dojo.byId("memLoginErrorBox").style.display = "block";
		dojo.byId("memLoginExpiredError").style.display = "block";
	}
	else if(data == "error3"){ //if member is expired error occurred
		memLoginLoadingBox.style.display = "none";
		memLoginBox.style.display = "block";
		dojo.byId("memLoginErrorBox").style.display = "block";
		dojo.byId("memInactiveError").style.display = "block";
	}
	else{ //otherwise no errors
		var msg = "";
		try{
			var msg = data.split("|"); //split the msg into two parts
		}
		catch(e){
			document.body.innerHTML = evt.responseText;
		}
		if(msg[0] == "ok"){ //first part says "ok" meaning successful login
			memNameLink.innerHTML = msg[1]; //2nd part contains the name of the person that just logged in
			beginLoginAnim(); //start the login animation
		}
		
	}
}
function purt(){
	dojo.html.setOpacity(memLoginLoadingBox, 1); //we set opacity to 100% since it might have been set to 0% if someone logged out and then logged back in
	memLoginLoadingBox.style.display = "block"; //show the loading symbol
	memLoginBox.style.display = "none"; //hide the login box
	
	dojo.lang.setTimeout(function(){beginLoginAnim();}, 500);
}
function beginLoginAnim(){
	var loadingSymbol = memLoginLoadingBox.innerHTML; //cache the loading symbol since were about to get rid of it
	memLoginLoadingBox.innerHTML = ""; //clear loading symbol
	dojo.fx.html.fadeOut(memLoginLoadingBox, 600); //fade the loading symbol box
	// wipe the loading symbol box to a smaller height
	dojo.fx.html.wipe(memLoginLoadingBox, 600, 133, 110, function(){ //after wipe animation occurrs
															memLoginLoadingBox.innerHTML = loadingSymbol; // put loading symbol back into loading symbol box
															memLoginAnimStep2(); // begin step two of animation
														}
					 );
}
function memLoginAnimStep2(){
	memLoginLoadingBox.style.display = "none"; //hide the loading symbol box
	memToolsBox.style.display = "block" //show the memTools box
	dummy.style.display = "block"; //show the dummy which has a height of zero right now
	dojo.html.setOpacity(memToolsBox, 0); // we want to make this div transparent so that it can fadeIn to opaque upon successful login
	dojo.fx.html.fadeIn(memToolsBox, 600); //fade in the memTools box
	//wipe the dummy to from 0px to 35px height
	dojo.fx.html.wipe(dummy, 700, 0, 47,	function(){//after wipe animation happens we wait for 2/10 of sec and then begin step 3 of animation
												dojo.lang.setTimeout(	function(){
																			memLoginAnimStep3();
																		}
																	, 100)
											}
					  );
	
}
function memLoginAnimStep3(){
	dummy.style.display = "none"; //hide the dummy
	logoutBox.style.display = "block"; //show the logout box that contains the name of the person and the logout link
	//rightTop.style.padding = "22px 20px 15px";
	//rightTop.style.backgroundImage = "url(res/bg_rightTopSub.jpg)";
	dojo.html.setClass(rightTop, "rightColumn_locate_attorney_member_wrap"); //set the class of the rightTopBar to show the yellow thing on top and to have the right padding
	myHome.style.backgroundColor = "#FEE459" // highlight the myHome link in the same yellow
	dojo.lang.setTimeout(	function(){ //wait for a second then fade away the highlight
								dojo.fx.html.colorFade(myHome, 3000, "#FEE459", "#FEFFEF");
							}
						, 1000);
}

//RUNS WHEN THE USER CLICKS SIGN OUT
function signOut(){
	memToolsBox.style.display = "none"; //hide the member tools box
	dojo.html.setOpacity(memLoginLoadingBox, 1); 
	memLoginLoadingBox.style.display = "block"; //show the loading symbol box
	memLoginLoadingBox.style.height = "110.5px"; //put the loading symbol box and the right height
	dojo.io.bind({ //send logout msg to server that will clear the session
		url: "ajax/ajax_logoutMember.cfm",
		handle: 	function(type, data, evt){
						if(data == "ok"){ //msg was rcved and response was sent back
							beginLogoutAnim();
						}
					},
		mimetype: "text/plain",
		method: "POST"
	});
}

function beginLogoutAnim(){
	dummy.style.height = "47px"; //put the dummy at the right height
	dummy.style.display = "block"; // show the dummy
	logoutBox.style.display = "none"; //hide the logout box (dummy makes the hiding the logout box not change the height of the right bar)
	dojo.html.setClass(rightTop, "rightColumn_locate_attorney_wrap"); //set the class to hide the yellow thing on top
	//wipe the dummy from its height to 0
	dojo.fx.html.wipe(dummy, 700, 47, 0,	function(){ //after wipe animation, hide dummy
												dummy.style.display="none";
												memLogoutAnimStep2();
											}
					 );
	/*var loadingSymbol = memLoginLoadingBox.innerHTML; //cache the loading symbol
	memLoginLoadingBox.innerHTML = ""; //get rid of loading symbol
	//fade out the loading symbol box
	dojo.fx.html.fadeOut(memLoginLoadingBox, 600,	function(){ //after fade anim, put loading symbol back into loading symbol box and begin step 2
														memLoginLoadingBox.innerHTML = loadingSymbol;
													}
						);*/
	dojo.html.setOpacity(memLoginLoadingBox, 0);
}

function memLogoutAnimStep2(){
	dojo.fx.html.wipe(memLoginLoadingBox, 600, 110, 133,	function(){
																 memLogoutAnimStep3();
															}
					);
}

function memLogoutAnimStep3(){
	//clear the values of the login box--|
	memLoginEmail.value = "";
	memLoginPassword.value = "";
	//-----------------------------------|
	memLoginLoadingBox.style.display = "none"; //hide the loading symbol box
	memLoginBox.style.display = "block"; //show the mem login box
	dojo.html.setOpacity(memLoginBox, 0); //make mem login box invisible so we can fade in
	dojo.fx.html.fadeIn(memLoginBox, 600); //fade in mem login box
}

dojo.event.connect(memSignInButton, "onclick", window, "signIn"); // when signIn button is clicked --> invoke the signIn() method
dojo.event.connect(memLogoutButton, "onclick", window, "signOut"); // when signIn button is clicked --> invoke the signIn() method