/** GLOBAL VARIABLES  **/
var gLostInformationFormID = "lostInformationForm";
var gLostInformationEmailAddressID = "lostInformationEmailAddress";
var gLostInformationResponseID = "lostInformationResponse";
var gLostInformationSendButtonID = "lostInformationSendButton";
var gLoginUsernameID = "username";
var gLoginPasswordID = "password";
var gLoginResponseID = "loginResponse";

/**	CONSTANTS  **/
var LOGIN_BACKEND_LOCATION = "includesSite/loginbackend.php";

/** resetDisplay -  hides response and form information and clears the values  **/
function resetDisplay( aResponseType ) {
	if(!aResponseType) {
		aResponseType = "all";
	}
	
	switch( aResponseType ) {
		case "all":
			getElement(gLostInformationFormID).style['display'] = 'none';
			getElement(gLostInformationResponseID).style['display'] = 'none';
			getElement(gLoginResponseID).innerHTML = '';
		break;
	}
}

/** loginSend - sends your login information  **/
function loginSend() {
	resetDisplay();
	
	getElement(gLoginResponseID).innerHTML = 'Processing Login . . .';
	
	var fnLoginSendConn = new XHConn();
	
	var fnUsername = getElement(gLoginUsernameID).value;
	var fnPassword = getElement(gLoginPasswordID).value;
	
	var fnWhenDone = function (oXML) { loginSendComplete(oXML); };
	
	var fnQueryString = "querytype=login&username=" + fnUsername + "&password=" + fnPassword + "&";
	
	fnLoginSendConn.connect(LOGIN_BACKEND_LOCATION, "GET", fnQueryString, fnWhenDone);	
}

/**  loginSendComplete - processes the response from the backend  **/
function loginSendComplete( aXML ) {
	getElement(gLoginResponseID).innerHTML = aXML.responseText;
	getElement(gLoginResponseID).style["display"] = "";
	
	if(aXML.responseText.indexOf("sucess") >= 0) {
		location.reload();
	}
}

/** loginLostPasswordFormDisplay - display the form to retrieve your information  **/
function loginLostPasswordFormDisplay( aShowForm, aQueryType ) {
	if( aShowForm ) {
		resetDisplay();
		
		getElement(gLostInformationFormID).style['display'] = '';
		getElement(gLostInformationResponseID).innerHTML = '';
		
		getElement(gLostInformationSendButtonID).onclick = function(){loginLostInformationSend(aQueryType);}
		switch(aQueryType) {
			case "lostpassword":	
				getElement(gLostInformationSendButtonID).value = "Email Password";
			break;
			
			case "lostusername":
				getElement(gLostInformationSendButtonID).value = "Email Username";
			break;
		}
	}
	else {
		getElement(gLostInformationFormID).style['display'] = 'none';
	}
}

/** loginLostInformationSend - connects to the backend to have the lost information emailed to the user  **/
function loginLostInformationSend(aQueryType) {
	var fnPasswordSendConn = new XHConn();
	
	var fnEmailAddress = getElement(gLostInformationEmailAddressID).value;
	
	var fnWhenDone = function (oXML) { loginLostInformationSendComplete(oXML); };
	
	var fnQueryString = "querytype=" + aQueryType + "&emailaddress=" + fnEmailAddress + "&";
	
	fnPasswordSendConn.connect(LOGIN_BACKEND_LOCATION, "GET", fnQueryString, fnWhenDone);	
}

/**  loginLostInformationSendComplete - processes the response from the backend  **/
function loginLostInformationSendComplete( aXML ) {
	loginLostPasswordFormDisplay(false);
	
	getElement(gLostInformationResponseID).innerHTML = aXML.responseText;
	getElement(gLostInformationResponseID).style["display"] = "";
}

/**  logoutSend - connects to the backend to logout the user  **/
function logoutSend() {
	var fnLogoutSendConn = new XHConn();
	
	var fnWhenDone = function (oXML) { logoutSendComplete(oXML); };
	
	var fnQueryString = "querytype=logout&";
	
	fnLogoutSendConn.connect(LOGIN_BACKEND_LOCATION, "GET", fnQueryString, fnWhenDone);	
}

/**  logoutSendComplete - processes the response from the backend  **/
function logoutSendComplete() {
	location.reload();
}