/*
	This script runs on the member search page and handles the ajax calls for
	the search disclaimer and search form.

*/


$(function() {  
		   
	var uname = "";
	var city = "";
	var state = "";
	var zip = "";

  // Hide the error message label by default.  this message will be 
  // used below to display a "something must be entered" message
  // if they submit the form without any values.
  $('.error').hide();  
  
  
/*  MEMBER SEARCH
		This function takes the form elements and loads the selected
		members in to the right side of the page.
*/
  $('#Send').click(function() {  
    // validate and process form here  
    
	// hide the error message again, if this isn't the first form submit
    $('.error').hide();  
	uname     = $("input#uname").val();
	city      = $("input#city").val();
	state     = $("select#state").val();
	country     = $("select#country").val();
	zip       = $("input#zip").val();
	
	// If nothing in the form has any data, display the error box
	if (uname == "" && country=="" && city == "" && state == "" && zip == "") {  
	  $("label#error_blank").show();  
	  $("input#uname").focus();  
	  return false;  
	}  
	
	var toLoad = '../_php/public/membersearch.php?country='+ escape(country) +'&uname='+ escape(uname) + '&city=' + escape(city) + '&state=' + escape(state) + '&zip=' + escape(zip);  
	
	function loadMembers(url) {

		$('#floatright').load(url,'',showNewContent);
	
		function showNewContent() {  

			$('.navLink').click(function() {
				loadMembers(this.href);
				return false;
			 });
		} 
	}
	
	loadMembers(toLoad);
	return false;
	
	  
  }); // END BUTTON CLICK FUNCTION

	$('#searchagreement').click(function() {  
			
			$.getJSON("../_php/public/membersearch_functions.php",{searchagree: 1, ajax: 'true'}, function(j){
				
				document.location.href='index.php';
			
			});					

	});


				  
/* COUNTRY SELECT BOX ONCHANGE
		This repopulates the state select box
		with states from the selected country	
*/			  
  $('select#country').change(function() {  
	
		$.getJSON("../_php/public/membersearch_functions.php",{id: $(this).val(), ajax: 'true', updatestate: 'true'}, function(j){
			
			var options = '';
			for (var i = 0; i < j.length; i++) {
				if (j[i] == "--Select All--")
					options += '<option value="">' + j[i] + '</option>';
				else
					options += '<option value="' + j[i] + '">' + j[i].toUpperCase() + '</option>';
			}
			
			$("select#state").html(options);
		
    	});					
							
  });
  
  
  $("div[id^='editmember_']")
  	.live('mouseover',
		function(){
			$(this).css({'background':'#aaaaaa','color':'#dddddd','cursor':'pointer'});	
						
	});
	
  $("div[id^='editmember_']")
  	.live('mouseout',
		function(){
			$(this).css({'background':'','color':'#666666'});		
	});
	
  $("div[id^='editmember_']")
  	.live('click',
		function(){
			args = $(this).attr('id').split('_');
			document.location.href='../member/profile/password_change.php?id='+args[1];
			
	});
  
  
});

