/*
* Stacx CX System
* Copyright (C) Stacx Interactive 2003 - 2004
* 
* See http://www.stacx.nl for more information or e-mail
* Stacx Interactive at info@stacx.nl
* 
* File				cx_formcheck.js
* Version			0.901
* Date				03-07-2004
* Author(s)			Stacx Interactive:
* 					Jeroen van Velthoven (jeroen@stacx.nl)
* 
* Description		A javascript formcheck object
*/

function cx_formcheck() {

	this.form								= false;		// The form to check
	this.has_focus							= false;		// The field in the form that currently has focus
	this.errors								= false;		// Errors in the form found by formcheck
	
	this.autotrim							= true;			// Automaticly trim form field values
	
	this.debug								= false;		// Record debug information when performing a formcheck
	this.debugdata							= false;		// Formcheck debuginformation
};

cx_formcheck.prototype.set_debug 			= function(debug) {

	this.debug								= debug;		// Set debug on or off
	if (this.debug) {						this._set_debugdata("Debugging is started"); }
};

cx_formcheck.prototype.set_autotrim			= function(autotrim) {

	this.autotrim							= autotrim;		// Set debug on or off
	if (this.debug) {						this._set_debugdata("Autotrim was set to '"+autotrim+"'"); }
};

cx_formcheck.prototype.set_form 			= function(theform) {

	this.form								= theform;		// Set the formobject to check
	if (this.debug) {						this._set_debugdata("'"+this.form.id+"' is selected to perform a formcheck on"); }
};

cx_formcheck.prototype.set_error 			= function(str_error) {

	// Create the errors array if not created yet
	if(!this.errors) { this.errors			= new Array; }
	
	// Add error to the errors array
	array_key								= this.errors.length;
	this.errors[array_key] 					= str_error;
};

cx_formcheck.prototype._set_debugdata		= function(str_debugdata) {

	// Create the debugdata array if not created yet
	if(!this.debugdata) { this.debugdata	= new Array; }
		
	// Add error to the errors array
	array_key								= this.debugdata.length;
	this.debugdata[array_key] 				= str_debugdata;
};

cx_formcheck.prototype.check_checkbox		= function(id,args,ret) {

	// Check input type text and multiline textarea

	var the_field 							= eval("this.form."+id);
	
	var type_of_check						= args[0];
	var error								= args[1];

	if (typeof the_field!="undefined") {	// Check if the field exists	
	
		if (type_of_check=="isset") {
			
			if (this.debug) {					this._set_debugdata("Checking if '"+id+"' is set!"); }
			
			var isChecked = false;
			for (var i=0;i<this.form.elements.length;i++) {
				if (this.form.elements[i].name == id && this.form.elements[i].checked) {
					isChecked 				= true;
					break;
				}
			}
			
			if (!isChecked) {
				if (ret) {
					return false;
				} else {
					this.set_error(error);
				}
			}
		}
			
		if (ret) {
			return true; }
					
	} else {
	
		if (this.debug) {					this._set_debugdata("Field '"+id+"' not found!"); }
		
		if (ret) {
			return false;
		} else {
			this.set_error(error);
			this.set_focus(the_field);
		}
	}
};

cx_formcheck.prototype.check_radio			= function(id,args,ret) {

	// Check input type text and multiline textarea

	var the_field 							= eval("this.form."+id);
	
	var type_of_check						= args[0];
	var error								= args[1];

	if (typeof the_field!="undefined") {	// Check if the field exists	
	
		if (type_of_check=="isset") {
			
			if (this.debug) {				this._set_debugdata("Checking if '"+id+"' is set!"); }
			
			var isChecked = false;
			for (var i=0;i<the_field.length;i++) { 
			
				if (this.debug) {			this._set_debugdata("Checking radiobutton "+i+":"+the_field[i].checked); }
				if (the_field[i].checked) {
					isChecked 				= true;
					break;
				}
			}
			
			if (!isChecked) {
				if (ret) {
					return false;
				} else {
					this.set_error(error);
				}
			}
		}
		else if (type_of_check=="ischecked") {
			
			if (this.debug) {				this._set_debugdata("Checking if '"+id+"' is set!"); }
			
			var fieldNum					= args[2];

			if (!the_field[fieldNum].checked) { 
			
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}
		}
		else if (type_of_check=="ischeckedvalue") {
			
			if (this.debug) {				this._set_debugdata("Checking if '"+id+"' is set!"); }
			
			var fieldValue					= args[2];
			
			var isChecked = false;
			for (var i=0;i<the_field.length;i++) { 
			
				if (this.debug) {			this._set_debugdata("Checking radiobutton "+i+":"+the_field[i].checked); }
				if (the_field[i].value==fieldValue && the_field[i].checked) {
					isChecked 				= true;
					break;
				}
			}
			
			if (!isChecked) {
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field[0]);
				}
			}
		}
		
		if (ret) {
			return true; }
							
	} else {
	
		if (this.debug) {					this._set_debugdata("Field '"+id+"' not found!"); }
		
		if (ret) {
			return false;
		} else {
			this.set_error(error);
			this.set_focus(the_field[0]);
		}
	}
};

cx_formcheck.prototype.check_textfield		= function(id,args,ret) {

	// Check input type text and multiline textarea
//TODO postal,phonenumber, ..... check

	var the_field 							= eval("this.form."+id);
	
	var type_of_check						= args[0];
	var error								= args[1];

	if (typeof the_field!="undefined") {	// Check if the field exists
	
		// Trim field value
		if (this.autotrim) { the_field.value = this.trim(the_field.value); }	
	
		if (type_of_check=="isset") {		// Check if the field has a string value

			if (this.debug) {				this._set_debugdata("Checking if field '"+id+"' is set -> "+the_field.value); }
			
//TODO		// Check for illegal characters
			var illegal_chars				= args[2];
			
			if (the_field.value=="") {
				
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}			
		}
		else if (type_of_check=="isint") {	// Check if the field has a integer value
			
			if (isNaN(the_field.value) || the_field.value.indexOf('.')!=-1) { 
			
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}			
		}
		else if (type_of_check=="isfloat") { // Check if the field has a float value (, and . allowed in float)
			
			var tmp_value					= the_field.value.replace(/,/,".");
			
			if (isNaN(tmp_value) || tmp_value.indexOf('.')==-1) { 
			
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}
		}
		else if (type_of_check=="equals") {	// Check if the filed value equals a given string

			var str_compare					= args[2];
			
			if (the_field.value!=str_compare) { 
			
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}
		}
		else if (type_of_check=="notequals") {	// Check if the filed value equals a given string

			var str_compare					= args[2];
			
			if (the_field.value==str_compare) { 
			
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}
		}
		else if (type_of_check=="isemail") { // Check if the field value is an e-mailadress

			if (!this.isemail(the_field.value)) {
						
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}
		}
		else if (type_of_check=="isdutchpostal") { // Check if the field value is an e-mailadress 

			if (!this.isdutchpostal(the_field.value)) {
						
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}
		}
		else if (type_of_check=="isnumber") { // Check if the field value is an e-mailadress 

			if (!this.isnumber(the_field.value)) {
						
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}
		}
		else if (type_of_check=="regexp") {	// Check if the field value matches a given regexp
		
			var str_regexp					= args[2];

			if (!this.match_regexp(the_field.value,objRegExp)) {
						
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}
		}
		else if (type_of_check=="fieldsize_in_range") {	// Check if the field value consists of the correct range of characters
		
			var min_field_size				= args[2];
			var max_field_size				= args[3];

			if ((min_field_size != false && the_field.value.length < min_field_size) || (max_field_size != false && the_field.value.length > max_field_size)) {
						
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}
		}
		else if (type_of_check=="number_in_range") { // Check if the filed value is a number and if it is between a certain range
		
			var min_field_size				= args[2];
			var max_field_size				= args[3];

			if (isNaN(the_field.value) || (min_field_size != false && the_field.value < min_field_size) || (max_field_size != false && the_field.value > max_field_size)) {
						
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field);
				}
			}
		}
		else if (type_of_check=="max_word_length") {
		
			var word_length					= args[2];
			var words						= the_field.value.split(" ");
			
			for (array_key in words) {
			
				if (words[array_key].length > word_length) {
				
					if (ret) {
						return false;
					} else {
						this.set_error(error);
						this.set_focus(the_field);
					}
					break;			
				}
			}
		}
		
		if (ret) {
			return true; }
			
	} else {
		
		// Debugwarning when the field was not found
		if (this.debug) {					this._set_debugdata("Textfield '"+id+"' was not found when checking!"); }
		
		if (!ret) {
			this.set_error(error); }
		else {
			return false;}
	};
};

cx_formcheck.prototype.check_select			= function(id,args,ret) {

	// Check input type text and multiline textarea
//TODO postal,phonenumber, ..... check

	var the_field 							= eval("this.form."+id);
	
	var type_of_check						= args[0];
	var error								= args[1];

	if (typeof the_field!="undefined") {	// Check if the field exists
	
		// Trim field value
		if (this.autotrim) { the_field.value = this.trim(the_field.value); }	
	
		if (type_of_check=="isset") {		// Check if the field has a string value

			if (this.debug) {				this._set_debugdata("Checking if select '"+id+"' is set -> "+the_field.value); }
			
			if (the_field.value=="") {
				
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field[0]);
				}
			}
		}
		else if (type_of_check=="equals") {	// Check if the filed value equals a given string

			var str_compare					= args[2];
			
			if (the_field.value!=str_compare) { 
			
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field[0]);
				}
			}
		}
		else if (type_of_check=="notequals") {	// Check if the filed value equals a given string

			var str_compare					= args[2];
			
			if (the_field.value==str_compare) { 
			
				if (ret) {
					return false;
				} else {
					this.set_error(error);
					this.set_focus(the_field[0]);
				}
			}
		}		

		if (ret) {
			return true; }
			
	} else {
		
		// Debugwarning when the field was not found
		if (this.debug) {					this._set_debugdata("Textfield '"+id+"' was not found when checking!"); }
		
		if (!ret) {
			this.set_error(error); }
		else {
			return false;}
	};
};

cx_formcheck.prototype.isemail				= function(str_in) { 				// Check if string is an e-mailadress
	
  	if (this.trim(str_in)=="" || !str_in.match(/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/)) {
		return false;
	}
	return true;
}

cx_formcheck.prototype.isnumber				= function(str_in) { 				// Check if string is a number
	
  	if (this.trim(str_in)=="" || !str_in.match(/^\d+$/)) {
		return false;
	}
	return true;
}

cx_formcheck.prototype.isdutchpostal		= function(str_in) { 				// Check if string is an e-mailadress
	
  	if (this.trim(str_in)=="" || !str_in.match(/^[1-9]{1}[0-9]{3}[ ]*[a-zA-Z]{2}$/)) {
		return false;
	}
	return true;
}

cx_formcheck.prototype.match_regexp			= function(str_in,objRegExp) { 		// Check if string matches the regexp
	
  	if (this.trim(str_in)=="" || !str_in.match(objRegExp)) {
		return false;
	}
	return true;
}

cx_formcheck.prototype.set_focus			= function(field) { 				// Set focus to the firstfield that has an error

	if (!this.has_focus) { this.has_focus	= field; }
}

cx_formcheck.prototype.trim					= function(str_in) {				// Remove leading and trailing spaces

	var str_out = (""+str_in).replace(/^\s+|\s+$/g, "");
	if (str_out == " ") { str_out = ""; }
	
  	return str_out;
}

cx_formcheck.prototype.show_errors_alert	= function(str_message) {

	if (this.errors) {						// Show errors
	
		var str_out							= str_message;
		for (array_key in this.errors) { str_out += this.errors[array_key] + "\n"; }
		
		alert(str_out + "\n");
	}

	// Return submitvalue
	if (!this.errors) { return true; }
	return false;
};

cx_formcheck.prototype.submit_form			= function() {

	// Show debugdata if debugging
	if (this.debug && this.debugdata) {
	
		var str_out							= "Debugger messages:                                                       \n\n";
		for (array_key in this.debugdata) { str_out += "- " + this.debugdata[array_key] + "\n"; }
		
		alert(str_out + "\n");
	}

	// Submit if no errors occured
	if (!this.errors) { return true; }
	
	// Set focus and cancel submit
	if (this.has_focus) { this.has_focus.focus(); }
	return false;
};
