// *****************************************************************//
// Global Config
// *****************************************************************//

_debug = true; // Set to False for production


// *****************************************************************//
// Startup
// *****************************************************************//

var validationPageReady = false;  // is the DOM ready?

function validationInit()
{
	validationPageReady = true;
	
	_messageAlert(); // show error (if any)
}

$(validationInit);

// *****************************************************************//
// Generic Utils
// *****************************************************************//

function rawunescape(str)
{
	str = str.replace(/\+/g, "%20");
	str = unescape(str);
	return(str);
}

// *****************************************************************//
// Helper Functions for Validation, etc
// *****************************************************************//


var clientSideValidation = true;

function skipValidation()
{
	clientSideValidation = false;
}

// {fieldName: 'testfield',validationType:'isPostalCode',errorMessage:'That is not a valid postal code'}
function validateForm(f)  // remaining arguments are validation objects
{	
	
  if( !clientSideValidation )
  	return true;

  for(var n=1;n<arguments.length;n++)
  {
		var d = arguments[n];
		
		if( d == null || d == undefined )
		  continue;
		
		var field = $('#' + d.fieldName,f).get(0);
	
		if( _debug && !field ) // dev code only
		{
			alert("Unknown or multiple fields: " + d.fieldName + " ... skipping");
			continue;
		}
	
		var val = $(field).val();
		
		switch(d.validationType)
		{
			case "isNumber":
				if( !val.isBlank() && !val.isNumber() )
				{
					throwError(d,f);
					return false; // do not submit form
				}
				break;
			case "isDouble":
				if( !val.isBlank() && !val.isDouble() )
				{
					throwError(d,f);
					return false; // do not submit form
				}
				break;
			case "isRequired":
				if( val.isBlank() )
				{
					throwError(d,f);
					return false; // do not submit form
				}
				break;		
			case "isAlphaNumeric":
				if( !val.isBlank() && !val.isAlphaNumeric() )
				{
					throwError(d,f);
					return false; // do not submit form
				}
				break;		
			case "isSelected":
				if( field.type == "select-one" && field.selectedIndex == 0 )
				{
					throwError(d,f);
					return false; // do not submit form
				}
				break;
			case "isEmail":
				if( !val.isBlank() && !val.isEmail() )
				{
					throwError(d,f);
					return false; // do not submit form
				}
				break;
			case "isPassword": // the original password field MUST be named / id'd as "password"
				if ( !val.isPassword(f.password.value) )
				{
					throwError(d,f);
					return false;
				}
				break;
			case "isValidPassword":
				if( !val.isBlank() && !val.isValidPassword() )
				{
					throwError(d,f);
					return false; // do not submit form
				}
				break;
			case "isChecked":
				if( !field.checked )
				{
					throwError(d,f);
					return false; // do not submit form
				}
				break;
				
			default:
				// Testing Only
				if( _debug ) 
					alert("####DEBUG####\n\nUnknown validationType " + d.validationType + " on " + d.fieldName + "\n\nValidation will skip this item"); // dev code only
				return false;
				break;
		}
  }
  return true;
}

function throwError(d,f)
{
	messageAlert(rawunescape(d.errorMessage),'error',$('#' + d.fieldName,f).get(0));
	window.scrollTo(0,0); // reset view
}

var cachedMessage = "";
var cachedField = undefined;
var cachedType = "";

function messageAlert(message,type,field)
{
	if( type == undefined )
	  type = 'error';

	cachedMessage = message;
	cachedField = field;
	cachedType = type;
	_messageAlert();
}


function _messageAlert()
{
	if( validationPageReady && cachedMessage != "" )
	{
		// Check for messageBox on page
	  var messageBox = $('#messageBox');
		
		if( messageBox.length == 0 ) { // Create it
			BOF.message(cachedMessage,cachedType);
		} else {
			clearAlert();
			messageBox.removeClass();
			messageBox.addClass("messageBox").addClass(cachedType);
			messageBox.html(rawunescape(cachedMessage));
		
			if( cachedField )
			{
				var fieldHolder = $(cachedField).parents('.formRow').get(0);
				
				$(fieldHolder).addClass(cachedType);
				$(cachedField).select();
			}
		}
	}
}

function clearAlert()
{
	$('#messageBox').removeClass();
	$('.formRow').removeClass('notice');
	$('.formRow').removeClass('error');
}


// Prototypes

String.prototype.isBlank = function()
{
	var s = this.replace(/ /g,"");
	if( s == "" )
	  return true;
	else
	return false;
}

String.prototype.isNumber = function()
{
	if( this.search(/^ *\d+ *$/) == -1 )
	  return false;
	else
  	  return true;
}

String.prototype.isDouble = function()
{
	var s = this.replace(/ /g,"");
	var r = Number(s);
	if( r == s )
	  return true;
	else
  	  return false;
}

String.prototype.isAlphaNumeric = function()
{
  s = $.trim(this);
  if( s.search(/^[\da-zA-Z ]+$/) == -1 )
    return false;
  return true;
}

String.prototype.isEmail = function()
{
	if( this.search(/@/) == -1 )
	  return false;
	else
  	  return true;
}

String.prototype.isPassword = function(txt)
{
	if ( this == txt )
		return true;
	else
		return false;
}

String.prototype.isValidPassword = function()
{
	if ( this.length >= 6 )
		return true;
	else
		return false;
}

