/* Biblioteka przydatnych klas i funkcji js */

/**
* Klasa Mask - wyrażenia regularne do walidacji pól formularza
*/
function Mask()
{
    this.regexp = null;
    this.str    = '';
    this.field  = null;
    this.e      = null;
}

/* wykonanie replace */
Mask.prototype.exec = function() {
    this.str = this.str.replace(this.regexp,""); 
}

/* kończymy działanie klasy */
Mask.prototype.post = function() {
    var oEvent = (this.e)?this.e:window.event;

    // pomijamy zmiany dla klawisza TAB - bo tracil sie focus
    if (oEvent && ((oEvent.which && oEvent.which!=9 && oEvent.which!=16 && oEvent.which!=37 && oEvent.which!=39) ||
           (oEvent.keyCode && oEvent.keyCode!=9 && oEvent.keyCode!=16 && oEvent.keyCode!=37 && oEvent.keyCode!=39))) 
    {
        this.field.value = this.str;
        this.field.focus();
    }
}

Mask.prototype.min = function(dig, min) {
    if (min != null && min != '')
        if (parseFloat(dig)<min)
            dig = min;
    return dig;
}

Mask.prototype.max = function(dig, max) {
    if (max != null && max != '')
        if (parseFloat(dig)>max)
            dig = max;
    return dig;
}

/* Filtrujemy pole - tylko liczby CAŁKOWITE */
Mask.prototype.Numbers = function(e, field) {
    this.regexp = /[^0-9]/g;
    this.str    = field.value;
    this.field  = field;
    this.e      = e;
    
    this.exec();
    this.post();
}

/* Filtrujemy pole - tylko liczby */
Mask.prototype.Float = function(e, field, precision) {
    this.regexp = /[^0-9,.]/g;
    this.str    = field.value.replace(',','.');
    this.field  = field;
    this.e      = e; 
    
    if (precision != null && precision != '' ) {
        idx = this.str.indexOf('.');
        if (idx>-1) {
            this.str = this.str.substr(0,idx+precision+1); 
        }
    }
    this.exec();
    this.post();
}

/* Filtrujemy pole - tylko liczby CAŁKOWITE i sprawdzamy zakres - jeśli zły to ustawiamy min lub max*/
Mask.prototype.NumbersMinMax = function(e, field,min,max) {
    this.regexp = /[^0-9]/g;
    this.str    = field.value;
    this.field  = field;
    this.e      = e;
    
    this.exec();
    
    this.str = this.min(this.str, min);
    this.str = this.max(this.str, max); 
   
    this.post();
}

/* Filtrujemy pole - tylko liczby i sprawdzamy zakres - jeśli zły to ustawiamy min lub max */
Mask.prototype.FloatMinMax = function(e, field, precision, min, max) {
    this.regexp = /[^0-9,.]/g;
    this.str    = field.value.replace(',','.');
    this.field  = field;
    this.e      = e;
    
    this.exec(); 
    
    if (precision != null && precision != '' ) {
        idx = this.str.indexOf('.');
        if (idx>-1) {
            this.str = this.str.substr(0,idx+precision+1); 
        }
    }
    this.str = this.min(this.str, min);
    this.str = this.max(this.str, max);

    this.post();
}


/* Maska daty w formacie DD-MM-YYYY */
Mask.prototype.Date = function(e, field) {
    this.regexp = /[^0-9]/g;
    this.str    = field.value;
    this.field  = field;
    this.e      = e;
    
    this.exec();

    tmp = this.str;
	this.str = tmp.substr(0,2);
    if (tmp.length>2)
        this.str += "-" + tmp.substr(2,2);
    if (tmp.length>4)
        this.str += "-" + tmp.substr(4,4);
    this.str = this.str.substr(0,10);
    
    this.post();
}

/* Maska daty w formacie YYYY-MM-DD */
Mask.prototype.DateDB = function(e, field) {
    this.regexp = /[^0-9]/g;
    this.str    = field.value;
    this.field  = field;
    this.e      = e;
    
    this.exec();

    tmp = this.str;
	this.str = tmp.substr(0,4);
    if (tmp.length>4)
        this.str += "-" + tmp.substr(4,2);
    if (tmp.length>6)
        this.str += "-" + tmp.substr(6,2);
    
    this.post();
}

/* Maska daty w formacie MM-YYYY */
Mask.prototype.DateMMYYYY = function(e, field) {
    this.regexp = /[^0-9]/g;
    this.str    = field.value;
    this.field  = field;
    this.e      = e;
    
    this.exec();
    
    tmp = this.str;
	this.str = tmp.substr(0,2);
    if (tmp.length>2)
        this.str += "-" + tmp.substr(2,4);

    this.post();
}

/* Maska daty w formacie YYYY-MM */
Mask.prototype.DateYYYYMM = function(e, field) {
    this.regexp = /[^0-9]/g;
    this.str    = field.value;
    this.field  = field;
    this.e      = e;
    
    this.exec();
    
    tmp = this.str;
	this.str = tmp.substr(0,4);
    if (tmp.length>4)
        this.str += "-" + tmp.substr(4,2);

    this.post();
}

/* Maska pieniędzy ( z kropką lub przecinkiem i dwoma miejscami po przecinku */
Mask.prototype.Money = function(e, field) {
    this.regexp = /[^0-9,.]/g;
    this.str = field.value.replace(',','.');
    this.field = field;
    this.e      = e; 
    
    this.exec();
    
    idx = this.str.indexOf('.');
    if (idx>-1) {
        this.str = this.str.substr(0,idx+3); 
    }

    this.post();
}

/* Obcinamy dlugość wpisanego tekstu do dl */
Mask.prototype.MaxLength = function(e, field, dl) {
    this.regexp = /[.]/g;
    this.str    = field.value;
    this.field  = field;
    this.e      = e;
   
    this.exec();

    this.str = this.str.substr(0,dl);
        
    this.post();
}

/* Maska czasu w formacie hh:mm:ss */
Mask.prototype.Time = function(e, field) {
    this.regexp = /[^0-9]/g;
    this.str    = field.value;
    this.field  = field;
    this.e      = e;
    
    this.exec();

    tmp = this.str;
	this.str = tmp.substr(0,2);
    if (tmp.length>2)
        this.str += ":" + tmp.substr(2,2);
    if (tmp.length>4)
        this.str += ":" + tmp.substr(4,2);

    this.post();
}

/* Maska numeru NIP 111-111-11-11 */
Mask.prototype.NIP = function(e, field) {
    this.regexp = /[^0-9]/g;
    this.str    = field.value;
    this.field  = field;
    this.e      = e;
    
    this.exec();

    tmp = this.str;
	this.str = tmp.substr(0,3);
    if (tmp.length>3)
        this.str += "-" + tmp.substr(3,3);
    if (tmp.length>6)
        this.str += "-" + tmp.substr(6,2);
    if (tmp.length>8)
        this.str += "-" + tmp.substr(8,2);

    this.post();
}

/* Maska wyniku meczu */
Mask.prototype.bet = function(e, field) {
    this.str    = field.value;
    this.field  = field;
    this.e      = e;
    this.regexp = /[-]/g;
	
	this.str = this.str.replace(this.regexp, ':');
	
    this.regexp = /[^0-9:]/g;

    this.exec();

    this.post();
}


