// JScript source code
// Header Functions

 jQuery(document).ready( function() {		
  
  //click event for the Twitter Follow Us link
  jQuery('#twFollowUs').click(function() {
	  s1 = s_gi("tjxmarshalls");
	  s1.prop12 = "Twitter";
	  s1.eVar13 = "Twitter (Subscription)";
	  s1.eVar7 = "Out";		
	  s1.linkTrackVars="prop10,prop11,prop12,events,eVar7,eVar13";
	  s1.linkTrackEvents = "";
	  s1.events = "";
	  var s_code1=s1.tl();
	  if(s_code1) document.write(s_code1);
		  s_objectID='mar_sub_twitter'; 
		  var s1=s_gi('tjxmarshalls');
		  s1.tl(this,'o','mar_sub_twitter');				
		  window.open('http://twitter.com/marshalls');
  });
  
  //Click event for the Facebook Like Us link
  jQuery('#fbLikeUs').click(function() {
	  s1 = s_gi("tjxmarshalls");
	  s1.prop12 = "Facebook";
	  s1.eVar13 = "Facebook (Subscription)";
	  s1.eVar7 = "Out";		
	  s1.linkTrackVars="prop10,prop11,prop12,events,eVar7,eVar13";
	  s1.linkTrackEvents = "";
	  s1.events = "";
	  var s_code1=s1.tl();
	  if(s_code1) document.write(s_code1);
		  s_objectID='mar_sub_facebook'; 
		  var s1=s_gi('tjxmarshalls');
		  s1.tl(this,'o','mar_sub_facebook');				
		  window.open('http://www.facebook.com/Marshalls');
  });
	  
  //Click event for RSS Stay Up To Speed
  jQuery('#rssStayUp').click(function() {				
	  document.getElementById('lightRSS').style.display='block';
	  document.getElementById('fade').style.display='block';
	  s_objectID='mar_sub_rss'; 
	  var s1=s_gi('tjxmarshalls');
	  s1.tl(this,'o','mar_sub_rss');			
  });
	  
  jQuery('#emailSignup').click(function() {
      document.getElementById('lightEmail').style.display='block';
	  document.getElementById('fade').style.display='block';  
  });
  jQuery('#emailSubscribe').click(function(){		
	  //assign values from form inputs			
	  var email = jQuery("input#email").val();			
	  var zip = jQuery("input#zip").val();
	  //run form field validations
	  validEmail = validateEmail(email);
	  validZipCode = validateZipCode(zip);
	  var error = false;
	  //test validation returns, if errors found show message layer
	  if( validEmail != 1 ) {
		  jQuery('#emailError').show();				
		  closeIt();
		  error = true;
	  }
	  if( validZipCode != 1 ) {
		  jQuery('#zipcodeError').show();
		  closeIt();
		  error = true;
	  }
	  if( error ) {
		  jQuery(this).disabled = false;
		  return false;
	  }			
	  //made it this far, form inputs are ok, now bundle up params in prep for post submission
	  var param = {
		  'email': email.toLowerCase(),
		  'zip_code': zip,
			  'source' : 'header',
			  'action' : 'email_subscription',
			  'email_format' : 'html'
		  };
		  //process form here	
	  var url = "http://www.marshallsonline.com/wp-content/tjx-commons/request-handlers/tjx-ajax-account-util-jsonp.php?callback=?";
	  jQuery.ajax ({
			  //cache: false, 
			  url      : url,
			  dataType : 'jsonp',
			  data     : param,
			  success  : function(retVal) {																			
			  jQuery(this).disabled = false;
						
			  if (retVal.code == "success")
			  {
				jQuery("input#email").val('enter email address');
				jQuery("input#zip").val('zip code');
				jQuery('#emailSuccess').show();
				closeIt();
				var s1 = s_gi("tjxmarshalls");
				s1.prop12 = "Email";
				s1.eVar13 = "Email (Subscription)";
				s1.eVar7 = "Out";		
				s1.linkTrackVars="prop10,prop11,prop12,events,eVar7,eVar13";
				s1.linkTrackEvents = "event2";
				s1.events = "event2";
				var s_code1=s1.tl();
				if(s_code1) document.write(s_code1);
		
			}
			else
			{
				var htmlString = '<p id="pError">' + retVal.data + '</p>';
				if(jQuery('#pError').length){							
				  jQuery('#pError').remove();
				}					
				jQuery('#postError').append(htmlString);
				jQuery('#postError').show();
				closeIt();
			}					
		}
	}
  );			
	return false;
	});
});
	function fireRSSEvent(category) {
		s1 = s_gi("tjxmarshalls");
		s1.linkTrackVars="prop11,prop12,eVar13,events";
		s1.prop11 = category.replace("'", "");
		s1.prop12 = "RSS";
		s1.eVar13 = "RSS (Subscription)";
		s1.eVar7  = "Out";
		var s_code1=s1.tl();
		if(s_code1) document.write(s_code1);
	}
	
	function validateZipCode(zip) {
		if(zip.length==0)
		{
			return 2;
		}
		else if (zip.length != 5)
		{
			return 3;
		}
		else if (isNaN(zip))
		{
			return 3;
		}
			else if (Number(zip) <= 0 )
		{
			return 3;
		}
		else {return 1;}
	}
	
	function validateEmail(email){
		if(email.length != 0)
		{
			if(isValidEmailAddress(jQuery.trim(email)))
			{
				return 1;
			}
			else {
			return 2;
			}
		}
		else {
			return 3;
		}
	}
	
	// Email validation
	function isValidEmailAddress(emailAddress) {
 
		var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
     
          
		//Apostrophe email handling
		emailAddress=replaceCharacters("'","",emailAddress);
		if (emailAddress.search(emailRegEx) == -1) 
		return false;
		else
		return true;
	}
	
	function replaceCharacters(inChar,replacewithChar,origString) {
		var newString = origString.split(inChar);
		newString = newString.join(replacewithChar);
		return(newString);
	}
	//Currently on used on the page based email subscribe
	function compareEmailFields(emailFirst,emailSecond) {
		if(emailSecond == emailFirst){
			return true;
		}
		return false;
	}
	
	//First name / Last name validation
	function validateFirstName(name) {	
		if(name.length != 0) {
			if(name.contains(/[^\w-']|-{2,}|'{2,}/)){
				return 3;
			}
			else {
				return 1;
			}
		}
		else {
			return 2;
		}
	}
	function validateLastName(name) {
		if(name.length == 0)
		{
			return 1;
		}
		else {			
			if(name.contains(/[^\w-']|-{2,}|'{2,}/)){
				return 3;
			}
			else {
				return 1;
			}
		}		
	}
	
	String.prototype.contains = function(str) {
		if(str){
			if(str instanceof RegExp){
				return str.test(this);
			}
			else{
				return (this.indexOf(str) > -1);
			}
		}
	}
	
	//Click functions to hide messaging layer
	jQuery('#emailSuccess').click(function () {
		jQuery('#emailSuccess').hide();
	});
	
	jQuery('#emailError').click(function () {
		jQuery('#emailError').hide();
	});
	
	jQuery('#zipcodeError').click(function () {
		jQuery('#zipcodeError').hide();
	});
	
	jQuery('#postError').click(function () {
		jQuery('#postError').hide();
	});
	
	//functions to close messaging windows on timeout
	var close = '';
  
	function closeIt(){
		if(close==''){
			close = setTimeout(actuallyCloseIt,5000);
		}else{
			clearTimeout(close);
			close = setTimeout(actuallyCloseIt,5000);
		}
	}
  
	function actuallyCloseIt(){
		jQuery('#emailError').hide();
		jQuery('#zipcodeError').hide();
		jQuery('#emailSuccess').hide();
		jQuery('#postError').hide();
	}
	//end functions to close messaging windows on timer
 
		

