///////////////////////////////////////////////////////////////////////
// This function will visually display the inut element.
//////////////////////////////////////////////////////////////////////
function ShowElement(ElementId)
{ 
	// make sure the element exists
	if (document.getElementById(ElementId) != null)
	{
		// hide "update in progress" text and display
		document.getElementById(ElementId).style.display="";
		document.getElementById(ElementId).style.visibility="visible";
	}
}

///////////////////////////////////////////////////////////////////////
// Trims leading/trailing spaces
//////////////////////////////////////////////////////////////////////
function trim(str) 
{ 
  str = str.replace( /^\s+/g, "" ); // strip leading 
  str = str.replace( /\s+$/g, "" ); // strip trailing 

  return str; 
} 

///////////////////////////////////////////////////////////////////////
// This function will visually hide the inut element.
//////////////////////////////////////////////////////////////////////
function HideElement(ElementId)
{ 
	// make sure the element exists
	if (document.getElementById(ElementId) != null)
	{
		// hide "update in progress" text and display
		document.getElementById(ElementId).style.display="none";
		document.getElementById(ElementId).style.visibility="hidden";
	}
}


//////////////////////////////////////////////////////////////////
// returns true a domain we care about
//////////////////////////////////////////////////////////////////
function endsWithGoodDomain(s) 
{
	if (s.length == 0)
	{
		return false;
	}
	
	// valid domains:
	//		.com  = commercial
	//		.net  = net
	//		.org  = organization
	//		.edu  = education
	//		.gov  = government
	//		.mil  = military
	//		.biz  = business
	//		.info = information
	//		.us   = United States
	//		.uk   = United Kingdom
	//		.au   = Australia
	//		.nz   = New Zealand
	//		.hk   = Hong Kong
	//		.ca   = Canada
	//		.tv   = Television
	var domainArray = new Array(
							".com",
							".net",
							".edu",
							".org",
							".gov",
							".mil",
							".biz",
							".info",
							".us",
							".uk",
							".au",
							".nz",
							".hk",
							".ca",
							".tv"
							);

	for (i=0; i<domainArray.length; i++)
	{
		if (s.toLowerCase().match(domainArray[i]+"$")==domainArray[i])
		{
			return true;
		}
	}
	
	return false;
}

//////////////////////////////////////////////////////////////////
// Determines if the input string represents a syntactically correct
// email address.
//////////////////////////////////////////////////////////////////
function isValidEmail(addr) 
{
	// make sure we just have 1 '@'
	var tokens = addr.split('@');
	
   if ((addr.indexOf("@")  == -1) 
   ||  (addr.indexOf(".")  == -1) 
   ||  (addr.indexOf(" ")  != -1) 
   ||  (addr.indexOf(",")  != -1) 
   ||  (addr.indexOf(";")  != -1) 
   ||  (addr.indexOf(":")  != -1) 
   ||  (addr.indexOf("/")  != -1) 
   ||  (addr.indexOf("\\") != -1) 
   ||  (addr.indexOf("!")  != -1) 
   ||  (addr.indexOf("@@") != -1) 
   ||  (addr.indexOf("..") != -1) 
   ||  (addr.indexOf("@.") != -1) 
   ||  (addr.indexOf(".@") != -1) 
   ||  (addr.indexOf(")")  != -1) 
   ||  (addr.indexOf("(")  != -1) 
   ||  (addr.indexOf("#")  != -1) 
   ||  (addr.indexOf("[")  != -1) 
   ||  (addr.indexOf("]")  != -1) 
   ||  (addr.indexOf("{")  != -1) 
   ||  (addr.indexOf("}")  != -1) 
   ||  (addr.indexOf("$")  != -1) 
   ||  (addr.indexOf("+")  != -1) 
   ||  (endsWithGoodDomain(addr)  == false) 
   ||  (tokens.length != 2)
      ) 
   {
      return false;
   } 
   else 
   {
		return true;
   }
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clickes
//////////////////////////////////////////////////////////////////
function submitEmailAddress(fieldId)
{
	document.getElementById(fieldId).value = trim(document.getElementById(fieldId).value);
	if (!isValidEmail(document.getElementById(fieldId).value))
	{
		alert ('a valid email address is required.');
		return;
	}
	
	submitViaAjaxJquery(fieldId);
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clicked for the signup with
// an associated edition dropdown list
//////////////////////////////////////////////////////////////////
function submitEmailAddressWithSelectedEdition(fieldId, editionFieldId)
{
	document.getElementById(fieldId).value = trim(document.getElementById(fieldId).value);
	if (!isValidEmail(document.getElementById(fieldId).value))
	{
		alert ('a valid email address is required.');
		return;
	}
	
	document.getElementById('edition_name').value = document.getElementById(editionFieldId).value;
	
	submitViaAjaxJquery(fieldId);
}

function submitEmailAddressLP(inForm,fieldId)
{
	var url = document.getElementById("subscribe_url").value;
	var popup = document.getElementById("popup");
	var fader = document.getElementById("fader");

	document.getElementById(fieldId).value = trim(document.getElementById(fieldId).value);
	if (!isValidEmail(document.getElementById(fieldId).value))
	{
		alert ('a valid email address is required.');
		return;
	}

	if (NumberOfBoxesChecked(inForm.editionNames) == 0)
		{
			alert ('please select at least 1 edition.');
			return;
		}	

	var editions  = "";
	var separator = "";

	for (var i = 0; i < inForm.editionNames.length; i++) 
	{
		// if this one is selected
		if (inForm.editionNames[i].checked == true)
		{
			editions += separator + inForm.editionNames[i].value;
			separator = ",";
		}
	}

	if (document.getElementById("emailAddress2"))
	{
		// hang onto the email address they entered so it can be re-submitted with the additional information
		document.getElementById("emailAddress2").value = document.getElementById(fieldId).value;
	}

	jQuery.facebox({ div: '#subscribe' });
	$.post(	url, 
			{ 
				'emailAddress'   : document.getElementById(fieldId).value, 
				'referrer'       : document.getElementById('referrer').value, 
				'processingStage': '1',
				'editionNamesComposite'   : editions
			},
			function (data){ parseJsonResultJquery(data); }, 
			'json');

}

function submitEmailAddressLPNoEdition(inForm,fieldId)
{
	var url = document.getElementById("subscribe_url").value;
	var popup = document.getElementById("popup");
	var fader = document.getElementById("fader");

	document.getElementById(fieldId).value = trim(document.getElementById(fieldId).value);
	if (!isValidEmail(document.getElementById(fieldId).value))
	{
		alert ('a valid email address is required.');
		return;
	}

	var editions  = document.getElementById('editionNames').value;

	if (document.getElementById("emailAddress2"))
	{
		// hang onto the email address they entered so it can be re-submitted with the additional information
		document.getElementById("emailAddress2").value = document.getElementById(fieldId).value;
	}

	jQuery.facebox({ div: '#subscribe' });
	$.post(	url, 
			{ 
				'emailAddress'   : document.getElementById(fieldId).value, 
				'referrer'       : document.getElementById('referrer').value, 
				'processingStage': '1',
				'editionNamesComposite'   : editions
			},
			function (data){ parseJsonResultJquery(data); }, 
			'json');

}

function submitEmailAddressLPNoEditionFromFriend(inForm,fieldId,fieldFriendId)
{
	var url = document.getElementById("subscribe_url").value;
	var popup = document.getElementById("popup");
	var fader = document.getElementById("fader");

	document.getElementById("idInviterText").className = "inviter";
	
	document.getElementById(fieldId).value = trim(document.getElementById(fieldId).value);
	if (!isValidEmail(document.getElementById(fieldId).value))
	{
		alert ('a valid email address is required.');
		return;
	}

	document.getElementById(fieldFriendId).value = trim(document.getElementById(fieldFriendId).value);
	if (!isValidEmail(document.getElementById(fieldFriendId).value))
	{
		document.getElementById("idInviterText").className = "inviter-error";
		alert ("your friend's email address is not valid.");
		return;
	}

	var editions  = document.getElementById('editionNames').value;

	if (document.getElementById("emailAddress2"))
	{
		// hang onto the email address they entered so it can be re-submitted with the additional information
		document.getElementById("emailAddress2").value = document.getElementById(fieldId).value;
	}

	jQuery.facebox({ div: '#subscribe' });
	$.post(	url, 
			{ 
				'emailAddress'          : document.getElementById(fieldId).value, 
				'referrerEmailAddress'  : document.getElementById(fieldFriendId).value, 
				'referrer'              : document.getElementById('referrer').value, 
				'processingStage'       : '1',
				'editionNamesComposite' : editions
			},
			function (data){ parseJsonResultJquery(data); }, 
			'json');

}

//////////////////////////////////////////////////////////////////
// submitViaAjax - submits just the email address
//////////////////////////////////////////////////////////////////
function submitViaAjaxJquery(fieldId)
{ 
	if (document.getElementById("emailAddress2"))
	{
		// hang onto the email address they entered so it can be re-submitted with the additional information
		document.getElementById("emailAddress2").value = document.getElementById(fieldId).value;
	}

	var url = document.getElementById("subscribe_url").value;
	
	document.getElementById("yourEmail").innerHTML = document.getElementById("emailAddress2").value;
	
	jQuery.facebox({ div: '#subscribe' });
	
	$.post(	url, 
			{ 
				'emailAddress'   : document.getElementById(fieldId).value, 
				'referrer'       : document.getElementById('referrer').value, 
				'processingStage': '1',
				'editionNames'   : document.getElementById('edition_name').value
			},
			function (data){ parseJsonResultJquery(data); }, 
			'json');
}

//////////////////////////////////////////////////////////////////
// This method is called after several different subscribe actions.
// For example, the home page subscribe (without edition selection) and
// the subscribe page action (with editions) both land here for common post processing.
//////////////////////////////////////////////////////////////////
function parseJsonResultJquery(commandBean)
{ 
	// now that we have submitted the email address, we will see if there are any 
	// Cost Per Acquition (CPA) tracer graphics we need to include.  Usually, these
	// graphics would just  be included on a "Thanks" page, but since we just AJAX'ed
	// the email address, I will programatically create an iframe(s) and use the tracer
	// graphic as the iframe's source

	if (document.getElementById("iframeCpaTracker1"))
	{
		urlTracker1 = document.getElementById("iframeCpaTracker1").value.replace("visitorId",commandBean.visitorId);
		iframeCpaTracker = document.createElement("IFRAME"); 
		iframeCpaTracker.setAttribute("src", urlTracker1); 
		iframeCpaTracker.style.width = 1+"px"; 
		iframeCpaTracker.style.height = 1+"px"; 
		document.body.appendChild(iframeCpaTracker); 
	}
	
	if (document.getElementById("iframeCpaTracker2"))
	{
		urlTracker2 = document.getElementById("iframeCpaTracker2").value.replace("visitorId",commandBean.visitorId);
		iframeCpaTracker2 = document.createElement("IFRAME"); 
		iframeCpaTracker2.setAttribute("src", urlTracker2); 
		iframeCpaTracker2.style.width = 1+"px"; 
		iframeCpaTracker2.style.height = 1+"px"; 
		document.body.appendChild(iframeCpaTracker2); 
	}
	
	// sometimes we use a tracer graphic to acknowledge a visitor just subscribed.  If we do, we will
	// have an image placeholder (idTi1) and a hidden field that holds the tracer url (idTid1Url)
	// we can currently have a bunch of these
	for (var i = 0; i <= 20; i++) 
	{
		if ( (document.getElementById("idTi"+i)) && (document.getElementById("idTid"+i+"Url")) )
		{
			var urlTracker = document.getElementById("idTid"+i+"Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
			// retrieve the tracer image (so the recipient can bookkeep))
			document.getElementById("idTi"+i).src = urlTracker;
		}
	}

	// sometimes we use a tracer graphic to acknowledge a visitor just subscribed.  If we do, we will
	// have an image placeholder (idTi1) and a hidden field that holds the tracer url (idTid1Url)
/*
	if ( (document.getElementById("idTi1")) && (document.getElementById("idTid1Url")) )
	{
		var urlTracker1 = document.getElementById("idTid1Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi1").src = urlTracker1;
	}
	if ( (document.getElementById("idTi2")) && (document.getElementById("idTid2Url")) )
	{
		var urlTracker2 = document.getElementById("idTid2Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi2").src = urlTracker2;
	}
	if ( (document.getElementById("idTi3")) && (document.getElementById("idTid3Url")) )
	{
		var urlTracker3 = document.getElementById("idTid3Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi3").src = urlTracker3;
	}
	if ( (document.getElementById("idTi4")) && (document.getElementById("idTid4Url")) )
	{
		var urlTracker4 = document.getElementById("idTid4Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi4").src = urlTracker4;
	}
	if ( (document.getElementById("idTi5")) && (document.getElementById("idTid5Url")) )
	{
		var urlTracker5 = document.getElementById("idTid5Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi5").src = urlTracker5;
	}
	if ( (document.getElementById("idTi6")) && (document.getElementById("idTid6Url")) )
	{
		var urlTracker6 = document.getElementById("idTid6Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi6").src = urlTracker6;
	}
	if ( (document.getElementById("idTi7")) && (document.getElementById("idTid7Url")) )
	{
		var urlTracker7 = document.getElementById("idTid7Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi7").src = urlTracker7;
	}
	if ( (document.getElementById("idTi8")) && (document.getElementById("idTid8Url")) )
	{
		var urlTracker8 = document.getElementById("idTid8Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi8").src = urlTracker8;
	}
	if ( (document.getElementById("idTi9")) && (document.getElementById("idTid9Url")) )
	{
		var urlTracker9 = document.getElementById("idTid9Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi9").src = urlTracker9;
	}
	if ( (document.getElementById("idTi10")) && (document.getElementById("idTid10Url")) )
	{
		var urlTracker10 = document.getElementById("idTid10Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi10").src = urlTracker10;
	}
	if ( (document.getElementById("idTi11")) && (document.getElementById("idTid11Url")) )
	{
		var urlTracker11 = document.getElementById("idTid11Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi11").src = urlTracker11;
	}
	if ( (document.getElementById("idTi12")) && (document.getElementById("idTid12Url")) )
	{
		var urlTracker12 = document.getElementById("idTid12Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi12").src = urlTracker12;
	}
	if ( (document.getElementById("idTi13")) && (document.getElementById("idTid13Url")) )
	{
		var urlTracker13 = document.getElementById("idTid13Url").value.replace("emailAddress", document.getElementById("emailAddress2").value);
		// retrieve the tracer image (so the recipient can bookkeep))
		document.getElementById("idTi13").src = urlTracker13;
	}
*/
	
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clickes
//////////////////////////////////////////////////////////////////
function faceboxPlay()
{
	if (confirm("Do Facebox?")) 
	{ 
		jQuery.facebox({ div: '#subscribe' });
	}

	
}

//////////////////////////////////////////////////////////////////
// Submits the other vitals via a normal post so we can show  Thanks paage.
//////////////////////////////////////////////////////////////////
function submitViaThanksPost(inProcessingStage)
{ 
	document.getElementById('processingStage').value = inProcessingStage;
	document.getElementById('submit_popup').action = document.getElementById('subscribe_url2').value;
	document.getElementById('submit_popup').method = "post";
	document.getElementById('submit_popup').submit();
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clickes
//////////////////////////////////////////////////////////////////
function submitOtherVitals()
{
	$(document).trigger('close.facebox');
	
//	submitViaThanksPost(2);
//	return;
	
	submitViaAjax2Jquery();
	
	setTimeout("showThanks()",  500);
	
	// make sure the form itself does not perform a submit action
	return false;
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clickes
//////////////////////////////////////////////////////////////////
function showThanks()
{
	jQuery.facebox({ div: '#subscribe_thanks' });
}

//////////////////////////////////////////////////////////////////
// submitViaAjax - submits just the email address
//////////////////////////////////////////////////////////////////
function submitViaAjax2Jquery(fieldId)
{ 
	var url = document.getElementById("subscribe_url").value;
	
	var editions  = "";
	var separator = "";
	
	if (document.submit_popup.editionNames != null)
	{
		for (var i = 0; i < document.submit_popup.editionNames.length; i++) 
		{
			// if this one is selected
			if (document.submit_popup.editionNames[i].checked == true)
			{
				editions += separator + document.submit_popup.editionNames[i].value;
				separator = ",";
			}
		}
	}
	
	$.post(	url, 
			{ 
				'emailAddress'    		: document.getElementById('emailAddress2').value, 
				'age'    				: document.getElementById('age').value, 
				'name'    				: document.getElementById('name').value, 
				'zip'    				: document.getElementById('zip').value, 
				'howDidYouHearAboutUs'  : document.getElementById('howDidYouHearAboutUs').value, 
				'income'    			: document.getElementById('income').value, 
				'processingStage' 		: '2',
				'editionNamesComposite' : editions
			},
			function (data){ parseJsonResult2Jquery(data); }, 
			'json');
}

//////////////////////////////////////////////////////////////////
// Parse the JSON result into an object
//////////////////////////////////////////////////////////////////
function parseJsonResult2Jquery(commandBean)
{ 
}


///////////////////////////////////////////////////////////////////////
// This function will return the value assigned to the currently
// selected Radio button.  Unlike other form elements for which
// form.element.value returns the current value, radio button
// operate differently.  This function mimics the action that
// "should" happen if form.radioButtonName.value operated
// like all the others.
//////////////////////////////////////////////////////////////////////
function RadioButtonValue(RadioButtonName, defaultValue)
{ 
	// loop thru all possible radio button values
   	for (var i = 0; i < RadioButtonName.length; i++) 
	{
	   // if this one is selected
	   if (RadioButtonName[i].checked == true)
	   {
		// return its value
		return RadioButtonName[i].value;
	   }
	}
	// we should never get here since one radio button should always be selected
	return defaultValue;
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clickes
//////////////////////////////////////////////////////////////////
function submitToFriend()
{
	submitFriendViaAjaxJquery();
	
//	setTimeout("showThanks()",  500);
	
	$(document).trigger('close.facebox');
	
	// make sure the form itself does not perform a submit action
	return false;
}

//////////////////////////////////////////////////////////////////
// submitViaAjax - submits just the email address
//////////////////////////////////////////////////////////////////
function submitFriendViaAjaxJquery()
{ 
	var url = document.getElementById("send_friend_url").value;
	
	var numValid = 0;
	var friends  = "";
	var separator = "";
	
   	for (var i = 0; i < document.send_friend_popup.mail.length; i++) 
	{
		// if this one is selected
		if (isValidEmail(document.send_friend_popup.mail[i].value))
		{
			numValid++;
			friends += separator + document.send_friend_popup.mail[i].value;
			separator = ",";
		}
	}
	
	$.post(	url, 
			{ 
				'contentId'    			: document.getElementById('contentId').value, 
				'contentType'   		: document.getElementById('contentType').value, 
				'senderName'    		: document.getElementById('senderName').value, 
				'senderEmailAddress'    : document.getElementById('senderEmailAddress').value, 
				'emailAddressComposite' : friends, 
				'noteToRecipients'    	: document.getElementById('noteToRecipients').value
			},
			function (data){ parseJsonFriendResultJquery(data); }, 
			'json');
}

//////////////////////////////////////////////////////////////////
// Parse the JSON result into an object
//////////////////////////////////////////////////////////////////
function parseJsonFriendResultJquery(commandBean)
{ 
}

//////////////////////////////////////////////////////////////////
// Parse the JSON result into an object
//////////////////////////////////////////////////////////////////
function addBackTo_input_js()
{ 
	var inputs = document.getElementsByTagName("textarea");
	for (var i=0; i<inputs.length; i++)
	{
		inputs[i].onfocus = function ()
		{		
			this.value = "";
		}
		inputs[i].onblur = function ()
		{
		}
	}
	
	// ALSO: remove zip code shit from input.js
	// ALSO: remove zip code shit from input.js
	// ALSO: remove zip code shit from input.js
	// ALSO: remove zip code shit from input.js
}
	
//////////////////////////////////////////////////////////////////////
// This function will determine the number of checkboxes with their
// value checked.
//////////////////////////////////////////////////////////////////////
function NumberOfBoxesChecked(CheckboxObject)
{ 
	var NumChecked = 0;

	// loop thru all options
   	for (var i = 0; i < CheckboxObject.length; i++) 
	{
	   // if this option is checked
	   if (CheckboxObject[i].checked)
	   {
		NumChecked++;
	   }
	}
	return NumChecked;
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clicks
//////////////////////////////////////////////////////////////////
function unsubscribeValid(inForm)
{
	if (!inForm.editionNames.length)
	{
		if (!inForm.editionNames.checked)
		{
			alert ('please select the edition from which you want to unsubscribe');
			return false;
		}
	}
	else
	{
		if (NumberOfBoxesChecked(inForm.editionNames) == 0)
		{
			alert ('please select the edition(s) from which you want to unsubscribe');
			return false;
		}
	}
	
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clicks
//////////////////////////////////////////////////////////////////
function unsubscribeAll(inForm)
{
	if (inForm.editionNames.length)
	{
		// loop thru all options
		for (var i = 0; i < inForm.editionNames.length; i++) 
		{
		   inForm.editionNames[i].checked = true;
		}
	}
	else
	{
		if (inForm.editionNames)
		{
			// check the lone edition
		   inForm.editionNames.checked = true;
		}
	}
	
	inForm.submit();
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clicks
//////////////////////////////////////////////////////////////////
function signinValid(inForm)
{
	inForm.emailAddress.value = trim(inForm.emailAddress.value);
	
	if (!isValidEmail(inForm.emailAddress.value))
	{
		alert ('a valid email address is required.');
		return false;
	}
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clicks
//////////////////////////////////////////////////////////////////
function friendFormValid(inForm)
{
	if (isBlank(inForm.senderName.value))
	{
		alert ('Please enter your name.');
		return false;
	}
	
	inForm.senderEmailAddress.value = trim(inForm.senderEmailAddress.value);
	
	if (!isValidEmail(inForm.senderEmailAddress.value))
	{
		alert (inForm.senderEmailAddress.value + ' is not a valid email address.');
		return false;
	}
			
	var atLeastOneEmail = false;
	
	// loop thru all emails
   	for (var i = 0; i < inForm.emailAddresses.length; i++) 
	{
		if (inForm.emailAddresses[i].value.length > 3)
		{
			if (!isValidEmail(inForm.emailAddresses[i].value))
			{
				alert (inForm.emailAddresses[i].value + ' is not a valid email address.');
				return false;
			}
			else
			{
				atLeastOneEmail = true;
			}
		}
	}
	
	if (!atLeastOneEmail)
	{
		alert ('enter at least one email address.');
		return false;
	}
			
	// if we get here, all is well
	
	return true;
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clicks
//////////////////////////////////////////////////////////////////
function inviteFriendFormValid(inForm)
{
	inForm.senderEmailAddress.value = trim(inForm.senderEmailAddress.value);
	
	if (isBlank(inForm.senderName.value))
	{
		alert ('Please enter your name.');
		return false;
	}
	
	if (!isValidEmail(inForm.senderEmailAddress.value))
	{
		alert (inForm.senderEmailAddress.value + ' is not a valid email address.');
		return false;
	}
			
	var atLeastOneEmail = false;
	
	// loop thru all emails
   	for (var i = 0; i < inForm.emailAddresses.length; i++) 
	{
		if (inForm.emailAddresses[i].value.length > 3)
		{
			if (!isValidEmail(inForm.emailAddresses[i].value))
			{
				alert (inForm.emailAddresses[i].value + ' is not a valid email address.');
				return false;
			}
			else
			{
				atLeastOneEmail = true;
			}
		}
	}
	
	if (!atLeastOneEmail)
	{
		alert ('enter at least one email address.');
		return false;
	}
			
	// if we get here, all is well
	
	return true;
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clicks
//////////////////////////////////////////////////////////////////
function askVjd(inForm)
{
	inForm.emailAddress.value = trim(inForm.emailAddress.value);
	
	if (!isValidEmail(inForm.emailAddress.value))
	{
		alert ('a valid email address is required.');
		return;
	}
	
	if (inForm.messageBody.value.length < 3)
	{
		alert ('please enter a question');
		return;
	}
	
	submitQuestionViaAjaxJquery(inForm);
}

//////////////////////////////////////////////////////////////////
// submitViaAjax - submits just the email address
//////////////////////////////////////////////////////////////////
function submitQuestionViaAjaxJquery(inForm)
{ 
	var url = document.getElementById('ask_vjd_url').value;
	
	$.post(	url, 
			{ 
				'emailAddress'   	: inForm.emailAddress.value, 
				'messageBody'       : inForm.messageBody.value, 
				'ajaxBehavior'		: 'true',
				'subject'   		: 'Ask VJD',
				'pageName'   		: 'Ask VJD'
			},
			function (data){ parseJsonResultAskVjdJquery(data); }, 
			'json');
			
	inForm.emailAddress.value = "";
	inForm.messageBody.value = "";
	
	HideElement('ask_vjd_button');
	ShowElement('ask_vjd_feedback');
//	document.getElementById('ask_vjd_feedback').class = 'green-box2';
}

//////////////////////////////////////////////////////////////////
// Parse the JSON result into an object
//////////////////////////////////////////////////////////////////
function parseJsonResultAskVjdJquery(commandBean)
{ 
}

//////////////////////////////////////////////////////////////////
// Determines if the input string contains only whitespace.
//////////////////////////////////////////////////////////////////
function isBlank(s) {
   if (s == "") return true
   for (var i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if ((c != " ") && (c != '\n') && (c != '\t')) return false;
   }
   return true; 
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clicks
//////////////////////////////////////////////////////////////////
function subscribeValid(inForm)
{
	inForm.emailAddress.value = trim(inForm.emailAddress.value);
	
	if (!isValidEmail(inForm.emailAddress.value))
	{
		alert ('a valid email address is required.');
//		return false;
		return;
	}
	
	if (NumberOfBoxesChecked(inForm.editionNames) == 0)
	{
		alert ('please select at least 1 edition');
//		return false;
		return;
	}
	
	subscribeViaAjaxJquery();
	
//	return false;
	return;
}

//////////////////////////////////////////////////////////////////
// submitViaAjax - submits just the email address
//////////////////////////////////////////////////////////////////
function subscribeViaAjaxJquery()
{ 
	// hang onto the email address they entered so it can be re-submitted with the additional information
	document.getElementById("emailAddress3").value = document.subscribeForm.emailAddress.value;
	if (document.getElementById("emailAddress2"))
	{
		document.getElementById("emailAddress2").value = document.subscribeForm.emailAddress.value;
	}
	
	var url = document.getElementById("subscribe_url2").value;
	
	var referrer  = "NONE";
	if (document.getElementById("referrer"))
	{
		referrer = document.getElementById("referrer").value;
	}
	var editions  = "";
	var separator = "";
	
   	for (var i = 0; i < document.subscribeForm.editionNames.length; i++) 
	{
		// if this one is selected
		if (document.subscribeForm.editionNames[i].checked == true)
		{
			editions += separator + document.subscribeForm.editionNames[i].value;
			separator = ",";
		}
	}
	
	$.post(	url, 
			{ 
				'emailAddress'    		: document.subscribeForm.emailAddress.value, 
				'processingStage' 		: '1',
				'referrer' 				: referrer,
				'editionNamesComposite' : editions
			},
			function (data){ parseJsonResultJquery(data); }, 
			'json');
			
	jQuery.facebox({ div: '#subscribe2' });
	
}

//////////////////////////////////////////////////////////////////
// Parse the JSON result into an object
//////////////////////////////////////////////////////////////////
function parseJsonResult3Jquery(commandBean)
{ 
}

//////////////////////////////////////////////////////////////////
// Called when the submit button is clickes
//////////////////////////////////////////////////////////////////
function changeEmailAddress(visitorId,fieldId,targetUrl)
{
	document.getElementById(fieldId).value = trim(document.getElementById(fieldId).value);
	if (!isValidEmail(document.getElementById(fieldId).value))
	{
		alert ('a valid email address is required.');
		return false;
	}
	
	submitChangeEmailAddressViaAjaxJquery(visitorId,fieldId,targetUrl);
	return false;
}

//////////////////////////////////////////////////////////////////
// submitViaAjax - submits just the email address
//////////////////////////////////////////////////////////////////
function submitChangeEmailAddressViaAjaxJquery(visitorId, fieldId, targetUrl)
{ 
	HideElement('submitEmailChange');
	$.post(	targetUrl, 
			{ 
				'subscriberId'  			: visitorId, 
				'unsubscribeEmailAddress'  	: document.getElementById(fieldId).value, 
				'ajaxBehavior'				: 'true',
				'emailAddressChangeOnly'	: 'true'
			},
			function (data){ parseJsonResultEmailAddressChangeJquery(data); }, 
			'json');
}

//////////////////////////////////////////////////////////////////
// This method is called after several different subscribe actions.
// For example, the home page subscribe (without edition selection) and
// the subscribe page action (with editions) both land here for common post processing.
//////////////////////////////////////////////////////////////////
function parseJsonResultEmailAddressChangeJquery(commandBean)
{ 
	if (commandBean.unsubscribeStatus == 'successful')
	{
		document.getElementById('feedback').innerHTML = 'Your email address has been changed';
	}
	else
	{
		document.getElementById('feedback').innerHTML = 'We were unable to change your email address';
	}
}

