
	function clearAll()
	{
		var f = document.forms[ 0 ];
		if ( f != null )
		{
			for( var i = 0; i < f.elements.length; i++ )
			{
				clearBackground( f.elements[ i ] );
			}
		}
	}
	
	function setBackground( field )
	{
		field.style.backgroundColor='#ffa';
	}
	
	function clearBackground( field )
	{
		field.style.backgroundColor='#fff';
	}
	
	function validate( elem, vtype )
	{
		switch( elem.type )
		{
			case "text":
				switch( vtype.toLowerCase() )
				{
					case "phone":
						formatPhone( elem );
						break;

					case "tollfree":
						formatTFPhone( elem );
						break;
					
					case "date":
						formatDate( elem );
						break;

					case "state":
						if ( elem.value.length != 2 ) {
							alert( "The State field can only have two characters. Please enter a correct State Code (e.g. 'WA')" );
							return( false );
						} else {
							elem.value = elem.value.toUpperCase();
						}
						break;

					case "email":
						verifyEmail( elem );
						break;
						
					case "alphanumeric":
						var re = /^[a-zA-Z0-9 ]+$/;
						if ( elem.value.length != 0 )
						{
							if ( re.test( elem.value ) == false )
							{
								var msg = "This field must contain only letters and numbers. \n"
									+ "Please remove any punctuation and try again.";
								alert( msg );
								elem.focus();
								return false;
							}
						}

						break;
				}
				break;
		}
		clearBackground( elem );
	}

	function verifyEmail( field ) {
		var sEmail = new String(field.value);
		if (((sEmail.indexOf("@") == -1) || (sEmail.indexOf(".") == -1)) && sEmail.length > 0) {
			alert("Please enter a valid email address in the format user@emailprovider.com");
			field.value = "";
			field.focus();
			return (false);
		}
		return (true);
	}

	function formatPhone( elem )
	{
		var p = elem.value;
		var n = ""; x = "";
		for ( i = 0; i < p.length; i++ )
		{
			if ( isNaN( p.charAt( i ) ) == false )
				n += p.charAt( i ).toString();
		}
		if ( ( n.length < 10 && n.length != 0 ) || ( n.length == 0 && p.length != 0 ) )
		{
			var msg = "Your telephone number appears incorrect.\n" +
					  "Please make sure to include your area code:\n" +
					  "example: (360) 555-1212";
			alert( msg );
			elem.focus();
			return false;
		}
		if ( n.length != 0 ) 
			elem.value = "(" + n.substr( 0, 3 ) + ") " + n.substr( 3, 3 ) + "-" + n.substr( 6, 4 );
	}

	function formatTFPhone( elem )
	{
		var p = elem.value;
		var n = ""; x = "";
		for ( i = 0; i < p.length; i++ )
		{
			if ( isNaN( p.charAt( i ) ) == false )
				n += p.charAt( i ).toString();
		}
		if ( ( n.length < 10 && n.length != 0 ) || ( n.length == 0 && p.length != 0 ) )
		{
			var msg = "Your telephone number appears incorrect.\n" +
					  "Please make sure to include your area code:\n" +
					  "example: 1-877-555-1212";
			alert( msg );
			elem.focus();
			return false;
		}
		if ( n.length != 0 ) 
			elem.value = "1-" + n.substr( 0, 3 ) + "-" + n.substr( 3, 3 ) + "-" + n.substr( 6, 4 );
	}

	function formatDate( elem )
	{
		var dateval = elem.value;
		var msg = "The date value you entered does not appear to be correct.\n"
				+ "Please enter dates in the format MM/DD/YYYY, where:\n\n"
				+ "  - MM is the month,\n"
				+ "  - DD is the date,\n"
				+ "  - YYYY is the four-digit year.";

		if ( dateval.length != 0 )
		{
			if ( dateval.indexOf( "/" ) == -1 )
			{
				alert( msg );
				elem.focus();
				return false;
			}

			var dateArray = elem.value.split( "/" );
			if ( dateArray.length != 3 )
			{
				alert( msg );
				elem.focus();
				return false;
			}
			
			if ( dateArray[ 2 ].length != 4 )
			{
				alert( msg );
				elem.focus();
				return false;
			}

			elem.value = zeroPad( dateArray[ 0 ] ) + "/" + zeroPad( dateArray[ 1 ] ) + "/" + dateArray[ 2 ];
		}
	}

	var submitted = false;
	function submitCheck()
	{
		if ( submitted )
		{
			alert( "This page has already been submitted. \nYou are only allowed to submit each page only once. Press OK to wait for the next page to load." );
			return false;
		}
		else 
		{
			submitted = true;
			return true;
		}
	}

	function validateFormFields( theForm ) {
		if ( theForm == null ) {
			alert( "An invalid form was submitted: theForm == null" );
			return false;
		}
		if ( isNotEmpty( theForm.businessName, "Business Name" ) == false )
			return false;
		if ( isNotEmpty( theForm.shipAddr1, "Street Address 1" ) == false )
			return false;
		if ( isNotEmpty( theForm.shipCity, "Street Address City" ) == false )
			return false;
		if ( isNotEmpty( theForm.shipState, "Street Address State" ) == false )
			return false;
		if ( isNotEmpty( theForm.shipZip, "Street Address Zip Code" ) == false )
			return false;
		if ( isNotEmpty( theForm.shipZip, "Street Address Zip Code" ) == false )
			return false;

		return true;
	}
	
	function isNotEmpty( elem, title ) {
		if ( elem != null ) {
			if ( elem.value.length == 0 ) {
				alert( "The " + title + " field cannot be empty. Please fix and submit again." );
				return( false );
			} else {
				return( true );
			}
		}
	}
	
	function populateMailing( elem ) {
		if ( elem != null ) {
			if ( elem.checked == true ) {
				document.nmccApp.mailAddr1.value = document.nmccApp.shipAddr1.value;
				document.nmccApp.mailAddr2.value = document.nmccApp.shipAddr2.value;
				document.nmccApp.mailCity.value = document.nmccApp.shipCity.value;
				document.nmccApp.mailState.value = document.nmccApp.shipState.value;
				document.nmccApp.mailZip.value = document.nmccApp.shipZip.value;
				
				document.nmccApp.mailAddr1.disabled = true;
				document.nmccApp.mailAddr2.disabled = true;
				document.nmccApp.mailCity.disabled = true;
				document.nmccApp.mailState.disabled = true;
				document.nmccApp.mailZip.disabled = true;
			} else {
				document.nmccApp.mailAddr1.value = "";
				document.nmccApp.mailAddr2.value = "";
				document.nmccApp.mailCity.value = "";
				document.nmccApp.mailState.value = "";
				document.nmccApp.mailZip.value = "";
				
				document.nmccApp.mailAddr1.disabled = false;
				document.nmccApp.mailAddr2.disabled = false;
				document.nmccApp.mailCity.disabled = false;
				document.nmccApp.mailState.disabled = false;
				document.nmccApp.mailZip.disabled = false;
			}
		}
	}