
	
	var StripeReader = Class.create();

	StripeReader.prototype = {
		initialize: function(elementID, sessionID) {
			this.sessionId  = sessionID;
			this.element    = $(elementID);
			this.inputValue = '';
			this.lastValue  = '';
			this.currentFinalValue = '';
			this.minimumLengthForChecking = 4;
			this.doneInputting = false;
			this.machineInput  = true;
			this.isFocused = false;
			new PeriodicalExecuter(this.periodicalCheck.bindAsEventListener(this), 0.1);
			// new PeriodicalExecuter(this.// periodicalCheck.bindAsEventListener(this), 0.1);
			
			Event.observe(this.element, 'keydown', this.update.bindAsEventListener(this));
			Event.observe(this.element, 'focus', this.resetAndReboot.bindAsEventListener(this));
			Event.observe(this.element, 'blur', this.blurredElement.bindAsEventListener(this));
			
			$(this.element).focus();
		},
		
		update: function(ev) {
			// alert(ev.keyCode);
			// console.log(ev.keyCode);
			if(ev.keyCode == 13)
			{
				ev.stop();
				return;
			}

			if(!this.doneInputting && this.machineInput)
			{
				this.inputValue += $F(this.element).charAt($F(this.element).length-1);
				$(this.element).value = '';
			}
		},
		
		periodicalCheck: function(el) {
			// console.log(this.isFocused);
			// if(this.machineInput && !this.isFocused)
				// $(this.element).focus();
			
			if(this.inputValue.length > this.minimumLengthForChecking && !this.doneInputting && this.machineInput)
			{
				if(this.inputValue == this.lastValue)
				{
					// this.inputValue = ';001086779=1211?';
					var finalValue     = this.inputValue.replace(';','').split('=')[0];
					// console.log(this.inputValue);
					// console.log(this.inputValue.replace(';','').split('=')[0]);
					// console.log(finalValue);
					this.doneInputting = true;
					this.currentFinalValueRaw = finalValue;
					this.currentFinalValue    = this.clearLeftZeros(finalValue);
					this.element.value = finalValue;
					this.element.blur();
					this.dispatchNewCard();
				}
				var self = this;
				window.setTimeout(function(){$(self.element).focus();},1000);
				this.lastValue = this.inputValue;
			}
		},
		
		resetAndReboot: function() {
			this.isFocused = true;
			if(this.machineInput)
			{
				this.inputValue    = '';
				this.lastValue     = '';
				$(this.element).value = '';
				this.doneInputting = false;
			}
		},
		
		blurredElement: function() {
			this.isFocused = false;
		},
		
		toggleInput: function(el) {
			if(this.machineInput)
			{
				this.machineInput = false;
				$(el).innerHTML = 'Stripe Reader Input';

			}
			else
			{
				this.machineInput = true;
				$(el).innerHTML = 'Manual input';
				$(this.element).focus();
			}
		},
		
		clearLeftZeros: function(str) {
			while(str.charAt(0) == 0)
				str = str.substr(1,(str.length-1));
			return str;
		},
		
		/*
		 * Chapman specifics
		 */
		dispatchNewCard: function() {
			/* Students is currently recruited */
			if($('sessionStudent-'+this.currentFinalValue))
			{
				var studentRow = $('sessionStudent-'+this.currentFinalValue);
				/* If student is already markes as attended, ask for his signature */
				/*
				 * CHANGED for no topaz launch 
				 */
				if(studentRow.down('select').selectedIndex == 1)
				{
					// var signatureLink = studentRow.down('a.signatureLink');
					// openSignatureWindow(signatureLink);
				}
				else
				{
					showLoading(studentRow.down('td').next('td'));
					new Ajax.Request(ajaxUrl, { postBody: 'ajaxAction=conductMarkStudentAsAttended&sessionId=' + this.sessionId + '&chapmanId=' + this.currentFinalValueRaw, onComplete: this.markStudentAsAttended_callback.bindAsEventListener(this,studentRow) });
				}
			}
			/* Make Ajax call to check if the student is registered or not in the database */
			else
			{
				showLoading('stripeReaderLoading');
				new Ajax.Request(ajaxUrl, { postBody: 'ajaxAction=conductCheckExistenceOfID&id='+this.currentFinalValueRaw+'&sessionId='+this.sessionId, onComplete: this.dispatchNewCard_callback.bindAsEventListener(this) });
			}			
		},
		
		markStudentAsAttended_callback: function(request, studentRow) {
			hideLoading(studentRow.down('td').next('td'));
			if(request.responseText == 'error')
				alert('Sorry but there was an error processing your request.');
			else
			{
				studentRow.down('select').selectedIndex = 1;
				new Effect.Highlight(studentRow, {duration:5});
				$(this.element).focus();
			}
		},
		
		dispatchNewCard_callback: function(request) 
		{
			hideLoading('stripeReaderLoading');
			if(request.responseText == 'error')
				alert('Sorry but there was an error processing your request.');
			else if(request.responseText == 'notRegistered')
			{
				if(confirm("This student isn't registered at the Recruiter Database. Would you like to register him now?"))
				{
					conductAddStudent();
					conductAddStudentNewAccount(this.currentFinalValueRaw);
				}
			}
			else
			{
				$('conductTableBody').insert({'bottom':request.responseText});
				var currentFinalValue = this.currentFinalValue;
				new Effect.ScrollTo('sessionStudent-'+this.currentFinalValue,{duration:0.5, afterFinish:function(){new Effect.Highlight('sessionStudent-'+currentFinalValue,{duration:5});}});
			}
		}
		
	};

	
