$('document').ready(function(){
	$('div.input input,div.input textarea,div.input select').focus(function(){
		$(this).parent().addClass('focused');
	});

	$('div.input input,div.input textarea,div.input select').blur(function(){
		$(this).parent().removeClass('focused');
	});

	$('#tier1 input,#tier1 textarea,#tier1 select').each(function(){
		$(this).removeAttr('id');
	})

	var breedCount = 0;
	$('#addBreed a').click(function(e){
		e.preventDefault();

		oldId = (breedCount + 1);
		var cloned = $('#tier' + oldId).clone(true);
		
		breedId = cloned.attr('id');

		cloned.attr('id', breedId.replace(/\d+/, breedCount + 2 ));
		cloned.find('legend').text('Tier ' + (breedCount + 2));
		
		cloned.find('input').each(function(){
			el = $(this);
			
			el.attr('name',el.attr('name').replace(/\d+/, breedCount + 1 ));
			el.attr('id',el.attr('id').replace(/\d+/, breedCount + 1 ));

			if(el.attr('type') == 'text') {
				el.attr('value','');
			}

			if(el.attr('type') == 'checkbox' || el.attr('type') == 'radio') {
				el.attr('checked',false);
				if($.browser.msie) {
					var temp = this.outerHTML;
					temp = temp.replace(new RegExp(breedCount), breedCount + 1);
					$(el).replaceWith(temp); 
				}
			}
			
		});
		
		cloned.find('select').each(function(){
			el = $(this);
			el.attr('name',el.attr('name').replace(/\d+/, breedCount + 1 ));
			el.attr('id',el.attr('id').replace(/\d+/, breedCount + 1 ));
		});

		cloned.find('textarea').each(function(){
			el = $(this);
			el.attr('name',el.attr('name').replace(/\d+/, breedCount + 1 ));
			el.attr('id',el.attr('id').replace(/\d+/, breedCount + 1 ));
		});

		cloned.find('label').each(function(el){
			el = $(this);
			el.attr('for',el.attr('for').replace(/\d+/, breedCount + 1 ));
		});
		
		breedCount++;		
		
		cloned.insertAfter('#tier' + oldId).hide().fadeIn('slow');

	});

	
	$('.atid').keypress(function(){
		$(this).parent().expose({api: true}).close();
		$(this).parent().find('div').remove();
	})
	
	$('.atid').focus(function(){
		
	});
	
	$('.atid').blur(function(){
		$(this).parent().find('div').remove();
		var num = $(this).val();

		var error = $("<div></div>");	
		error.css('color','#ff0000')

		if($(this).hasClass('required') || $(this).val() != "") {
		
			if(!num.match(/^AT[0-9]{9,9}$/i)) {
				error.append('Bitte geben Sie die Nummer korrekt ein! (z.B. AT123456789)<br />');
	 		} else {
				base = num.substr(2);
				nums = base.split("");
				proof = nums.splice(6,1);
				quer = 0;
								
				sum = 0;			
				$.each(nums,function(i,n){
					sum += parseInt(n);				
					if(i%2 == 0) sum += parseInt(n);
				})
			
		
				while(sum.toString().split("").length > 1){					
					quer = 0;					
					$.each(sum.toString().split(""),function(i,n){
						quer += parseInt(n);
					})
					sum = quer;
				}
			
				if(quer != proof) {
					error.append('Die angegebene Nummer ist ungültig!<br />');
				}
			
			}
		}
				
		if(error.text() != "") {
			error.insertAfter(this);
			$(this).parent().expose({api: true}).load();
			$(this).focus();			
		} else {
			$(this).parent().expose({api: true}).close();
		}
		
	})

});


