
$(document).ready(function(){
	$('.main-nav > li').hover(
		function(){
			$(this).addClass("over").children('ul').show();
		},
		function(){
			$(this).removeClass("over").children('ul').hide();
			
		}
	);
	
	$('.more-info-cb').click(function(){
		$(this).next('.more-options').slideToggle(222);
	});
	
	$('.content-toggle').click(function(){
		$('.content-table').slideToggle(222);
	});
	
	$('.what-you-get .tabination > .tabs > li, .partners .tabination > .tabs > li').click(function(){
		$('.tabination .tabs li').removeClass('tab-on');
		$(this).addClass('tab-on');
		$('.tabination .tab-content').hide();
		var thisTab = $(this).children('a').attr('href');
		$(thisTab).show();
		return false;
	});

	
	$(".tabs > li > a").click(function(){
						var tabid = $(this).parent("li").attr("id").split("-");
						$(".tabs > li").removeClass("tab-on");
						$(this).parent("li").addClass("tab-on");
						$(".tab-content").hide();
						$("#tab-content-"+tabid[1]).show();
 
						
					});
	
	
	$(".what-you-get td a").click(function(){
		$('.tabination .tabs li').removeClass('tab-on');
		$("#"+$(this).attr("class").replace("-td", "")+"-tab-tab").addClass('tab-on');
		$('.tabination .tab-content').hide();
		$("#"+$(this).attr("class").replace("-td", "")+"-tab").show();
		$(window).scrollTop($($(this).attr("href")).position.top());
		return false;
		
	});

	// Salesforce Form Validation
	
	$(".salesforce-form").submit(function() {
		formErrors = false;
		emailErrors = false;
		phoneErrors = false;
		$("label, input").removeClass("error");
		
  		$(this).find("input").each(function(index) {
  			if (($(this).siblings("label").children("span").html() == "*") && ($(this).val().length==0)){
  				formErrors = true;
  				$(this).siblings("label").addClass("error");
  				$(this).addClass("error");
  			}
  		});
  		
  		$(this).find("select").each(function(index) {
  			if (($(this).siblings("label").children("span").html() == "*") && ($(this).val() == 0)){
  				formErrors = true;
  				$(this).siblings("label").addClass("error");
  				$(this).addClass("error");
  			}
  		});
  		
  		$(this).find("textarea").each(function(index) {
  			if (($(this).siblings("label").children("span").html() == "*") && ($(this).val().length==0)){
  				formErrors = true;
  				$(this).siblings("label").addClass("error");
  				$(this).addClass("error");
  			}
  		});
  		  		
  		if ($('#email').length > 0){
	  		if(!validateEmail($('#email').val())){
	  			emailErrors = true;
	  			$('#email').addClass('error');
	  			$('#email').siblings('label').addClass('error');
	  		}
  		}
  		
  		if ($('#phone').length > 0){
	  		if(testAndFormatPhone($('#phone').val()) == 'fake'){
	  			phoneErrors = true;
	  			$('#phone').siblings('label').addClass('error');
	  			$('#phone').addClass('error');
	  			
	  		}else{
	  			$('#phone').val(testAndFormatPhone($('#phone').val()));
	
	  		}
  		}
  		
  		if (formErrors || emailErrors || phoneErrors){
  			if(formErrors && emailErrors && phoneErrors){
  				$(".errors").html("Some required info is missing from your submission. Also, the email address and phone number in your submission appear to be invalid").slideDown(500);
  			}else if(formErrors && phoneErrors){
  				$(".errors").html("Some required info is missing from your submission and the phone number in your submission appears to be invalid").slideDown(500);
  			}else if(emailErrors && phoneErrors){
  				$(".errors").html("The email address and phone number in your submission appear to be invalid").slideDown(500);
  			}
  			else if(emailErrors && formErrors){
  				$(".errors").html("Some required info is missing from your submission and the email address in your submission appears to be invalid").slideDown(500);
  			}else if(emailErrors){
  				$(".errors").html("The email address in your submission appears to be invalid").slideDown(500);
  			}else if(phoneErrors){
  				$(".errors").html("The phone number in your submission appears to be invalid").slideDown(500);
  			}else{
  				$(".errors").html("Some required info is missing from your submission").slideDown(500);
  			}
  			
  			if ($(".errors").length != ""){
  				var destination = $(".errors").offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
			}
  			return false;
  		} else {
  			return true;
  		}
	});
	
	// All fields required Form Validation
	$(".form-all-req").submit(function() {
		
		formErrors = false;
		emailErrors = false;
		phoneErrors = false;
		
		$(this).find("input").each(function(index) {
  			if ($(this).val().length==0){
  				formErrors = true;
  			}
  		});
  		
  		if ($('#email').length > 0){
	  		if(!validateEmail($('#email').val())){
	  			emailErrors = true;
	  		}
  		}
  		
  		if ($('#phone').length > 0){
	  		if(testAndFormatPhone($('#phone').val()) == 'fake'){
	  			phoneErrors = true;	  			
	  		}else{
	  			$('#phone').val(testAndFormatPhone($('#phone').val()));
	
	  		}
  		}
  		
  		if (formErrors || emailErrors || phoneErrors){
  			if(formErrors && emailErrors && phoneErrors){
  				$(".errors").html("Some required info is missing from your submission. Also, the email address and phone number in your submission appear to be invalid").slideDown(500);
  			}else if(formErrors && phoneErrors){
  				$(".errors").html("Some required info is missing from your submission and the phone number in your submission appears to be invalid").slideDown(500);
  			}else if(emailErrors && phoneErrors){
  				$(".errors").html("The email address and phone number in your submission appear to be invalid").slideDown(500);
  			}
  			else if(emailErrors && formErrors){
  				$(".errors").html("Some required info is missing from your submission and the email address in your submission appears to be invalid").slideDown(500);
  			}else if(emailErrors){
  				$(".errors").html("The email address in your submission appears to be invalid").slideDown(500);
  			}else if(phoneErrors){
  				$(".errors").html("The phone number in your submission appears to be invalid").slideDown(500);
  			}else{
  				$(".errors").html("Some required info is missing from your submission").slideDown(500);
  			}
  			
  			if ($(".errors").length != ""){
  				var destination = $(".errors").offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-20}, 500 );
			}
			
			return false;
			
  		} else {
  			return true;
  		}
	});
});


function validateEmail(email) { 
	 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 
	 return re.test(email);
}

function testAndFormatPhone(phoneNumber){
	var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

	if (regexObj.test(phoneNumber)) {
    	return phoneNumber.replace(regexObj, "($1) $2-$3");
	} else {
   		return 'fake';
	}
}

