$(function() {
	if(document.getElementById("FeatureNotAvailable")) {
		$('.opt-in-noscript').css('display','block');
		$('.opt-in-form').css('display','none');
	} else {
		$('.opt-in-noscript').css('display','none');
		$('.opt-in-form').css('display','block');
	}

	$('.empty').focus(function() {
		if(this.className.indexOf("empty") != -1) {
			this.value = "";
			$(this).removeClass('empty');
		}
	});
	
	$('.opt-in-form input').blur(function() {
		if(this.value == '') {
			$(this).addClass('empty');
			if(this.className.indexOf("email") != -1) {
				this.value = "(Required)";
			} else {
				this.value = "(Optional)";
			}
		}
	});
	
	$('.btn_sign-up-now').removeAttr('disabled');

	$('.opt-in-form').validate({
		onfocusout: false,
			onkeyup: false,
		rules: {
			EMAIL_ADDRESS: {
				required: true,
				email: true
			}
		},
		messages: {
			EMAIL_ADDRESS: {
				required: 'Oops, it looks you forgot to enter your email address.',
				email: 'Oops, it looks like the email address you entered is invalid.'
			}
		},
		errorPlacement: function(error, element) {
			$('label.error[generated="true"]').remove();
			error.insertAfter('.opt-in-form');
		}
	});

	$('.opt-in-form input').keydown(function(e) {
		if (e.keyCode == 13) return validateOptInForm();
	});

	$('.btn_sign-up-now').click(function() {
		validateOptInForm();
		return false;
	});
});

function validateOptInForm() {
	var formIsValid = $('.opt-in-form').valid();
	if(formIsValid) return subscribeToEmails();
}

function subscribeToEmails()
{
	// Get parent page name
	var parentPage = '';
	if (s.prop1 != undefined) {
		var parentPage = s.prop1;
	} else if (s.pageName != undefined) {
		var parentPage = s.pageName;
	}
	var parentSite = s.prop5;

	// set the source attribute on the form with s.prop1 value
	if (document.OptInForm != undefined && document.OptInForm.SOURCE != undefined) {
		document.OptInForm.SOURCE.value = parentPage;
	}
	
	$('.footer-opt-in label.error[generated="true"]').remove();
	$('.body-opt-in label.error[generated="true"]').remove();
	$('.btn_sign-up-now').addClass('disabled');
	$('.btn_sign-up-now').attr('disabled','disabled');
	var pathname = window.location.pathname.split('/', 3)[2];
	if (pathname == undefined  || pathname == "Canada" || pathname == "US" ) {
		if ((window.location.hostname == "www.epson.ca") || (window.location.hostname == "epson.ca") || (window.location.hostname == "epsonstore.ca")) {
			var postProxy = window.location.protocol + '//www.epson.ca/cgi-bin/ceStore/jsp/responsys/OptInProxy.jsp';
		} else {
			var postProxy = window.location.protocol + '//www.epson.com/cgi-bin/Store/jsp/responsys/OptInProxy.jsp';
		}
	} else {
		var postProxy = window.location.protocol + '//' + window.location.hostname + '/cgi-bin/' + window.location.pathname.split('/', 3)[2] + '/jsp/responsys/OptInProxy.jsp';
	}
	$.post(postProxy, $('.opt-in-form').serialize(), function(statusCode, errorLevel) {
		if($.trim(statusCode) == '200') {
			$('.opt-in-form').css('display','none');
			$('.opt-in-thankyou').css('display','block');
			// Omniture tagging to capture sign-up event
			s.pageName = "Opt in on " + parentPage;
			s.channel = "Opt in"
			s.prop1 = "Opt in on " + parentPage;
			s.prop5 = parentSite;
			s.eVar38 = "Opt in on " + parentPage;
			var s_code=s.t();if(s_code)document.write(s_code);			
			//if(navigator.appVersion.indexOf('MSIE')>=0)document.write(unescape('%3C')+'!-'+'-');
			// Omniture tagging end
		} else {
			$('.opt-in-error').css('display','block');
			$('.btn_sign-up-now').removeAttr('disabled');
			$('.btn_sign-up-now').removeClass('disabled');
		}
	}, 'text');

	return false;
}

