﻿function hasAttribute(el, attr) {
    if (el.hasAttribute) return el.hasAttribute(attr);
    if (attr == 'class') attr = 'className'
    else if (attr == 'for') attr = 'htmlFor';
    var node = el.getAttributeNode(attr);
    return node && node.specified
}


function HTML5FormSupport() {
    var forms = document.forms;
    var i = forms.length;
    if (i) {
        var input = document.createElement('input');
        var supportsPlaceholder = 'placeholder' in input;
        var supportsAutofocus = 'autofocus' in input;
        if (!supportsPlaceholder || !supportsAutofocus) {
            var focusElement;
            while (i--) {
                var elements = forms[i].elements;
                var j = elements.length;
                while (j--) {
                    var el = elements[j];
                    var placeHolder;
                    if (!supportsPlaceholder && el.type == 'text' && (placeHolder = el.getAttribute('placeholder'))) {
                        if (el.value == '') el.value = placeHolder;
                        //addEvent(el, 'focus', placeholderFunc);
                        YAHOO.util.Event.addListener(el, 'focus', placeholderFunc);
                        YAHOO.util.Event.addListener(el, 'blur', placeholderFunc); 
                    }
                    if (!supportsAutofocus && hasAttribute(el, 'autofocus')) focusElement = el
                }
            }
            if (focusElement) focusElement.focus();
        }
    }
}

function placeholderFunc() {
    if (this.value == this.getAttribute('placeholder')) {
        this.value = ''
    }
    else {
        if (YAHOO.lang.trim(this.value) == '') {
            this.value = this.getAttribute('placeholder')
        }
        else this.select()
    }
}

