$(document).ready(function() {
	
	
	
	/* ==========================================================
	
	FORM VALIDATION JAVASCRIPT
	
	========================================================== */
	
    
    
    // Share Story Form
	$("#share_story_form").validate({
		rules: {
			company: "required",
			name1: "required",
			address: "required",
			phone: "required",
			email: {
				email: true,
				required: true
			}
		}
			
	}); // End of Validate Plugin
	
    
    // Partner Support Form
	$("#support_form").validate({
		rules: {
			company: "required",
			name1: "required",
			address: "required",
			phone: "required",
			email: {
				email: true,
				required: true
			}
		}
			
	}); // End of Validate Plugin
	
	
	// Contact Us Form
	$("#contact_form").validate({
		rules: {
			company: "required",
			name1: "required",
			address: "required",
			phone: "required",
			email: {
				email: true,
				required: true
			}
		}
	
	}); // End of Validate Plugin
	
	
	
	// Media Contact Form
	$("#media_form").validate({
		rules: {
			company: "required",
			name1: "required",
			address: "required",
			phone: "required",
			email: {
				email: true,
				required: true
			}
		}
	
	}); // End of Validate Plugin
	
	
	
	// Needs Analysis - Financial Institutions Form
	$("#needs_analysis").validate({
		rules: {
			company: "required",
			name1: "required",
			phone: "required",
			plans: "required",
			zip: "required",
			email: {
				email: true,
				required: true
			}
		}
	}); // End of Validate Plugin
	
	
	
	// Needs Analysis - Business Form
	// Contact Us Form
	$("#business").validate({
		rules: {
			company: "required",
			name1: "required",
			phone: "required",
			email: {
				email: true,
				required: true
			}
		}
	
	}); // End of Validate Plugin
	
	
	
	
	/* ==========================================================
	
	TABS JAVASCRIPT
	
	========================================================== */
	
	// Setup the jQuery tabs
	$('#product_information > ul').tabs({ fx: { opacity: 'toggle', fxSpeed: 'normal' } });
	$('#solutions_information > ul').tabs({ fx: { opacity: 'toggle', fxSpeed: 'normal' } });
	
	// A little pixie dust to make anchors to tabs load correctly ...
	$('a[href*=#]').click(function() { 
		var id = this.href.toString().replace(/^[^#]*#/,'#');
		if ($('a[href='+id+']').parents('.ui-tabs-nav').length > 0) {
			$('a[href='+id+']').parents('.ui-tabs-nav').tabs('select',id); return false;
		}
	});
	
	/* ==========================================================
	
	CONTACT US JAVASCRIPT
	
	========================================================== */
	
	
	// This hides the other interest input field and then shows it if the correct radio button is pressed
   	$("#other_interest, #referral_from_specific, #referral_from_other").hide();
   	
	$("input:radio[name=interested]").click(function() {
    	if ( $(this).val() == 'Other' ) {
        		$("#other_interest").show("fast");
        	}
    		else {
        		$("#other_interest").hide("fast"); 
        	}
	});
	
	$("input:radio[name=referral]").click(function() {
    	if ( $(this).val() == 'Referral From' ) {
        		$("#referral_from_specific").show("fast");
        	}
    		else {
        		$("#referral_from_specific").hide("fast"); 
        	}
	});
	
	$("input:radio[name=referral]").click(function() {
    	if ( $(this).val() == 'Other' ) {
        		$("#referral_from_other").show("fast");
        	}
    		else {
        		$("#referral_from_other").hide("fast"); 
        	}
	});

		
	
	

	/* ==========================================================
	
	PRODUCTS SUBNAV JAVASCRIPT
	
	========================================================== */
	

	// Collapse all dropdown lists
	$("#product_information dd, #solutions_information dd, .solution_menu, .solution_menu dd").hide();
	
	
	// Create triggers to open dropdown lists
	$("#product_information dt, #solutions_information dt").click(function(){
		$(this).next().toggle();
		$(this).toggleClass("open");
		return false;
	});
	
	
	// Create triggers to open dropdown lists
	$("#solutions_products ul.section li").click(function(){
		$(".solution_menu:visible").toggle(); // This closed any open solution side nav like an accordian!
		$(this).parent().next().toggle();
		$(this).toggleClass("open");
		return false;
	});	
	
	
	// Automatically toggle solutions nav if class is analysis which is set based on segment_3
	$("#solutions_products li.analysis").parent().next().toggle();
	$(this).toggleClass("open");	

	
	// Nifty bit of code to iterate all dd until the dt comes up. Prevents collapsing entire list.
	$("dt.solution").click(function() { 	// This is for toggling multiple dd elements
		var obj = $(this).next();			// when you click dt.solution
		while (obj.is("dd")) {				// it captures the next element (dd) as a variable
			obj.toggle();					// then until the next element ceases to be a dd
			obj = obj.next();				// it toggles the element contained in the variable (dd)
		}									// then it flips onto the next one!
		return false;
	});	





	/* ==========================================================
	
	POPUP WINDOW JAVASCRIPT
	
	========================================================== */
	
	
	// This launches all links with a rel of video_demo in a new window
	$('a.lightbox').click(function(){
   		window.open(this.href,'mywindow','height=440,width=400,scrollTo,resizable=0,scrollbars=0,location=0','false');
        return false;
    });
    
    
    // This launches all rel links in a new window
    $('a[rel="share_story"]').click(function(){
        window.open(this.href,'mywindow','height=580,width=400,scrollTo,resizable=0,scrollbars=0,location=0','false');
		return false;
    });
    
    
    // This launches all rel links in a new window
    $('a[rel="video_story"]').click(function(){
        window.open(this.href,'mywindow','height=400,width=420,scrollTo,resizable=0,scrollbars=0,location=0','false');
		return false;
    });
    
    
    $('a[rel="email_page"]').click(function(){
   		window.open(this.href,'mywindow','height=580,width=400,scrollTo,resizable=0,scrollbars=0,location=0','false');	
		return false;
    });
    
    
    
    	
	
	/* ==========================================================
	
	NEEDS ANALYSIS FORM JAVASCRIPT
	
	========================================================== */
	
	
	// FINANCIAL INSTITUTION
	
   	$("#institution_type_other, #rdc_functionality_other, #institution_functionality_other, #refer_other, #outcome_1_panel, #outcome_2_panel, #outcome_3_panel, #contact").hide();
   	
	$("#institution_type input:radio").click(function() {
    	if ( $(this).val() == 'Other' ) {
        	$("#institution_type_other").show("fast");
        }
    	else {
        	$("#institution_type_other").hide("fast"); 
        }
	});
	
	$("#rdc_functionality_other_checkbox").click(function() {
    	if ( $(this).val() == 'Other' ) {
        	$("#rdc_functionality_other").toggle("fast");
        }
	});
	
	$("#institution_functionality_other_checkbox").click(function() {
    	if ( $(this).val() == 'Other' ) {
        	$("#institution_functionality_other").toggle("fast");
        }
	});
	
	$("#who_refer input:radio").click(function() {
    	if ( $(this).val() == 'Other' ) {
        	$("#refer_other").show("fast");
        }
    	else {
        	$("#refer_other").hide("fast"); 
        }
	});
	
	
	
	 // Checks for the clicked radio button then compares the value to return the user to a specific page
	$("#needs_analysis_financial #workitout").click(function() {
	var subvert = false;
	var plans = $("input:radio[name=plans]:checked").val();
		
		if ( plans == 'Aggressively grow core deposits' || plans == 'We already have a solution; however it is not meeting our needs' ) {
			$("#financial_institution").fadeOut();
        	$("#outcome_1_panel").fadeIn(500);
			$("#contact").fadeIn(500);
			$("html, body").animate({scrollTop:0}, "slow");
			subvert = true;	
		}
		else if ( plans == 'Not completely sure, but know I need a solution' || plans == 'Need it to protect existing core deposits' ) {
			$("#financial_institution").fadeOut();
        	$("#outcome_2_panel").fadeIn(500);
			$("#contact").fadeIn(500);
			$("html, body").animate({scrollTop:0}, "slow");
			subvert = true;
		}
		else if ( plans == 'We don’t need a Remote Deposit solution' || plans == 'We are too small to implement Remote Deposit' ) {
			$("#financial_institution").fadeOut();
        	$("#outcome_3_panel").fadeIn(500);
			$("#contact").fadeIn(500);
			$("html, body").animate({scrollTop:0}, "slow");
			subvert = true;
		}
		
		return subvert;
	});


	
	
	
	// BUSINESS
	
   	$("#payments_other, #payment_types_other, #refer_other, #contact").hide();
 		
	$("#payment_types_other_checkbox").click(function() {
    	if ( $(this).val() == 'Other' ) {
        	$("#payment_types_other").toggle("fast");
        }
	});
	
	$("input:radio[name=payments]").click(function() {
    	if ( $(this).val() == 'Other' ) {
        	$("#payments_other").show("fast");
        }
    	else {
        	$("#payments_other").hide("fast"); 
        }
	});
	

						
	$("#needs_analysis_business #workitout").click(function() {
		var hourly_rate = 30;
		var returned_check = 5;
		var lockbox = parseFloat($("input:radio[name=lockbox]:checked").val());
		var number_of_hours = parseInt($("#number_of_hours").val());
		var number_of_checks = parseInt($("#number_of_checks").val());
		var number_of_returns = parseInt($("#number_of_returns").val());
		var result = (((hourly_rate*number_of_hours)*4)+(lockbox*number_of_checks)+(returned_check*number_of_returns));				
		if(!isNaN(result))
		{
			$("#business_results").append(result.toFixed(2));
			$("#business_questions").fadeOut();
			$("#contact").fadeIn(500);
			$("html, body").animate({scrollTop:0}, "slow"); 
		}
		else
		{
			$("#formula_result").val('There was an error');
		}
		
		console.log(hourly_rate,returned_check,lockbox,number_of_hours,number_of_checks,number_of_returns,result);
		return false; 
	});
	
	
	

});


