function OpenWindow(pageURL, width, height)
{
    return window.open(pageURL, "mywin","left=20, top=20, width=" + width + ", height=" + height + ", toolbar=0, resizable=0, location=0, directories=0, menubar=0, status=0, scrollbars=1");
}

// close any open validator callout extenders
function closeExistingCallout()
{
    if (AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout != null)
    {
        AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout.hide();
    }
}

/// <reference name="MicrosoftAjax.js" />

Type.registerNamespace('YourQuest');

YourQuest.URQuestTextBox = function(element) {
    YourQuest.URQuestTextBox.initializeBase(this, [element]);

    this._safeKeys = [];
    this._maxLength = 0;
    this._validators = [];

    //delegates
    this._keyDownPressDelegate;
    this._keyReleaseDelegate;
    this._pageLoadDelegate;
}

YourQuest.URQuestTextBox.prototype = {
    initialize: function() {
        YourQuest.URQuestTextBox.callBaseMethod(this, 'initialize');


        //if the user setup the maxLength, then we will setup the events for that.
        if (this._maxLength != 0) {
            //setup the safeKeys
            this._setupSafeKeys();

            //create a delegate for the keypress event
            this._keyDownPressDelegate = Function.createDelegate(this, this._keyDownPress);

            //hook to the keypress event
            $addHandler(this.get_element(), 'keydown', this._keyDownPressDelegate);

        }

        //The rest of this is needed for the validator stuff...

        //create a delegate for the keypress event
        this._keyReleaseDelegate = Function.createDelegate(this, this._keyRelease);
        $addHandler(this.get_element(), 'keyup', this._keyReleaseDelegate);

        //hook to the page load event to check for validators...
        this._pageLoadDelegate = Function.createDelegate(this, this._pageLoadEvent);
        Sys.Application.add_load(this._pageLoadDelegate);
    },

    dispose: function() {

        //remove the handlers we created
        $removeHandler(this.get_element(), 'keyup', this._keyReleaseDelegate);
        if (this._maxLength != 0)
            $removeHandler(this.get_element(), 'keydown', this._keyDownPressDelegate);

        //remove page load event
        Sys.Application.remove_load(this._pageLoadDelegate);

        this._safeKeys = null;
        this._keyDownPressDelegate = null;
        this._keyReleaseDelegate = null;
        this._pageLoadDelegate = null;

        //call base dispose
        YourQuest.URQuestTextBox.callBaseMethod(this, 'dispose');
    },

    _pageLoadEvent: function() {
        //This checks to see if the control has any validators attached to it.  If it does, we will
        //validate the textbox on each keystroke.
        if (this.get_element().Validators) {
            //add the validators to the internal validators array
            var e = this.get_element();
            var length = e.Validators.length;
            for (var i = 0; i < length; ++i) {
                Array.add(this._validators, e.Validators[i]);
            }
        }


    },

    _setupSafeKeys: function() {
        ///Used to setup the safe keys
        //setup the keys
        Array.add(this._safeKeys, 37); //arrows
        Array.add(this._safeKeys, 38);
        Array.add(this._safeKeys, 39);
        Array.add(this._safeKeys, 40);
        Array.add(this._safeKeys, 127); //delete keys
        Array.add(this._safeKeys, 8);
        Array.add(this._safeKeys, 9); //horizontal tab
        Array.add(this._safeKeys, 35); //home, end
        Array.add(this._safeKeys, 36);
        Array.add(this._safeKeys, 13); //enter key

    },

    _keyDownPress: function(evt) {
        /* This event happens specifically just to stop the users key strokes if the input has gone
        over the max length.  It does not actually truncate anything.  The _input method is used to
        actually fire off when text goes in and truncate it according.
       
        This method does however let users do a few keystrokes so they can still get around in the
        box as its full.
        */

        //Sys.Debug.trace('key press, ctrl:' +evt.ctrlKey + ' key ' + String.fromCharCode(evt.keyCode) + ' code:' + evt.keyCode);    

        if (this.get_element().value.length >= this._maxLength) {
            //make sure its not some of the keys a user needs to be able to use ie. delete, backspace, home, end.
            for (var i = 0; i < this._safeKeys.length; ++i) {
                //check to see if we hit a safe key
                if (evt.keyCode == this._safeKeys[i])
                    return;
            }

            //check for a select all -- ctrl + A
            var keyCodeString = String.fromCharCode(evt.keyCode);
            if (evt.ctrlKey && (keyCodeString == 'A' || keyCodeString == 'C' || keyCodeString == 'V' || keyCodeString == 'X'))
                return;

            //stop the key
            evt.preventDefault();
        }
    },

    _keyRelease: function(evt) {
        //this method will fire off each key release, and it will determine what to check against
        //based on what has been assigned.

        //do we check for the length?
        if (this._maxLength != 0) {
            if (this.get_element().value.length > this._maxLength) {
                this.get_element().value = this.get_element().value.substring(0, this._maxLength);
            }
        }

        //stop validation from initial tab into box...
        if (evt.keyCode != 9) {
            //do we fire off the validators?
            if (this._validators.length != 0) {
                Array.forEach(this._validators, function(item) { ValidatorValidate(item); })
            }
        }
    },

    //properties
    set_maxLength: function(value) {
        this._maxLength = value;
    },
    get_maxLength: function() {
        return this._maxLength;
    }
}//end prototype

YourQuest.URQuestTextBox.registerClass('YourQuest.URQuestTextBox', Sys.UI.Control);

/// <reference name="MicrosoftAjax.js" />

Type.registerNamespace('YourQuest');

YourQuest.PersonStatus = function() {
    YourQuest.PersonStatus.initializeBase(this);

    //bool for user being logged in...
    this._isLoggedIn;
    this._xmlHttp;

    //this is a timestamp used to determine if a user has be auth'd in the last 5 minutes using this function
    this._timeStamp;
    this._timeDifferenceAllowed; //in minutes
}

YourQuest.PersonStatus.prototype = {

    initialize: function() {
        YourQuest.PersonStatus.callBaseMethod(this, 'initialize');

        this._isLoggedIn = false;
        //create a global handler for use in other spots
        window.PersonStatus = this;

        //setup the amount of time before preforming another check
        this._timeDifferenceAllowed = 5;
        this._timeStamp = new Date();

    },

    dispose: function() {
        YourQuest.PersonStatus.callBaseMethod(this, 'dispose');
    },

    CheckStatus: function() {
        //Returns true or false based on if the user is logged in.

        if (this._doesUserNeedToAuthAgain()) {
            this._checkForStatus();
            this._timeStamp = new Date();
        }
    },

    _checkForStatus: function() {
        ///This acutal goes out and hits the page to determine if user is logged in still...
        this._isLoggedIn = false;
        var url = window.location + '?test=' + new Date().getMilliseconds().toString();

        if (window.XMLHttpRequest) {
            this._xmlHttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            try {
                this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) { }
        }

        //if we have something, lets make the call
        if (this._xmlHttp) {
            this._xmlHttp.open("GET", url, false);
            this._xmlHttp.onreadystatechange = Function.createDelegate(this, this._onSuccess);
            this._xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            try {
                this._xmlHttp.send();
            }
            catch (e) {
                this._isLoggedIn = false;
            }
        }

        //in ie7 we have to actually redirect the user...
        if (!this._isLoggedIn)
            this._redirectUser();
    },

    _redirectUser: function() {
        //Redirects the users to the login page
        var url = new String();
        url = window.location.toString();
        url = url.toLowerCase();
        var returnUrlIndex = url.indexOf('securesite');

        //setup the redirect url, and the returnurl
        var redirectURL;

        if (returnUrlIndex != -1) {
            redirectURL = url.substring(0, returnUrlIndex);
        }
        else {
            redirectURL = '/login.aspx';
        }

        window.location = redirectURL + 'login.aspx?ReturnUrl=' + window.location.pathname;
    },


    _doesUserNeedToAuthAgain: function() {

        var currentTimeStamp = new Date();
        //find the difference
        var diff = currentTimeStamp - this._timeStamp;

        //now figure if we went more then the time we alloted
        diff = Math.floor(diff / (1000 * 60)); //find the minutes
        if (diff > this._timeDifferenceAllowed)
            return true;
        else
            return false;

    },

    _determineAction: function(text) {
        /*looks to see if the form action is pointing to login.aspx page 
        true if user logged in
        false if not
        */

        var regPattern = /(<form.*action="Login.aspx\?ReturnUrl=)/;
        var result = regPattern.exec(text);

        var returnVal = result ? false : true;

        return returnVal;
    },

    _onSuccess: function() {
        if (this._xmlHttp.readyState == 4) {
            if (this._xmlHttp.status == 200) {
                if (this._determineAction(this._xmlHttp.responseText))
                    this._isLoggedIn = true;
            }
        }
    }

}//end prototype

YourQuest.PersonStatus.registerClass('YourQuest.PersonStatus', Sys.Component);

function getFlashMovieObject(movieName) {

    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[movieName]) {
            return document.embeds[movieName];
        }
    }
    else //If IE
    {
        return document.getElementById(movieName);
    }
} 

/* This is used for a list of checkboxes that you want
one of the items checked at all times */
(function($) {
    $.fn.oneCheckboxSelectedAtAllTimes = function() {
        //list of checkboxes
        var checkBoxes = this.filter('input:checkbox');
        //add a click function to each one to make sure one is checked
        checkBoxes.click(function() {
            checkedState = $(this).attr('checked');
            //if we are trying to uncheck it, we need to make sure something else is checked
            if (!checkedState) {
                //are any others checked?
                var otherBoxChecked = false;
                $(checkBoxes).each(function() {
                    if (!otherBoxChecked) {
                        otherBoxChecked = $(this).attr('checked');
                    }
                });
                //if nothing else is checked, return false
                if (!otherBoxChecked) return false;
            }
        });
    }
})(jQuery);

/* This is used to make a list of checkboxes to become
mutually exclusive */
(function($) {
    $.fn.makeCheckboxesMutuallyExclusive = function() {
       //list of checkboxes
       var checkBoxes = this.filter('input:checkbox');
       //add a click function to each one to make sure one is checked
       checkBoxes.click(function() {
           checkedState = $(this).attr('checked');
           //loop on all checkboxes and uncheck them
           $(checkBoxes).each(function() {
                 $(this).attr('checked', false);
           });
           $(this).attr('checked', checkedState);
        });
    }
})(jQuery);
