/*
    Podmiana w pojemniku np. span tekstu (tylko tekstu) na button.
*/
var ElemShowHide = Class.create();
ElemShowHide.prototype = {
    initialize: function(tabDataEdit, objVN) {
        for(i=0; i<tabDataEdit.length; i++) {
            this.showHide(tabDataEdit[i], objVN);
        }
    },

    showHide: function(tabParam, vn) {
          $(tabParam[0]).observe('click', function(event){
            this.showHideOne(tabParam[0], tabParam[1], tabParam[2], tabParam[3], tabParam[4], 'edytuj', 'anuluj');
            vn.refresh();
            Event.stop(event);
        }.bind(this));
    },

/*
    param:
    1. id przycisku wywolujacego funkcje,
    2. id elementu zawierajacego content,
    3. wartosc artybutu "name" inputa,
    4. id dla etykiety lable.
    5. nazwa przycisku wywolujacego funkcje dla edycji
    6. nazwa przycisku wywolujacego funkcje powrotu z edycji

*/
    showHideOne: function(idButton, idBox, InputName, labelId, classInput, editButtonCnt, backButtonCnt) {
        var valueInput = $(idBox).innerHTML;
        var codeInput = '<input type="text" id="'+labelId+'" name="'+InputName+'" value="'+valueInput+'" class="'+classInput+'" /><span id="box-old-content'+idButton+'" style="display: none;"></span>';
        var isClass = 0;

        $(idBox).classNames().each(function(classItem) {
                  if(classItem == 'chenge-field')
                      isClass = 1;
        });

        if(isClass == 0) {
            $(idBox).update(codeInput);
        	$('box-old-content'+idButton).update(valueInput);
            $(idButton).update(backButtonCnt);
            $(idBox).addClassName('chenge-field');
        }

        if(isClass == 1) {
            //$(idBox).update($(labelId).value);
            $(idBox).update($('box-old-content'+idButton).innerHTML);
            $(idButton).update(editButtonCnt);
            $(idBox).removeClassName('chenge-field')
        }
    }
};

/*
    User: operacje zwiazane ze zdjeciem
*/
var PhotoUser = Class.create();
PhotoUser.prototype = {
    initialize: function(delLinkId, changeLinkId, backLinkId, uploadLinkId ,photoBoxId, uploadBoxId, fileuploadId, urlChangePhoto) {
        this.delLink = $(delLinkId);
        this.changeLink = $(changeLinkId);
        this.backLink = $(backLinkId);
        this.uploadLink = $(uploadLinkId);
        this.photoBox = $(photoBoxId);
        this.uploadBox = $(uploadBoxId);
        this.fileupload = $(fileuploadId);
        this.urlChangePhoto = urlChangePhoto;

        this._setActions();
    },

    _setActions: function() {
        this.delLink.observe('click', function(event){
            alert('UWAGA: akcja usuwania nie jest podpięta.');
            Event.stop(event);
        }.bind(this));

        this.changeLink.observe('click', function(event){
            this._uploadPhotoBox();
            Event.stop(event);
        }.bind(this));

        this.backLink.observe('click', function(event){
            this._showPhotoBox();
            Event.stop(event);
        }.bind(this));

        this.uploadLink.observe('click', function(event) {
            this._upload();
            Event.stop(event);
        }.bind(this));
    },

    _showPhotoBox: function() {
        if(this.uploadBox.visible())
            this.uploadBox.hide();
        if(!this.photoBox.visible())
            this.photoBox.show();
    },

    _uploadPhotoBox: function() {
        if(this.photoBox.visible())
            this.photoBox.hide();
        if(!this.uploadBox.visible())
            this.uploadBox.show();
    },

    _upload: function() {
        var url = this.urlChangePhoto;
        var pars = 'uploadedfile'+this.fileupload.value;

        new Ajax.Request(url, {
            method: 'get',
            parameters: pars,

            onLoading: function() {},
            onFailure: function() {},
            onSuccess: function() {},
            onComplete: function(transport) {
                if(transport.responseText == 1) {
                    alert('Uwaga: upload nie jest gotowy.');
                    this._showPhotoBox();
                }
            }.bind(this)
        });
    }
};

/*
    Edycja hasla usera: "Wyswietlenie pojemnika edycji hasla"

    param:
    1. class elementow przed pojemnikiem edycji hasla,
    2. id pojemnika z elementami do zmiany hasla;
*/
function showPassBox (classEditElements, idPassBox) {
    $$('.'+classEditElements).each(function(elem) {
        if(elem.visible()) {
            elem.hide();
        }
    });

    if(!$(idPassBox).visible()) {
        $(idPassBox).show();
    }
}
/*
    Edycja hasla usera: "Ukrywanie pojemnika edycji hasla"

    param:
    1. class elementow przed pojemnikiem edycji hasla,
    2. id pojemnika z elementami do zmiany hasla;
*/
function hidePassBox (classEditElements, idPassBox, idErrorsPass) {
    $$('.'+classEditElements).each(function(elem) {
        if(!elem.visible()) {
            elem.show();
        }
    });

    if($(idPassBox).visible()) {
        $(idPassBox).hide();
    }

    if($(idErrorsPass).visible()) {
        $(idErrorsPass).hide();
    }
}

/*
 * Change password
 */
var PasswordUser = Class.create();
PasswordUser.prototype = {
    initialize: function(idPassInput, idUsrInput, idBoxLoader, idBoxErrorMessage, idInputNewPass, idInputNewPassCopy, urlNewPass) {
        this.PassInput = $(idPassInput);
        this.UsrInput = $(idUsrInput);
        this.BoxLoader = $(idBoxLoader);
        this.BoxErrorMessage = $(idBoxErrorMessage);
        this.InputNewPass = $(idInputNewPass);
        this.InputNewPassCopy = $(idInputNewPassCopy);
        this.urlNewPass = urlNewPass;

        //Messages
        this.tabErrorsTexts = new Array();
        this.tabErrorsTexts[0] = 'Obecne haslo bylo niepoprawne.';
        this.tabErrorsTexts[1] = 'Nowe i stare haslo byly identyczne.';
        this.tabErrorsTexts[2] = 'Nowe haslo bylo nieprawidlowe.';
        this.tabErrorsTexts[3] = 'Kopia hasla byla nieprawidlowa.';
        this.tabErrorsTexts[4] = 'Blad podczas zmiany hasla.';
    },

    _isValid: function() {
        if(!this._isCorrectPass())                return this.tabErrorsTexts[0];
        else if(!this._areDiffrentPass())        return this.tabErrorsTexts[1];
        else if(!this._isCorrectNewPass())        return this.tabErrorsTexts[2];
        else if(!this._isCorrectCopyNewPass())    return this.tabErrorsTexts[3];
        else                                     return 'noerrors';
    },

    _clearFields: function() {
        this.PassInput.value = this.InputNewPass.value = this.InputNewPassCopy.value = '';
    },

    _setNoErrors: function() {
        this.BoxErrorMessage.update();
        if(this.BoxErrorMessage.visible())
            this.BoxErrorMessage.hide();
    },

    _setError: function(msg) {
        this.BoxErrorMessage.update(msg);
        if(!this.BoxErrorMessage.visible())
            this.BoxErrorMessage.show();
    },

    changePass: function() {
        var rs = false;
        var msg;
        var updatePass;

        if(!this.BoxLoader.visible())
            this.BoxLoader.show();

        msg = this._isValid();
        if(msg == 'noerrors') {
            rsUpdatePass = this._setNewPass();
            if(rsUpdatePass) rs = true;
            else this._setError(this.tabErrorsTexts[4]);
        }
        else this._setError(msg);

        this._clearFields();

        if(this.BoxLoader.visible())
            this.BoxLoader.hide();

        return rs;
    },

    _setNewPass: function() {
        var rs = false;
        var url = this.urlNewPass;
        //var usrId = this.UsrInput.value;
        var newPass = this.InputNewPass.value;
        var pars = 'newPass='+newPass;//usrIdAjax='+usrId+'&

        new Ajax.Request(url, {
          asynchronous:        false,
          method:             'post',
          postBody:         pars,

          onLoading: function() {},
          onFailure: function() {},
          onSuccess: function() {},
          onComplete: function(transport) {
                var rsChangePass = transport.responseText;
                if(rsChangePass) rs = true;
          }.bind(this._setNewPass)
        });

        return rs;
    },

    _isCorrectPass: function() {
        var rs = false;

        var url = this.urlNewPass;
        //var usrId = this.UsrInput.value;
        var pass = this.PassInput.value;
        var pars = 'pass='+pass;//usrIdAjax='+usrId+'&

        new Ajax.Request(url, {
          asynchronous:        false,
          method:            'post',
          postBody:         pars,

          onLoading: function()    {},
          onFailure: function()    {},
          onSuccess: function()    {},
          onComplete: function(transport) {
            var rsCorrectPass = transport.responseText;
            if(rsCorrectPass) rs = true;
          }.bind(this._isCorrectPass)
        });

        return rs;
    },

    _isCorrectNewPass: function() {
        var newPass = this.InputNewPass.value;
        if (newPass == '')
            return false;

        var result = false;
        var filtr1 = /^(\w*(?=\w*\d)(?=\w*[a-z])\w*)$/;//(?=\w*[A-Z])
        var filtr2 = /.{6,}/;
        if (filtr1.test(newPass) && filtr2.test(newPass)) {
            result = true;
        }
        return result;
    },

    _isCorrectCopyNewPass: function() {
        var newPass = this.InputNewPass.value;
        var newPassCopy = this.InputNewPassCopy.value;

        if(newPass === newPassCopy)
            return true;

        return false;
    },

    _areDiffrentPass: function() {
        var pass =         this.PassInput.value;
        var newPass =     this.InputNewPass.value;

        if(pass == newPass)
            return false;

        return true;
    }
}