var newsletterBox = {
	mainTabs: function(){
	//show/hide box
		$('a.newsletterDrop').click(function () {
			if($(this).parent().hasClass('active')){
				$(this.rel).removeClass('tab_contents_active');
				$(this).parent().removeClass('active');
			}
			else{
				//remove active class
				$('ul#tabs_container > li.newsletterDrop > li.active').removeClass('active');
				//add active class to clicked tab
				$(this).parent().addClass('active');
				// Remove the 'tab_contents_active' class from the visible tab contents.
				$('.tab_contents_container > div.tab_contents_active').removeClass('tab_contents_active');
				// Add the 'tab_contents_active' class to the associated tab contents.
				$(this.rel).addClass('tab_contents_active');
			}
			return false;
		});
	}, //end mainTabs
	init: function(){
	//blur and focus
		var formInputFname = $('input#form-input-fname');
		var formInputLname = $('input#form-input-lname');
		var formInputEmail = $('input#form-input-email');
		//default values
		var defaultValueFname = '*First Name';
		var defaultValueLname = '*Last Name';
		var defaultValueEmail = '*Email Address';
		//first name
		if(formInputFname.val().length > 0){
			formInputFname.focus(function(){
				if($(this).val() == defaultValueFname){
					$(this).val('');
				}
			});
			formInputFname.blur(function(){
				if($(this).val() == ''){
					$(this).val(defaultValueFname);
				}
			});
		}
		//last name
		if(formInputLname.val().length > 0){
			formInputLname.focus(function(){
				if($(this).val() == defaultValueLname){
					$(this).val('');
				}
			});
			formInputLname.blur(function(){
				if($(this).val() == ''){
					$(this).val(defaultValueLname);
				}
			});
		}
		//email
		if(formInputEmail.val().length > 0){
			formInputEmail.focus(function(){
				if($(this).val() == defaultValueEmail){
					$(this).val('');
				}
			});
			formInputEmail.blur(function(){
				if($(this).val() == ''){
					$(this).val(defaultValueEmail);
				}
			});
		}
		//submit function dropdown
		function submitAndOpenEnewsletterPopup(parentObj){
			var parObjClass = parentObj.attr('class');
			var form = "";
			var locationOfForm = "";
			var email;
			var first;
			var last;
			var source;
			var goAhead = true;
			if(parObjClass == 'newsletterForm'){
				email = $("input#form-input-email").val();
				first = $("input#form-input-fname").val();
				last = $("input#form-input-lname").val();
				source = "Top Nav";
			} else if(parObjClass == 'enewsMLeft'){
				form = "Sidebar";
				email = $("#frmEmailSignup #txtEmail").val();
				first = $("#frmEmailSignup #txtFirstName").val();
				last = $("#frmEmailSignup #txtLastName").val();
				source = "Sidebar";
			} else if(parObjClass == 'homeSlice'){
				email = $(".formyPart #email").val();
				first = $(".formyPart #fname").val();
				last = $(".formyPart #lname").val();
				source = "Scrolling Nav";
			}
			if(source == "Sidebar" && $('#chkConfirm').attr('checked') != true){
				alert('Please click the checkbox to receive enewsletter.');
				goAhead = false;
				return false;
			} else if(source == "Top Nav" && $('div.checkBox'+form+' input[type=checkbox]').attr('checked') != true){
				alert('Please click the checkbox to receive enewsletter.');
				goAhead = false;
				return false;
			} else if(source == "Scrolling Nav" && $('.checkyBox input[type=checkbox]').attr('checked') != true){
				alert('Please click the checkbox to receive enewsletter.');
				goAhead = false;
				return false;
			}
			if(goAhead){
				var validation = popupValidation.validate();
				if(validation){
					var query = 'email='+email+'&first='+first+'&last='+last+'&source='+source;
					ajax.go('GET', '/itrucks/eNewsletterProvider', query, true, closeNewsletterBox);
					ajax.go('GET', '/itrucks/ENewsletterListTrak', 'email='+email+'&firstName='+first+'&lastName='+last+'&source='+source + '&crvs='+$("#crvs").val()+'&fromWhere='+$("input#fromWhere").val(), true, logResults);
					emailSubmitBox.open('?'+query);
					// omniture
					if(source != "Scrolling Nav"){
						genOmn.init("events:event7, eVar5:"+source);
					}
					//spotlight yo!
					var axel = Math.random() + '';
					var a = axel * 10000000000000;
					$('body').append('<img src="http://ad.doubleclick.net/activity;src=3067057;type=2011s176;cat=int_e973;ord=' + a + '?" width="1" height="1" alt=""/>');
					
					//$('body').append('<iframe //src=""http://fls.doubleclick.net/activityi;src=3067057;type=2011s176;cat=int_e973;ord=' + a + '?"" //width=""1"" height=""1"" frameborder=""0"" style=""display:none""></iframe>');
				}
				else{
					return false;
				}
				return false;
			}
		};
		$('input.submitNewsletterSignup').live('click',function(){
			var toAddress = $('#form-input-email').val();
			var validEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			var isValid = validEmail.test(toAddress);
			if (toAddress == '' || toAddress == '*Email Address' || !isValid) {
				alert("Please enter a valid email address.");
			} else if ($('#form-input-fname').val() == '' || $('#form-input-fname').val() == '*First Name') {
				alert("Please enter your first name.");
			} else if ($('#form-input-lname').val() == '' || $('#form-input-lname').val() == '*Last Name') {
				alert("Please enter your last name.");
			} else {
				submitAndOpenEnewsletterPopup($(this).parent());
			}
		});
		$('#stayInTheKnow #btnSignup').live('click',function(){
			var toAddress = $('#stayInTheKnow #txtEmail').val();
			var validEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			var isValid = validEmail.test(toAddress);
			if (toAddress == '' || toAddress == 'Enter Email' || !isValid) {
				alert("Please enter a valid email address.");
			} else if($('#stayInTheKnow #txtFirstName').val() == '') {
				alert("Please enter your first name.");
			} else if($('#stayInTheKnow #txtLastName').val() == '') {
				alert("Please enter your last name.");
			} else {
				$("#frmEmailSignup").hide();
				$(".confirmed").show();
				submitAndOpenEnewsletterPopup($(this).parent());
			}
		});
	//submit function popup
		$('input.submitPop').click(function(){
			if($('div.checkBox input[type=checkbox]').attr('checked') != true){
				alert('Please click the checkbox to receive enewsletter.');
				return false;
			}
			else{
				var validation = popupValidation.validate();
				if(validation){
					ajax.go('GET', '/itrucks/eNewsletterProvider', 'email='+$("input#form-input-email").val()+'&first='+$("input#form-input-fname").val()+'&last='+$("input#form-input-lname").val(), false, closePopBox);
				}
				else{
					return false;
				}
				return false;
			}
		});
	//close dropdown
		function closeNewsletterBox(isPopup){
			if(isPopup){
				$('#popupHolder',window.parent.document).animate({opacity:0},500,function(){
					$('#popupHolder',window.parent.document).hide();
				});
				$('#popupScreen',window.parent.document).hide();
			}
			$('#newsletterBox').removeClass('tab_contents_active');
			$('a.newsletterDrop').parent().removeClass('active');
		}
	//log results
		function logResults(callback){
		}
	//close popup
		function closePopBox(){
			$('#popupHolder',window.parent.document).animate({opacity:0},500,function(){
				$('#popupHolder',window.parent.document).hide();
			});
			$('#popupScreen',window.parent.document).hide();
		}
	//validation
		var popupValidation = {
			validate:function(form){
				var finalPass = true;
				var didPass = true;
				$('input.required'+form).each(function(){
					didPass = popupValidation.required($(this));
					if(!didPass){
						finalPass = false;
					}
				});
				if(!finalPass){
					popupValidation.message();
				}
				return finalPass;
			},
			required: function($obj){
				var didPass = true;
				if($obj.val() == '' || $obj.val() == '*First Name' || $obj.val() == '*Last Name' || $obj.val() == '*Email Address'){
					$obj.css({'border':'1px solid red'});
					didPass = false;
				}
				return didPass;
			},
			message: function(){
				alert('Please fix the fields outlined in red.');
				return false;
			}
		}
	//ajax call
		var ajax = {
				go: function(type, url, data, returnData, callback) {
					$.ajax({
						type: type,
						url: url,
						data: data,
						success: function(msg){
						if(returnData)
							callback(msg);
						else
							callback();
						},
					error: function(XMLHttpRequest, textStatus, errorThrown){
						alert('There has been an error with your request, please try again.');
					}
				});
			}
		};
	} //end init
} //end newsletterBox var
