//global vars  
var communityRegForm = $("#communityRegForm");  
var yourname = $("#name");  
var nameInfo = $("#nameInfo");  
var email = $("#email");  
var emailInfo = $("#emailInfo");  
var phone = $("#phone");  
var phoneInfo = $("#phoneInfo");  
var students = $("#students");  
var studentsInfo = $("#studentsInfo");  
var mylocation = $("#location");
var island = $("#island");
var communitySubmit = $("#communitySubmit");

function validateName(){  
		//if it's NOT valid  
    if(yourname.val().length < 4){  
        yourname.addClass("error");  
        nameInfo.text("We want names with more than 3 letters!");  
        nameInfo.addClass("error");  
        return false;  
    }  
    //if it's valid  
    else{  
        yourname.removeClass("error");  
        nameInfo.text("Valid!");  
        nameInfo.removeClass("error");  
        return true;  
    }  
}

function validateEmail(){  
		var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
		if (regex.test(email.val())) {
			email.removeClass("error");
			emailInfo.text("Valid!");
			emailInfo.removeClass("error");
			return true;
		} else {
			email.addClass("error");
			emailInfo.text("Not a valid email.");
			emailInfo.addClass("error");
			return false;
		}
}  

function validatePhone(){  
		var regex = /^[0-9]{10}$/
		if (phone.val().length < 10) {
			phone.addClass("error");
			phoneInfo.text("Enter a 10 digit phone number.");
			phoneInfo.addClass("error");
			return false;
		}

		if (regex.test(phone.val())) {
			phone.removeClass("error");
			phoneInfo.text("Valid!");
			phoneInfo.removeClass("error");
			return true;
		} else {
			phone.addClass("error");
			phoneInfo.text("Not a valid phone number.");
			phoneInfo.addClass("error");
			return false;
		}
}  

function validateStudents(){  
		var regex = /^[0-9]+$/
		if (students.val().length < 1) {
			students.addClass("error");
			studentsInfo.text("Please enter how many.");
			studentsInfo.addClass("error");
			return false;
		}

		if (students.val() < 1) {
			students.addClass("error");
			studentsInfo.text("Please enter a value of 1 or more.");
			studentsInfo.addClass("error");
			return false;
		}

		if (regex.test(students.val())) {
			students.removeClass("error");
			studentsInfo.text("Valid!");
			studentsInfo.removeClass("error");
			return true;
		} else {
			students.addClass("error");
			studentsInfo.text("Not a valid number.");
			studentsInfo.addClass("error");
			return false;
		}
}  

//On blur  
yourname.blur(validateName);  
email.blur(validateEmail);  
phone.blur(validatePhone);  
students.blur(validateStudents);

//On key press  
yourname.keyup(validateName);
email.keyup(validateEmail);  
phone.keyup(validatePhone);  
students.keyup(validateStudents);

function filterLocation() {
	var filterKey = island.val();
	var firstVal = -1;
	
	mylocation.children("option").each(function() {
		if ($(this).attr("island_id") == filterKey) {
			$(this).show();
			$(this).css('display','block');
			$(this).css('visibility','visible');
			
			if (firstVal < 0) firstVal = $(this).val();
			
		} else {
			$(this).hide();
			$(this).css('display','none');
			$(this).css('visibility','hidden');
		}
	});
	mylocation.val(firstVal);
}

//island.change(filterLocation);
//filterLocation();
island.change(buildLocations);
buildLocations();

//On Submitting  
communityRegForm.submit(function(){  
    if(validateName() & validateEmail() & validateStudents()) {

				var data =
					 'name='+yourname.val()
					+'&email='+email.val()
					+'&phone='+phone.val()
					+'&students='+students.val()
					+'&location_id='+mylocation.val()
					+'&location_name='+$('#location :selected').text()
					;
					
				communitySubmit.attr("disabled","true");
				$("#communityLoading").show();

				$.ajax({
					url: "process_community.php",
					type: "POST",
					cache: false,
					data: data,
					success: function(html) {
						if (html == 1) {
							$("#communityLoading").fadeOut('slow');

							communityRegForm.fadeOut('slow');
						
							$("#communityDone").fadeIn('slow');
						} else {
							communitySubmit.attr("disabled","false");
							alert('Sorry, unexpected error. Please try again later.');
						}
					}
				});
			
        return false  
		} else  
        return false;  
}); 


function buildLocations() {
	var sel = document.getElementById("location");
	sel.options.length = 0; //Clear all options.
	var tempArray = islandLocations[island.val()];
	for (i = 0; i < tempArray.length; i++) {
		sel.options[i] = new Option(tempArray[i].split("=")[0],tempArray[i].split("=")[1]);
	}
}

