$(function() {
	/*
	number of fieldsets
	*/
	var fieldsetCount = $('#formElem').children().length;
	
	/*
	current position of fieldset / navigation link
	*/
	var current 	= 1;
    
	/*
	sum and save the widths of each one of the fieldsets
	set the final sum as the total width of the steps element
	*/
	var stepsWidth	= 0;
    var widths 		= new Array();
	$('#steps .step').each(function(i){
        var $step 		= $(this);
		widths[i]  		= stepsWidth;
        stepsWidth	 	+= $step.width();
    });
	$('#steps').width(stepsWidth);
	
	/*
	to avoid problems in IE, focus the first input of the form
	*/
	$('#formElem').children(':first').find(':input:first').focus();	
	
	/*
	show the navigation bar
	*/
	$('#navigation').show();
	
	/*
	when clicking on a navigation link 
	the form slides to the corresponding fieldset
	*/
 	//---------------------Start of the register button save and continue works----------------------------------//
	 $('#registerButton').bind('click',function(e){
		var fname = $('#fname').val();
		var address = $('#address').val();
		var dob = $('#dob').val();
		var gender = $('#gender1').val();
		var email = $('#email').val();
		$('#fname_error').html('');
		$('#address').html('');
		$('#dob_error').html('');
		$('#gender_error').html('');
		$('#email_error').html('');
		if(fname == ''){
			$('#fname_error').html('<strong>This field is required</strong>');	
		}
		if(address == ''){
			$('#address_error').html('<strong>This field is required</strong>');	
		}
		if(dob == ''){
			$('#dob_error').html('<strong>This field is required</strong>');	
		}
		if(gender == ''){
			$('#gender_error').html('<strong>This field is required</strong>');	
		}
		if(email == ''){
			$('#email_error').html('<strong>This field is required</strong>');	
		}
		if(fname != '' && dob != '' &&  gender != '' &&  email != '' && address != ''){ 
				updateBasicProfile();
				/*var validationvalue=validation_employeeform();
				if(validationvalue==0){
					return false;
				}alert('xss');*/
					/// animation ... 									
				var nthis	= $('#navigation'); 
				var prev	= current; 
				nthis.closest('ul').find('li').removeClass('selected');
				nthis.addClass('selected');
				
				/*
				we store the position of the link
				in the current variable	
				*/
				current = nthis.index() + 2;   
				/*
				animate / slide to the next or to the corresponding
				fieldset. The order of the links in the navigation
				is the order of the fieldsets.
				Also, after sliding, we trigger the focus on the first 
				input element of the new fieldset
				If we clicked on the last link (confirmation), then we validate
				all the fieldsets, otherwise we validate the previous one
				before the form slided
				*/
				$('#steps').stop().animate({
					marginLeft: '-' + widths[current-1] + 'px'
				},500,function(){
					if(current == fieldsetCount)
						validateSteps();
					else
						validateStep(prev);
					$('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();	
				});
				$('html, body').animate({scrollTop : 0},500);
				//e.preventDefault();
				
		}
			
    });
	 //---------------------End of the register button save and continue works----------------------------------//
	  //---------------------Start of the register button save and continue works----------------------------------//

	 $('#registerButton2').bind('click',function(e){
		 /*alert('3');*/
		/*var validationvalue=validation_employeeform1();
		if(validationvalue==0){
			return false;
		}*/
		/// animation ... 	
		var rname = $('#rname').val();
		var relation = $('#relation').val();
		var contact = $('#contact').val();
		
		$('#rname_error').html('');
		$('#relation_error').html('');
		$('#contact_error').html('');
		
		if(rname == ''){
			$('#rname_error').html('<strong>This field is required</strong>');	
		}
		if(relation == ''){
			$('#relation_error').html('<strong>This field is required</strong>');	
		}
		if(contact == ''){
			$('#contact_error').html('<strong>This field is required</strong>');	
		}
		if(rname != '' && relation != '' && contact != ''){
			updateReference();
			var nthis	= $('#navigation'); 
			var prev	= current; 
			nthis.closest('ul').find('li').removeClass('selected');
			nthis.addClass('selected');
				////////////////////animation 2
				current = nthis.index() + 3; 
			/*
			animate / slide to the next or to the corresponding
			fieldset. The order of the links in the navigation
			is the order of the fieldsets.
			Also, after sliding, we trigger the focus on the first 
			input element of the new fieldset
			If we clicked on the last link (confirmation), then we validate
			all the fieldsets, otherwise we validate the previous one
			before the form slided
			*/
			$('#steps').stop().animate({
				marginLeft: '-' + widths[current-1] + 'px'
			},500,function(){
				if(current == fieldsetCount)
					validateSteps();
				else
					validateStep(prev);
				$('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();	
			});
			$('html, body').animate({scrollTop : 0},500);
			e.preventDefault();
		}
    });
	 //---------------------End of the register button save and continue works----------------------------------//
	  //---------------------Start of the register button save and continue works----------------------------------//

	 $('#registerButton3').bind('click',function(e){
												 
		updateExperience();
				/// animation ... 									
		var nthis	= $('#navigation'); 
		var prev	= current; 
		nthis.closest('ul').find('li').removeClass('selected');
        nthis.addClass('selected');
			current = nthis.index() + 4; 
		/*
		animate / slide to the next or to the corresponding
		fieldset. The order of the links in the navigation
		is the order of the fieldsets.
		Also, after sliding, we trigger the focus on the first 
		input element of the new fieldset
		If we clicked on the last link (confirmation), then we validate
		all the fieldsets, otherwise we validate the previous one
		before the form slided
		*/
        $('#steps').stop().animate({
            marginLeft: '-' + widths[current-1] + 'px'
        },500,function(){
			if(current == fieldsetCount)
				validateSteps();
			else
				validateStep(prev);
			$('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();	
		});
		$('html, body').animate({scrollTop : 0},500);
        e.preventDefault();
		
    });
	 //---------------------End of the register button save and continue works----------------------------------//
	 
	  //---------------------Start of the register button save and continue works----------------------------------//

	 $('#registerButton4').bind('click',function(e){
		
				/// animation ... 
		$('#emp_id_error').html('');
		$('#empstat2_error').html('');
		$('#dt_join_error').html('');
		//$('#blood_grp_error').html('');
		//$('#esi_details_error').html('');
		$('#emp_contract_error').html('');
		//$('#contractPeriod_error').html('');
		//$('#emp_prob_period_error').html('');		
		var emp_id = $('#emp_id').val();
		var empstat2 = $('#empstat2').val();
		var dt_join = $('#dt_join').val();
		//var blood_grp = $('#blood_grp').val();
		//var esi_details = $('#esi_details').val();
		var emp_contract = $('#emp_contract').val();
		/*var contractPeriod = $('.contractPeriod').val();*/
		
		//var contractPeriod = $("input[name='contractPeriod']").val();
		var contractPeriodTemp = '0';
		//var emp_prob_period = $('#emp_prob_period').val();
		if(emp_id==''){
			$('#emp_id_error').html('<strong>This field is required</strong>');		
		}
		if(empstat2==''){
			$('#empstat2_error').html('<strong>This field is required</strong>');		
		}
		if(dt_join==''){
			$('#dt_join_error').html('<strong>This field is required</strong>');		
		}
		/*if(blood_grp==''){
			$('#blood_grp_error').html('<strong>This field is required</strong>');		
		}
		if(esi_details==''){
			$('#esi_details_error').html('<strong>This field is required</strong>');		
		}*/
		if(emp_contract==''){
			$('#emp_contract_error').html('<strong>This field is required</strong>');		
		}
		/*if(contractPeriod=='' && emp_contract == 'L'){
			contractPeriodTemp = '1';
			$('#contractPeriod_error').html('<strong>This field is required</strong>');		
		}*/
		/*if(emp_prob_period==''){
			$('#emp_prob_period_error').html('<strong>This field is required</strong>');		
		}*/
		if(emp_id!='' && empstat2!='' && dt_join!=''   && emp_contract!='' && contractPeriodTemp!='1' ){
			updateDetails();
			var nthis	= $('#navigation'); 
			var prev	= current; 
			nthis.closest('ul').find('li').removeClass('selected');
			nthis.addClass('selected');
				current = nthis.index() + 5; 
			/*
			animate / slide to the next or to the corresponding
			fieldset. The order of the links in the navigation
			is the order of the fieldsets.
			Also, after sliding, we trigger the focus on the first 
			input element of the new fieldset
			If we clicked on the last link (confirmation), then we validate
			all the fieldsets, otherwise we validate the previous one
			before the form slided
			*/
			$('#steps').stop().animate({
				marginLeft: '-' + widths[current-1] + 'px'
			},500,function(){
				if(current == fieldsetCount)
					validateSteps();
				else
					validateStep(prev);
				$('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();	
			});
			$('html, body').animate({scrollTop : 0},500);
			e.preventDefault();
		}
    });
	 //---------------------End of the register button save and continue works----------------------------------//
	 
	 
	   //---------------------Start of the register button save and continue works----------------------------------//

	 $('#registerButton5').bind('click',function(e){
												 
		
				/// animation ... 
		
		$('#accountname_error').html('');
		$('#bank_error').html('');
		$('#accountno_error').html('');
		$('#bankadd_error').html('');
		$('#bankcode_error').html('');
		
		var accountname = $('#accountname').val();
		var bank = $('#bank').val();
		var accountno = $('#accountno').val();
		var bankadd = $('#bankadd').val();
		var bankcode = $('#bankcode').val();
		
		if(accountname == ''){
			$('#accountname_error').html('<strong>This field is required</strong>');	
		}
		if(bank == ''){
			$('#bank_error').html('<strong>This field is required</strong>');	
		}
		if(accountno == ''){
			$('#accountno_error').html('<strong>This field is required</strong>');	
		}
		if(bankadd == ''){
			$('#bankadd_error').html('<strong>This field is required</strong>');	
		}
		if(bankcode == ''){
			$('#bankcode_error').html('<strong>This field is required</strong>');	
		}
		if(accountname != '' && bank != '' && accountno != '' && bankadd != '' && bankcode != ''){
			 updateBank();
			var nthis	= $('#navigation'); 
			var prev	= current; 
			nthis.closest('ul').find('li').removeClass('selected');
			nthis.addClass('selected');
				current = nthis.index() + 6; 
			/*
			animate / slide to the next or to the corresponding
			fieldset. The order of the links in the navigation
			is the order of the fieldsets.
			Also, after sliding, we trigger the focus on the first 
			input element of the new fieldset
			If we clicked on the last link (confirmation), then we validate
			all the fieldsets, otherwise we validate the previous one
			before the form slided
			*/
			$('#steps').stop().animate({
				marginLeft: '-' + widths[current-1] + 'px'
			},500,function(){
				if(current == fieldsetCount)
					validateSteps();
				else
					validateStep(prev);
				$('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();	
			});
			$('html, body').animate({scrollTop : 0},500);
			e.preventDefault();
		}
    });
	 //---------------------End of the register button save and continue works----------------------------------//
	 
	   //---------------------Start of the register button save and continue works----------------------------------//

	 //---------------------End of the register button save and continue works----------------------------------//
    $('#navigation a').bind('click',function(e){ 
		var $this	= $(this);
		var prev	= current;
		$this.closest('ul').find('li').removeClass('selected');
        $this.parent().addClass('selected');
		/*
		we store the position of the link
		in the current variable	
		*/
		current = $this.parent().index() + 1; 
		/*
		animate / slide to the next or to the corresponding
		fieldset. The order of the links in the navigation
		is the order of the fieldsets.
		Also, after sliding, we trigger the focus on the first 
		input element of the new fieldset
		If we clicked on the last link (confirmation), then we validate
		all the fieldsets, otherwise we validate the previous one
		before the form slided
		*/
        $('#steps').stop().animate({
            marginLeft: '-' + widths[current-1] + 'px'
        },500,function(){
			if(current == fieldsetCount)
				validateSteps();
			else
				validateStep(prev);
			$('#formElem').children(':nth-child('+ parseInt(current) +')').find(':input:first').focus();	
		});
		$('html, body').animate({scrollTop : 0},500);
        e.preventDefault();
    });
	
	/*
	clicking on the tab (on the last input of each fieldset), makes the form
	slide to the next step
	*/
	$('#formElem > fieldset').each(function(){
		var $fieldset = $(this);
		$fieldset.children(':last').find(':input').keydown(function(e){
			if (e.which == 9){
				$('#navigation li:nth-child(' + (parseInt(current)+1) + ') a').click();
				/* force the blur for validation */
				$(this).blur();
				e.preventDefault();
			}
		});
	});
	
	/*
	validates errors on all the fieldsets
	records if the Form has errors in $('#formElem').data()
	*/
	function validateSteps(){
		var FormErrors = false;
		for(var i = 1; i < fieldsetCount; ++i){
			var error = validateStep(i);
			if(error == -1)
				FormErrors = true;
		}
		$('#formElem').data('errors',FormErrors);	
	}
	
	/*
	validates one fieldset
	and returns -1 if errors found, or 1 if not
	*/
	function validateStep(step){
		if(step == fieldsetCount) return;
		
		var error = 1;
		var hasError = false;
		$('#formElem').children(':nth-child('+ parseInt(step) +')').find(':input:not(button)').each(function(){
			var $this 		= $(this);
			var valueLength = jQuery.trim($this.val()).length;
			
			if(valueLength == ''){
				hasError = true;
				$this.css('background-color','#FFEDEF');
			}
			else
				$this.css('background-color','#FFFFFF');	
		});
		var $link = $('#navigation li:nth-child(' + parseInt(step) + ') a');
		$link.parent().find('.error,.checked').remove();
		
		var valclass = 'checked';
		if(hasError){
			error = -1;
			valclass = 'error';
		}
		$('<span class="'+valclass+'"></span>').insertAfter($link);
		
		
		return error;
	}
	
	/*
	if there are errors don't allow the user to submit
	*/
	$('#registerButton').bind('click',function(){
		/*alert('In 1 ');	*/
		if($('#formElem').data('errors')){
			alert('Please correct the errors in the Form');
			return false;
		}	
	});
	
});
