/* On page-load functions */
jQuery(document).ready(function(){

		/* Home page cycle through client images.
		Requires jQuery and the Cycle plugin (see plugins.js) */
		jQuery('.ecClientCycle').cycle({ 
			delay:  1500, 
			speed:  250
		}); 

		/* Stripe only the table we want striped (any that has a class of stripe-me)
		Requires jQuery */
		jQuery('.stripe-me tbody tr:nth-child(odd)').addClass('odd');

		/* Lightbox popup initialize on anything with a ceebox class
		Requires jQuery and the ceebox plugin */
		jQuery('.ceebox').ceebox({videoWidth:640,htmlWidth:640,videoRatio:1.5});
		
		/* Sitefinity doesn't give control to have a default value for the search
		input. LAME. This fills it out with 'search our site' and gets it to
		clear when clicked.
		Requires jQuery */
		jQuery('.sfsearchBox .sfsearchTxt').each(function(){
            var search_default_val = 'Search our site';
			this.value = search_default_val;
            $(this).focus(function() {
                if(this.value == search_default_val) {
                    this.value = '';
                }
            });
            $(this).blur(function() {
                if(this.value == '') {
                    this.value = search_default_val;
                }
            });
        });
		
		/* Autoclear a form field with a default value 
		Requires jQuery */
		jQuery('.default-value').each(function() {
            var default_value = this.value;
            $(this).focus(function() {
                if(this.value == default_value) {
                    this.value = '';
                }
            });
            $(this).blur(function() {
                if(this.value == '') {
                    this.value = default_value;
                }
            });
        });
		
});
   


/* Forms */
function createInput(name, val) {
	return jQuery('<input type="hidden">').attr('name', name).val(val);
}

function submitSalesForceForm2() {
	try {
		var $ = jQuery;
		var validator = $('form').validate({
			messages: {
				"name": '*',
				"email": '*'
			},
			onsubmit: false
		});
		
		var valid = true;
		$('.email-signup input').each(function(idx, item) {
			valid = valid && validator.element(item);
		});
		if(valid) {
			var domainCustomerIsUsing = document.domain;
			var thankYouURL = 'http://'+domainCustomerIsUsing+'/thank-you-newsletter';
			var salesForceForm = $('<form>').attr('method', 'POST').attr('action', 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8').css('display', 'none');
			salesForceForm.append(createInput('oid', '00D70000000JbNX'));
			// salesForceForm.append(createInput('retURL', 'http://wip.ecompliance.com/thank-you-newsletter'));
			salesForceForm.append(createInput('retURL', thankYouURL));
			salesForceForm.append(createInput('00N70000002KzDG', 'Club Zero'));
			salesForceForm.append(createInput('00N70000002KzDB', '1'));
			salesForceForm.append(createInput('name', jQuery('.email-signup input[name="name"]').val()));
			salesForceForm.append(createInput('email', jQuery('.email-signup input[name="email"]').val()));
			$('body').append(salesForceForm);
			salesForceForm.get(0).submit();
		}
	} catch(e) {
		alert('Failed to submit the form.');
		if(console && console.log) {
			console.log(e);
		}
	}
}

function submitRequestDemoForm() {
	try {
		var $ = jQuery;
		var validator = $('form').validate({
			messages: {
				"name": '*',
				"phone": '*',
				"email": '*',
				"product": '*'
			},
			onsubmit: false
		});
		var valid = true;
		$('#request-a-demo-form input').each(function (idx, item) {
			valid = validator.element(item) && valid;
		});
		$('#request-a-demo-form select').each(function (idx, item) {
			valid = validator.element(item) && valid;
		});
		if (!valid) {
			if (!e) var e = window.event;
			e.cancelBubble = true;
			e.returnValue = false;
			if (e.stopPropagation) e.stopPropagation();
			if (e.preventDefault) e.preventDefault();
		}
		return valid;
	} catch (e) {
		if (console && console.log) {
			console.log(e);
		}
		alert('Failed to submit the form.');
	}
}

