$(document).ready(function(){  

	setInterval("checkAnchor()", 300);
	
	
});


var currentAnchor = null;  

function checkAnchor(){  

	//Check if it has changed  
	if(currentAnchor != document.location.hash){  
		currentAnchor = document.location.hash;  
		if(!currentAnchor){  
			var action = "";
		} else { 
			var splits = currentAnchor.substring(1).split('&');  
			var action = splits[0];  
			delete splits[0];  
			var params = splits.join('&');  
			var query = "action=" + action + params;  
		
		}  
	
	    $.get("/member_directory/index.php", query,
				function(data){
					$("#member_directory").html(data);	    
				}
		);
    }  
}


function search(keywords, city){
	
    $.get("/member_directory/index.php", { action: "search", keywords: keywords, city: city },
			function(data){
				$("#member_directory_results").html(data);
			}
	);

	
}



function login(email, password){
	
    $.post("/member_directory/index.php?action=login_submit", { email: email, password: password },
			function(data){
				if (data.substr(0, 7) == "Success"){

					if (data.substr(8) == "Admin"){ 
						location.href = "/index.php/locate";
					} else {

						
						var profile = window.open ("/member_directory/index.php?action=profile_edit",
							"profile_editor","location=0,status=1,scrollbars=1,width=600,height=650");
						profile.focus();
					    
						$.get("/member_directory/index.php", {action: 'directory'} ,
								function(mydata){
									$("#member_directory").html(mydata);
										showProfile(data.substr(8));
								}
						);
					}
				} else {
					$("#login_message").html("Your email or password did not match our records, please try again or request to have your password reset."); 
				}
			}
	);

	
}


function resetPassword(email){
	
    $.post("/member_directory/index.php?action=reset_password_submit", { email: email},
			function(data){
				if (data == "Success"){
					location.href="#login&msg=reset";
				} else {
					$("#reset_message").html(data);
				}
			}
	);

	
}





function requestAccount(){

	window.open ("/member_directory/index.php?action=request_account",
			"profile_editor","location=0,status=1,scrollbars=1,width=600,height=650");
}


function showProfile(id){
	
	if ($("#member_directory_results") != "undefined"){
	
	    $.get("/member_directory/index.php", { action: "profile", id: id },
				function(data){
					$("#member_directory_results").html(data);
				}
		);
   
	}
}


function editProfile(id){
	var profile = window.open ("/member_directory/index.php?action=profile_edit&id=" + id,
			"profile_editor","location=0,status=1,scrollbars=1,width=600,height=650");
	profile.focus();
}

function deleteProfile(id){
	
	if (confirm('Are you sure you want to delete this profile?  This action cannot be undone')){
	
	    $.get("/member_directory/index.php", { action: "profile_delete", id: id },
				function(data){
					$("#search_form").submit();
				}
		);
	}
}


