window.onload = function()
{
	toolsOnLoad();
	pageControlsOnLoad();
	
	if (self['contactUsOnLoad'])
		contactUsOnLoad();
	
	if (self['feedbackOnLoad'])
		feedbackOnLoad();
	
	// show / hide javascript help messages, and sign in box
	
	if (document.getElementById('container-no-js'))
		document.getElementById('container-no-js').style.display = 'none';
	
	if (document.getElementById('container-sign-in'))
		document.getElementById('container-sign-in').style.display = 'block';

	if (document.getElementById('container-signed-in'))
		document.getElementById('container-signed-in').style.display = 'block';

	if (document.getElementById('container-member-tips'))
		document.getElementById('container-member-tips').style.display = 'block';

	if (document.getElementById('container-poll'))
		document.getElementById('container-poll').style.display = 'block';

	if (document.getElementById('signin'))
	{
		document.getElementById('signin').onsubmit = signinOnSubmit;
	}
		
}

function signinOnSubmit()
{
	var errors = new Array();
	var fields = new Array();
	
	if (document.getElementById('email').value.trim() == '')
	{
		errors[errors.length] = 'your email address';
		fields[fields.length] = document.getElementById('email');
	}
	else
	{
		//if (!document.getElementById('email').value.isEmailAddress())
		if (!IIsValidEmail(document.getElementById('email').value))
		{
			errors[errors.length] = 'a VALID email address';
			fields[fields.length] = document.getElementById('email');
		}
	}
	
	if (document.getElementById('password').value.trim() == '')
	{
		errors[errors.length] = 'your password';
		fields[fields.length] = document.getElementById('password');
	}
	
	if (errors.length > 0)
	{
		alert("Sorry, you need to provide more information. Please enter:\n - " + errors.join("\n - "));
		
		fields[0].focus();
		
		if (!fields[0].options)
			fields[0].select();
		
		return false;
	}
	
	
	showSigninStatus();
	
	return true;
}

function showSigninStatus()
{
	if (document.getElementById('container-sign-in-controls'))
	{
		document.getElementById('container-sign-in-controls').style.display = 'none';
		
		if (document.getElementById('container-sign-in-status'))
		{
			document.getElementById('container-sign-in-status').innerHTML = 'Please Wait...';
			document.getElementById('container-sign-in-status').style.display = 'block';
		
		}
	}
}

function IIsValidEmail(email) {
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
