$(document).ready(function(){
    // Default Text
    $(":input").each(function(){
        if ($(this).attr("value") == "")
            $(this).attr("value", $(this).attr("defaulttext"));
        $(this).blur(function(){
            if ($(this).attr("value") == "")
                $(this).attr("value", $(this).attr("defaulttext"));
            $(".hint").remove();
        });
        $(this).focus(function(){
            if ($(this).attr("value") == $(this).attr("defaulttext"))
                $(this).attr("value", "");
            if (typeof($(this).attr("hint")) != 'undefined')
                $(this).after("<span class='hint'>" + $(this).attr("hint") + "</span>");
        });
    });
	$("#username").keyup(function(){
		var subdomain = $(this).val().replace(/\W/gi, "").toLowerCase();
		if ($("#username").text() != subdomain) {
			$("#username").val(subdomain);
			$("#username_link").text(subdomain);
			checkSubdomain(subdomain);
		}
	});
	var subdomainIsValid = false;
	function checkSubdomain(subdomain) {
		$("#subdomain").text(subdomain);
		var ajaxFeedback = '';
		if (subdomain != '' && subdomain != $("#username").attr('defaulttext')) {
			ajaxFeedback = '(Checking...)';
			subdomainIsValid = false;
			$.post("ajax/check_username", { username: subdomain }, function(data){
				if (data.available) {
					ajaxFeedback = '(<span class="green"><strong>Available!</strong></span>)';
					subdomainIsValid = true;
				} else {
					ajaxFeedback = '(<span class="red"><strong>Not Available!</strong></span>)';
					subdomainIsValid = false;
				}
				$("#form_errors").html(ajaxFeedback);
			}, "json");
		} else {
			subdomainIsValid = false;
		}
		$("#form_errors").html(ajaxFeedback);
	}
	checkSubdomain($("#username").val());
    $("form").submit(function(){
        $(this)
            .find(":input").each(function(){
                // Default text check
                if ($(this).attr("value") == $(this).attr("defaulttext"))
                    $(this).attr("value", "");
            });
        if ($(this).attr("name") == "register") {
            // Validation for Registration Form
            with (document.register) {
                var regex1 = /^[A-Za-z\-' ]{1,25}$/;
                if (!regex1.test(first_name.value))
                    first_name.focus();
                else if (!regex1.test(last_name.value))
                    last_name.focus();
                else if (email.value == "")
                    email.focus();
                else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email.value)) {
                    alert("You have entered an invalid email address.");
                    email.focus();
                }
                else if (username.value == "")
                    username.focus();
                else if (password.value == "")
                    password.focus();
                else if (password.value != password2.value)
                    password2.focus();
                else if (resume_file.value == "" && !builder.checked) {
                    alert('If you do not have a resume to upload, you can use our Resume Builder.');
                    builder.focus();
                }
				else if (resume_file.value != "" && builder.checked) {
                    alert('You need to choose ONE: Either upload your resume, OR use our builder.');
                    builder.focus();
                }
                else if (!agree.checked) {
                    alert('You must agree to our Privacy Policy and Terms of Service.');
                    agree.focus();
                }
				else if (!subdomainIsValid) {
					alert('This username has already been taken. Please choose another!');
					username.focus();
				}
                else
                    return true;
                return false;
            }
        } else if ($(this).attr("name") == "login") {
            // Validation for Registration Form
            with (document.login) {
                if (email.value == "") {
                    alert("You haven't entered an email address");
                    email.focus();
                }
                else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email.value)) {
                    alert("You have entered an invalid email address.");
                    email.focus();
                }
                else if (password.value == "") {
                    alert("You haven't entered a password");
                    password.focus();
                }
                else
                    return true;
                return false;
            }
        }
    });
    // jQuery Corners
    $(".rounded").corners("10px");
});
