﻿google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");

google.setOnLoadCallback(function() {
    $(document).ready(function() {
        initValidation();
    });
});

function initValidation() {
    $(".userform input").focus(function() { checkIfRequired(this) }).blur(function() { checkIfRequired(this) });
    $(".userform select[type='select-one']").blur(function() { checkSelectRequired(this) });
    $(".userform").submit(function() {
        if ($(".userform").find('.required:visible').size() != 0) {
            alert('Please fill in all fields marked with *');
            return false;
        }
        else {
            return true;
        } 
    });
}

function findElements() {
    $(".userform p .required").each(function() {
        validateForm(this);
    });
}

function checkIfRequired(what) {
    if ($(what).val() != "") {
        if ($(what).parent().children('.required').size() == 1) {
            $(what).parent().children('.required').hide();
        }
    }
    else {
        if ($(what).parent().children('.required:hidden').size() == 1) {
            $(what).parent().children('.required').show();
        }
    }
}

function checkSelectRequired(what) {
    if ($(what).val() != 0) {
        if ($(what).parent().children('.required').size() == 1) {
            $(what).parent().children('.required').hide();

            if ($(what).val() == "Other") {
                $("#Specify").show();
            }
            else if ($(what).val() == "Recommendation") {
                $("#Specify").show();
            }
            else {
                $("#Specify").hide();
            }
        } 
    }
    else {
        if ($(what).parent().children('.required:hidden').size() == 1) {
            $(what).parent().children('.required').show();
            $("#Specify").hide();
        }
    }
}

function validateForm(what) {
    if ($(".userform").find('.required:visible').size() != 0) {
        alert('Please fill in all fields marked with *');
        return false;
    }
    else {
        return true;
    }  
}