﻿//AUTHOR:   Jebb Burditt
//DATE:     April 19, 2011
//VERSION:  1.1

var searchBoxDefault = ["", "Search", "Recherche"];    //default text in search boxes
var unsavedChangesConfirmation = "You haven't saved your changes.";
 
/*
Example: to make input field into search box use $("#searchBoxID").searchTextBox();
<body>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#searchBoxID").searchTextBox();
        });
    </script>
    
    <form id="form1">
        <input type="text" id="searchBoxID">
    </form>
</body>
*/

//check for unsaved changes
var hasChanges = false;
jQuery.fn.protectData = function() {
    $(":input").not(".noprotect").change(function() {
        hasChanges = true;
    });
    window.onbeforeunload = function() {
        if (hasChanges) return unsavedChangesConfirmation
    };
};

//turn all input elements of a form with class 'date' into a date picker
jQuery.fn.dateInputify = function() {
    $(".date:input").each(function(i, el) {
        $(el).datepicker({
            changeMonth: true,
			changeYear: true,
            showOn: 'button',
            buttonImage: '../../images/icons/calendar.gif',
            buttonImageOnly: true
        });
    });
};

//turn all input elements of a form with class 'search' into a search box
jQuery.fn.searchTextBox = function(lang) {
    //searchInput show/hide default text
    if (!lang) lang = 1;            //default to English
    $(this).focus(function() {
        if ($(this).attr("value") == searchBoxDefault[lang]) $(this).attr("value", "");
    });
    $(this).blur(function() {
        if ($(this).attr("value") == "") $(this).attr("value", searchBoxDefault[lang]);
    });
    $(this).attr("value", searchBoxDefault[lang]);      //set default text
    $(this).blur();
};

/* NOT FINISHED! It doesn't quite work yet.
In .NET if you need to add a defaultbutton to textboxes, add a Panel around the form fields 
and use DefaultButton attribute
*/

//disable enter key of all inputs with class 'noenter' in form
jQuery.fn.disableEnterKeyForm = function() {
    $(".noenter:input").each(function(i, el) {
        $(el).disableEnterKey();
    });
};

//disable enter key of element
jQuery.fn.disableEnterKey = function() {
    $(this).keypress(function(event) {
        if (event.keycode == '13') {
            event.preventDefault();
            return false;
        }
    });
};            
