/**
 * stepsForm.js v1.0.0
 * http://www.codrops.com
 *
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright 2014, Codrops
 * http://www.codrops.com
 */
;( function( window ) {
	
	'use strict';

	var transEndEventNames = {
			'WebkitTransition': 'webkitTransitionEnd',
			'MozTransition': 'transitionend',
			'OTransition': 'oTransitionEnd',
			'msTransition': 'MSTransitionEnd',
			'transition': 'transitionend'
		},
		transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
		support = { transitions : Modernizr.csstransitions };

	function extend( a, b ) {
		for( var key in b ) { 
			if( b.hasOwnProperty( key ) ) {
				a[key] = b[key];
			}
		}
		return a;
	}

	function stepsForm( el, options ) {
		this.el = el;
		this.options = extend( {}, this.options );
  		extend( this.options, options );
  		this._init();
	}

	stepsForm.prototype.options = {
		onSubmit : function() { return false; }
	};

	stepsForm.prototype._init = function() {
		// current question
		this.current = 0;

		// login
		this.login = [].slice.call( this.el.querySelectorAll( 'ol.login > li' ) );
		// total login
		this.loginCount = this.login.length;
		// show first question
		classie.addClass( this.login[0], 'current' );
		
		// next question control
		this.ctrlNext = this.el.querySelector( 'button.next' );

		// progress bar
		this.progress = this.el.querySelector( 'div.progress' );
		
		// question number status
		this.questionStatus = this.el.querySelector( 'span.number' );
		// current question placeholder
		this.currentNum = this.questionStatus.querySelector( 'span.number-current' );
		this.currentNum.innerHTML = Number( this.current + 1 );
		// total login placeholder
		this.totalQuestionNum = this.questionStatus.querySelector( 'span.number-total' );
		this.totalQuestionNum.innerHTML = this.loginCount;
		// error message
		this.error = this.el.querySelector( 'span.error-message' );
		
		// init events
		this._initEvents();
	};

	stepsForm.prototype._initEvents = function() {
		var self = this,
			// first input
			firstElInput = this.login[ this.current ].querySelector( 'input' ),
			// focus
			onFocusStartFn = function() {
				firstElInput.removeEventListener( 'focus', onFocusStartFn );
				classie.addClass( self.ctrlNext, 'show' );
			};

		// show the next question control first time the input gets focused
		firstElInput.addEventListener( 'focus', onFocusStartFn );

		// show next question
			this.ctrlNext.addEventListener('click', function (ev) {
				ev.preventDefault();
				self._nextQuestion();
			});

		// pressing enter will jump to next question
		document.addEventListener( 'keydown', function( ev ) {
			var keyCode = ev.keyCode || ev.which;
			// enter
			if( keyCode === 13 ) {
				ev.preventDefault();
				self._nextQuestion();
			}
		} );

		// disable tab
		this.el.addEventListener( 'keydown', function( ev ) {
			var keyCode = ev.keyCode || ev.which;
			// tab
			if( keyCode === 9 ) {
				ev.preventDefault();
			} 
		} );
	};

/*	stepsForm.prototype._nextQuestion = function() {
		if( !this._validade() ) {
			return false;
		}

		// check if form is filled
		if( this.current === this.loginCount - 1 ) {
			this.isFilled = true;
		}
		// clear any previous error messages
		this._clearError();

		// current question
		var currentQuestion = this.login[ this.current ];

		// increment current question iterator
		++this.current;

		// update progress bar
		this._progress();

	if( !this.isFilled ) {
			// change the current question number/status
			this._updateQuestionNumber();

			// add class "show-next" to form element (start animations)
			classie.addClass( this.el, 'show-next' );

			// remove class "current" from current question and add it to the next one
			// current question
			var nextQuestion = this.login[ this.current ];
			classie.removeClass( currentQuestion, 'current' );
			classie.addClass( nextQuestion, 'current' );
		}

		// after animation ends, remove class "show-next" from form element and change current question placeholder
		var self = this,
			onEndTransitionFn = function( ev ) {
				if( support.transitions ) {
					this.removeEventListener( transEndEventName, onEndTransitionFn );
				}
				if( self.isFilled ) {
					self._submit();
				}
				else {
					classie.removeClass( self.el, 'show-next' );
					self.currentNum.innerHTML = self.nextQuestionNum.innerHTML;
					self.questionStatus.removeChild( self.nextQuestionNum );
					// force the focus on the next input
					nextQuestion.querySelector( 'input' ).focus();
				}
			};

		if( support.transitions ) {
			this.progress.addEventListener( transEndEventName, onEndTransitionFn );
		}
		else {
			onEndTransitionFn();
		}
	}*/

	//Check

	/*stepsForm.prototype._check = function() {
		app.controller('LoginController', function ($scope, $http, API_URL) {

			$scope.save = function (login) {

				var email = login.user_name;
				var password = login.password;
				var url = 'http://192.168.1.134:8021/expodine_reports/api/expo_emailexist';
				$http({
					method: 'POST',
					url: url,
					data: {"email": login.user_name},
				}).success(function (response) {
					if (response === 'Email Exist') {
                           return 'yes';

	               }
               else {
                return 'no';
                 }
                }).error(function (response) {
         return 'error';
         });
 }
 });
	}*/


	// updates the progress bar by setting its width
	stepsForm.prototype._progress = function() {
		this.progress.style.width = this.current * ( 100 / this.loginCount ) + '%';
	}

	// changes the current question number
	stepsForm.prototype._updateQuestionNumber = function() {
		// first, create next question number placeholder
		this.nextQuestionNum = document.createElement( 'span' );
		this.nextQuestionNum.className = 'number-next';
		this.nextQuestionNum.innerHTML = Number( this.current + 1 );
		// insert it in the DOM
		this.questionStatus.appendChild( this.nextQuestionNum );
	}


	// submits the form
	stepsForm.prototype._submit = function() {
		this.options.onSubmit( this.el );
	}

	// the validation function

	stepsForm.prototype._validade = function() {
		// current question´s input
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var email =  document.getElementById( 'username').value;
		var password =  document.getElementById( 'password').value;
		var count =  document.getElementById( 'count').innerHTML;

		if(count === '1')
		{
			if (email === '')
			{
				this._showError('EMPTYSTR');
				return false;
			}

			if (email != '') {
				if (!emailReg.test(email))
				{
					this._showError('INVALIDEMAIL');
					return false;
				}
			}
			if(true)
			{
				//savedata(email);
			}

		}
		if(count === '2')
		{
			if (password === '') {
				this._showError('EMPTYSTR');
				return false;
			}
		}
		return true;
	}

	// TODO (next version..)
	stepsForm.prototype._showError = function( err ) {
		var message = '';
		switch( err ) {
			case 'EMPTYSTR' :
				message = 'Please fill the field before continuing';
				break;
			case 'INVALIDEMAIL' : 
				message = 'Please fill a valid email address';
				break;
			// ...
		};
		this.error.innerHTML = message;
		classie.addClass( this.error, 'show' );
	}

	// clears/hides the current error message
	stepsForm.prototype._clearError = function() {
		classie.removeClass( this.error, 'show' );
	}

	// add to global namespace
	window.stepsForm = stepsForm;

})( window );

	app.controller('LoginController', function ($scope, $http, API_URL) {

			$scope.save = function (login) {

				var email = login.user_name;
				var password = login.password;
				var url = 'http://192.168.1.134:8021/expodine_reports/api/expo_emailexist';
				$http({
					method: 'POST',
					url: url,
					data: {"email": login.user_name},
				}).success(function (response)
				{
					if (response === 'Email Exist') {

					stepsForm.prototype._nextQuestion = function()
					{

							if( this.current === this.loginCount - 1 ) {
								this.isFilled = true;
							}
							// clear any previous error messages
							this._clearError();

							// current question
							var currentQuestion = this.login[ this.current ];

							// increment current question iterator
							++this.current;
							// update progress bar

							//this._progress();
							if( !this.isFilled ) {
								// change the current question number/status
								this._updateQuestionNumber();
								// add class "show-next" to form element (start animations)
								classie.addClass( this.el, 'show-next' );

								// remove class "current" from current question and add it to the next one
								// current question
								/*var nextQuestion = this.login[ this.current ];
								classie.removeClass( currentQuestion, 'current' );
								classie.addClass( nextQuestion, 'current' );*/
							}

							// after animation ends, remove class "show-next" from form element and change current question placeholder
						/*	var self = this,
								onEndTransitionFn = function( ev ) {
									if( support.transitions ) {
										this.removeEventListener( transEndEventName, onEndTransitionFn );
									}
									if( self.isFilled ) {
										self._submit();
									}
									else {
										classie.removeClass( self.el, 'show-next' );
										self.currentNum.innerHTML = self.nextQuestionNum.innerHTML;
										self.questionStatus.removeChild( self.nextQuestionNum );
										// force the focus on the next input
										nextQuestion.querySelector( 'input' ).focus();
									}
								};

							if( support.transitions ) {
								this.progress.addEventListener( transEndEventName, onEndTransitionFn );
							}
							else {
								onEndTransitionFn();
							}*/

							var reslt = $http({
								method: 'POST',
								url: url,
								data: {"email": login.user_name},
							}).success(function (response)
							{
								if (response === 'Email Exist')
								{
                                   alert('okkk');
								}
								else
								{
									alert('yessss');
								}
							}).error(function (response) {
							});

               	}
					}
					else {
						alert('yes');
					}
				}).error(function (response) {
					alert('error');
				});
			}

		});
