﻿/*
        http://docs.jquery.com/Plugins/Authoring
*/


// TODO: consider renaming jQueryFormHelper.js to jQuery.Resortime.js


(function ($) {

    $.fn.setAsChecked = function () {
        this.attr('checked', true);
        return this;
    }
    $.fn.setAsUnchecked = function () {
        this.attr('checked', false);
        return this;
    }

})(jQuery);





jQuery.fn.exists = function () {
    return $(this).length > 0;
}

function getFieldByWildcardEndsWith(fieldName) {
    // TODO: this function isn't named correctly --  *= is "contains"   $= is "ends with"   and ^= is "begins with"
    // TODO: consider $.elementsThatEndWith(segment)
    return $("[id*=_" + fieldName + "]");
}

function getFieldByIdMatch(fieldName) {
    return $("[id=" + fieldName + "]");
}

function getFieldById(fieldName) {
    return getFieldByWildcardEndsWith(fieldName);
}

function validateFieldHasValue(fieldName, message) {
    var field = getFieldByWildcardEndsWith(fieldName);
    if (field.val() == "") {
        alert(message);
        field.focus();
        return false;
    }
    return true;
}

function hasValidEmailAddress(emailText) {
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    if ($(this).length == 0) return false;
    if ($(this).val == 0) return false;
    if (!emailReg.test(emailText)) return false;

    return true;
}

function hasInvalidEmailAddress(emailText) {
    return (!hasValidEmailAddress(emailText));
}

