$(document).ready(function(){

	$('.result_submit').click(function(){
		
		$(this).closest('form').submit();
		return false;
		
	});
	
	/*** search again validation ***/
	$('#search-again,#search-submit').submit(function(){
		
		var type = $('#s-type').attr('value');
		var subtype = $('#s-subtype').attr('value');		
		var formfields = required[type][subtype];
		
		if(type == 'phone' || subtype == 'phone'){
			var phonereg = /^\(?\d{3}\)?-?\s?\d{3}-?\s?\d{4}$/;
			if(!phonereg.test($('input[name="phone"]').attr('value'))){
				alert('Please enter a valid 10 digit phone number');
				return false;
			}			
		}
		
		for(i in formfields){
			if(i == 'msg'){continue;}
			if($('input[name="'+formfields[i]+'"]').attr('value') == ''){
				alert(required[type][subtype]['msg']);
				return false;
			}
		}
	});
	
	$('#phone-search').submit(function(){
		
		var threereg = /^\d{3}$/;
		var fourreg  = /^\d{4}$/;
		if(!threereg.test($('input[name=areaCode]').val())){
			alert('Please enter a valid three digit area code');
			return false;
		}
		
		if(!threereg.test($('input[name=prefix]').val())){
			alert('Please enter a valid three digit prefix');
			return false;
		}
		
		if(!fourreg.test($('input[name=suffix]').val())){
			alert('Please enter a valid four digit suffix');
			return false;
		}
		
	});
	
	/* load FPS API data */
	$('.cache-flag').each(function(){
		
		var source = $(this).attr('rel');
		var hash   = $('#free-results').attr('hash');
		var token  = $('#free-results').attr('token');
		var query  = $('#free-results').attr('query');
		
		$.post('/ajax/getFPSData',{'source':source,'hash':hash,'token':token,'query':query}, function(data){
		
			var parent = $('div[rel="'+source+'"]').parent();
			$('div[rel="'+source+'"]').replaceWith(data);
			$('#'+source+'-loader').remove();
			
			var finished = true;
			$(parent).children().each(function(){
				if($(this).hasClass('FPS_Loading'))
					finished = false;
			});
			
			if(finished){
				
				var empty = true;
			
				 $(parent).children().each(function(){
				
					empty = false;
				
				});
				
				if(empty){
					source = $(parent).attr('source');
					$(parent).html('<div class="empty-tab-msg">Sorry, we could not find any '+source+' results!</div>');
				}
			
			}
		});
		
	});
	
	$('.free-result-set').each(function(){
		
		if(!$(this).is(":visible")){	
			var empty = true;
				
			 $(this).children().each(function(){
				empty = false;
			});
			
			if(empty){
				source = $(this).attr('source');
				$(this).html('<div class="empty-tab-msg">Sorry, we could not find any '+source+' results!</div>');
			}
		}
	});
	
	/* hide all inactive seo tabs */
	$('#free-tabs li.tab-results').each(function(){
		if(!$(this).hasClass('active-tab')){
			$(this).hide();
		}
	});
	
	/* tab view switch */
	$('.seo-tab-link').click(function(){
		
		$('ul.seo-tabs li').each(function(){
			$(this).removeClass('active');
		});
		
		$(this).parent('li').addClass('active');
		
		$('#free-tabs li.tab-results').each(function(){
			$(this).hide();
		});
		
		/* close external view if open */
		$('#free-results').show();
		
		$('#'+ $(this).attr('rel') +'-results').fadeIn();
		
		$('div.resultblock:eq(1)').css('margin-top','45px');
		
		return false;
		
	});
	
});

var required = {
	
	people:{
		name:{0:'ln',msg:'Please enter a Last Name'},
		address:{0:'sn',1:'zip',msg:'Please enter an Address and Zip Code'},
		phone:{0:'phone',msg:'Please enter a Phone Number'}
		},
	background:{
		name:{0:'ln',msg:'Please enter a Last Name'},
		address:{0:'sn',1:'zip',msg:'Please enter an Address and Zip Code'},
		phone:{0:'phone',msg:'Please enter a Phone Number'}
		},
	criminal:{
		nation:{0:'ln',1:'fn',msg:'Please enter a First and Last Name'},
		municipal:{0:'ln',1:'fn',msg:'Please enter a First and Last Name'}
		},
	sex_offender:{
		name:{0:'ln',1:'fn',msg:'Please enter a First and Last Name'}
		},
	phone:{
		phone:{0:'phone',msg:'Please enter a Phone Number'}
		},
	divorce:{
		record:{0:'ln',msg:'Please enter a Last Name'}
		},
	death:{
		record:{0:'ln',msg:'Please enter a Last Name'}
		},
	birth:{
		record:{0:'ln',msg:'Please enter a Last Name'}
		},
	marriage:{
		bride:{0:'ln',msg:'Please enter a Last Name'},
		groom:{0:'ln',msg:'Please enter a Last Name'}
		},
	property:{
		name:{0:'ln',msg:'Please enter a Last Name'},
		address:{0:'sn',1:'zip',msg:'Please enter an Address and Zip Code'},
		phone:{0:'phone',msg:'Please enter a Phone Number'}
		},
	bankruptcy:{
		name:{0:'ln',msg:'Please enter a Last Name'},
		address:{0:'sn',1:'zip',msg:'Please enter an Address and Zip Code'},
		phone:{0:'phone',msg:'Please enter a Phone Number'}
		}
	}
