function accessEdition(path,navSection,edition) {
	path = "";
	if (navSection == '') {
		path = path + "/" + edition + "/index.htm";
	} else {
		path = path + "/entry_by_section/" + edition + "/" + navSection;
	}
	window.location.href=path;
}

function initSignupInputs()
{
	var inputs = document.getElementsByTagName("input");
	var i=0;
	var clearEmailAddressFieldHandler = function () { if (this.value == "email address") { this.value = ""; } };
	var labelEmailAddressFieldHandler = function () { if (this.value == "" && this.name == "emailAddress") { this.value = "email address"; } };
	var clearZipCodeFieldHandler = function () { if (this.value == "zip code") { this.value = ""; } };
	var labelZipCodeFieldHandler = function () { if (this.value == "" && this.name == "zip") { this.value = "zip code"; } };
	for (i=0; i<inputs.length; i++)
	{
		if (inputs[i].type == "text" && (inputs[i].name == "emailAddress"))
		{		
			inputs[i].onfocus = clearEmailAddressFieldHandler;
			inputs[i].onblur = labelEmailAddressFieldHandler;
		}
		if (inputs[i].type == "text" && (inputs[i].name == "zip"))
		{
			inputs[i].onfocus = clearZipCodeFieldHandler;
			inputs[i].onblur = labelZipCodeFieldHandler;
		}
	}
}

var errorText = "";

var badPostalCodes = ["00000","11111","33333","66666","77777","88888","99999","01234","98765","34567","24680","13579"];
						
function validateZip(field) {
	var valid = "0123456789";
	var hyphencount = 0;
	var value = field.value;
	var i=0;
	if (value.length!=5 && value.length!=10) {
		errorText += "The zip code is invalid. Please re-enter it.\n";
		return false;
	}
	for (i=0; i < value.length; i++) {
		temp = value.substring(i, i+1);
		if (temp == "-") {hyphencount++;}
		if (valid.indexOf(temp) == "-1") {
			errorText += "There are invalid characters in your zip code.";
			return false;
		}
	for (i=0; i < badPostalCodes.length; i++) {
	    if (value == badPostalCodes[i]) {
			errorText += "'" + value + "' is an invalid zip code.";
			return false;
		}
	}
	if ((hyphencount > 1) || ((value.length==10) && (value.charAt(5)!="-"))) {
		errorText += "The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.\n";
		return false;
	   }
	}
	return true;
}

function checkForInvalidZip(zipField) {
  errorText = "";
  if (! validateZip(zipField)) {
    alert("Error: '" + errorText);
  }
}

function initSignupPage ()
{
	 initSignupInputs();
}

function validateEmailAndZipForm(inForm,numInvites)
{ 
	var valid = true;
		
	errorText = "";
	
	if (! inForm.policy.checked)
	{
		errorText += 'Please indicate you accept the terms and privacy statement by checking the box near the Subscribe button';
		return;
	}
	document.getElementById("emailAddress").value = trim(document.getElementById("emailAddress").value);
	if (!isValidEmail(document.getElementById("emailAddress").value))
	{
		errorText += 'a valid email address is required.';
		if (correctedEmailAddress)
		{
			if (correctedEmailAddress != '')
			{
				errorText += '\n Did you mean: '+correctedEmailAddress;
			}
		}
		return;
	}	
	document.getElementById("zip").value = trim(document.getElementById("zip").value);
//	if (!validateZip(document.getElementById("zip")))
//	{
//		errorText += 'a valid zip code is required.';
//		return;
//	}	
	
	if (errorText != '')
	{
		valid = false;
	}

	return valid;
}

function validateEmailForm(inForm,numInvites)
{ 
	var valid = true;
		
	errorText = "";
	
	if (! inForm.policy.checked)
	{
		errorText += 'Please indicate you accept the terms and privacy statement by checking the box near the Subscribe button';
		return;
	}
	document.getElementById("emailAddress").value = trim(document.getElementById("emailAddress").value);
	if (!isValidEmail(document.getElementById("emailAddress").value))
	{
		errorText += 'a valid email address is required.';
		return;
	}	
	
	if (errorText != '')
	{
		valid = false;
	}

	return valid;
}

function submitSpecialsEmail(inForm)
{
	errorText = '';
	
	if ( !validateEmailForm(inForm) )
	{
		// slap them around...
		alert(errorText+ '\n');
		return;
	}
	
//	inForm.action = $('followup_url').value;
	inForm.action = document.getElementById('followup_url').value;
//	inForm.method = "post";
	
	inForm.submit();
}

function toggleSupplementSelection(inputField)
{
	var field = document.getElementById("supplement01");
	if (inputField.checked) {
	 field.value=1;
	 } else {
	 field.value=0;
	}
}

function processSpecialsEditionClick()
{
	var nyField = document.getElementById("new-york");
	var specialsField = document.getElementById("specialsEditionField");
	if (specialsField.checked && (! nyField.checked)) { alert("Error: To Subscribe to \"New York Specials\" you must have the \"New York\" edition checked"); nyField.checked = true;}
}

function disableSubscribeButton() {
	element = document.getElementById('btn-subscribe');
	if (element)
	{
		element.href = 'javascript:none';
		element.innerHTML='<blink><i><b>PROCESSING...</b></i></blink>';
		element.className='processing-subscribe ';
	}
}


function submitSignupEmailAndZip(inForm)
{
	
	errorText = '';
	
	if ( !validateEmailAndZipForm(inForm) )
	{
		// slap them around...
		alert(errorText+ '\n');
		return;
	}
	
	// disable the button immediately to prevent double clicks
	disableSubscribeButton();
	
//	inForm.action = $('followup_url').value;
	inForm.action = document.getElementById('followup_url').value;
//	inForm.method = "post";
	
	inForm.submit();
}

function submitSignupCoregForm(inForm)
{
	
	var valid = true;
	errorText = '';
//Want the little red error messages to pop up if something is false, but then go away if its true. Only submits once all valid.
	if(document.getElementById("firstName").value.length==0)
	{
		errorText += 'a valid first name is required.';
		//alert(errorText+ '\n');
		document.getElementById('errorFirstName').style.display = 'block';
		valid = false;
		//return;
	}
	else{document.getElementById('errorFirstName').style.display = 'none';}
	if(document.getElementById("lastName").value.length==0)
	{
		errorText += 'a valid last name is required.';
		//alert(errorText+ '\n');
		document.getElementById('errorLastName').style.display = 'block';
		valid = false;
		//return;
	}
	else{document.getElementById('errorLastName').style.display = 'none';}
	if (!isValidEmail(document.getElementById("emailAddress").value))
	{
		errorText += 'a valid email address is required.';
		//alert(errorText+ '\n');
		document.getElementById('errorEmail').style.display = 'block';
		valid = false;
		//return;
	}
	else{document.getElementById('errorEmail').style.display = 'none';}
	if(document.getElementById("birthYear").value.length!=4)
	{
		errorText += 'a valid year is required.';
		//alert(errorText+ '\n');
		document.getElementById('errorDob').style.display = 'block';
		valid = false;
		//return;
	}
	else{document.getElementById('errorDob').style.display = 'none';}
	if (document.getElementById("zip").value.length!=5)
	{
		errorText += 'a valid zip code is required.';
		//alert(errorText+ '\n');
		document.getElementById('errorZip').style.display = 'block';
		valid = false;
		//return;
	}
	else{document.getElementById('errorZip').style.display = 'none';}
	if (document.getElementById("phone").value.length < 7)
	{
		errorText += 'a valid phone number is required.';
		//alert(errorText+ '\n');
		document.getElementById('errorPhoneNumber').style.display = 'block';
		valid = false;
		//return;
	}
	else{document.getElementById('errorPhoneNumber').style.display = 'none';}
	//If not valid, return before submit happens
	if(valid==false)
	{	
		return;
	}
	//if (!validateZip(document.getElementById("zip").value))
	//{
	//	alert(errorText+ '\n');
	//	return;
	//}

	
	
	// disable the button immediately to prevent double clicks
	element = document.getElementById('btn-subscribe').style.display='none';
	element.innerHTML='<blink><i><b>PROCESSING...</b></i></blink>';
	element.className='processing-subscribe ';
	
//	inForm.action = $('followup_url').value;
	inForm.action = document.getElementById('followup_url').value;
//	inForm.method = "post";
	
	inForm.submit();
}

function submitSignupFollowup(inForm)
{
	if (document.getElementById("specialsEditionField") != null) {
		document.getElementById("processingStage").value=3;
	}
//	inForm.action = $('thanks_url').value;
	inForm.action = document.getElementById('thanks_url').value;
	inForm.submit();
}

if (window.addEventListener) {
	window.addEventListener("load", initSignupPage, false);
	}
else if (window.attachEvent) {
	window.attachEvent("onload", initSignupPage);
	}

