/**
 * @overview Bonjour Quebec Ext settings
 * This code is loaded into the browser after core Ext code
 * @author Chris Scott
 */

/**
 * @namespace BQ
 */
Ext.namespace('BQ', 'BQ.widget', 'BQ.user', 'BQ.album', 'BQ.video', 'BQ.content', 'BQ.data', 'BQ.carnet');

/**
 * Tell RExt.Application to NOT validate response Content-Type.  with Rails, it's easy
 * to set this to "application/json" -- with php, not sure.
 */
App.setValidateResponses(false);

Ext.BLANK_IMAGE_URL = "/static/js/ext-2.2/resources/images/default/s.gif";

/**
 * set field-validation msg to appear on side of form
 */
Ext.form.Field.prototype.msgTarget = 'side';

/**
 * App::t
 * implement translation function
 * translates a list of words through BQJSLabels["words_" + query] OR BQJSLabels[query]
 * Ext.StoreMgr.get('i18n').t();
 * @param {Mixed} words
 */

App.t = function() {

    var buf = [];
    for(var i = 0, len = arguments.length; i < len; i++){
		if (typeof arguments[i] != 'undefined') {
			var word = arguments[i].toLowerCase();
			if (BQJSLabels['words_' + word]) {
				word = BQJSLabels['words_' + word];
			}
			else
				if (BQJSLabels[word]) {
					word = BQJSLabels[word];
				}
				else
					if (BQJSLabels['phrase_' + word]) {
						word = BQJSLabels['phrase_' + word];
					}
					else {
						word = 'i18n:' + word;
					}
			buf[buf.length] = word;
		}
    }
    return buf.join(' ');   // <-- join the array of words on ' ' to form a sentence.
}

/**
 * App.urlFor
 * given a key and an url, return a string that combines the two with a lookup from the BQJSConfig array of keys
 * @param {Object} key
 * @param {Object} url
 */
App.urlFor = function(key, url) {
    try {
        return BQJSConfig["app_" + key] + '/' + url;
    }
    catch (e) {
        App.handleException(this, e);
    }
}

/**
 * Translate Ext.MessageBox buttons
 */
// i18n Ext.MessageBox buttons
Ext.MessageBox.buttonText = {
    cancel : App.t('cancel'),
    no: App.t('no'),
    ok: App.t('ok'),
    yes : App.t('yes')
};


/**
 * Custom email validation to support weirdo domain names like .museum and .travel
 */
Ext.apply(Ext.form.VTypes, {
    bqemail : function(val, field) {
        var email = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/; //trying a new regEx copied from BQMUser from backend
        return email.test(val.toLowerCase()); /* Vincent Maillot vincent.maillot@cossette.com 09/10/19 Ticket 6057 : pb majuscules */
    },
    bqemailText : App.t('err_invalid_email')
});

/**
 * Custom email validation to support weirdo domain names like .museum and .travel
 */
Ext.apply(Ext.form.VTypes, {
    bqemaillist : function(val, field) {
        var valid = true;
        var regExp = /^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,6}$/;
        var emails = val.split(",");
        for (var i = 0; i < emails.length; i++) {
            var email = emails[i].trim();
            if (regExp.test(email) == false) {
                valid = false;
            }
        }
        return valid;
    },
    bqemaillistText : App.t('err_invalid_email_list')
});

/**
* Vincent Maillot vincent.maillot@cossette.com
* 10/02/10
* Ticket 6313 => protection de l'image agrandie
**/
document.oncontextmenu =  function(e) 
{
    // FFX
    if(e && e['target'] && e['target']['tagName'] && e['target']['tagName'] == 'IMG') {e.stopPropagation();}
    // IE
    if(event && event['srcElement'] && event['srcElement']['tagName'] && event['srcElement']['tagName'] == 'IMG') { return false; }
}
;