/* Nice checkboxes -- sa nu mai scriu asta niciodata ! */
(function ($) {
    $.fn.extend({
        niceCheckbox: function (options) {
            var settings = { theme: 'default' };
            $.extend(settings, options);

            this.each(function () {
                var me = $(this);
                var me_plain = this;
                //me.addClass("input-check");
				
				// Chose this instead of 'display: none', because in IE7, it seems it's not working unless they're displayed..
                me.css("position", "absolute");
				me.css("opacity", "0");

                var ncb = $('<span class="nice-checkbox theme-' + settings['theme'] + '"><span class="cb"></span></span>');
				if($(this).is(':checked'))
					ncb.addClass('checked');
					
                me.after(ncb);
                ncb.click(function () {
                    //me_plain.checked = !me_plain.checked;
                    //me.attr('onclick').call(me_plain
					//alert('span');
					
					/*if(!$(this).is('.checked'))
						$(this).prev().attr('checked', false).click();
					else
						$(this).prev().attr('checked', true).click();
					*/
					//alert('PL');
					$(this).prev()[0].click();
					return false;
                });

				
                $(this).click(function (evt) {
					//alert('input ' + $(this).is(':checked'));
					//evt.stopPropagation();
					//alert('asda');
					if($(this).is('[type="radio"]')) {
						$('[name="' + $(this).attr('name') + '"]').each(function() {
							$(this).next().removeClass('checked');
						});
					}
					
					if($(this).next().is('.checked')) {
						$(this).next().removeClass('checked');
					}
					else {
						$(this).next().addClass('checked');
					}
                });
            });
            return this;
        }
    });
})(jQuery);

/*
$(function() { 
	$('.nice-radio').click(function() {
		var cb;
		$(this).find('input[type="radio"]').each(function() {
			this.checked = true;
			cb = this;
		});
		
		$('input[name="'+cb.name+'"]').parents('.nice-radio').removeClass('checked');
		if(cb.checked)
			$(this).addClass('checked');
		else
			$(this).removeClass('checked');
	});
});*/

/* Nice selects -- sa nu mai scriu asta niciodata ! */
(function($) {
	$.fn.extend({
		niceSelect: function(options) {
			var settings = {theme: 'default', defaultText: ''};
			$.extend(settings, options);
		
			this.each(function() {
				var the_html = 
							'<div class="nice-select theme-' + settings.theme + '" style="'+$(this).attr('style')+'">' + 
							'<span>';
				var sel = $(this).find(':selected');
				/*if(sel.length > 0) {
					the_html +=  sel.eq(0).html();
				}
				else
					the_html += "Choose";
				*/
				if(!$(this).is('.select-no-default-text') && (sel.html().length == 0))
					the_html += settings.defaultText;
				else
					the_html += sel.html();
					
				the_html += '</span>'+
							'<ul>';
				$(this).find('option').each(function() {
					the_html += '<li title="' + $(this).attr('value') + '">' + $(this).html() + '</li>'
				});
				the_html += '</ul>' + 
					//		'<input type="hidden" value="';
							'';
				if(sel.lenght > 0) {
					//the_html +=  sel.eq(0).attr('value');
				}
				the_html += ''+
							//'" name="' + $(this).attr('name') + '" />' +
							'</div>';
			
				$(this).before(the_html);
				$(this).hide();
			});
			$('.nice-select span').click(function() {
				$(this).parent().find('ul').width($(this).parents('.nice-select').width());
				$(this).parent().css('z-index', 9876);
				var drop = $(this).next().show().css('visibility', 'hidden');
				var H = drop.outerHeight();
				if(drop.parent().offset().top - $('html').scrollTop() > $(window).height() - H - 20) {
					drop.css('top', -H);
				}
				else
					drop.css('top', $(this).parents('.nice-select').outerHeight());
				drop.css('visibility', 'visible');
				$(this).next().slideDown();
			});
			$('.nice-select').mouseleave(function() {
				$(this).find('ul').slideUp(function() {$(this).parent().css('z-index', 100);});
			});
			$('.nice-select li').click(function() {
				
				//$(this).parents('.nice-select').find('input').attr('value', $(this).attr('title'));
				$(this).parents('.nice-select').find('span').html($(this).html());
                //$(this).parents('.value').find('select').val($(this).attr('title'));

                $(this).parents('.nice-select').nextAll('select:eq(0)').val($(this).attr('title')).change();
				$(this).parent().slideUp();
			});
			return this;
		}
	});
})(jQuery);
/*
 * jBrowserBookmark - Browser bookmark plugin for use with jQuery
 *
 * Copyright (c) 2010 Andrew Holgate
 *
 * Requirements: jQuery v1.1.3 and beyond.
 *
 * Project homepage:  http://plugins.jquery.com/project/jBrowserBookmark
 *
 *
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 */
(function($){$.fn.jBrowserBookmark=function(options,lang){var defaults=$.extend(opts={language:{'':['Press [key] + ',' to bookmark this page.']},defaultLanguage:'',functionButton:['CTRL','CMD']},options);var docUrl=window.location.href;var docTitle=document.title;var browserName;browserName=getBrowser();return this.each(function(){$(this).click(function(e){e.preventDefault();try{switch(browserName){case'konqueror':case'firefox':window.sidebar.addPanel(docTitle,docUrl,'');break;case'msie':window.external.AddFavorite(docUrl,docTitle);break;case'opera':if(versionOpera()<11){$(this).attr('rel','sidebar').attr('title',docTitle).attr('href',docUrl);break;}else{throw error;}default:throw error;}}catch(error){var hotkey=getHotkey(browserName);var alertText=getLanguageText();if(/mac/.test(navigator.platform.toLowerCase()=='mac')){prefix=alertText[0].replace('[key]',opts.functionButton[1]);}else{prefix=alertText[0].replace('[key]',opts.functionButton[0]);}alert(prefix+hotkey+alertText[1]);}});});function getBrowser(){if($.browser.msie){return'msie';}if($.browser.mozilla){return'firefox';}if($.browser.opera){return'opera';}if($.browser.safari&&/chrome/.test(navigator.userAgent.toLowerCase())){return'chrome';}if($.browser.safari){return'safari';}if(/konqueror/.test(navigator.userAgent.toLowerCase())){return'konqueror';}}function getHotkey(browserName){switch(browserName){case'konqueror':return'B';break;case'opera':return(versionOpera()<9)?'T':'D';break;default:return'D';break;}}function getLanguageText(){var languageChosen;if(opts.language[lang]!=undefined){languageChosen=lang;}else{if(opts.language[navigator.language.toLowerCase().substring(0,2)]!=undefined){languageChosen=navigator.language.toLowerCase().substring(0,2);}else{if(opts.language[opts.defaultLanguage]!=undefined)languageChosen=opts.defaultLanguage;else{for(var i in opts.language){languageChosen=i;break;}}}}return opts.language[languageChosen];}function versionOpera(){version=navigator.userAgent.substring(navigator.userAgent.toLowerCase().indexOf('version/')+8);return parseInt(version.substring(0,version.indexOf('.')));}};})(jQuery);




function bCheckChange(type,check){
		
	current = $('#'+check).attr('checked');
	
	if(check == 'add_criminal'){
		other = $('#add_business').attr('checked');
		if(current === true && other === true){
			$('#input-product_id-bcheck').val('s:2:"85";');
			$('#bcheck-price').html('$79.95');
			$('#input-product_id-bcont').val('s:3:"408";');
			$('#cont-bcheck-price').html('$69.95');
		}
		else if(current === true && other === false){
			$('#input-product_id-bcheck').val('s:1:"3";'); 
			$('#bcheck-price').html('$69.95');
			$('#input-product_id-bcont').val('s:3:"390";');
			$('#cont-bcheck-price').html('$59.95');
		}
		else if(current === false && other === true){
			$('#input-product_id-bcheck').val('s:2:"84";'); 
			$('#bcheck-price').html('$59.95');
			$('#input-product_id-bcont').val('s:3:"407";');
			$('#cont-bcheck-price').html('$49.95');
		}
		else{
			$('#input-product_id-bcheck').val('s:1:"1";'); 
			$('#bcheck-price').html('$49.95');
			$('#input-product_id-bcont').val('s:3:"389";');
			$('#cont-bcheck-price').html('$39.95');
		}
	
	}else if(check == 'add_business'){
		other = $('#add_criminal').attr('checked');
		if(current === true && other === true){
			$('#input-product_id-bcheck').val('s:2:"85";');
			$('#bcheck-price').html('$79.95');
			$('#input-product_id-bcont').val('s:3:"408";');
			$('#cont-bcheck-price').html('$69.95');
		}
		else if(current === true && other === false){
			$('#input-product_id-bcheck').val('s:2:"84";');
			$('#bcheck-price').html('$59.95');
			$('#input-product_id-bcont').val('s:3:"407";');
			$('#cont-bcheck-price').html('$49.95');
		}
		else if(current === false && other === true){
			$('#input-product_id-bcheck').val('s:1:"3";');
			$('#bcheck-price').html('$69.95');
			$('#input-product_id-bcont').val('s:3:"390";');
			$('#cont-bcheck-price').html('$59.95');
		}
		else{
			$('#input-product_id-bcheck').val('s:1:"1";');
			$('#bcheck-price').html('$49.95');
			$('#input-product_id-bcont').val('s:3:"389";');
			$('#cont-bcheck-price').html('$39.95');
		}	
	}
}

$(document).ready(function() {
	
	/*** bookmark ***/
	$('#bookmark').jBrowserBookmark();
	
	/*** search validation ***/
	$('#search-submit').submit(function(){
		var type = $('#s-type').attr('value');
		var subtype = $('#s-subtype').attr('value');
		
		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;
			}			
		}
		
		var fields = required[type][subtype];
		for(i in fields){
			if(i == 'msg'){continue;}
			if($('input[name="'+fields[i]+'"]').attr('value') == ''){
				alert(required[type][subtype]['msg']);
				return false;
			}
		}
	});
	
	$('#phone-search-form').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;
		}
		
	});
	
	$('.nav .submenu').parents('li:eq(0)').mouseenter(function() {
		$(this).find('.submenu').slideDown();
	});
	$('.nav .submenu').parents('li:eq(0)').mouseleave(function() {
		$(this).find('.submenu').slideUp();
	});


	$('select.nice_select').niceSelect({theme: 'aps', defaultText: ''});

/* Order page */
	/* People/background - the 3 boxes page */
	$('#order-unltd input[type="radio"]').click(function() {
		$('#order-unltd div.price .price').html($(this).next().find('.price').html());
	});
	$('#order-unltd label').click(function() {
		$('#order-unltd div.price .price').html($(this).find('.price').html());
		$(this).prev('input').attr('checked','checked');
	});

/* Upsell page */
	/* People + Unlimited upsell */
	$('#upsell-ppl .product input').click(function() {
		var prod = $(this).parents('.product:eq(0)');
		
		$('.summary .current-product .name').html(prod.find('.name').html());
		$('.summary .current-product .price').html(prod.find('.price').html());
		$('.summary .total .price').html(prod.find('.price').html());
		$('form input#product-id').val(prod.find('.product-id').val());
			
	});
	/* Other report upsells */
	$('#upsell-ppl .product input.special-upsell').click(function() {
		var prod = $(this).parents('.product:eq(0)');
		
		$('.summary .current-product .name').html(prod.find('.name').html());
		$('.summary .current-product .price').html(prod.find('.price').html());
		$('.summary .total .price').html(prod.find('.price').html());
		$('form input#input-productId').val(prod.find('.product-id').val());
			
	});
	/* Background checks upsell */
	$('#upsell-back .product input').click(function() {
		
		data = $('#upsell_back').serialize();
		id   = $('#upsell_back').attr('rel');
		$.post('/order/upsell/backdata',{'post':data,'id':id},function(data){
			
			res = $.parseJSON(data);
			
			$('.summary .current-product .name').html(res.name);
			$('.summary .current-product .price').html(res.price);
			$('.summary .total .price').html(res.price);
			$('form input#input-product_id').val(res.productid);
				
		});
		
	});
	
	$('p.product-selection label.check').click(function(){
		
		if($(this).prev('input').attr('checked')){
			$(this).prev('input').removeAttr('checked');
		}else{
			$(this).prev('input').attr('checked','checked');
		}
		check = $(this).prev('input').attr('id');
		bCheckChange('label',check);
	});


/* OLDER STUFF from PRN*/

	$('.searchPage .formContainer .tabs li').click(function() {
		$(this).addClass('active').siblings().removeClass('active');
		$('.formContainer form').hide().filter('.form-' + ($(this).prevAll().length + 1)).show();
		return false;
	});
	
	$('.searchPage li.field input, .searchPage li.field select').each(function() {
		if($(this).val() == "")
			$(this).prev().show();
		
	});
	
	
	/* Results page */
		// Make the names submit the form, taking you to the Order page.
	
	/*
	$('#resultTable a.view-details, #resultTable input.view-details').click(function() {
		$('.result-values input').attr('disabled', 'disabled');
		$(this).parents('tr.entry').find('input.person-id').attr('disabled', '');
		$(this).parents('tr.entry').find('input.person-name').attr('disabled', '');
		$(this).parents('form').submit();
		return false;
		// don't return false. might not get to the other event.
	});
	
	$('#resultTable a.view-details, #resultTable input.view-details').click(function() {
		$('.result-values input').attr('disabled', 'disabled');
		$(this).parents('tr.entry').find('input.person-id').attr('disabled', '');
		$(this).parents('tr.entry').find('input.person-name').attr('disabled', '');
	});
	*/
		// On submitting the form (ex. clicking `View Details`), set a name for the `input.person-id` in the corresponding entry.
		// Do so, because it needs to be passed via POST onwards.
	
	
	
	/* Upsells */
	
	/* people core upsell */
	$('input[name="upsell"]').click(function() {
		$('#product-name').html($(this).parents('.product:eq(0)').find('.name').html());
		$('#product-price').html($(this).parents('.product:eq(0)').find('.price').html());
		$('#product-total').html($(this).parents('.product:eq(0)').find('.price').html());
		
		//$('#product-id').val($(this).parents('.product:eq(0)').find('.product-id').val());
		$('#input-product_id').attr('value',$(this).attr('value'));
	});
	
	/* unlimited upsell */
	$('#tdMembership input[name="vars[product_id]"]').click(function() {
		var price = $(this).next().find('.price').html();
		$('#tdMembership .total_price').html(price);
	});
	
	/* membership continuity bubble */
	$('#box-membership #bubble a').click(function(){
		$('#box-membership #bubble').hide();
		return false;	
	});
	/* continuity checkbox */
	$('#continue-btn').click(function(){
		check = $('#continuity_price').attr('checked');
		if(check === false){
			$('#box-membership #bubble').show();
			return false;		
		}
	});
	
	/* background continuity upsells */
	$('.upsell_check').change(function(){
		
		check = $(this).attr('id');
		bCheckChange('click',check);	
		
	});
	
	/* account auto renew submit */
	$('#renew_btn').click(function(){
		
		if($(this).attr('rel') == 'cancel'){
			var check = confirm('Are you sure you want to cancel Automatic Renewal?\n\nBy cancelling out of Automatic Renewal, your membership will expire at the end of the membership time frame. If you cancel, you will not be eligible for certain members-only promotions and discounts. In order to renew, you will need to come back and sign up for another membership');
			if(check === true){
				window.location = '/myaccount/autorenew/cancel';
			}
		}else{
			window.location = '/myaccount/autorenew/renew';
		}
		
		return false;
		
	});
	
	/* background check upsells */

		/* on the order page */
	$('#tdReport .addon input').click(function() {
		var sum = 0;
		$('#tdReport .addon :checked').each(function() {
			sum += parseInt($(this).val());
			
		});
		
		$('.idcboxfooter .product').hide().filter('#product-' + b_prods[sum]).show();
		$('#tdReport #input-product_id').val(serialize(b_prods[sum]));
	});
		/* on the upsell page */
		
	$('#divReportAddon .addon input').click(function() {
		var sum = 0;
		/* for the visible checkboxes*/
		$('#divReportAddon .addon :checked').each(function() {
			sum += parseInt($(this).val());
		});
		/* for the hidden options already included (see the order/upsells/background.ctp view for more) */
		$('#divReportAddon .addon.included input').each(function() {
			sum += parseInt($(this).val());
		});

		$('.summary .current-product .name').html(prods[b_prods[sum]].name);
		$('.summary .current-product .price').html('$' + prods[b_prods[sum]].price);
		$('.summary .total .price').html('$' + prods[b_prods[sum]].price);
		
		$('#product-id').val(b_prods[sum]);
		
		
	});
	
	/* 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();
		});
		
		$('#free-results').show();
		
		$('#'+ $(this).attr('rel') +'-results').fadeIn();
		
		$('div.info:eq(1)').css('margin-top','25px');
		
		return false;
		
	});
	
	/* text label replace */
	$('#search-again .input-wrap input').focus(function(){
	
		value = $(this).attr('label');
		
		if($(this).attr('value') == value){
			$(this).attr('value','');
		}
	
	});
	
	$('#search-again .input-wrap input').blur(function(){
	
		if($(this).attr('value') == ''){
			$(this).attr('value',$(this).attr('label'));
		}
	
	});
	
	/* clear search-again fields before submit and validate */
	$('#search-again-form').submit(function(){
		
		$('#search-again-form input[type=text]').each(function(){
			if($(this).attr('value') == $(this).attr('label')){
				$(this).attr('value','');
			}
		});
		
		var type = $('#s-type').attr('value');
		var subtype = $('#s-subtype').attr('value');
		var fields = required[type][subtype];
		for(i in fields){
			if(i == 'msg'){continue;}
			if($('input[name="'+fields[i]+'"]').attr('value') == ''){
				$('#search-again-form input[type=text]').each(function(){
					if($(this).attr('value') == ''){
						$(this).attr('value',$(this).attr('label'));
					}
				});
				alert(required[type][subtype]['msg']);
				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(){
				
					if($(this).css('display') != 'none')
						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>');
			}
		}
	});
	
	$('.view-details').click(function(){
		
		$(this).closest('form').submit();
		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'}
		}
	}

/* This one's somewhat hardcoded.. that's the way the addons are.. different product id for whatever addon permutation.. (can't do more than this) */
var b_prods = {
	// checkbox config -> corresp product
	0:	1,
	1:  3,
	2: 84,
	3: 85,
	4: 86,
	5: 87,
	6: 88,
	7: 89
}

var prods = {
	1: {name: 'Comprehensive Background Check', price: 49.95 },
	3: {name: 'Comprehensive Background Check with Nationwide Criminal', price: 69.95 },
	84: {name: 'Comprehensive Background Check with Corp/FBN', price: 59.95 },
	85: {name: 'Comprehensive Background Check with Corp/FBN And Nationwide Criminal', price:  79.95},
	86: {name: 'Comprehensive Background Check with Professional Licenses', price: 59.95},
	87: {name: 'Comprehensive Background Check with Professional Licenses And Nationwide Criminal', price: 79.95 },
	88: {name: 'Comprehensive Background Check with Professional Licenses And Corp/FBN', price: 69.95 },
	89: {name: 'Comprehensive Background Check with Professional Licenses, Corp/FBN, And Nationwide Criminal', price: 89.95 },
}
