// baseURI defined in DOM Level 3 -> not supported in IE
if (typeof document.baseURI === 'undefined') {
    document.baseURI = Ext.DomQuery.select('html head base')[0].href
}

Ext.namespace('Itransition.form');

/*
 * untill fixed by Ext.js:
 * Overriding default field filtering for Safari & Opera
 * @see http://projects.itransition.com/browse/INTERACTIVE-362
*/
if (Ext.isSafari || Ext.isOpera) {
    Ext.override(Ext.form.TextField, {
        filterKeys : function(e){
            var k = e.getKey();
            if (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1)){
                return;
            }
        }
    });
}

Itransition.form.EmailTextField = Ext.extend(Ext.form.TextField, {
    fieldLabel: 'Email',
    name: 'email',
    vtype: 'email',
    validationEvent: 'blur' // validate when field loses focus
});

Ext.reg('textfield-email', Itransition.form.EmailTextField);

Ext.getDom = function(el){
    if(!el || !document){
        return null;
    }
    return Ext.type(el.dom) === 'element' ? el.dom : (typeof el == 'string' ? document.getElementById(el) : el);
};

(function(){
    var defaultTitle = {}

    Itransition.setDocumentTitle = function(title){
        if (title.section && title.section !== defaultTitle.section) {
            delete defaultTitle.subSection
        }

        Ext.apply(defaultTitle, title)

        var d = defaultTitle
        var section = d.section ? ' > ' + d.section : ''
        var subSection = d.subSection ? ' > ' + d.subSection : ''

        document.title = d.project + section + subSection

        return document.title
    }
})()

/**
 * The email regexp has one character that should have been escaped.
 * 
 * @see http://extjs.com/forum/showthread.php?p=244495
 */
Ext.form.VTypes.email = function(v){
    return /^([\w]+)([\.\-][\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,4}$/.test(v);
}
