/****************************************************************************
 File:  ps5JoinFormCheck.js
 Author:  John M. Suprock
 Company:  Castle Communications, Inc.
 
 Copyright 2006 November
 
 **Terms and Conditions of Use:  
 
   Use is permitted for active Partnersoft clients only.
*****************************************************************************

Specifications:
	
	// Checks a join form
	class ps5JoinFormCheck
		
		// Public
		function ps5JoinFormCheck (myJoinForm)
		function setAlertMode (myAlertMode)
		function getAlertMode ()
		function getJoinForm ()
		function getFieldChecks ()
		function getErrors ()
		function validateForm ()
		function checkForm ()  // alias for validateForm()
		function alertError ()
		function alertErrors ()
		function isValid ()
		function isInvalid ()
		
		// Private
		function setJoinForm (myJoinForm)
		function setFieldChecks (myFieldChecks)
		function setErrors (myError)
		function clearErrors ()
	
	// Checks a join field
	class ps5JoinFieldCheck
		
		// Public
		function ps5JoinFieldCheck (myJoinField)
		function getJoinField ()
		function getFieldValue ()
		function getError ()
		function getFieldName ()
		function getFieldDesc ()
		function validateField ()
		function isValid ()
		function isInvalid ()
		
		// Private
		function setJoinField (myJoinField)
		function setError (myError)
		function clearError ()
		
		// Static
		function getFieldCheck (myJoinField)
		function isEmpty (myEmail)
		function isNotEmpty (myEmail)
		function setFieldMap (fieldMap)
		function getFieldMap ()
	
	// Checks an email field
	class ps5EmailCheck
		
		// Public
		function validateField
		
		// Static
		function isValidEmail (myEmail)
		function isNotValidEmail (myEmail)
	
	// Checks a select box
	class ps5SelectBoxCheck
		
		// Public
		function validateField
		
		// Static
		function isSelected (mySelectBox)
		function isNotSelected (mySelectBox)
	
	// Checks a radio button
	class ps5RadioButtonCheck
		
		// Public
		function validateField
		
		// Static
		function isChecked (mySelectBox)
		function isNotChecked (mySelectBox)
	
Examples:

formCheck = new ps5JoinFormCheck(myForm);

formCheck.validate();

formCheck.isValid();

if ( formCheck.isNotValid() ) alertErrors();
if ( formCheck.isNotValid() ) alertError();

**************************************************/

// Object extension utility

Object.prototype.inherits = function (parent) 
{
	// Initialize thisClass reference
	var thisClass = this.prototype.constructor;
	var parentClass = parent.prototype.constructor;
	
	// Initialize inherited attributes
	var newPrototype = new Function();
	newPrototype.prototype = parent.prototype;
	thisClass.prototype = new newPrototype;
	thisClass.prototype.constructor = thisClass;
	
	// Initialize static parentClass reference
	thisClass.parent = parent.prototype;
	
	// Initialize object parentClass reference
	thisClass.prototype.parent = parent.prototype;
}



// Checks a join form
function ps5JoinFormCheck (myJoinForm)
{
	// Member variables
	this.joinForm    = null;
	this.fieldChecks = new Array();
	this.errors      = null;
	this.alertMode   = null;
	
	this.setJoinForm(myJoinForm);
	this.setAlertMode(0);
	
	var elements = myJoinForm.elements;
	var uniques  = new Array;
	for ( var i = 0; i < elements.length; i++ ) {
		var fieldName = elements[i].name;
		if ( uniques[fieldName] ) continue;
		uniques[fieldName] = true;
		
		var fieldCheck = ps5JoinFieldCheck.getFieldCheck(elements[i]);
		if ( ! fieldCheck ) continue;
		
		this.getFieldChecks().push(fieldCheck);
	}
}

// Public member function definitions

ps5JoinFormCheck.prototype.setAlertMode = function (alertMode)
{
	this.alertMode = alertMode;
}

ps5JoinFormCheck.prototype.getAlertMode = function ()
{
	return this.alertMode;
}

ps5JoinFormCheck.prototype.getJoinForm = function ()
{
	return this.joinForm;
}

ps5JoinFormCheck.prototype.getFieldChecks = function ()
{
	return this.fieldChecks;
}

ps5JoinFormCheck.prototype.getErrors = function ()
{
	return this.errors;
}

ps5JoinFormCheck.prototype.validateForm = function ()
{
	this.clearErrors();
	
	var fieldChecks = this.getFieldChecks();
	var fieldCheckMap = new Array();
	var errors = new Array();
	
	for ( var i = 0; i < fieldChecks.length; i++ ) {
		fieldCheckMap[ fieldChecks[i].getFieldName() ] = fieldChecks[i];
	}
	
	for ( var i = 0; i < fieldChecks.length; i++ ) {
	  var fieldName = fieldChecks[i].getFieldName();
	  var datField = fieldCheckMap[ 'fpost_dat' ];
	  
		if ( ! fieldChecks[i].validateField() ) {
			if ( fieldName == 'password' && datField && 
			 datField.getFieldValue().match( /\bELECTRA\b/ )
			) {
				continue;
			}
			
			errors.push( fieldChecks[i].getError() );
		}
	}
	
	this.setErrors(errors);
	
	if ( this.isInvalid() ) { 
		if ( this.getAlertMode() == 2 ) { 
			this.alertErrors();
		}
		else if ( this.getAlertMode() == 1 ) { 
			this.alertError();
		}
	}
	
	return this.isValid();
}

// Alias
ps5JoinFormCheck.prototype.checkForm = ps5JoinFormCheck.prototype.validateForm;

ps5JoinFormCheck.prototype.isValid = function ()
{
	return this.getErrors().length ? false : true;
}

ps5JoinFormCheck.prototype.isInvalid = function ()
{
	return ! this.isValid();
}

ps5JoinFormCheck.prototype.alertError = function ()
{
	alert( this.getErrors()[0] );
}

ps5JoinFormCheck.prototype.alertErrors = function ()
{
	alert( this.getErrors().join('\n') );
}

// Private member function definitions

ps5JoinFormCheck.prototype.setJoinForm = function (myJoinForm)
{
	this.joinForm = myJoinForm;
}

ps5JoinFormCheck.prototype.setFieldChecks = function (myFieldChecks)
{
	this.fieldChecks = myFieldChecks;
}

ps5JoinFormCheck.prototype.setErrors = function (myErrors)
{
	this.errors = myErrors;
}

ps5JoinFormCheck.prototype.clearErrors = function ()
{
	this.setErrors( new Array );
}


// Checks a join field
function ps5JoinFieldCheck (myJoinField)
{
	// Member variables
	this.joinField   = null;
	this.error       = null;
	
	this.setJoinField(myJoinField);
}

// Public member function definitions

ps5JoinFieldCheck.prototype.getJoinField = function ()
{
	return this.joinField;
}

ps5JoinFieldCheck.prototype.getError = function ()
{
	return this.error;
}

ps5JoinFieldCheck.prototype.getFieldName = function ()
{
	return this.getJoinField().name;
}

ps5JoinFieldCheck.prototype.getFieldValue = function ()
{
	return this.getJoinField().value;
}

ps5JoinFieldCheck.prototype.getFieldDesc = function ()
{
	var fieldName = this.getFieldName();
	var isEmpty = fieldMap[fieldName].match(/^\s*$/);
	
	return isEmpty ? fieldName : fieldMap[fieldName];
}

ps5JoinFieldCheck.prototype.validateField = function ()
{
	this.clearError();
	
	if ( ps5JoinFieldCheck.isEmpty( this.getJoinField() ) ) {
		this.setError( this.getFieldDesc() + ' is empty.' );
	}
	
	return this.isValid();
}

ps5JoinFieldCheck.prototype.isValid = function ()
{
	return this.getError() == null;
}

ps5JoinFieldCheck.prototype.isInvalid = function ()
{
	return ! this.isValid();
}

// Private member function definitions

ps5JoinFieldCheck.prototype.setJoinField = function (myJoinField)
{
	this.joinField = myJoinField;
}

ps5JoinFieldCheck.prototype.setError = function (myError)
{
	this.error = myError;
}

ps5JoinFieldCheck.prototype.clearError = function ()
{
	this.setError(null);
}

// Static member function definitions

ps5JoinFieldCheck.getFieldCheck = ps5JoinFieldCheck.prototype.getFieldCheck = function (myJoinField)
{
  var fieldType = myJoinField.type;
  var fieldName = myJoinField.name;
  var fieldCheck = null;
	
	if ( ! ps5JoinFieldCheck.getFieldMap()[fieldName] ) { 
		return false;
	}
	
	if ( fieldType == 'select-one' || fieldType == 'select-multiple' ) { 
		fieldCheck = new ps5SelectBoxCheck(myJoinField);
	}
	else if ( fieldType == 'radio' ) { 
//alert(fieldName + ' => ' + ps5JoinFieldCheck.getFieldMap()[fieldName]);
		fieldCheck = new ps5RadioButtonCheck(myJoinField.form[ myJoinField.name ]);
	}
	else if ( fieldName.match(/email/i) ) { 
		fieldCheck = new ps5EmailCheck(myJoinField);
	}
	else { 
		fieldCheck = new ps5JoinFieldCheck(myJoinField);
	}
	
	return fieldCheck;
}

ps5JoinFieldCheck.isEmpty = ps5JoinFieldCheck.prototype.isEmpty = function (myJoinField)
{
	return myJoinField.value.match( /^\s*$/ ) ? true : false;
}

ps5JoinFieldCheck.isNotEmpty = ps5JoinFieldCheck.prototype.isNotEmpty = function (myJoinField)
{
	return ! ps5JoinFieldCheck.isEmpty(myJoinField);
}

ps5JoinFieldCheck.setFieldMap = ps5JoinFieldCheck.prototype.setFieldMap = function (fieldMap)
{
	ps5JoinFieldCheck.prototype.fieldMap = fieldMap;
}

ps5JoinFieldCheck.getFieldMap = ps5JoinFieldCheck.prototype.getFieldMap = function ()
{
	return ps5JoinFieldCheck.prototype.fieldMap;
}


// Checks an email field
function ps5EmailCheck (myEmailField) 
{
	this.parent.constructor.call(this, myEmailField);
}

ps5EmailCheck.inherits(ps5JoinFieldCheck);

// Public member function definitions

ps5EmailCheck.prototype.validateField = function () 
{ 
	if ( ! this.parent.validateField.call(this) ) 
		return false;
	
	if ( ps5EmailCheck.isNotValidEmail( this.getJoinField() ) ) {
		this.setError( this.getFieldDesc() + ' is not a valid email address.' );
	}
	
	return this.isValid();
}

// Static member function definitions

ps5EmailCheck.isValidEmail = ps5EmailCheck.prototype.isValidEmail = function (myEmailField)
{
  var regexp = /^\S+@[^.\s]+([.][^.\s]+)+$/;
	
	return myEmailField.value.match(regexp) ? true : false;
}

ps5EmailCheck.isNotValidEmail = ps5EmailCheck.prototype.isNotValidEmail = function (myEmailField)
{
	return ! ps5EmailCheck.isValidEmail(myEmailField);
}


// Checks a select box
function ps5SelectBoxCheck (mySelectBox) 
{
	this.parent.constructor.call(this, mySelectBox);
}

ps5SelectBoxCheck.inherits(ps5JoinFieldCheck);

// Public member function definitions

ps5SelectBoxCheck.prototype.validateField = function () 
{ 
	this.clearError();
	
	if ( ps5SelectBoxCheck.isNotSelected( this.getJoinField() ) ) {
		this.setError( this.getFieldDesc() + ' is not selected.' );
	}
	
	return this.isValid();
}

// Static member function definitions

ps5SelectBoxCheck.isSelected = ps5SelectBoxCheck.prototype.isSelected = function (mySelectBox)
{
	return ps5SelectBoxCheck.isNotSelected(mySelectBox) ? false : true;
}

ps5SelectBoxCheck.isNotSelected = ps5SelectBoxCheck.prototype.isNotSelected = function (mySelectBox)
{
	return ps5JoinFieldCheck.isEmpty(mySelectBox) || 
	 mySelectBox.value.match( /\bselect\b/i );
}


// Checks a radio button
function ps5RadioButtonCheck (myRadioButton) 
{
	this.parent.constructor.call(this, myRadioButton);
}

ps5RadioButtonCheck.inherits(ps5JoinFieldCheck);

// Public member function definitions

ps5RadioButtonCheck.prototype.getFieldName = function ()
{
	return this.getJoinField()[0].name;
}

ps5RadioButtonCheck.prototype.getFieldValue = function ()
{
	for ( var i = 0; this.getJoinField().length; i++ ) {
		if ( this.getJoinField()[i].checked ) {
			return this.getJoinField()[i].value;
		}
	}
	
	return null;
}

ps5RadioButtonCheck.prototype.validateField = function () 
{ 
	this.clearError();
	
	if ( ps5RadioButtonCheck.isNotChecked( this.getJoinField() ) ) {
		this.setError( this.getFieldDesc() + ' is not selected.' );
	}
	
	return this.isValid();
}

// Static member function definitions

ps5RadioButtonCheck.isChecked = ps5RadioButtonCheck.prototype.isChecked = function (myRadioButton)
{
	for ( var i =  0; i < myRadioButton.length; i++ ) {
		if ( myRadioButton[i].checked ) return true;
	}
	
	return false;
}

ps5RadioButtonCheck.isNotChecked = ps5RadioButtonCheck.prototype.isNotChecked = function (myRadioButton)
{
	return ! ps5RadioButtonCheck.isChecked(myRadioButton);
}


// Initialization of static data members

if ( 
	typeof(ps5JoinFieldCheck.prototype.fieldMap) == 'undefined' || 
	ps5JoinFieldCheck.prototype.fieldMap == null 
) { 
	var fieldMap = new Array;
	
	fieldMap['firstname'] = 'First name';
	fieldMap['lastname']  = 'Last name';
	fieldMap['state']     = 'State';
	fieldMap['zip']       = 'Zip code';
	fieldMap['country']   = 'Country';
	fieldMap['emailaddr'] = 'Email address';
	fieldMap['username']  = 'Username';
	fieldMap['password']  = 'Password';
	fieldMap['fpost_dat'] = 'Join package';
	
	ps5JoinFieldCheck.setFieldMap(fieldMap);
}
