//-- Web Page Form Validation Framework Javascript --

//---
// error message functions
//---

function setErrorMsg( sTotalMsg, sMsg )
{
  if( sMsg != true )
  {
    return( sTotalMsg + sMsg );
  }
  return sTotalMsg;
}

function getErrorMsg( sMsg, field )
{
  var validFormElement = document.getElementById( field );
  // default the error display to be the name
  var displayLabel = validFormElement.name;
  var validFormLabel = document.getElementById( "label_" + field );
  if ( validFormLabel )
  {
     displayLabel = validFormLabel.value;  
  }
  
  return "<li><a href=\"javascript:setFocus('" + field + "')\">" + displayLabel + "</a><span class=\"formErrorText\"> - " + sMsg + "</span></li>";
}

function setFocus( obj )
{
  if ( obj )
  {
    var focusElement = document.getElementById( obj );
    focusElement.focus();
  }
}

function displayErrors( sErrMsg )
{
  var sMsg = new String;
  oMsg = document.getElementById( "errorMsg" );
  if ( sErrMsg == "" )
  {
    oMsg.innerHTML = "";
    return true;
  }
  sMsg += "<hr noshade size=\"1\">";
  sMsg += sErrMsg;
  sMsg += "<hr noshade size=\"1\">";
  if ( oMsg )
  {
  	oMsg.innerHTML = sMsg;
  }
  return false;
}

function resetErrors( frm )
{
  var frm2;
  frm2 = eval( "document." + frm );
  for (var i = 0; i< frm2.length; ++i)
  {
    frm2[i].className=null;
    var oLabel = eval( "document.all.label_" + frm2[i].name );
    if ( oLabel ) oLabel.className='formLabel';
  }
}

function displayValidation( msg )
{
	window.status = msg;
}

function hideValidation()
{
	window.status = '';
}

//---
// individual field validation functions
//---

function isValidName( name )
{
	var PATTERN = /^[A-Z][A-Za-z\s\-\.']+$/;
	if ( name.length > 0 )
	{
		if ( ( name.length > 50 ) || ( !PATTERN.test( name ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidLoginText( loginText )
{
	var PATTERN = /^[A-Za-z\s\-\.']+$/;
	if ( loginText.length > 0 )
	{
		if ( !PATTERN.test( loginText ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidPassword( password )
{
	var PATTERN = /^[A-Za-z0-9]+$/;
	if ( password.length > 0 )
	{
		if ( !PATTERN.test( password ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidEmail( email )
{	
	var PATTERN = /^[\w\-\.]+@[\w\-\.]+$/ ;
	if ( email.length > 0 )
	{
		if ( ( email.length > 70 ) || ( !PATTERN.test( email ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidPhoneExtension( phoneExtension )
{
	var PATTERN = /^\d+$/;
	if ( phoneExtension.length > 0 )
	{
		if ( ( phoneExtension.length > 10 ) || ( !PATTERN.test( phoneExtension ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidAddress( address )
{
	var PATTERN = /^[\w\s\.]+$/;
	if ( address.length > 0 )
	{
		if ( ( address.length > 50 ) || ( !PATTERN.test( address ) ) )
		{
			return false;
		}
	}
	
	return true;
}	

function isValidCity( city )
{
	var PATTERN = /^[A-Z,a-z,\s]+$/;
	if ( city.length > 0 )
	{
		if ( ( city.length > 50 ) || ( !PATTERN.test( city ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidApartment( apartment )
{
	var PATTERN = /^\w+$/;
	if ( apartment.length > 0 )
	{
		if ( ( apartment.length > 10 ) || ( !PATTERN.test( apartment ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidPOBox( pobox )
{
	var PATTERN = /^\w+$/;
	return ( PATTERN.test( pobox ) );
}	

function isValidDriversLicense( license )
{
	var PATTERN = /^\w+$/;
	if ( license.length > 0 )
	{
		if ( ( license.length > 20 ) || ( !PATTERN.test( license ) ) )
		{
			return false;
		}
	}
	
	return true;
}	

function isValidDate( date )
{
	var PATTERN = /^\d{2}\/\d{2}\/\d{4}$/;
	if ( date.length > 0 )
	{
		if( ( date.length != 10 ) || ( !PATTERN.test( date ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidDay( day )
{
	var PATTERN = /^\d{2}$/;
	if ( day.length > 0 )
	{
		if ( ( day.length != 2 ) ||
		     ( !PATTERN.test( day ) ) ||
		     ( day > 31 ) ||
		     ( day < 1 ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidYear( year )
{
	var PATTERN = /^\d{4}$/;
	if ( year.length > 0 )
	{
		if ( ( year.length != 4 ) ||
		     ( !PATTERN.test( year ) ) ||
		     ( year > 9999 ) ||
		     ( year < 1900 ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidMonthNumber( month )
{
	var PATTERN = /^\d{2}$/;
	if ( month.length > 0 )
	{
		if ( ( month.length != 2 )  ||
		     ( !PATTERN.test( month ) ) ||
		     ( month > 12 ) ||
		     ( month < 01 ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidTime( time )
{
	var PATTERN = /^\d{2}:\d{2}$/;
	if ( time.length > 0 )
	{
		if ( ( time.length != 5 ) || ( !PATTERN.test( time ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidLetter( letter )
{
	var PATTERN = /^[A-Za-z]$/;
	if ( letter.length > 0 )
	{
		if ( ( letter.length != 1 ) || ( !PATTERN.test( letter ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidDigit5( digit5 )
{
	var PATTERN = /^\d{5}$/;
	if ( digit5.length > 0 )
	{
		if( ( digit5.length != 5 ) || ( !PATTERN.test( digit5 ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidDigit4( digit4 )
{
	var PATTERN = /^\d{4}$/;
	if ( digit4.length > 0 )
	{
		if ( ( digit4.length != 4 ) || ( !PATTERN.test( digit4 ) ) )
		{   
		    	return false;
		}
	}
	
	return true;
}

function isValidDigit3( digit3 )
{
	var PATTERN = /^\d{3}$/;
	if ( digit3.length > 0 )
	{
		if ( ( digit3.length != 3 ) || ( !PATTERN.test( digit3 ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidDigit2( digit2 )
{
	var PATTERN = /^\d{2}$/;
	if ( digit2.length > 0 )
	{
		if ( ( digit2.length != 2 ) || ( !PATTERN.test( digit2 ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidDigit1( digit1 )
{
	var PATTERN = /^\d$/;
	if ( digit1.length > 0 )
	{
		if ( ( digit1.length != 1 ) || ( !PATTERN.test( digit1 ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidDigit( digit )
{
	var PATTERN = /^\d$/;
	if ( digit.length > 0 )
	{
		if ( ( digit.length != 1 ) || ( !PATTERN.test( digit ) ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidDouble( doubleValue )
{
	var PATTERN = /^\d+\.\d+$/;
	
	if ( doubleValue.length > 0 )
	{
		if ( !PATTERN.test( doubleValue ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidMoney( money )
{
	var PATTERN = /^\d+\.\d{2}$/;
	
	if ( money.length > 0 )
	{
		if ( !PATTERN.test( money ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidAlpha( alpha )
{
	var PATTERN = /^[A-Za-z]+$/;
	
	if ( alpha.length > 0 )
	{
		if ( !PATTERN.test( alpha ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidNumeric( numeric )
{
	var PATTERN = /^\d+$/;
	
	if ( numeric.length > 0 )
	{
		if ( !PATTERN.test( numeric ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidAlphaNumeric( alphaNumeric )
{
	var PATTERN = /^[A-Za-z0-9]+$/;
	
	if ( alphaNumeric.length > 0 )
	{
		if ( !PATTERN.test( alphaNumeric ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidParagraph( paragraph )
{
	var PATTERN = /^[\w\s\.\?\!:;'",\-\(\)]+$/;
	
	if ( paragraph.length > 0 )
	{
		if ( !PATTERN.test( paragraph ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidLowerCase( lowerCase )
{
	var PATTERN = /^[a-z]+$/;
	
	if ( lowerCase.length > 0 )
	{
		if ( !PATTERN.test( lowerCase ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidUpperCase( upperCase )
{
	var PATTERN = /^[A-Z]+$/;
	
	if ( upperCase.length > 0 )
	{
		if ( !PATTERN.test( upperCase ) )
		{
			return false;
		}
	}
	
	return true;
}

function isValidMonthYearDate( monthYearDate )
{
	var PATTERN = /^\d{2}\/\d{4}$/;
	
	if ( monthYearDate.length > 0 )
	{
		if ( ( monthYearDate.length != 7 ) || ( !PATTERN.test( monthYearDate ) ) )
		{
			return false;
		}
	}
	
	return true;
}
		
function isValidPhoneNumber( phoneNumber )
{
	var PATTERN = /^\s*(\(?\d\d\d(\)|\-|\.)?)\s*(\d\d\d)\s*(\-|\.)?\s*(\d\d\d\d)$/;
	
	if ( phoneNumber.length > 0 )
	{
		if ( !PATTERN.test( phoneNumber ) )
		{
			return false;
		}
	}
	
	return true;
}	
