﻿var maxWordCount = 200;

function countWords(text, displayElem) {
    var words = text.split(/[\s\.\?]+/);
    var wordCount = words.length;
    if (words.length > 0 && words[words.length - 1] == "")
        wordCount--;

    if (displayElem) {
        var wordsLeft = maxWordCount - wordCount;
        var color = (wordsLeft > 20 ? '' : (wordsLeft > 0 ? 'yellow' : 'red'));
        displayElem.css('color', color);
        displayElem.html(wordCount + ' words');
    }

    return wordCount;
}

function hasValue(str) {
    return /\S+/.test(str);
}

function validPhoneNumber(data) {
    return /^\(?([1-9]\d{2})(\) ?|[.-])?(\d{3})[.-]?(\d{4})$/.test(data);
}

function validEmailAddress(data) {
    //return /^([\w]+)(\.[\w]+)*@([\w\-]+)(\.[\w]{2,7})(\.[a-z]{2})?$/i.test(data);
    return /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/i.test(data);
}


function isFormValid() {
    var formFields = {
        nominatorName: { valid: hasValue($("#nominatorName").val()), errorMessage: "Please provide your name in the space provided above." },
        nominatorPhone: { valid: validPhoneNumber($("#nominatorPhone").val()), errorMessage: "Please provide your phone number, including area code, so we can contact you regarding your nomination." },
        nominatorEmail: { valid: (hasValue($("#nominatorEmail").val()) ? validEmailAddress($("#nominatorEmail").val()) : true), errorMessage: "Your email provided does not appear to be valid; please re-enter." },
        nomineeName: { valid: hasValue($("#nomineeName").val()), errorMessage: "Please provide the name of the nominee in the space provided above." },
        nomineeProgram: { valid: hasValue($("#nomineeProgram").val()), errorMessage: "Please provide the name of the nominee's program." },
        nomineePhone: { valid: validPhoneNumber($("#nomineePhone").val()), errorMessage: "Please provide the nominee's phone number, including area code." },
        nomineeEmail: { valid: (hasValue($("#nomineeEmail").val()) ? validEmailAddress($("#nomineeEmail").val()) : true), errorMessage: "The email address provided for the nominee does not appear to be valid; please re-enter." },
        nomineeJustification: { valid: (hasValue($("#nominationJustification").val()) && countWords($("#nominationJustification").val()) <= maxWordCount), errorMessage: "Please provided justification for you nominee in 200 words or less." }
    };

    var errList = $("#error-list");
    errList.html("");

    var valid = true;
    jQuery.each(formFields, function(i, val) {
        var x = { idx: i, value: val };
        valid = valid && val.valid;
        if (!val.valid) {
            errList.html(errList.html() + "<li>" + val.errorMessage + "</li>");
        }
    });

    if (!valid) {
        $("#error-area").fadeIn();
    }

    return valid;
}

$(document).ready(function() {
    // provide visual feedback for submit button
    $("div.field-submit").hover(
			function() {
			    $(this).addClass("field-submit-hover");
			},
			function() {
			    $(this).removeClass("field-submit-hover");
			}
		);


    // watch the number of words in the textarea
    $("#nominationJustification").keypress(function(e) {
        countWords($(this).val(), $("#words-left"));
        //if(e.charCode >= 48 ) {
        //var wordCount = countWords($(this).val());
        //$("#words-left").html(wordCount +' words');
        //return (wordCount <= maxCount);
        //}	
    });

    $("#nominationJustification").change(function(e) {
        countWords($(this).val(), $("#words-left"));
    });



    $("#nomination").submit(function() {
        // Get form values and validate...

        return isFormValid();
    });


});

