var CardSwtOperations = Class.create();
CardSwtOperations.prototype = {
	initialize: function(ucpId, url, urlReload, loaderBoxId, reloadDataBoxId, idErrorBox) {
		this.ucpId = ucpId;
		this.loaderBoxId = loaderBoxId;
		this.reloadDataBoxId = reloadDataBoxId;
		this.urlReload = urlReload;
		this.url = url;
		this.idErrorBox = idErrorBox;
		this.serial  = '';
		this.number = '';

		this.errorMessage1 = 'Nieprawidłowy serial i/lub numer.';
		this.errorMessage2 = 'Kombinacja serial, number już istnieje.';
	},

	_clearField: function(idSerial, idNumber){
		$(idSerial).value = '';
		$(idNumber).value = '';
	},

	_refresh: function(serial, number){
		this.serial  = serial;
		this.number = number;
	},

	_isValid: function() {
		errorbox = $(this.idErrorBox);
		var result = false;
		if(this.serial.match(/^.{1,}$/) != null && this.number.match(/^[0-9]{1,}$/) != null) {
			result = true;
		}
		//wyswietlenie info. o bledzie
		if(!result) {
			errorbox.innerHTML = this.errorMessage1;
			if(!errorbox.visible())
				errorbox.show();
		}
		else {
			if(errorbox.visible())
				errorbox.hide();
		}

		return result;
	},

	_setActions: function() {
		//bezposrednio uzyte atrybuty metody initialize
		this._showLayer(this.loaderBoxId);
		this._addCard(this.ucpId, this.url, this.urlReload, this.serial, this.number, this.loaderBoxId, this.reloadDataBoxId, this.idErrorBox, this.errorMessage2);
	},

	_addCard: function(ucpId, url, urlReload, serial, number, loaderBoxId, reloadDataBoxId, idErrorBox, errorMessage) {
		this.loaderBoxIdAC = loaderBoxId;
		this.ucpIdAC = ucpId;
		this.ErrorBoxAC = $(idErrorBox);
		this.urlReloadAC = urlReload;
		this.reloadDataBoxIdAC = reloadDataBoxId;
		this.errorMessageAC =  errorMessage;
		var pars = 'cardSwtOperations=1&ucpIdAjax='+ucpId+'&serialAjax='+serial+'&numberAjax='+number;

		new Ajax.Request(url, {
			method: 'get',
			parameters: pars,

			onLoading: function() {},
			onFailure: function() {},
			onSuccess: function() {},
			onComplete: function(transport) {
				//sprawdzenie czy karta swt juz istnieje i wyswietlenie info. o bledzie
				if(transport.responseText == -1) {
					this.ErrorBoxAC.innerHTML = this.errorMessageAC;
					if(!this.ErrorBoxAC.visible())
						this.ErrorBoxAC.show();

					if($(this.loaderBoxIdAC).visible())
						$(this.loaderBoxIdAC).hide();
				}
				else {
					if(this.ErrorBoxAC.visible())
						this.ErrorBoxAC.hide();

					//odswierzenie box-a z kartami swt
					this._reloadCards(this.ucpIdAC, this.urlReloadAC, this.reloadDataBoxIdAC, this.loaderBoxIdAC);
				}
			}.bind(this)
		});
	},

	_showLayer: function (loaderBoxId) {
		var loaderBox = $(loaderBoxId);
		if(!loaderBox.visible()) {
			loaderBox.show();
		}
	},

	_reloadCards: function(ucpId, url, reloadDataBoxId, loaderBoxId ) {
		this.loaderBox = $(loaderBoxId);
		var pars = 'ucpId='+ucpId;
		new Ajax.Updater( {success: reloadDataBoxId}, url, {
			method: 'get',
			parameters: pars,

			onComplete: function() {
				if(this.loaderBox.visible()) {
					this.loaderBox.hide();
				}
			}.bind(this)
		});
	},

	_deleteCardSwt: function(ucdId, url) {
		var pars = 'deleteCardSwt=1&ucdId='+ucdId;

		new Ajax.Request(url, {
			method: 'get',
			parameters: pars,

			onLoading: function() {},
			onFailure: function() {},
			onSuccess: function() {},
			onComplete: function(transport) {
				if(transport.responseText == -1) {
					alert('Wystąpił problem z bazą danych, podczas usuwania karty SWT.');
				}
				//bezposrednio uzyte atrybuty metody initialize
				this._reloadCards(this.ucpId, this.urlReload, this.reloadDataBoxId, this.loaderBoxId);
			}.bind(this)
		});
	},

	_showAddCardBox: function(linkId ,boxId) {
		var box = $(boxId);
		var link = $(linkId);
		if(!box.visible())
			box.show();
		if(link.visible())
			link.hide();
	},

	_hideAddCardBox: function(linkId ,boxId, errorFieldId) {
		var box = $(boxId);
		var link = $(linkId);
		var errorField = $(errorFieldId);
		if(box.visible())
			box.hide();
		if(!link.visible())
			link.show();
		if(errorField.visible())
			errorField.hide();
	}
};