/*******************************************************
********************************************************
Title: exacttarget_subscriber_form_v1.js
Version: 1.0
Author: Jeremiah Weeden-Wright
Date Updated: 08/18/2011

Description:
	On the page load, the page is loaded setting up 
	javascript for exact target.

	On splits:
		0 = facilityName
		1 = member id
		2 = list id
		3 = extended attributes boolean

********************************************************
********************************************************/

// Load on page load
jQuery(function() {
	// Hide or show attributes based on selected value
	var selectedValue = jQuery("#facilityChoice option:selected").val();
	if(selectedValue != null) {
		var extendedAtts = 	jQuery("#facilityChoice option:selected").val().split(",")[3];
		if(extendedAtts != null && extendedAtts.indexOf('true') != -1) {
			jQuery("#extended").show();
		} else {
			jQuery("#extended").hide();
		}
	} else {
		// No dropdown means only 1 value, detect the extended or no
		var extendedAtts = jQuery("#extendedAtts").val();
		if(extendedAtts != null && extendedAtts.indexOf('true') != -1) {
			jQuery("#extended").show();
		} else {
			jQuery("#extended").hide();
		}
	}
	
	// When the drop down changes, set the member and list ids correctly
	jQuery("#facilityChoice").change('click', function() {

		// URL for the form
		var formUrl = "http://cl.exct.net/subscribe.aspx?lid=";
		
		// Get Values from drop down to populate form
		var facilityVals = jQuery("#facilityChoice option:selected").val();
		var facilityName = jQuery.trim(facilityVals.split(",")[0]);
		var facilityName = facilityName + " - " + jQuery("#hostname").val();
		var memberId = jQuery.trim(facilityVals.split(",")[1]);
		var listId = jQuery.trim(facilityVals.split(",")[2]);
		if (facilityVals.split(",")[3] != null) {
			var extendAtts = jQuery.trim(facilityVals.split(",")[3]);
		}
		
		// Set values on form to match new selection in drop down
		jQuery("#enewsletter form").attr("action",formUrl + listId);
		jQuery("#exactTargetMemberId").val(memberId);
		jQuery("#exactTargetOofR").val(facilityName);

		// Show extended or short attributes depending on choice in dropdown
		if(extendAtts != null && extendAtts.indexOf('true') != -1) {
			jQuery("#extended").show();
		} else {
			jQuery("#extended").hide();
		}
	});
});

function CheckEnewsletterForm() {
	// Check required values
	if (jQuery("#firstName").val() == "") {
		alert("Please enter a first name.");
		return false;
	}
	if (jQuery("#lastName").val() == "") {
		alert("Please enter a last name.");
		return false;
	}
	if (jQuery("#email").val() == "") {
		alert("Please enter an email.");
		return false;
	}
	if (jQuery("#zip").val() == "") {
		alert("Please enter a zip code.");
		return false;
	}

	// Check email for formatting
	if (!(jQuery("#email").val().match("^([a-zA-Z0-9]+[a-zA-Z0-9._%+-]*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4})$"))) {
		alert("Please enter a valid email address.");
		return false;
	}
	
	// Check for zip being valid (5 digit number)
	if (!(jQuery("#zip").val().match(/^\d{5}$/))) {
		alert("The zip code entered is invalid. Please enter a 5 digit value.");
		return false;
	}
	
	// If birthdate isn't blank, make sure its the correct format
	if (jQuery("#DOB").val() != "") {
		if (!(jQuery("#DOB").val().match(/^\d{1,2}\/\d{1,2}\/\d{4}$/))) {
			alert("The birthdate entered is invalid. Please enter your birthdate as MM/DD/YYYY.");
			return false;
		}
	}
}
