﻿Ext.AtpUtilities = function(){
    var msgCt;

    function createBox(t, s){
        return ['<div class="msg">',
                '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
                '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s, '</div></div></div>',
                '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
                '</div>'].join('');
    }
    return {
        msg : function(title, format){
            if(!msgCt){
                msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
            }
            msgCt.alignTo(document, 't-t');
            var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
            var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
            m.slideIn('t').pause(2).ghost("b", {remove:true});
        },
        
        replaceAll: function(theString,searchChar,newChar) {
			var strTemp = new String(theString)
			return strTemp.split(searchChar).join(newChar);
		},
		
        trim: function(s) {
			while (s.substring(0,1) == ' ') s = s.substring(1,s.length);
			while (s.substring(s.length-1,s.length) == ' ') s = s.substring(0,s.length-1);
			return s;
		},
		
		checkAlphaNumeric: function (inputStr)
		{
		  var iChars = "!@#$%^&*()+=-[]\\;,./{}|:<>?";

		  for (var i = 0; i < inputStr.length; i++)
  			if (iChars.indexOf(inputStr.charAt(i)) != -1) 
  				return true;

		  return false
		},

        init : function(){
            var s = Ext.get('extlib'), t = Ext.get('exttheme');
            if(!s || !t){ // run locally?
                return;
            }
            var lib = Cookies.get('extlib') || 'yahoo', 
                theme = Cookies.get('exttheme') || 'aero';
            if(lib){
                s.dom.value = lib;
            }
            if(theme){
                t.dom.value = theme;
                Ext.get(document.body).addClass('x-'+theme);
            }
            s.on('change', function(){
                Cookies.set('extlib', s.getValue());
                setTimeout(function(){
                    window.location.reload();
                }, 250);
            });

            t.on('change', function(){
                Cookies.set('exttheme', t.getValue());
                setTimeout(function(){
                    window.location.reload();
                }, 250);
            });
        }
    };
}();

Ext.onReady(Ext.AtpUtilities.init, Ext.AtpUtilities);

//** Number **//
Ext.apply(Ext.form.VTypes, {
    'tagsList': function(){
        var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
        return function(strValue){
            //check for numeric characters
            var vr = TagsHelper.validate(strValue);
            if (!vr) this.tagsListText = TagsHelper.getMessage();
            return TagsHelper.validate(strValue);
        }
    }(),
    'tagsListText': 'תגיות לא תקינות'
})