﻿/// <reference path="jquery-1.3.2.js" />
/// <reference path="jquery.validate.js" />
/* This script is used with Site.Master */

// global variable
var Faws = Faws || {};

$(document).ready(
    function () {
        // when an ajax error occurs, show a dialog
        /* N.B. a $(xxx).load will sometimes fail quietly without triggering this callback */
        $("#main").ajaxError(function (event, options, error) {
            debugger;
        });

        // save some global info about the current page
        // Institution ID and whether it is marked for publication.
        var app = $("#site_WebApplicationName").val() || "";
        app = $.trim(app);
        if (app === "/") {
            app = "";
        }
        Faws.WebApplicationName = app;

        // Connect help to an element with an ID of help_XXX, where XXX is the topic
        Faws.connectHelp = function (el) {
            var $el = $(el);

            // extract topic from element id
            var subject = $el[0].id.substring(5);
            $el.qtip(Faws.getQtipOption({
                content: {
                    url: Faws.makeUrl('/Help/ShowHelp?topic=' + subject)
                },
                style: {
                    width: 550
                }
            }));
        };

        // default option for using title as content
        // draggable and sortable interfere with default positioning,
        // so we may need to use the mouse
        Faws.QtipOption = {
            style: {
                name: 'light',
                tip: 'leftTop'
            },
            position: {
                type: 'absolute',
                corner: {
                    target: 'bottomRight',
                    tooltip: 'leftTop'
                }
            }
        };

        // get a Qtip option that overrides the defaults
        // this is a deep copy, so overriding position.corner.target works
        Faws.getQtipOption = function (obj) {
            if (!obj) {
                obj = {};
            }
            return $.extend(true, {}, Faws.QtipOption, obj);
        };

        Faws.makeUrl = function (relpath) {
            return Faws.WebApplicationName + relpath;
        };

        // validation methods
        // Negative Whole Numbers
        $.validator.addMethod("digitsnegative", function (value, element) {
            return this.optional(element) || /^-?(?:\d+)?$/.test(value);
        }, "Please enter a valid whole number (-999999999 to 999999999)");

        // Timed Regular Expression
        $.validator.addMethod("time", function (value, element) {
            return this.optional(element) || /^(?:(?:(([2][0-3]{1})|([0-1][0-9]{1})|([0-9]{1})):)?(([0-5]?[0-9]{1})):)?([0-5]?[0-9]{1})(\.[0-9][0-9])$/.test(value);
        }, "Please enter a valid time 00:00:00.00");

        // Distance Regular Expression
        $.validator.addMethod("distance", function (value, element) {
            return this.optional(element) || /^([0-9]{1,3}')?(([0-1]?[0-1]{1})|([0-9]{1}))(\.[0-9][0-9])$/.test(value);
        }, "Please enter a valid distance 000'00.00");

        // User to add a special validation method for checkboxes associated with textboxes
        // Assumes the element is a checkbox element
        $.validator.addMethod("requiredextended", function (value, element) {
            var checked = $(element).is(":checked");
            if (checked) {
                var $tr = $(element).parents("tr");
                var els = $tr.find(".number");
                var returnValue = false;

                if (els.length > 0) {
                    for (elItem = 0; elItem < els.length; elItem++) {
                        if ($(els[elItem]).val().length > 0) {
                            returnValue = true;
                            break;
                        }
                    }
                }
                else {
                    returnValue = true;
                }
                return returnValue;
            }
            else {
                // Not checked, so not required
                return true;
            }
        }, "Number Required");
    });
    
