/*************************************************************************
 *
 * (C) Copyright 2003, 2004, 2005 Daxal Communications.
 * All rights reserved.
 *
 * javascript functions for our webmail form.
 * origional design submitted to James Walker for contract work.
 *
 * 2004-01-03 Fixed false positive against autofillers with javascript enabled.
 *	javascript runs first, autofiller runs next, user clicks webmail, [alert].
 *  This happens because we assume an event will occur to clear the isAutoFilled
 *  flag.
 *
 */


var strFillAddress = "Email@surfthe.us";
var strClickEmail = "Click in this field to type your email address for webmail.";
var strTypeEmail = "Type your email address in full in here, to check your email.  For example, jamesanderson@surfthe.us";
var strClickPassword = "Click in this field to enter your password for webmail.";
var strTypePassword = "Your password is case sensitive.  If your password was not all caps, make sure the caps lock is OFF.";
var strClickLoginNeedPassword = "Type your email address and password to login."
var strClickLogin = "Now that you've typed your email address and password, click Login to read your email."
var strBadLogin = "You must use an email address and password to check email.  Please try again.";

var strSavedStatus = "";
var isAnyFocused = false;
var webmailform;


function prevalidate(f)
{
/* 2005-01-03 scotte
   fixed false postive when firefox autofills this.  Our event handler doesn't invoke, so it assumed
   no input was available.
*/
	if (!webmailform) { return(true); }
	if ((webmailform.password.value=="") ||
		((webmailform.username.isAutoFilled==true) && (webmailform.username.value == strFillAddress)) ) {
		setStatus(strBadLogin);
		alert(strBadLogin);
		return (false);
	}
	return (true);
}

function eventMouseOver(id)
{
 if (id.name == "username")
 {
   window.status = ((id.isFocused) ? strTypeEmail : strClickEmail);
 }
 else if (id.name == "password")
 {
   window.status = ((id.isFocused) ? strTypePassword : strClickPassword);
 }
 else if (id.name == "do.login")
 {
   if (!webmailform) { return(false); }
   if ((webmailform.username.value == "") || ((webmailform.username.value != strFillAddress) && (webmailform.username.dg)) || (webmailform.password.value == ""))
   {
     window.status = (strClickLoginNeedPassword);
   }
   else
   {
     window.status = (strClickLogin);
   }
 }
}

function eventMouseOut(id)
{
 if ((id.name == "username") || (id.name == "password") || (id.name == "do.login"))
 {
   if (strSavedStatus == "")
   {
     window.status = ("");
   }
   else
   {
     restoreStatus();
   }
 }
}

function eventFocus(id)
{
 setFocused(id);
 if (id.name == "username")
 {
   if ((id.value == strFillAddress) && (id.isAutoFilled))
   {
     id.value = "";
   }
   else
   {
     id.isAutoFilled = false;
   }
   setStatus(strTypeEmail);
 }
 else if (id.name == "password")
 {
   setStatus(strTypePassword);
 }
}

function eventBlur(id)
{
 clearFocused(id);
 if (id.name == "username")
 {
   if (id.value == "")
   {
     fillUsername(id);
   }
   else
   {
     id.isAutoFilled = false;
   }
   clearStatus();
 }
}

function setFocused(id)
{
 id.isFocused = isAnyFocused = true;
}

function clearFocused(id)
{
 id.isFocused = isAnyFocused = false;
}

function isFocused(id)
{
 if ((id.isFocused) || (isAnyFocused))
 {
   return true;
 }
 else
 {
   id.isFocused = isAnyFocused = false;
   return false;
 }
}

function fillUsername(id)
{
 id.isAutoFilled = true;
 id.value = strFillAddress;
}

function setStatus(strText)
{
 if (strSavedStatus != strText)
 {
   strSavedStatus = strText;
 }
 window.status = strText;
}

function restoreStatus()
{
 window.status = strSavedStatus;
}

function clearStatus()
{
 strSavedStatus = "";
 window.status = "";
}

function dispatchEvent(event, element)
{

	if (!event) return(false);
	if (!element) return(false);

	if (event == 'onmouseover') {
		eventMouseOver(element);
		return;
	}
	if (event == 'onmouseout') {
		eventMouseOut(element);
		return;
	}
	if (event == 'onblur') {
		eventBlur(element);
		return;
	}
	if (event == 'onfocus') {
		eventFocus(element);
		return;
	}
}

function initObjectsToFix()
{
	var e;
	webmailform = document.getElementById('form.webmail');
	if (!webmailform) { return(false) }

	e = document.getElementById('webmail.username');
	if (e) {
		e.onmouseover	=	dispatchEvent('onmouseover',this);
		e.onmouseout	=	dispatchEvent('onmouseoout',this);
		e.onblur		=	dispatchEvent('onblur',this);
		e.onfocus		=	dispatchEvent('onfocus',this);
		fillUsername(e);
	}

	e = document.getElementById('webmail.password');
	if (e) {
		e.onmouseover	=	dispatchEvent('onmouseover',this);
		e.onmouseout	=	dispatchEvent('onmouseoout',this);
		e.onblur		=	dispatchEvent('onblur',this);
		e.onfocus		=	dispatchEvent('onfocus',this);
	}

	e = document.getElementById('do.login');
	if (e) {
		e.onmouseover	=	dispatchEvent('onmouseover',this);
		e.onmouseout	=	dispatchEvent('onmouseoout',this);
	}
}

function initObjects()
{
	var e;
	webmailform = document.getElementById('form.webmail');
	if (!webmailform) { return(false) }

	e = document.getElementById('webmail.username');
	if (e) {
		fillUsername(e);
	}
}

window.onload = initObjects;
