// PLEASE COMMENT YOUR JS WELL
// PLACE jQUERY HERE
	//hide all news on company page
	$("[class^='news-text-']").hide();
	///////////////////////////////
	// Prepare to submit a form
	$(".btn-submit").click(function(){
		var target = $(this).closest("form");
		target.find("input[name='submitted']").val("y");
	});
	// Add zoom and target to .pdf links
	$("a[href*='.pdf']").each(function(){
		var href = $(this).attr("href");
		$(this).attr("href", href+"#zoom=90").attr("target", "_blank");								  
	});
	
	// Open external links in new window
	$("a[href^='http']").each(function(){
		$(this).attr("target", "_blank");								   
	});	
	
	//Apply now form submit
	$(".apply-now-btn").click(function(){
		
		var error = "";
		//validate mandatory field
		if($(".business-name").val() == "")
		{
			$(".business-name").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".address-1").val() == "")
		{
			$(".address-1").css("border", "1px solid red");
			error = 1;
		}
		
		/*if($(".address-2").val() == "")
		{
			$(".address-2").css("border", "1px solid red");
			error = 1;
		}
		*/
		
		if($(".city").val() == "")
		{
			$(".city").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".zip-code").val() == "")
		{
			$(".zip-code").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".country").val() == "")
		{
			$(".country").css("border", "1px solid red");
			error = 1;
		}
		else
		{
			if($(".country").val() == "United States")
			{
				if($(".state").val() == "")
				{
					$(".state").css("border", "1px solid red");
					error = 1;
				}
			}
		}
		
		if($(".contact-name").val() == "")
		{
			$(".contact-name").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".phone").val() == "")
		{
			$(".phone").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".email").val() == "")
		{
			$(".email").css("border", "1px solid red");
			error = 1;
		}
		
		
		if(error != "")
			return false;
		else
			$("#form-apply-now").submit();
	});
	
	$(".iso-form-btn").click(function(){
		var error = "";
		
		if($(".applicant-name").val() == "")
		{
			$(".applicant-name").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".street-address").val() == "")
		{
			$(".street-address").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".city").val() == "")
		{
			$(".city").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".state").val() == "")
		{
			$(".state").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".zip-code").val() == "")
		{
			$(".zip-code").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".business-name").val() == "")
		{
			$(".business-name").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".work-phone").val() == "")
		{
			$(".work-phone").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".email").val() == "")
		{
			$(".email").css("border", "1px solid red");
			error = 1;
		}
		
		if($(".application-month").val() == "")
		{
			$(".application-month").css("border", "1px solid red");
			error = 1;
		}
		
		if(error != "")
			return false;
		else
			$("#become-an-iso-agent").submit();
		
	});
	///////////////////////
	//home page iso form
	$(".hp-iso-form-btn").live("click", function(){
		if($(".hp-email").val() == "")
			$(".hp-email").css("border", "1px solid red");
		else
			$("#form-become-an-iso-agent").submit();
	});
	////////////////////
	//Show/Hide news on company page
	$("[id^='news-title-']").click(function(){
		var news_id = $(this).attr("id").replace("news-title-", "")	;
		//hide all open news
		$("[class^='news-text-']").slideUp("slow");
		//open selected news
		$(".news-text-"+news_id).slideToggle("slow");
	});

$(function(){
	
	/* Homepage Fader
	############################################################*/
	if ( $("#container").hasClass("home-template") ){		
		// Set some stuff up
		// Get the number of items. This is going to be used a lot.
		how_many();
		
		// Set the z-index for the <li> stack
		$("#fade li").each(function(){
			$(this).css("z-index", num - $(this).index());						 
		});
		
		// Set the first <li> in the stack as the default bg during the transition from last to first <li>
		$("#fade").css("background-image", $("#fade li:first-child").css("background-image"));
		
		// Set the first items to .active
		$(".fading-items :first-child").addClass("active");
		
		// Run the fader
		start_fade();
	}
	
	$("#slideshow-bullets li").click(function(){
	    clearInterval(startup);	
		var index = $(this).index();
		$(".fading-items").each(function(){
			$(this).children().removeClass("active").eq(index).addClass("active");
		});
		var target = $("#fade .active");
		target.css("display", "block");
		target.prevAll().css("display", "none");
		target.nextAll().css("display", "block");
		restart = setTimeout(function(){ start_fade(); }, 3000);
	});
	
    /* Table Striping
    #######################################################*/
	$(".table-striped").each(function(){
		var $tr = $(this).find("tr");
		$tr.each(function(){
			$(this).children().last().addClass("last");
		});
		$tr.filter(":odd").addClass("odd");
	});
	
    /* Merchant Account Rates - position CTAs to match content <h2>s
	    Each table has an id which matches a class on each of the CTAs
		Add 15px to each CTA to account for content div top padding
    #######################################################*/	
	if ( $(".cta-merchant-applications").length ){
		var h2 = new Array();
		$("table[id]").each(function(){
			pos = $(this).position().top;
			h2.push ( h2[$(this).attr("id")] = pos+15 );			
		});
		//console.log(h2);
		for( i in h2 ){
		  //console.log( i + ":" + h2[i] );
		  $(".cta-merchant-applications." + i).css("top", h2[i]);
		}		
	}
	
	
	
});
// PLACE CUSTOM FUNCTIONS HERE
function how_many(){
	num = $("#fade li").length;	
	return num;
}

function start_fade(){
	// Clear the restart to avoid timing collisions
	if ( typeof restart !== "undefined" ){
		clearTimeout(restart);
	}
	// Run it!
	startup = setInterval(function(){
		// Pass the number of <li>s in the stack
		fade(num);						   
	}, 8000);	
}

function fade(num){
	var current = $("#fade li.active");
	current.fadeOut('fast');

	// Get current index
	var index = current.index();
	// This is going to affect <li>s, text, and bullets
	$("#hero .active").removeClass("active");
	// If this is the last <li> in the stack, reset stuff (remember that .index() is 0-based)
	if ( index == num - 1 ){			
		$("#fade li:first").addClass("active").fadeIn('fast', function(){
			$("#fade li").css("display", "block");		
		});
		$(".slideshow-text:first").addClass("active");
		$("#slideshow-bullets li:first").addClass("active");		
	} else {
		// Otherwise just transition to next <li>
		$(".fading-items").each(function(){
			$(this).children().eq(index).next().addClass("active");	
		});
	}	
}
	

