var button = 'Continue';
var msg1 = '';
var ns4, ns6, ie;
var topBlock;

ns4 = (document.layers) ? true : false ;
ie = (document.all) ? true : false ;
ns6 = (document.getElementById && ! document.all) ? true : false ;

if (ns4) topBlock = document.topMsgDiv;
if (ie)  topBlock = topMsgDiv.style;
if (ns6) topBlock = document.getElementById("topMsgDiv");

if(ie)
{
	function disableEnter(e)
	{
		if (window.event.keyCode == 13) 
			window.event.keyCode = 0;
	}
	
	document.attachEvent('onkeypress',disableEnter);
	
}

if(ns6)
{
	function disableEnter(e)
	{	
		return (e.keyCode == 13) ? false : true ;
	}

	document.addEventListener('keypress',disableEnter,true);
}

if(ns4)
{
	window.captureEvents(Event.ONLOAD | Event.RESIZE);
	window.onload = function(event) {  fixDIV();  };
	window.onresize = function (evt) { fixDIV(); };

	function fixDIV()
	{
		topBlock.visibility = 'show';
	   	if(currentErr != null && currentErr != '')
			writeOnTop( currentErr );
	}

}


var errors = new Array();
var prevErrFields = new Array();
var errFields = new Array();
var currentErr = '';

function error(errkey)
{
	for(var i = 1; i < arguments.length ; i++)
  	{
		if(arguments[i] != null)
			errFields[ i - 1] = (( arguments[i].id == null || arguments[i].id == '' ) ? arguments[i].name : arguments[i].id );
  	}

    currentErr = errors[errkey];
	writeOnTop( currentErr );
 	markFields( errFields );
}

function writeOnTop(msg)
{
	writeLayer("topMsgDiv",'<SPAN CLASS=\'error\'>'+ msg +'</SPAN>');
	ns6 ? window.scrollTo( parseInt(0), parseInt(0) ) : window.scrollTo(0, 0) ;
}

function markFields(fields)
{
	for(var n = 0; n < prevErrFields.length ; n++)
  	{
		for(var k = 0; k < fields.length; k++)
		{
			if( prevErrFields[n] != fields[k] )
				changeColor( prevErrFields[n],"#000000" );
		}
  	}

  	for(var i = 0; i < fields.length; i++)
  	{
		changeColor( fields[i],"#990000" );
	 	prevErrFields[i] = fields[i];
  	}
}

function writeLayer(id, text)
{
	if (ns4)
		rewriteLayer (id, text); 

	else if (ie)
	   document.all[id].innerHTML = text;

    else if (ns6)
       document.getElementById(id).innerHTML = text;

}

function changeColor(field, acolor )
{
  	var pid = field + "Label"; // ex. for 'zip' --> pid = 'zipLabel'
  	if(ie)
  	{
		if(document.all[pid] != null && document.all[pid].style != null)
       		document.all[pid].style.color = acolor;
  	}
	else if(ns6)
	{
		if(document.getElementById(pid) != null && document.getElementById(pid).style != null)
			document.getElementById(pid).style.color = acolor;
	}
}

function rewriteLayer (idOrPath, html)
{
	if (ns4)
  	{
		var l = idOrPath.indexOf('.') != -1 ? eval(idOrPath)
             				: document[idOrPath];

      	if (!l.ol)
		{
      		var ol = l.ol = new Layer (l.clip.width, l);
      		ol.clip.width = l.clip.width;
      		ol.clip.height = l.clip.height;
      		ol.bgColor = l.bgColor;
      		l.visibility = 'hide';
      		ol.visibility = 'show';
   		}
   			
   		var ol = l.ol;
    	ol.document.open();
    	ol.document.write(html);
		ol.document.close();
  	}
}


var illegalCharStr = '<>{}[]~`$%^=?/\\\":;';
errors['illegalChars'] = 'You have entered one or more illegal characters.<br>Please make the necessary corrections, and try again.<br>';

var i = 0;

// On page load, setup each form in document with events
while(document.forms[i])
{
    var fm = document.forms[i];
	var elts = document.forms[i].elements;
	
	// Setup onblur event to trim() and check for Illegal chars
	if(elts != null)
	{
		for(var e = 0; e < elts.length ; e++)
			checkField(elts[e]);
	}

	// Setup trim() and check for Illegal chars when form is submitted
	fm.onsubmit = function(){
		if(elts != null)
		{
			for(var e = 0; e < elts.length ; e++)
			{
				trim(elts[e]);
				if( hasIllegal(elts[e]) )
					return false;
			}
		}
		return true;
	}

	++i; // next form
}

//-------------------------------------------------------------------------
// trim leading and trailing whitespace
function trim(elt)
{
	if(elt.type != 'text' && elt.type != 'password')
		return true;

	var va = elt.value;

	// trim trailing spaces
	while('' + va.charAt(0) == ' ' )
		va = va.substring(1, va.length);

	// trim leading spaces
	while('' + va.charAt(va.length-1) == ' ')
		va = va.substring(0, va.length-1);

	// display trimmed value back to user
	elt.value = '';
	elt.value = va;
}

//--------------------------------------------------------------------------
// trim fields & check for illegal chars in fields at soon as elt changes
function checkField(elt)
{
	elt.onblur = function(){ trim(elt); hasIllegal(elt); }
}

function hasIllegal(elt)
{
	if(elt.type != 'text' && elt.type != 'password' && elt.type != 'textarea')
		return false;

	var va = elt.value;

	if(elt.type == 'textarea')
		illegalCharStr = illegalCharStr.slice(0,4);
	
	var stripped = stripCharsInBag (va, illegalCharStr);
	if(va != '' && stripped.length < va.length)
	{
		error('illegalChars', elt);
		return true;
	}

	return false;
}

function stripCharsInBag (s, bag)
{   
	var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function okRequired(f,require)
{
	return allFilled(require) ? true : false ; 
}


errors['missingFields'] = "Some additional information is required."
if( !ns4 ) errors['missingFields'] += "  Please review the field(s) marked in red."
errors['missingFields'] += "<br>Make the necessary changes, then click <B>\"" + button + "\"</B> again.";

//--------------------------------------------------------------------------
// report an error if any field is missing from an array of required fields
function allFilled(require)
{
	// since we have to display all the missing fields at once and not one at a time,
    // we cannot use the usual error reporting function 'error()'.
			
	var n = 0;
	var allerrors = new Array();
	for(var i = 0; i < require.length; i++)
	{
		if( require[i] == null)
		continue;
					
		// netscape 4 does not understand field.value if field is a drop-down
		var isEmpty = false;
		if( !ns4 ) 
			isEmpty = (require[i].value == '');
		else 
		{
			if( require[i].type.indexOf('select') == -1 )
		  		isEmpty = (require[i].value == ''); // same as above
			else 
			    isEmpty = (require[i].options[ require[i].selectedIndex ].value == '');
		}
				
		if( isEmpty )
		{	     
			allerrors[n] = (( require[i].id == null || require[i].id == '')
				? require[i].name : require[i].id);
			++n;
		}
	}
		   
	if( allerrors.length )
	{
		writeOnTop( errors['missingFields'] );
 		markFields( allerrors );			     
		return false;
	}
		 
	return true;		
}