﻿/*
 * FormFocus - simple jQuery plugin for contact forms
 * Version: 1.0 (09/22/2010)
 * Copyright (c) 2010 David Dierking
 * Requires: jQuery v1.3+
*/

/*
 * Installation Instructions
 * Install the following on your form page to initialize the plugin
    $(document).ready(function(){
        $("#divFields").formfocus();
    });
    
 * for advanced options
   $(document).ready(function(){
        $("#divFields").formfocus({
            focus : true|false,
            blur : true|false,
            keyup : true|false,
            label : true|false,
            submitBtn : string
        });
    });
 *Be sure to include blank.gif in your /images directory. This prevents the dynamically created labels from being selectable in IE.
*/

(function ($) {
    $.fn.formfocus = function(options)
    {
        var defaults = {
            focus : true,
            blur : true,
            keyup : true,
            label : true,
            submitBtn : ''
        };
        var options = $.extend(defaults, options);
        if(options.submitBtn == "" || options.submitBtn == undefined || options.submitBtn == null){
        	$btns = this.find("input[type=submit], input[type=button], input[type=image]");
        	if($btns.length > 0)
        		options.submitBtn = $btns.attr("id");
        }
        this.find("input[type=text],input[type=password], textarea")
        .focus(function()
        {
			$(this).data('safeEnter_InAutocomplete', false);
            if(options.focus && !options.label)
            {
                $txt = $(this);
                if($txt.val() == $txt.attr("Default"))
                    $txt.val("");
            }
        })
        .blur(function()
        {
            if(options.blur && !options.label)
            {
                $txt = $(this);
                if($txt.val() == "")
                    $txt.val($txt.attr("Default"));
            }
        })
        .keyup(function(event)
        {
            if(options.keyup && options.label)
            {
                $txt = $(this);
                if($txt.val() == "")
                    $txt.prev().html($txt.attr("Default"));
                else
                    $txt.prev().html("");
                    //$txt.prev().css("display", "none");
            }
        })
        .keypress(function(event)
        {
            var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
            
            switch(keyCode)
            {
				case 13:
					// Fire the event if:
                    //   - we're not currently in the browser's Autocomplete, or
                    //   - this isn't a textbox, or
                    //   - this is Opera (which provides its own protection)
					if (!$(this).data('safeEnter_InAutocomplete') || !$(this).is('input[type=text]') || $.browser.opera)
                    {
                        var btn = document.getElementById(options.submitBtn);
            			if(btn != null){
							document.getElementById(options.submitBtn).click();
							$(this).data('safeEnter_InAutocomplete', false);
							return false;
						}
                    }
                    $(this).data('safeEnter_InAutocomplete', false);
					break;
				case 40:
				case 38:
				case 34:
				case 33:
					// down=40,up=38,pgdn=34,pgup=33
					$(this).data('safeEnter_InAutocomplete', true);
					break;
				default:
					$(this).data('safeEnter_InAutocomplete', false);
                    break;
            }
            
            if (keyCode == 13) {
            	
            }
            else
            return true;
        })
        .each(function()
        {
            $txt = $(this);
            if(options.label)
            {
                $txt.before("<label class='txtLabel' style='position: absolute; z-index: 0;'></label>");
                $txt.css({"z-index" : "1", "position" : "relative", "background" : "url(/images/blank.gif)"});
                if($txt.val() == "")
                    $txt.prev().html($txt.attr("Default"));
                    //$txt.prev().css("display", "block");
            }
            else
            {
                if($txt.val() == "")
                    $txt.val($txt.attr("Default"));
            }
        });
    }
})(jQuery);
