// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function showNewStuff() {
	$('#foo').toggle();
}


function selectMainContentDiv(eventObject) {
	// Find the clicked li element and the associated contente div
	clicked_li = $(eventObject.target);
	div_id = clicked_li.attr('id');

	// Remove the active class from the currently active li and add it to the new one
	$('.active-audience-link').removeClass('active-audience-link');
	$(clicked_li).addClass('active-audience-link');

	// Fade out the current div and set it to inactive
	fade_duration = 200; // in milliseconds
	$('#main_content_section .active-main-content-section').fadeOut(fade_duration, function() {
		$('#main_content_section .active-main-content-section').removeClass('active-main-content-section');

		// Fade in the new div and set it to active
		$('#' + div_id + '_content').addClass('active-main-content-section');
		$('#' + div_id + '_content').fadeIn(fade_duration);
	});

	// Hash containing div id of slider groups parid with desired subpage redirect
	redirectTarget = {photographers: "why-use-snapizzi", schools: "school-photographer", event: "event-photographer", partners: "why-use-snapizzi"};
	// Set Learn More button target to different sub-pages based on active selection
	$('#learn_more_button').attr("href", "/main/" + redirectTarget[div_id]);
}

function checkForSelectedNavLink(elm, action_name) {
	elm = $(elm);
	link = elm.attr('href');

	// Bail unless we have a link
	if (!(link && link.length > 0))
		return;
	
	// Pull out the text after the last '/'
	parts = link.split('/');
	target = parts[parts.length - 1];

	// Select the link if its target is the same as the current action
	if (target == action_name)
		elm.addClass('active-link');
}
function submitFreeTrial() {
	if (!freeTrialFormIsValid())
		return false;
  $.ajax({
    url: '/homepage_form/free_trial',
    type: "POST",
		dataType: 'json',
    data: $('#free_trial_form').serialize(),
    success: function(resp) {
			if (resp['status'] == 'success')
				document.location = resp['url']
			else {
				$('.form-errors').html(resp['error_message']);
				$('.form-errors').show();
			}
    },
    error: function(html) {
			$('.form-errors').html("I'm sorry, there appears to be a server error. If this problem persists, please contact <a href='mailto:support@snapizzi.com'>support@snapizzi.com</a> to start your free trial.");
			$('.form-errors').show();
    }
  }, 'json');
	return false;
}
function freeTrialFormIsValid() {
	message = [];
	// make sure customer contact fields are filled in
	customerContacts = {first_name: 'First Name', 
		last_name: 'Last Name', 
		email: 'Email Address', 
		phone: 'Phone Number', 
		company: 'Company Name'};
	$.each(customerContacts, function(key, value) {
		if ($('form input[name="customer_contact['+key+']"]').val() == "") {
			$('form input[name="customer_contact['+key+']"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = ['Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.'];
		}
		else
			$('form input[name="customer_contact['+key+']"]').parent().prev().css({'color': '', 'font-weight': ''});
	});

	// if we have an email, make sure it's valid
	if ( $('form input[name="customer_contact[email]"]').val() != "" ){
		var email_regex = /^([A-Za-z0-9_\-\.\+])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (email_regex.test($('form input[name="customer_contact[email]"]').val()) == false) {
			$('form input[name="customer_contact[email]"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = ['Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.'];
		}
		else
			$('form input[name="customer_contact[email]"]').parent().prev().css({'color': '', 'font-weight': ''});
	}

	// other fields: subdomain, password, password confirmation.
	// back end will check for unwanted strings and chars.
	otherFields = {subdomain: 'Subdomain',
		password: 'Password',
		password_confirmation: 'Password Confirmation'};
	$.each(otherFields, function(key, value) {
		if ( $('form input[name="'+key+'"]').val() == "" ) {
			$('form input[name="'+key+'"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = ['Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.'];
		}
		else
			$('form input[name="'+key+'"]').parent().prev().css({'color': '', 'font-weight': ''});
	});

	// terms and conditions box checked
	if ($('#agree_to_terms').attr('checked') == false)
		message.push('Please confirm that you have read and understood the Terms and Conditions and Privacy Policy.');

	if ( ($('form input[name="plan_type"]').val().substring(0,3) == 'sub') && 
			($('form select[name="customer_contact[country]"]').val() != 1)) {
		$('.form-errors').html('Subscription model is available for the North American franchise only. Please go back to <a href="/main/flexible-pricing">the flexible pricing table</a> and choose a revenue share plan that fits your needs.');
		$('.form-errors').show();
		return false;
	}

	if (message.length > 0){
		$('.form-errors').html(message.join('<br/><br/>'));
		$('.form-errors').show();
		return false;
	}
	else{
		$('.form-errors').hide();
		return true;
	}
}

function submitFreeTrialPaymentInfo() {
	message = [];
	// make sure text fields are filled in
	creditCardInfo = ['first_name',
								 'last_name',
								 'address_street_1',
								 'address_city',
								 'address_state',
								 'address_zip',
								 'number', 
								 'cvv'];
	$.each(creditCardInfo, function(index) {
		if ($('form input[name="credit_card['+creditCardInfo[index]+']"]').val() == "") {
			$('form input[name="credit_card['+creditCardInfo[index]+']"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = ['Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.'];
		}
		else
			$('form input[name="credit_card['+creditCardInfo[index]+']"]').parent().prev().css({'color': '', 'font-weight': ''});
	});

	// CVV is correct length
	if ( (($('form select[name="credit_card[card_type]"]').val() == "Amex") && 
			 ($('form input[name="credit_card[cvv]"]').val().length != 4))
			||
			(($('form select[name="credit_card[card_type]"]').val() != "Amex") && 
			($('form input[name="credit_card[cvv]"]').val().length != 3)) 
			) {
			$('form input[name="credit_card[cvv]"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = ['Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.'];
	}
	else {
		$('form input[name="credit_card[cvv]"]').parent().prev().css({'color': '', 'font-weight': ''});
	}

	if (message.length > 0){
		$('.form-errors').html(message.join('<br/><br/>'));
		$('.form-errors').show();
		return false;
	}
	else{
		$('.form-errors').hide();
		return true;
	}
}

function submitSupportRequest() {
	message = [];
	// make sure customer contact fields are filled in
	customerContacts = {first_name: 'First Name', 
		last_name: 'Last Name', 
		email: 'Email Address', 
		phone: 'Phone Number', 
		company: 'Company Name'};
	$.each(customerContacts, function(key, value) {
		if ($('form input[name="customer_contact['+key+']"]').val() == "") {
			$('form input[name="customer_contact['+key+']"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = 'Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.';
		}
		else
			$('form input[name="customer_contact['+key+']"]').parent().prev().css({'color': '', 'font-weight': ''});
	});

	// if we have an email, make sure it's valid
	if ( $('form input[name="customer_contact[email]"]').val() != "" ){
		var email_regex = /^([A-Za-z0-9_\-\.\+])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (email_regex.test($('form input[name="customer_contact[email]"]').val()) == false) {
			$('form input[name="customer_contact[email]"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = 'Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.';
		}
	else
		$('form input[name="customer_contact[email]"]').parent().prev().css({'color': '', 'font-weight': ''});
	}

	if (message.length > 0){
		$('.form-errors').html(message);
		$('.form-errors').show();
		return false;
	}
	else{
		$('form input').parent().prev().css({'color': '', 'font-weight': 'normal'});
		$('.form-errors').hide();
	}
}

function submitContactRequest() {
	message = [];
	// make sure customer contact fields are filled in
	customerContacts = {first_name: 'First Name', 
		last_name: 'Last Name', 
		email: 'Email Address', 
		phone: 'Phone Number', 
		company: 'Company Name'};
	$.each(customerContacts, function(key, value) {
		if ($('form input[name="customer_contact['+key+']"]').val() == "") {
			$('form input[name="customer_contact['+key+']"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = 'Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.';
		}
		else
			$('form input[name="customer_contact['+key+']"]').parent().prev().css({'color': '', 'font-weight': ''});
	});

	// if we have an email, make sure it's valid
	if ( $('form input[name="customer_contact[email]"]').val() != "" ){
		var email_regex = /^([A-Za-z0-9_\-\.\+])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (email_regex.test($('form input[name="customer_contact[email]"]').val()) == false) {
			$('form input[name="customer_contact[email]"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = 'Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.';
		}
		else
			$('form input[name="customer_contact[email]"]').parent().prev().css({'color': '', 'font-weight': ''});
	}

	if (message.length > 0){
		$('.form-errors').html(message);
		$('.form-errors').show();
		return false;
	}
	else{
		$('form input').parent().prev().css({'color': '', 'font-weight': 'normal'});
		$('.form-errors').hide();
	}
}

function submitFranchiseInfoRequest() {
	message = [];
	// make sure customer contact fields are filled in
	customerContacts = {first_name: 'First Name', 
		last_name: 'Last Name', 
		email: 'Email Address', 
		phone: 'Phone Number', 
		company: 'Company Name'};
	$.each(customerContacts, function(key, value) {
		if ($('form input[name="customer_contact['+key+']"]').val() == "") {
			$('form input[name="customer_contact['+key+']"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = 'Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.';
		}
		else
			$('form input[name="customer_contact['+key+']"]').parent().prev().css({'color': '', 'font-weight': ''});
	});

	// if we have an email, make sure it's valid
	if ( $('form input[name="customer_contact[email]"]').val() != "" ){
		var email_regex = /^([A-Za-z0-9_\-\.\+])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (email_regex.test($('form input[name="customer_contact[email]"]').val()) == false) {
			$('form input[name="customer_contact[email]"]').parent().prev().css({'color': 'red', 'font-weight': 'bold'});
			message = 'Required information is missing or incorrectly formatted. Please correct the area(s) marked in <span style="color: red;">red</span>.';
		}
		else
			$('form input[name="customer_contact[email]"]').parent().prev().css({'color': '', 'font-weight': ''});
	}

	if (message.length > 0){
		$('.form-errors').html(message);
		$('.form-errors').show();
		return false;
	}
	else{
		$('form input').parent().prev().css({'color': '', 'font-weight': 'normal'});
		$('.form-errors').hide();
	}
}
function loadFeaturecast(abbr, title) {
	$('#featurecast_video').html('<img src="/images/ajax-loader.gif"/>');
	$('#featurecast_video').load("/main/featurecast_embed?abbr="+abbr);
	$('h3.video-title').html(title);
	$('.featurecast-list').hide();
	$('.show-list').show();
	return false;
}
