﻿// The next line adds a reference to an intellisense file for JQuery
/// <reference path="jquery-1.3.2-vsdoc.js" />

function getEatoutDateFormat() {
    return "DD, d MM yy";
}

function parseTimePicker(index, domElement) {
    // Parses the value if an HTML Input element for the Date returned by the model
    //  and formats using a locale string. 
    if (jQuery.fn.timepicker) {
        value = domElement.value;
        value = jQuery.trim(value);

        // Init the timepicker control
        // timepicker input must have an ID
        if (domElement.id) jQuery("#" + domElement.id + "").timepicker();

        return true;
    }
    else {
        // timepicker not installed so exit.
        return false;
    }
}

function parseDatePicker(index, domElement) {
    // Parses the value if an HTML Input element for the Date returned by the model
    //  and formats using a locale string. Then instantiates a DatePicker calendar.
    // Dependancies: jQuery UI.
    if (jQuery.datepicker) {
        var datePickerFormat = getEatoutDateFormat();

        value = domElement.value;
        value = jQuery.trim(value);

        // If element is empty, don't attempt to parse
        if (value.length != 0) {

            // If this element has already been parsed, don't attempt to re-format the date. 
            //  This condition occurs on a Page refresh.
            if (value.substring(10, 11) == "T") {

                // Parse the date format to create a JavaScript Date object
                var date = new Date();
                date.setFullYear(new Number(value.substring(0, 4)));
                date.setMonth(new Number(value.substring(5, 7)) - 1);
                date.setDate(new Number(value.substring(8, 10)));

                domElement.value = jQuery.datepicker.formatDate(datePickerFormat, date);
            }
        }

        // Init the datepicker control
        // DatePicker input must have either an ID or a Name
        if (domElement.name) jQuery("input[name = '" + domElement.name + "']:first").datepicker({ dateFormat: datePickerFormat });
        else if (domElement.id) jQuery("#" + domElement.id + "").datepicker({ dateFormat: datePickerFormat });

        return true;
    }
    else {
        // jQuery UI not installed so exit.
        return false;
    }

}
// Validator object
function Validator(info) {
    // info = the jQuery object that displays the validation information

    // params
    this.info = info;
    this.IsValid = true;

    // functions
    this.CheckLength = function(query, name, min, max) {
        // Validates the length of the text of the object returned by query
        // query = jQuery selector query to select the object to validate
        // name = the descriptive name of the object to use in the validation message
        // min = min length
        // max = max length
        var obj = $(query);
        obj.removeClass('ui-state-error');
        if (obj.val && obj.val().length > max) {
            obj.addClass('ui-state-error');
            this.UpdateInfo(obj, name + " is too long. Must be less than " + max + " characters.");
            this.IsValid = false;
            return this.IsValid;
        }
        else if (obj.val && obj.val().length < min) {
            obj.addClass('ui-state-error');
            this.UpdateInfo(obj, name + " cannot be blank.");
            this.IsValid = false;
            return this.IsValid;
        }
        else {
            return true;
        }
    }

    this.UpdateInfo = function(obj, text) {
        // update the info div and place below the offending input
        $(obj).after(info.text(text).effect("highlight", {}, 1500));
        $(obj).focus();
    }

    this.CheckEmail = function(query, name) {
        var obj = $(query);
        // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
        if (this.CheckRegex(obj, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,
                    "Email") == false) {
            this.IsValid = false;
            this.UpdateInfo(obj, name + " must be a valid email address.");
            return false;
        }
        else return true;
    }

    this.CheckRegex = function(obj, regex, name) {
        obj.removeClass('ui-state-error');
        if (obj.val && !(regex.test(obj.val()))) {
            obj.addClass('ui-state-error');
            this.UpdateInfo(obj, name);
            this.IsValid = false;
            return this.IsValid;
        }
        else {
            return true;
        }
    }
}
