// --------------------------------------------
// SIGN UP - HOMEPAGE (JQUERY FRAMEWORK)
// --------------------------------------------

// ON PAGE LOAD
$(function() { 
		 
		// SIGN UP SUBMIT
	   	$("#input_submit").click(function() {  
			var username = $("#input_username").val();
			var email = $("#input_email").val();  
			var password = $("#input_password").val();
			var passwordconfirm = $("#input_passwordconfirm").val();
			var agreeterms = $('#agree_terms').attr('checked'); 
			
			var error = 0;
			var missinginput = 0;
			var passwordwrong = 0;
			var mustagree = 0;
			
			var errormessage;
			
			error = 0;
			errormessage = "";
			
			// is username missing?
			if (username.length < 1) {
				missinginput = 1;
				error = 1;
			}
			
			// is email missing?
			if (email.length < 1) {
				missinginput = 1;
				error = 1;
			}
			
			// is password missing?
			if (password.length < 1) {
				missinginput = 1;
				error = 1;
			}
			
			// do passwords match?
			if (password != passwordconfirm) {
				missinginput = 0;
				passwordwrong = 1;
				error = 1;
			}
			
			// is username the same as password?
			if (username == password) {
				username_pass_mismatch = 1;
				error = 1;
			}
			
			// user agreed to terms?
//			if (agreeterms == false && missinginput == 0 && passwordwrong == 0) {
//				mustagree = 1;
//				error = 1;
//			}
			
			// SHOW errorbox if necessary
			if (error == 1) {
				if (missinginput == 1) {
					errormessage = "You must fill in all of the fields.";
					
					$("#signup_errorbox").show();
					$("#signup_errorbox").html(errormessage);
					
				}
				if (passwordwrong == 1) {
					errormessage = "Passwords do not match.";
					
					$("#signup_errorbox").show();
					$("#signup_errorbox").html(errormessage);
				}
//				if (mustagree == 1) {
//					errormessage = "You must agree to the terms of use.";
//					$("#signup_errorbox").show();
//					$("#signup_errorbox").html(errormessage);
//				}
				if (username_pass_mismatch = 1) {
					errormessage = "Username and password cannot be the same.";
					$("#signup_errorbox").show();
					$("#signup_errorbox").html(errormessage);
				}
				
				// CANCEL FORM SUBMIT
				return false;
				
			} else {
				if ($('#ishome').val() == "yes")
				{
				// NO ERRORS... SHOW SPAM DETECTION
				$("#signup_initial").hide();
				$("#signup_antispam").show();
				
				// CANCEL FORM SUBMIT
				return false;
				}
			}
		});
		
		// NOT AN OWNER CHECKBOX
		$("#notowner").click(function() {
			// DISABLE INPUT BOX
			if ($('#notowner:checked').val() !== null) {
			  $('#resortname_1').attr("disabled", true);
			  $('#resortname_2').attr("disabled", true);
			  $('#resortname_3').attr("disabled", true);
			  
			}
			
			// ENABLE INPUT BOX
			if ($('#notowner:checked').val() == null) {
			  $('#resortname_1').removeAttr("disabled");
			  $('#resortname_2').removeAttr("disabled");
			  $('#resortname_3').removeAttr("disabled");
			}
									  
		});
		
		// ADD RESORT
		$("#addresort").click(function() {
			if ($('#resortname-2').is(":visible") == false) {
			  $('#resortname-2').show(); 
			} else {
				$('#resortname-3').show();
			}
									  
		});
		
		// LOOKUP RESORT NAME #1
		$("#resortname_1").keyup(function() {
			var inputString1 = $("#resortname_1").val();
			
			// RUN AJAX CALL
			if(inputString1.length == 0) {
				// Hide the suggestion box.
				$('#suggestions-1').hide();
			} else {
				$.post("ajax_autocomplete.php", {queryString1: ""+inputString1+""}, function(data){
					if(data.length >0) {
						$('#suggestions-1').show();
						$('#autoSuggestionsList-1').html(data);
					}
				});
			}
		});
		
		// LOOKUP RESORT NAME #2
		$("#resortname_2").keyup(function() {
			var inputString2 = $("#resortname_2").val();
			
			// RUN AJAX CALL
			if(inputString2.length == 0) {
				// Hide the suggestion box.
				$('#suggestions-2').hide();
			} else {
				$.post("ajax_autocomplete.php", {queryString2: ""+inputString2+""}, function(data){
					if(data.length >0) {
						$('#suggestions-2').show();
						$('#autoSuggestionsList-2').html(data);
					}
				});
			}
		});
		
		// LOOKUP RESORT NAME #3
		$("#resortname_3").keyup(function() {
			var inputString3 = $("#resortname_3").val();
			
			// RUN AJAX CALL
			if(inputString3.length == 0) {
				// Hide the suggestion box.
				$('#suggestions-3').hide();
			} else {
				$.post("ajax_autocomplete.php", {queryString3: ""+inputString3+""}, function(data){
					if(data.length >0) {
						$('#suggestions-3').show();
						$('#autoSuggestionsList-3').html(data);
					}
				});
			}
		});
});

// fill resort input box
function fill1(resortname, resortid) {
    $('#resortname_1').val(resortname);
    $('#suggestions-1').hide();
}

function fill2(resortname, resortid) {
    $('#resortname_2').val(resortname);
    $('#suggestions-2').hide();
}

function fill3(resortname, resortid) {
    $('#resortname_3').val(resortname);
    $('#suggestions-3').hide();
}