// JavaScript Document
// klio settings

Ext.isBorderBox = true;

/* === 20 fevrier === */
if (Ext.isIE8) Ext.apply(Ext.Element.prototype, {
			
	mask : function(msg, msgCls) {
		var VISIBILITY      = "visibility",
            DISPLAY         = "display",
            HIDDEN          = "hidden",
            NONE            = "none",
            XMASKED         = Ext.baseCSSPrefix + "masked",
            XMASKEDRELATIVE = Ext.baseCSSPrefix + "masked-relative",
            data            = Ext.Element.data;
			
		var me  = this,
			dom = me.dom,
			setExpression = dom.style.setExpression,
			dh  = Ext.DomHelper,
			EXTELMASKMSG = Ext.baseCSSPrefix + "mask-msg",
			el,
			mask;
		
		if (!(/^body/i.test(dom.tagName) && me.getStyle('position') == 'static')) {
			me.addCls(XMASKEDRELATIVE);
		}
		el = data(dom, 'maskMsg');
		if (el) {
			el.remove();
		}
		el = data(dom, 'mask');
		if (el) {
			el.remove();
		}

		mask = dh.append(dom, {cls : Ext.baseCSSPrefix + "mask"}, true);
		data(dom, 'mask', mask);

		me.addCls(XMASKED);
		mask.setDisplayed(true);

		if (typeof msg == 'string') {
			var mm = dh.append(dom, {cls : EXTELMASKMSG, cn:{tag:'div'}}, true);
			data(dom, 'maskMsg', mm);
			mm.dom.className = msgCls ? EXTELMASKMSG + " " + msgCls : EXTELMASKMSG;
			mm.dom.firstChild.innerHTML = msg;
			mm.setDisplayed(true);
			mm.center(me);
		}

		if (!Ext.supports.IncludePaddingInWidthCalculation && setExpression && !Ext.isIE8) {
		  mask.dom.style.setExpression('width', 'this.parentNode.offsetWidth + "px"');
		}

	   
		if (!Ext.supports.IncludePaddingInHeightCalculation && setExpression && !Ext.isIE8) {
			mask.dom.style.setExpression('height', 'this.parentNode.offsetHeight + "px"');
		}
		
		else if (Ext.isIE && !(Ext.isIE7 && Ext.isStrict) && me.getStyle('height') == 'auto') {
			mask.setSize(undefined, me.getHeight());
		}
		return mask;
	}
});


//4,07 combo bug ie6,ie7
Ext.override(Ext.form.field.ComboBox, {
    setHiddenValue: function(values){
        var me = this, i;
        if (!me.hiddenDataEl) {
            return;
        }
        values = Ext.Array.from(values);
        var dom = me.hiddenDataEl.dom,
            childNodes = dom.childNodes,
            input = childNodes[0],
            valueCount = values.length,
            childrenCount = childNodes.length;
 
        if (!input && valueCount > 0) {
            me.hiddenDataEl.update(Ext.DomHelper.markup({tag:'input', type:'hidden', name:me.name + "-hidden"}));
            childrenCount = 1;
            input = dom.firstChild;
        }
        while (childrenCount > valueCount) {
            dom.removeChild(childNodes[0]);
            -- childrenCount;
        }
        while (childrenCount < valueCount) {
            dom.appendChild(input.cloneNode(true));
            ++ childrenCount;
        }   
        for (i = 0; i < valueCount; i++) {
            childNodes[i].value = values[i];
        }
    }
});

//header tooltip plugin
Ext.define('Ext.ux.grid.HeaderToolTip', {
	alias: 'plugin.headertooltip',
	init : function(grid) {
		var headerCt = grid.headerCt;
		grid.headerCt.on("afterrender", function(g) {
			grid.tip = Ext.create('Ext.tip.ToolTip', {
				target: headerCt.el,
				delegate: ".x-column-header",
				trackMouse: true,
				renderTo: Ext.getBody(),
				listeners: {
					beforeshow: function(tip) {
						var c = headerCt.down('gridcolumn[id=' + tip.triggerElement.id  +']');
						if (c  && c.tooltip)
							tip.update(c.tooltip);
						else
							return false;
					}
				}
			});
		});
	}
});

Ext.onReady(function() {
	
	Ext.QuickTips.init();
	
	
	if(Ext.ux.statusbar.ValidationStatus) {
		Ext.apply(Ext.ux.statusbar.ValidationStatus.prototype, {
          	showText : 'Formulaire invalide (cliquer ici pour plus de détails...)'
    		,hideText : 'Cliquez à nouveau ici pour masquer l\'alerte'
			,submitText : 'Sauvegarde...'    
        });
	}

	
	if(Ext.toolbar.Paging){
        Ext.apply(Ext.toolbar.Paging.prototype, {
            beforePageText : "Page",
            afterPageText  : "sur {0}",
            firstText      : "Première page",
            prevText       : "Page précédente",
            nextText       : "Page suivante",
            lastText       : "Dernière page",
            refreshText    : "Actualiser la liste",
            displayMsg     : "{0} à {1} sur {2}",
            emptyMsg       : 'Aucune donnée à afficher'
        });
    }
	
	if (Ext.form.field.Date){
        Ext.apply(Ext.form.field.Date.prototype, {
            format            : "d/m/Y",
            altFormats        : "d/m/y|d-m-y|d-m-Y|d/m|d-m|dm|dmy|dmY|d|Y-m-d",
			invalidText       : "{0} n'est pas une date valide - elle doit être au format jj/mm/aaaa",
			getSubmitValue: function() {
				var me = this,
					format = me.submitFormat || me.format,
					value = me.getValue();
					
				//return value ? Ext.Date.format(value, format) : null;
				return value ? Ext.Date.format(value, format) : "";
			}
        });
    }
	
	
	if (Ext.form.field.Number){
		Ext.apply(Ext.form.field.Number.prototype, {
			// allow dot as decimalSeparator even if this is not the locale setting
			baseChars : '0123456789.',
			// force decimals string
			forceDecimals : true,
			valueToRaw: function(value) {
				var me = this,decimalSeparator = me.decimalSeparator,precision = me.decimalPrecision,allowDecimals = me.allowDecimals,forceDecimals = me.forceDecimals;
				value = me.parseValue(value);
				value = me.fixPrecision(value);
				value = Ext.isNumber(value) ? value : parseFloat(String(value).replace(decimalSeparator, '.'));
				
				if (isNaN(value)) return '';
				else {
					value = String(value).replace('.', decimalSeparator);
					if (value.indexOf(decimalSeparator)>=0 && forceDecimals) {
						var dec = value.substr(value.indexOf(decimalSeparator)+1);
						for(var i=dec.length;i<precision;i++) {
							value = value+"0";
						}
					}
					else if (forceDecimals && allowDecimals) {
						value = value+decimalSeparator+"00";
					}
					return value;
				}
			}
		});
	}
	
	
	if(Ext.form.field.Date){
        Ext.apply(Ext.form.field.Date.prototype, {
            format            : "d/m/Y",
            altFormats        : "d/m/y|d-m-y|d-m-Y|d/m|d-m|dm|dmy|dmY|d|Y-m-d"
        });
    }
	
	
	
	
	if(Ext.ux.statusbar.ValidationStatus){
        Ext.apply(Ext.ux.statusbar.ValidationStatus.prototype, {
			
			init : function(sb){
				sb.on('render', function(){
					this.statusBar = sb;
					this.monitor = true;
					this.errors = Ext.create('Ext.util.MixedCollection');
					this.listAlign = (sb.statusAlign === 'right' ? 'br-tr?' : 'bl-tl?');
					
					if (this.form) {
						this.formPanel = Ext.getCmp(this.form);
						this.basicForm = this.formPanel.getForm();
						this.startMonitoring();
						this.basicForm.on('beforeaction', function(f, action){
							if(action.type === 'submit'){
								// Ignore monitoring while submitting otherwise the field validation
								// events cause the status message to reset too early
								this.monitor = false;
							}
						}, this);
						var startMonitor = function(){
							this.monitor = true;
						};
						this.basicForm.on('actioncomplete', startMonitor, this);
						this.basicForm.on('actionfailed', startMonitor, this);
					}
				}, this, {single:true});
				sb.on({
					scope: this,
					afterlayout:{
						single: true,
						fn: function(){
							// Grab the statusEl after the first layout.
							//sb.statusEl.getEl().on('click', this.onStatusClick, this, {buffer:200});
							//klio
							if (sb.statusEl) sb.statusEl.getEl().on('click', this.onStatusClick, this, {buffer:200});
							else sb.getEl().on('click', this.onStatusClick, this, {buffer:200});
						} 
					}, 
					beforedestroy:{
						single: true,
						fn: this.onDestroy
					} 
				});
			}
	
	 });
    }
	
});


/* === 23/01/2012 Input Text Mask === */
Ext.define('Ux.InputTextMask', {
   constructor: function(mask, clearWhenInvalid) {
      if(clearWhenInvalid === undefined)
         this.clearWhenInvalid = true;
      else
         this.clearWhenInvalid = clearWhenInvalid;
      this.rawMask = mask;
      this.viewMask = '';
      this.maskArray = new Array();
      var mai = 0;
      var regexp = '';
      for(var i=0; i<mask.length; i++){
         if(regexp){
            if(regexp == 'X'){
               regexp = '';
            }
            if(mask.charAt(i) == 'X'){
               this.maskArray[mai] = regexp;
               mai++;
               regexp = '';
            } else {
               regexp += mask.charAt(i);
            }
         } else if(mask.charAt(i) == 'X'){
            regexp += 'X';
            this.viewMask += '_';
         } else if(mask.charAt(i) == '9' || mask.charAt(i) == 'L' || mask.charAt(i) == 'l' || mask.charAt(i) == 'A') {
            this.viewMask += '_';
            this.maskArray[mai] = mask.charAt(i);
            mai++;
         } else {
            this.viewMask += mask.charAt(i);
            this.maskArray[mai] = RegExp.escape(mask.charAt(i));
            mai++;
         }
      }

      this.specialChars = this.viewMask.replace(/(L|l|9|A|_|X)/g,'');
      return this;
   },

   init: function(field) {
      this.field = field;
		this.field.regex=new RegExp("^[^_]*$"); // klio
		
      if (field.rendered){
         this.assignEl();
      } else {
         field.on('render', this.assignEl, this);
      }

      field.on('blur',this.removeValueWhenInvalid, this);
      field.on('focus',this.processMaskFocus, this);
	  
   },

   assignEl: function() {
      this.inputTextElement = this.field.inputEl.dom;
      this.field.inputEl.on('keypress', this.processKeyPress, this);
      this.field.inputEl.on('keydown', this.processKeyDown, this);
      if(Ext.isSafari || Ext.isIE){
         this.field.inputEl.on('paste',this.startTask,this);
         this.field.inputEl.on('cut',this.startTask,this);
      }
      if(Ext.isGecko || Ext.isOpera){
         this.field.inputEl.on('mousedown',this.setPreviousValue,this);
      }
      if(Ext.isGecko){
        this.field.inputEl.on('input',this.onInput,this);
      }
      if(Ext.isOpera){
        this.field.inputEl.on('input',this.onInputOpera,this);
      }
   },
   onInput: function(){
      this.startTask(false);
   },
   onInputOpera: function(){
      if(!this.prevValueOpera){
         this.startTask(false);
      }else{
         this.manageBackspaceAndDeleteOpera();
      }
   },

   manageBackspaceAndDeleteOpera: function(){
      this.inputTextElement.value=this.prevValueOpera.cursorPos.previousValue;
      this.manageTheText(this.prevValueOpera.keycode,this.prevValueOpera.cursorPos);
      this.prevValueOpera=null;
   },

   setPreviousValue: function(event){
      this.oldCursorPos=this.getCursorPosition();
   },

   getValidatedKey: function(keycode, cursorPosition) {
      var maskKey = this.maskArray[cursorPosition.start];
      if(maskKey == '9'){
         return keycode.pressedKey.match(/[0-9]/);
      } else if(maskKey == 'L'){
         return (keycode.pressedKey.match(/[A-Za-z]/))? keycode.pressedKey.toUpperCase(): null;
      } else if(maskKey == 'l'){
         return (keycode.pressedKey.match(/[A-Za-z]/))? keycode.pressedKey.toLowerCase(): null;
      } else if(maskKey == 'A'){
         return keycode.pressedKey.match(/[A-Za-z0-9]/);
      } else if(maskKey){
         return (keycode.pressedKey.match(new RegExp(maskKey)));
      }
      return(null);
   },

   removeValueWhenInvalid: function() {
      if(this.clearWhenInvalid && this.inputTextElement.value.indexOf('_') > -1){
         if (this.inputTextElement.value==this.viewMask) this.inputTextElement.value = ''; // klio
      }
	 this.field.validate(); // klio
   },

   managePaste: function() {
      if(this.oldCursorPos==null){
        return;
      }
      var valuePasted=this.inputTextElement.value.substring(this.oldCursorPos.start,this.inputTextElement.value.length-(this.oldCursorPos.previousValue.length-this.oldCursorPos.end));
      if(this.oldCursorPos.start<this.oldCursorPos.end){
         this.oldCursorPos.previousValue =
            this.oldCursorPos.previousValue.substring(0,this.oldCursorPos.start)+
            this.viewMask.substring(this.oldCursorPos.start,this.oldCursorPos.end)+
            this.oldCursorPos.previousValue.substring(this.oldCursorPos.end,this.oldCursorPos.previousValue.length);
         valuePasted=valuePasted.substr(0,this.oldCursorPos.end-this.oldCursorPos.start);
      }
      this.inputTextElement.value=this.oldCursorPos.previousValue;
      keycode = {
         unicode: '',
         isShiftPressed: false,
         isTab: false,
         isBackspace: false,
         isLeftOrRightArrow: false,
         isDelete: false,
         pressedKey: ''
      }
      var charOk=false;
      for(var i=0;i<valuePasted.length;i++){
         keycode.pressedKey=valuePasted.substr(i,1);
         keycode.unicode=valuePasted.charCodeAt(i);
         this.oldCursorPos=this.skipMaskCharacters(keycode,this.oldCursorPos);
         if(this.oldCursorPos===false){
            break;
         }
         if(this.injectValue(keycode,this.oldCursorPos)){
            charOk=true;
            this.moveCursorToPosition(keycode, this.oldCursorPos);
            this.oldCursorPos.previousValue=this.inputTextElement.value;
            this.oldCursorPos.start=this.oldCursorPos.start+1;
         }
      }
      if(!charOk && this.oldCursorPos!==false){
         this.moveCursorToPosition(null, this.oldCursorPos);
      }
      this.oldCursorPos=null;
   },

   processKeyDown: function(e){
      this.processMaskFormatting(e,'keydown');
   },

   processKeyPress: function(e){
      this.processMaskFormatting(e,'keypress');
   },

   startTask: function(setOldCursor){
      if(this.task==undefined){
         this.task=new Ext.util.DelayedTask(this.managePaste,this);
     }
      if(setOldCursor!== false){
         this.oldCursorPos=this.getCursorPosition();
     }
     this.task.delay(0);
   },

   skipMaskCharacters: function(keycode, cursorPos){
      if(cursorPos.start!=cursorPos.end && (keycode.isDelete || keycode.isBackspace))
         return(cursorPos);
      while(this.specialChars.match(RegExp.escape(this.viewMask.charAt(((keycode.isBackspace)? cursorPos.start-1: cursorPos.start))))){
         if(keycode.isBackspace) {
            cursorPos.dec();
         } else {
            cursorPos.inc();
         }
         if(cursorPos.start >= cursorPos.previousValue.length || cursorPos.start < 0){
            return false;
         }
      }
      return(cursorPos);
   },

   isManagedByKeyDown: function(keycode){
      if(keycode.isDelete || keycode.isBackspace){
         return(true);
      }
      return(false);
   },

   processMaskFormatting: function(e, type) {
      this.oldCursorPos=null;
      var cursorPos = this.getCursorPosition();
      var keycode = this.getKeyCode(e, type);
      if(keycode.unicode==0){//?? sometimes on Safari
         return;
      }
      if((keycode.unicode==67 || keycode.unicode==99) && e.ctrlKey){//Ctrl+c, let's the browser manage it!
         return;
      }
      if((keycode.unicode==88 || keycode.unicode==120) && e.ctrlKey){//Ctrl+x, manage paste
         this.startTask();
         return;
      }
      if((keycode.unicode==86 || keycode.unicode==118) && e.ctrlKey){//Ctrl+v, manage paste....
         this.startTask();
         return;
      }
      if((keycode.isBackspace || keycode.isDelete) && Ext.isOpera){
        this.prevValueOpera={cursorPos: cursorPos, keycode: keycode};
        return;
      }
      if(type=='keydown' && !this.isManagedByKeyDown(keycode)){
         return true;
      }
      if(type=='keypress' && this.isManagedByKeyDown(keycode)){
         return true;
      }
      if(this.handleEventBubble(e, keycode, type)){
         return true;
      }
      return(this.manageTheText(keycode, cursorPos));
   },

   manageTheText: function(keycode, cursorPos){
      if(this.inputTextElement.value.length === 0){
         this.inputTextElement.value = this.viewMask;
      }
      cursorPos=this.skipMaskCharacters(keycode, cursorPos);
      if(cursorPos===false){
         return false;
      }
      if(this.injectValue(keycode, cursorPos)){
         this.moveCursorToPosition(keycode, cursorPos);
      }
      return(false);
   },

   processMaskFocus: function(){
      if(this.inputTextElement.value.length == 0){
         var cursorPos = this.getCursorPosition();
         this.inputTextElement.value = this.viewMask;
         this.moveCursorToPosition(null, cursorPos);
      }
   },

   isManagedByBrowser: function(keyEvent, keycode, type){
      if(((type=='keypress' && keyEvent.charCode===0) ||
         type=='keydown') && (keycode.unicode==Ext.EventObject.TAB ||
         keycode.unicode==Ext.EventObject.RETURN ||
         keycode.unicode==Ext.EventObject.ENTER ||
         keycode.unicode==Ext.EventObject.SHIFT ||
         keycode.unicode==Ext.EventObject.CONTROL ||
         keycode.unicode==Ext.EventObject.ESC ||
         keycode.unicode==Ext.EventObject.PAGEUP ||
         keycode.unicode==Ext.EventObject.PAGEDOWN ||
         keycode.unicode==Ext.EventObject.END ||
         keycode.unicode==Ext.EventObject.HOME ||
         keycode.unicode==Ext.EventObject.LEFT ||
         keycode.unicode==Ext.EventObject.UP ||
         keycode.unicode==Ext.EventObject.RIGHT ||
         keycode.unicode==Ext.EventObject.DOWN)){
            return(true);
      }
      return(false);
   },

   handleEventBubble: function(keyEvent, keycode, type) {
      try {
         if(keycode && this.isManagedByBrowser(keyEvent, keycode, type)){
            return true;
         }
         keyEvent.stopEvent();
         return false;
      } catch(e) {
         alert(e.message);
      }
   },

   getCursorPosition: function() {
      var s, e, r;
      if(this.inputTextElement.createTextRange){
         r = document.selection.createRange().duplicate();
         r.moveEnd('character', this.inputTextElement.value.length);
         if(r.text === ''){
            s = this.inputTextElement.value.length;
         } else {
            s = this.inputTextElement.value.lastIndexOf(r.text);
         }
         r = document.selection.createRange().duplicate();
         r.moveStart('character', -this.inputTextElement.value.length);
         e = r.text.length;
      } else {
         s = this.inputTextElement.selectionStart;
         e = this.inputTextElement.selectionEnd;
      }
      return this.CursorPosition(s, e, r, this.inputTextElement.value);
   },

   moveCursorToPosition: function(keycode, cursorPosition) {
      var p = (!keycode || (keycode && keycode.isBackspace ))? cursorPosition.start: cursorPosition.start + 1;
      if(this.inputTextElement.createTextRange){
         cursorPosition.range.move('character', p);
         cursorPosition.range.select();
      } else {
         this.inputTextElement.selectionStart = p;
         this.inputTextElement.selectionEnd = p;
      }
   },

   injectValue: function(keycode, cursorPosition) {
      if (!keycode.isDelete && keycode.unicode == cursorPosition.previousValue.charCodeAt(cursorPosition.start))
         return true;
      var key;
      if(!keycode.isDelete && !keycode.isBackspace){
         key=this.getValidatedKey(keycode, cursorPosition);
      } else {
         if(cursorPosition.start == cursorPosition.end){
            key='_';
            if(keycode.isBackspace){
               cursorPosition.dec();
            }
         } else {
            key=this.viewMask.substring(cursorPosition.start,cursorPosition.end);
         }
      }
      if(key){
         this.inputTextElement.value = cursorPosition.previousValue.substring(0,cursorPosition.start)
            + key +
            cursorPosition.previousValue.substring(cursorPosition.start + key.length,cursorPosition.previousValue.length);
         return true;
      }
      return false;
   },

   getKeyCode: function(onKeyDownEvent, type) {
      var keycode = {};
      keycode.unicode = onKeyDownEvent.getKey();
      keycode.isShiftPressed = onKeyDownEvent.shiftKey;

      keycode.isDelete = ((onKeyDownEvent.getKey() == Ext.EventObject.DELETE && type=='keydown') || ( type=='keypress' && onKeyDownEvent.charCode===0 && onKeyDownEvent.keyCode == Ext.EventObject.DELETE))? true: false;
      keycode.isTab = (onKeyDownEvent.getKey() == Ext.EventObject.TAB)? true: false;
      keycode.isBackspace = (onKeyDownEvent.getKey() == Ext.EventObject.BACKSPACE)? true: false;
      keycode.isLeftOrRightArrow = (onKeyDownEvent.getKey() == Ext.EventObject.LEFT || onKeyDownEvent.getKey() == Ext.EventObject.RIGHT)? true: false;
      keycode.pressedKey = String.fromCharCode(keycode.unicode);
      return(keycode);
   },

   CursorPosition: function(start, end, range, previousValue) {
      var cursorPosition = {};
      cursorPosition.start = isNaN(start)? 0: start;
      cursorPosition.end = isNaN(end)? 0: end;
      cursorPosition.range = range;
      cursorPosition.previousValue = previousValue;
      cursorPosition.inc = function(){cursorPosition.start++;cursorPosition.end++;};
      cursorPosition.dec = function(){cursorPosition.start--;cursorPosition.end--;};
      return(cursorPosition);
   }
});

Ext.applyIf(RegExp, {
   escape: function(str) {
      return new String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
   }
});
