﻿jQuery(document).ready(function() {
	jQuery('.pictageServicesMenu .menu > li').hover(
		function(){
			jQuery(this).find('.tooltip').fadeIn(200);
		},
		function(){
			jQuery(this).find('.tooltip').fadeOut(400);
		}
	);
	/*  CONTACT FORM */
	// pre-selects approriate free items depending on how user got to this page (checks query string "c" value using gup() function in url_variables.js)
	switch(gup('promoItem')) {
		case 'inspire_book':
			jQuery('#dynamicLeadFreeStuff input#freeInspireBook').attr('checked','checked');
			break;
		case 'sample_prints':
			jQuery('#dynamicLeadFreeStuff input#freeSamplePrints').attr('checked','checked');
			break;
		case 'site_demonstration':
			jQuery('#dynamicLeadFreeStuff input#freeSiteDemo').attr('checked','checked');
			break;
		case 'price_list':
			jQuery('#dynamicLeadFreeStuff input#freePriceList').attr('checked','checked');
			break;
	}
	
	// Shows and hides Address fields and Price List disclaimer
	jQuery('.dynamicLeadFreeStuff input').change(function() {
		if (jQuery('.freeInspireBook:checked').length > 0 || jQuery('.freeSamplePrints:checked').length > 0) {
			showHideDynamicElements(jQuery(this).parents('.siteLeadForm').find('.dynamicLeadAddress'));
		} else {
			hideDynamicElements(jQuery(this).parents('.siteLeadForm').find('.dynamicLeadAddress'));
		}
		// sets dynamic text in header
		if (jQuery('.freeInspireBook:checked').length > 0 && jQuery('.freeSamplePrints:checked').length > 0) {
			jQuery(this).parents('.siteLeadForm').find('.dynamicLeadAddress h3 span.dynamicText').text('free book and sample prints');
		} else if (jQuery('.freeInspireBook:checked').length > 0) {
			jQuery(this).parents('.siteLeadForm').find('.dynamicLeadAddress h3 span.dynamicText').text('free book');
		} else if (jQuery('.freeSamplePrints:checked').length > 0) {
			jQuery(this).parents('.siteLeadForm').find('.dynamicLeadAddress h3 span.dynamicText').text('sample prints');
		}
		
		// Show price list disclaimer
		if (jQuery('.freePriceList:checked').length > 0) {
			showHideDynamicElements(jQuery(this).parents('.siteLeadForm').find('.dynamicPriceListDisclaimer'));
		} else {
			hideDynamicElements(jQuery(this).parents('.siteLeadForm').find('.dynamicPriceListDisclaimer'));
		}
	});
	jQuery('#newAccountsContact .dynamicLeadFreeStuff input').change(); // runs the change function at time of load to check for checked boxes, only on the Contact page version of the form
	
	// Shows and hides webpage URL field
	/*jQuery('.siteLeadForm input[name="00N70000002M3ZI"]').change(function() {
		if (jQuery(this).val() == 'Yes') {
			showHideDynamicElements(jQuery(this).parents('.siteLeadForm').find('fieldset.websiteAddress'),true);
		} else {
			jQuery('#contact_00N70000002J6lP').val('');
			jQuery('#contact_URL').val('');
			hideDynamicElements(jQuery(this).parents('.siteLeadForm').find('fieldset.websiteAddress'));
		}
	});*/
	
	// Strips "http://" or "https://" out of entry if it's there
	jQuery('.siteLeadForm input[name="00N70000002J6lP"]').change(function() {
		jQuery(this).val(jQuery(this).val().replace('http://',''));
		jQuery(this).val(jQuery(this).val().replace('https://',''));
	});
	
	// Shows and hides "How'd you hear about us" photographer referral and magazine fields
	jQuery('.siteLeadForm .lead_source').change(function() {
		if (jQuery(this).val() == 'Referral from Photographer') {
			showHideDynamicElements(jQuery(this).parents('.siteLeadForm').find('fieldset.lead_source_photographer'),true)
		} else if (jQuery(this).val() == 'Magazine Article') {
			showHideDynamicElements(jQuery(this).parents('.siteLeadForm').find('fieldset.lead_source_magazine'),true);
		} else {
			hideDynamicElements(jQuery(this).parents('.siteLeadForm').find('.dynamicLeadSource .dynamic'));
		}
	});
	
	// Shows and hides "Number of Annual Events" field
	jQuery('.siteLeadForm .primary-event-type').change(function() {
		if (jQuery(this).val() == 'Wedding') {
			showHideDynamicElements(jQuery(this).parents('.dynamicEventType').find('fieldset.number-annual-events'),true)
		} else {
			hideDynamicElements(jQuery(this).parents('.dynamicEventType').find('.dynamic'));
		}
	});
	
	// runs limit text function on textarea in specific question section
	jQuery('.specific-question textarea').keyup(function(){
		limitText(jQuery('.specific-question textarea'),jQuery('.specific-question .remaining'),5000);
	});
});

// show & hide function. If second parameter ("unique") is true, only one item in a group will be permitted to show at a time
function showHideDynamicElements(selected,unique) {
	selected.slideDown(250, function() {
		if (!jQuery(this).data('open')) {
			jQuery(this).children().css({'opacity':0,'visibility':'visible'}).animate({opacity:1},250,function() {
				if(jQuery.browser.msie){ //fixes IE's ugly filter-based opacity
					jQuery(this).css({opacity:''});
				}
			});
			jQuery(this).data('open',true);
		}
	});
	if (unique === true) {
		selected.siblings('.dynamic').children().animate({'opacity':0},250, function() {
			jQuery(this).css({'opacity':1,'visibility':'hidden'}).parent().slideUp(250).data('open',false);
		});
	}
}

// hide function
function hideDynamicElements(selected) {
	selected.children().animate({'opacity':0},250, function() {
		jQuery(this).css({'opacity':1,'visibility':'hidden'}).parent().slideUp(250).data('open',false);
	});
}

// limit text function
function limitText(limitField, limitCount, limitNum) {
	if (limitField.val().length > limitNum) {
		limitField.val(limitField.val().substring(0, limitNum));
	} else {
		limitCount.html(limitNum - limitField.val().length);
	}
}

function formProc(formRef, valFct) {
if (!valFct()) return false;
var pointUnit = 1;
var elems = formRef.elements;
var points = 0;
// do they have a site
if(elems['00N70000002M3ZI'].value == "Yes" || jQuery(formRef).find('input[name="00N70000002M3ZI"]:checked').val() == 'Yes') {
points+=pointUnit;
}
// events booked per year - Commented out per JIRA CRE-126
/*var eventsPerYear = elems['00N50000001K358'].value;
if (eventsPerYear != "Under 10" && eventsPerYear != "10 - 20") {
points+=pointUnit;
}*/
//call the JS function with the points for the tracking and set hidden points here for salesforce
// kenshoo_conv('1b1d2ecf-f579-4786-8d72-33d5e5a99c62','conv',points,'','','USD');
jQuery(formRef).find('input[name="retURL"]').val(jQuery(formRef).find('input[name="retURL"]').val() + '&qualified=' + points);
elems['00N70000002ZNbd'].value = points;
return true;
} 


// CONTACT FORM VALIDATION
function validateInputsContact()
{
	// first name
	if ( String( trimlr( document.forms['newAccountsContact'].elements['first_name'].value ) ).length==0 )
	{
		alert("Please enter your first name.");
		document.forms['newAccountsContact'].elements['first_name'].focus();
		return false;
	}
	// last name
	if ( String( trimlr( document.forms['newAccountsContact'].elements['last_name'].value ) ).length==0 )
	{
		alert("Please enter your last name.");
		document.forms['newAccountsContact'].elements['last_name'].focus();
		return false;
	}
	// phone
	if ( String( trimlr( document.forms['newAccountsContact'].elements['phone'].value ) ).length==0 )
	{
		alert("Please enter your phone number.");
		document.forms['newAccountsContact'].elements['phone'].focus();
		return false;
	}
	// email
	if ( String( trimlr( document.forms['newAccountsContact'].elements['email'].value ) ).length==0 )
	{
		alert("Please enter your email address.");
		document.forms['newAccountsContact'].elements['email'].focus();
		return false;
	}
	if ( !validateEmail( trimlr( document.forms['newAccountsContact'].elements['email'].value ) ) )
	{
		alert("Please enter a valid email address format. e.g. marysmith@gmail.com");
		document.forms['newAccountsContact'].elements['email'].focus();
		return false;
	}
	// Website Yes No
	if ( String( trimlr( document.forms['newAccountsContact'].elements['00N70000002M3ZI'].value ) ).length==0 )
	{
		alert("Please tell us if you have a studio website/blog.");
		document.forms['newAccountsContact'].elements['00N70000002M3ZI'].focus();
		return false;
	}
	// Website URL
	if (jQuery('#newAccountsContact input.websiteAddress:checked').val() == 'Yes' ) {
		if ( String( trimlr( document.forms['newAccountsContact'].elements['00N70000002J6lP'].value ) ).length==0 ) {
			alert("Please tell us the URL of your website/blog.");
			document.forms['newAccountsContact'].elements['00N70000002J6lP'].focus();
			return false;
		}
		if ( !validateURL(trimlr(document.forms['newAccountsContact'].elements['00N70000002J6lP'].value))) {
			alert('Please enter a valid URL.');
			document.forms['newAccountsContact'].elements['00N70000002J6lP'].focus();
			return false;
		}
	}
	// events booked per year
	if ( String( trimlr( document.forms['newAccountsContact'].elements['00N50000001K358'].value ) ).length==0 )
	{
		alert("Please tell us how many events you book per year.");
		document.forms['newAccountsContact'].elements['00N50000001K358'].focus();
		return false;
	}
	// How did you hear about Pictage 
	if ( String( trimlr( document.forms['newAccountsContact'].elements['lead_source'].value ) ).length==0 )
	{
		alert("Please tell us how you heard about Pictage.");
		document.forms['newAccountsContact'].elements['lead_source'].focus();
		return false;
	}

	return true;
}
function trimlr(value)
{
var myexpregI = /^ / ;
var myexpregD = / $/ ;
value = String(value).replace( myexpregD ,'' ) ;
value = String(value).replace( myexpregI ,'' ) ;
if ( ( myexpregD.test(value) ) || ( myexpregI.test(value) ) ) { value = trimlr(value) }
return value ;
}
