function accessEdition(path,navSection,edition) {
	var 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");
	for (var i=0; i<inputs.length; i++)
	{
		if (inputs[i].type == "text" && (inputs[i].name == "emailAddress"))
		{
		
			inputs[i].onfocus = function ()
			{
				if (this.value == "email address")
					this.value = "";
			}
			inputs[i].onblur = function ()
			{
				if (this.value == "" && this.name == "emailAddress") this.value = "email address";
			}
		}
		if (inputs[i].type == "text" && (inputs[i].name == "zip"))
		{
			inputs[i].onfocus = function ()
			{
				if (this.value == "zip code")
					this.value = "";
			}
			inputs[i].onblur = function ()
			{
				if (this.value == "" && this.name == "zip") 
				{ this.value = "zip code"; } 
				else { 
					//checkForInvalidZip(this); 
				}
			}
		}
	}
}

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;
	if (value.length!=5 && value.length!=10) {
		errorText += "The zip code is invalid. Please re-enter it.\n";
		return false;
	}
	for (var 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 (var 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.';
		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.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.method = "post";
	
	inForm.submit();
}

function submitSignupFollowup(inForm)
{
	if (document.getElementById("specialsEditionField") != null) {
	 	document.getElementById("processingStage").value=3;
	}
	inForm.action = $('thanks_url').value;
	inForm.submit();
}

if (window.addEventListener)
	window.addEventListener("load", initSignupPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initSignupPage);

