///-------------------------------------------------------------------------
//  @project    core
//  @version    1.05
//  @author     Niel Astle
//  @company    Liquid Edge Solutions
//	@copyright  Copyright Liquid Edge Solutions. All rights reserved.
//  @desc       Client side user interface for 'com_form' component
///-------------------------------------------------------------------------
// classes
///-------------------------------------------------------------------------
var com_form_validation = function(the_cid, the_type, the_display) { // com_form_validation constructor
	/// init properties
	this.cid = the_cid;
	this.type = the_type;
	this.display = the_display;

	if (com_form.messages[this.type] == null) {
	  	this.display = new Array(this.type);
	  	this.type = 'unknown';
	}
}

///-------------------------------------------------------------------------
var com_form = function(the_cid, the_validations) { // com_form constructor
	// init properties
  	this.cid = the_cid; // unique component id
  	this.e_form = document.forms[the_cid + '_form']; // form element
  	this.validations = (the_validations == null ? new Array() : the_validations); // form validation elements

  	/// debug
  	//if (this.e_form == null) alert('DEV: The form \'' + the_cid + '\' does not exist');
}

///-------------------------------------------------------------------------
// static
///-------------------------------------------------------------------------
com_form.messages = {
  	empty 			: 'The {0} field was empty',
	zero 			: 'The {0} field was empty',
	date 			: 'The {0} field was empty',
	notequal 		: 'The {0}- and {1} fields must have the same value',
	length 			: 'The {0} field must be {1} characters long',
	maxlength 		: 'The {0} field length must be less than or equal to {1} characters',
	minlength 		: 'The {0} field length must be more than or equal to {1} characters',
	emptylist 		: 'The {0} field was empty',
	emptylistnew	: 'The {0} field was empty',
	email 			: 'The {0} field must contain a valid email address',
	unchecked 		: 'The {0} field must be checked',
	maxnumber		: 'The {0} field must be less than {1}',
	minnumber		: 'The {0} field must be more than {1}',
	complexpw		: 'The {0} field must contain at least 1 letter and 1 number with a length of no less than 6 characters',
	custom			: '{0}',
	/// debug
	unknown			: 'DEV: There are no validation for type: {0}'
}


///-------------------------------------------------------------------------
// prototypes
///-------------------------------------------------------------------------
com_form.prototype = { // com_form prototype
	///-------------------------------------------------------------------------
	validate: function() { // perform input validation and submit form
	  	var error = '';
	  	for (var i in this.validations) {
	  	  	var new_error = false;

	  	  	// custom validation
	  	  	if (this.validations[i].type == 'custom') {
	  	  		eval(this.validations[i].cid);
	  	 	}
	  		else {
		  	  	// predefined validation
		  	  	switch (typeof this.validations[i].cid) {
		  	  	  	case 'string' :
				  	  	var the_value = this.e_form[this.validations[i].cid].value;
				  	  	switch (this.validations[i].type) {
				  	  	  	case 'empty' 		: new_error = (the_value == '' || the_value == 'null'? true : false); break;
				  	  	  	case 'zero' 		: new_error = (the_value == 0 ? true : false); break;
				  	  	  	case 'date' 		: new_error = (the_value == '' ? true : false); break;
				  	  	  	case 'length' 		: new_error = (the_value.length != this.validations[i].display[1] ? true : false); break;
				  	  	  	case 'maxlength' 	: new_error = (the_value.length > this.validations[i].display[1] ? true : false); break;
				  	  	  	case 'minlength'	: new_error = (the_value.length < this.validations[i].display[1] ? true : false); break;
				  	  	  	case 'emptylist'	:
				  	  	  		var value = $(this.validations[i].cid).get('value');
				  	  	  		new_error = (value == 0 || value == 'null' ? true : false);
				  	  	  		break;
				  	  	  	case 'emptylistnew'	:
				  	  	  		var value = $(this.validations[i].cid).get('value');
				  	  	  		if (value == -2) new_error = ($('__' + this.validations[i].cid + 'add').get('value') == '' ? true : false);
				  	  	  		else new_error = (value == 0 || value == 'null' ? true : false);
				  	  	  		break;
				  	  	  	case 'email' 		:
								var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
								new_error = !filter.test(the_value);
				  	  	  		break;
				  	  	  	case 'unchecked'	: new_error = (!this.e_form[this.validations[i].cid].checked); break;
				  	  	  	case 'maxnumber'	: new_error = (parseInt(the_value) > this.validations[i].display[1] ? true : false); break;
				  	  	  	case 'minnumber'	: new_error = (parseInt(the_value) < this.validations[i].display[1] ? true : false); break;
				  	  	  	case 'complexpw'	: if (the_value) new_error = (core.form.isComplexPassword(the_value, 6, true, true) ? false : true); break;
				  	  	}
				  	  	break;
				  	case 'object' :
				  	  	switch (this.validations[i].type) {
				  	  	  	case 'notequal' 	: new_error = (this.e_form[this.validations[i].cid[0]].value != this.e_form[this.validations[i].cid[1]].value ? true : false); break;
				  	  	}
				  	  	break;
		  	  	}
		  	}
			if (new_error) error += this.get_message(this.validations[i]);
		}
		if (error) alert('The following input errors were found\n' + error);
	  	return (error ? false : true);
	},

	///-------------------------------------------------------------------------
	get_message: function(the_com_form_validation) { // returns the formatted message for the message type
	  	var display = (typeof the_com_form_validation.display == 'string' ? new Array(the_com_form_validation.display) : the_com_form_validation.display);
	  	var message = ' - ' + com_form.messages[the_com_form_validation.type] + '\n';
	  	for (var i in display) {
	  	  	var regex = new RegExp('\\{' + i + '\\}', 'ig');
	  	  	message = message.replace(regex, display[i]);
	  	}
	  	return message;
	}

	///-------------------------------------------------------------------------
}

///------------------------------------------------------------------------

