// member.js

var updateMemberBar; // for ajax
		
function updateMemberBarStateChange() {
	// Check the ready state.
	if (updateMemberBar.readyState == XMLHTTPREQUEST_READY_STATE_COMPLETED) {
	    // Fill in the table, if data is available.
		try {
			var data = updateMemberBar.responseText;
//			alert("responseText data=" + data);
			if (data != null && data.length > 0) {
			    document.getElementById("memberBar").innerHTML = data;
			} else {
			  	alert("Server response is currently slow.  Please refresh the page or try again in a moment.");
			}
		} catch (ex) {
		}
	}
}

function initiateUpdateMemberBar(event) {
    
	// Perform an asynchronous request to get get the information to update the menu bar.
	var url = "common/member.php";
	updateMemberBar.onreadystatechange = updateMemberBarStateChange;
	updateMemberBar.open("POST", url, true);
	updateMemberBar.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	updateMemberBar.send("ajax=true");
}

function memberBarInitialize() {
	updateMemberBar = getXMLHttpRequest();
}

// Called when the user selects Member Login from the member bar.
// updateFunctions is an array of ajax functions to call to update the page
//     after logging in successfully (does not include the call to update the
//     member bar since this should happen on all pages)
// returns status of true for success or false for fail
function memberBarLogin(updateFunctions) {
	var status = false;
	var id = openDialog("member_login.php", "185px", "340px");
	if (id !== null && id !== '' && id !== '0' && id != undefined) {

	    initiateUpdateMemberBar();  // Update the menu bar on the page
				
		for (ajaxFunction in updateFunctions) {
			eval(updateFunctions[ajaxFunction]);
		}
		status = true;
	}
	return status;
}
		
function memberBarCreateAccount(updateFunctions, args) {
	var status = false;
	
	// Pop up a secondary window to take down their personal info
	var createStatus = openDialog("member_profile.php?action=create" + args, "650px", "1020px");

	if (createStatus == 0) {
	    // After returning from the dialog, call any functions to refresh the page.
	    initiateUpdateMemberBar();  // user is logged in automatically upon successful account creation.
		for (ajaxFunction in updateFunctions) {
			eval(updateFunctions[ajaxFunction]);
		}
		status = true;
	}
	return status;
}
		
function memberBarUpdateProfile(updateFunctions) {
	var status = false;
	// Check if already logged in
/*
	if (!loggedIn) {
	    // If not logged in then Log in first and retrieve profile to session.

		id = openDialog("member_login.php", "185px", "340px");
		if (id !== null && id !== '' && id !== '0' && id != undefined) loggedIn = true;
		else loggedIn = false;
	} 
	if (loggedIn) {
*/	
	    // Pop up a secondary window to edit their personal info
    	var updateStatus = openDialog("member_profile.php?action=update", "650px", "1020px");
		
		if (updateStatus == 0) { 
    		// After returning from the dialog, refresh the member bar and other areas.
    		initiateUpdateMemberBar();  // username and other user data may have changed
    		for (ajaxFunction in updateFunctions) {
    			eval(updateFunctions[ajaxFunction]);
    		}
			status = true;
		}
		return status;
/*		
	} else {
	    alert ("Please log in first");
	}
*/
}
