/*  registration.js
	Javascript code for SFMidsingles conference 2007
	Updated for 2010 - 6/13/08
	Page code
	Validation code
	*/
	
var currentPage = 1;
var maxPages	= 2;

// On page load, should always start at page 1
// But since browsers often keep form info on reload, we'll try validating if the anchor exists
var subpage = window.location.hash;

function pageLoad() 
{
	if(subpage.indexOf("#R") > -1)
	{
		var hashPage = parseInt(subpage.substring(2));
		if(!isNaN(hashPage) && hashPage > 0 && hashPage <= maxPages)
		{
			for(var i = 1; i < hashPage; i++)
			{
				if(validateForm(i) == false)
				{
					currentPage = i;
					setPage(currentPage);
					window.location.hash = "#R" + currentPage;
					setInterval("hashCheck();", 500); // four times a second
					return;
				}
			}
			currentPage = hashPage;
		}
		else
		{
			currentPage = 1;
		}
		setPage(currentPage);
		window.location.hash = "#R" + currentPage;
	}
	else
	{
		if(subpage.length > 0)
		{
			window.location.hash = "#R1";
		}
		setPage(currentPage);
	}
	setInterval("hashCheck();", 500); // four times a second
}

window.onload = pageLoad;

function hashCheck()
{
	if(window.location.hash != subpage)
	{
		subpage = window.location.hash;
		if(subpage.indexOf("#R") > -1)
		{
			var hashPage = parseInt(subpage.substring(2));
			if(!isNaN(hashPage) && hashPage > 0 && hashPage <= maxPages)
			{
				for(var i = 1; i < hashPage; i++)
				{
					if(validateForm(i) == false)
					{
						currentPage = i;
						setPage(currentPage);
						window.location.hash = "#R" + currentPage;
						return;
					}
				}
				currentPage = hashPage;
			}
			else
			{
				currentPage = 1;
			}
			setPage(currentPage);
			window.location.hash = "#R" + currentPage;
		}
		else
		{
			if(subpage.length > 0)
			{
				window.location.hash = "#R1";
			}
			setPage(currentPage);
		}
	}
}


function setPage(newPage)
{
	if(newPage > 0 && newPage <= maxPages)
	{
		for(var i = 1; i <= maxPages; i++)
		{
			var curPage = document.getElementById("Page" + i);
			if(i == newPage)
			{
				curPage.style.display = "block";
			}
			else
			{
				curPage.style.display = "none";
			}
		}
	}
}

function buttonContinue()
{
	if(currentPage < maxPages)
	{
		if(validateForm(currentPage) == true)
		{
		
			var lastPageEl = document.getElementById("Page" + currentPage);
			var nextPageEl = document.getElementById("Page" + (currentPage + 1));
			if(lastPageEl && nextPageEl)
			{
				lastPageEl.style.display = "none";
				nextPageEl.style.display = "block";
				currentPage = (currentPage + 1);
				window.onbeforeunload = confirmClose;
				window.location = "#R" + currentPage;
			}
			else
			{
				alert("Error while registering. If this continues, contact support@sfmidsingles.com.\n(Continue: Page Element(s) Not Found)");
			}
		}
	}
}

function buttonGoBack()
{
	if(currentPage > 1)
	{
		if(true) // validate not needed
		{
		
			var lastPageEl = document.getElementById("Page" + currentPage);
			var nextPageEl = document.getElementById("Page" + (currentPage-1));
			if(lastPageEl && nextPageEl)
			{
				lastPageEl.style.display = "none";
				nextPageEl.style.display = "block";
				currentPage = (currentPage-1);
				if(currentPage == 1)
				{
					window.onbeforeunload = null;
				}
				window.location = "#R" + currentPage;
			}
			else
			{
				alert("Error while registering. If this continues, contact support@sfmidsingles.com.\n(Go Back: Page Element(s) Not Found)");
			}
		}
	}
}

function buttonSubmitRegistration()
{
	if(validateForm(currentPage))
	{
		//alertBox("Submit currently disabled.");
		//return;
		window.onbeforeunload = null;
		document.RegForm.submit();
	}
	//alert(validateForm(currentPage)==true);
}

function validateForm(formPage)
{
	formValid = true;
	
	if(formPage == 1)
	{
		if(formValid && document.RegForm.FirstName.value.length < 2)
		{
			formValid = false;
			alertBox("Please enter your first name.");
		}
		if(formValid && document.RegForm.LastName.value.length < 2)
		{
			formValid = false;
			alertBox("Please enter your last name.");
		}
		if(formValid && document.RegForm.BirthMonth.selectedIndex < 1)
		{
			formValid = false;
			alertBox("Please enter your birthday (*Month*, Day, Year).");
		}
		if(formValid && (isNaN(parseInt(document.RegForm.BirthDay.value)) || parseInt(document.RegForm.BirthDay.value) < 1 || parseInt(document.RegForm.BirthDay.value) > 31))
		{
			formValid = false;
			alertBox("Please enter your birthday (Month, *Day*, Year).");
		}
		if(formValid && (isNaN(parseInt(document.RegForm.BirthYear.value)) || parseInt(document.RegForm.BirthYear.value) < 1900 || parseInt(document.RegForm.BirthYear.value) > 2000))
		{
			formValid = false;
			alertBox("Please enter your birthday (Month, Day, *Year*).");
		}
		// AGE CHECK
		var enteredAge = new Date();
		enteredAge.setFullYear(parseInt(document.RegForm.BirthYear.value));
		enteredAge.setMonth(parseInt(document.RegForm.BirthMonth.options[document.RegForm.BirthMonth.selectedIndex].value)-1);
		enteredAge.setDate(parseInt(document.RegForm.BirthDay.value));
		enteredAge.setHours(0,0,0);
		// Upper and Lower Age Limits - compared to September 10, 2010
		var upperAge = new Date((2010 - 46),(9 - 1), 10);
		var lowerAge = new Date((2010 - 25),(9 - 1), 10);  // Official limit is 27, but setting to 25 to accommodate Stanford 2nd ward
		if(enteredAge.getTime() > lowerAge.getTime())
		{
			formValid = false;
			alertBox("You must be at least 27 years old by September 10, 2010 in order to attend.\nRemember that ID's will be checked!");
		}
		if(enteredAge.getTime() < upperAge.getTime())
		{
			formValid = false;
			alertBox("You must be no older than 45 years of age on September 10, 2010 in order to attend.\nRemember that ID's will be checked!");
		}
		//alert(enteredAge + "\n" + upperAge + "\n" + lowerAge);
		//alert(enteredAge.getTime() + "\n" + upperAge.getTime() + "\n" + lowerAge.getTime());
		
		if(formValid && !(document.RegForm.Gender[0].checked || document.RegForm.Gender[1].checked))
		{
			formValid = false;
			alertBox("Please select your gender.");
		}
		if(formValid && document.RegForm.City.value.length < 2)
		{
			formValid = false;
			alertBox("Please enter your city and select your state.");
		}
		// Check for non-US
		var USAregex = /^[\s.-]*U[\s.-]*S[\s.-]*A?[\s.-]*$|^\s*UNITED\s*STATES?\b\s*/i;
		if(formValid && (document.RegForm.State.selectedIndex < 0 || document.RegForm.State.options[document.RegForm.State.selectedIndex].value == "ZZ"))
		{
			// Check if USA in country field (USA, US, United State(s))
			//alert(document.RegForm.Country.value.match(/^[\s.-]*U[\s.-]*S[\s.-]*A?[\s.-]*$|^\s*UNITED\s*STATES?\b\s*/i));
			if(document.RegForm.Country.value.match(USAregex))
			{
				formValid = false;
				alertBox("Please select your state.\nIf you are outside the United States, enter your country.");
			}
		}
		// Check for US-state with non-US country 
		if(formValid && !document.RegForm.Country.value.match(USAregex))
		{
			if(document.RegForm.State.selectedIndex > -1 && document.RegForm.State.options[document.RegForm.State.selectedIndex].value != "ZZ")
			{
				formValid = false;
				alertBox("If you are outside the United States, please select 'Non-US State' at end of State list.\nOtherwise, enter 'United States' in the Country field.");
			}
		}
		if(formValid && document.RegForm.Country.value.length < 1)
		{
			formValid = false;
			alertBox("Please enter your country.");
		}
		
		if(formValid && !IsEmailValid("RegForm","Email"))
		{
			formValid = false;
			alertBox("Important! Please enter a valid email!");
		}
		if(formValid && document.RegForm.Email.value != document.RegForm.Email2.value)
		{
			formValid = false;
			alertBox("Please re-check your email. Email entries do not match.");
		}
		// Add email to third page
		document.getElementById("showEmail").innerHTML = document.RegForm.Email.value;
		
		// PHONE (optional)
		if(formValid && document.RegForm.Phone.value.length > 0 && document.RegForm.Phone.value.length < 7)
		{
			formValid = false;
			alertBox("Please enter a valid phone number.");
		}
		
		
	}
	else if(formPage == 2)
	{
		// verify checkbox
		if(document.RegForm.AcceptDisclaimer.checked == false)
		{
			formValid = false;
			alertBox("Please read the Terms, Conditions and Release of Liability and check the acceptance box at the end.");
		}
	}
	return formValid;
}


// Make DHTML?

function alertBox(msg)
{
	alert(msg);
}

function confirmClose()
{
//		event.returnValue = "Your registration data will be lost if you press Okay.\nTo edit your registration, cancel this and use the \"Go Back\" button.";
		return "Your registration data will be lost if you press \"OK\".\nTo edit your registration, cancel this and use the \"Go Back\" button.";
}

// -----------------------------------------------------------------
// Function    : IsEmailValid
// Language    : JavaScript
// Description : Checks if given email address is of valid syntax
// Copyright   : (c) 1998 Shawn Dorman
// http://www.goodnet.com/~sdorman/web/IsEmailValid.html
// -----------------------------------------------------------------
// Ver    Date    Description of modification
// --- ---------- --------------------------------------------------
// 1.0 09/04/1996 Original write
// 1.1 09/30/1998 CHG: Use standard header format
// -----------------------------------------------------------------
// Source: Webmonkey Code Library
// (http://www.hotwired.com/webmonkey/javascript/code_library/)
// -----------------------------------------------------------------

function IsEmailValid(FormName,ElemName)
{
var EmailOk  = true
var Temp     = document.forms[FormName].elements[ElemName]
var AtSym    = Temp.value.indexOf('@')
var Period   = Temp.value.lastIndexOf('.')
var Space    = Temp.value.indexOf(' ')
var Length   = Temp.value.length - 1   // Array is from 0 to length-1

if ((AtSym < 1) ||                     // '@' cannot be in first position
    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    (Period == Length ) ||             // Must be atleast one valid char after '.'
    (Space  != -1))                    // No empty spaces permitted
   {  
      EmailOk = false
//      alert('Please enter a valid e-mail address!')
//      Temp.focus()
   }
return EmailOk
}
