/******************************************************************************/
/* JS-FORMCHECKER                                                             */
/* ==============                                                             */
/*                                                                            */
/* (C) 2005, Polynorm AG                                                      */
/*                                                                            */
/* Author:   Marcel Tschopp <m.tschopp@phpwheezl.net>                         */
/* Created:  08.08.2005                                                       */
/* Modified: 08.08.2005 - Marcel Tschopp                                      */
/*                                                                            */
/* This script uses the DOM model to set special formchecking related         */
/* attributes to input elements.                                              */
/*                                                                            */
/* Valid attributes are:                                                      */
/* - fc_name     (name of the field, would be displayed in alert box)         */
/* - fc_required ("true"|"false")                                             */
/* - fc_type     ("email", "integer" ,"float", "alpha")                       */
/* - fc_minsize  (any number)                                                 */
/* - fc_maxsize  (any number)                                                 */
/******************************************************************************/

// Language specific texts
// =======================
var txt_title      = 'Folgende Felder wurden nicht korrekt ausgefüllt:\n\n\n';
var txt_max        = '(max. %x Zeichen)';
var txt_min        = '(min. %x Zeichen)';
var txt_numMin     = '(grösser %x)';
var txt_numMax     = '(kleiner %x)';
var txt_notEqual   = '(%first und %second müssen gleich sein)';
var txt_required   = '(*)';
var txt_typ        = [];
txt_typ['email']   = '(inkorrekte E-Mail-Adresse)';
txt_typ['integer'] = '(darf nur aus Zahlen von 0-9 bestehen)';
txt_typ['float']   = '(muss eine korrekte Fliesskommazahl sein)';
txt_typ['alpha']   = '(darf nur aus Buchstaben (A-z) bestehen)';
// set your own texts in html-document AFTER this script is included


// Input types
// ===========
var types = [];
types['email']   = /^[\w-]+?(\.[\w-]+)*@([\w-]+?\.)+([a-z]){2,}$/i;
types['integer'] = /^[0-9]*$/;
types['alpha']   = /^[A-Z]*$/i;
types['float']   = /^[0-9]*([,.][0-9]+)?$/;
// add your own types in html-document AFTER this script is included


// Colors
// ======
var err_color = false;
var ok_color = '#FFFFFF';
// set error color to a color of your choice to mark the inputs with illegal values


// Silent mode
// ===========
var useSilentCheck = false;
var silentCheck = null;


// Should be called whenever a form is submitted
// silent mode: <form onsubmit="checkForm(this, true)">
// normal mode: <form onsubmit="checkForm(this, false)">
function checkForm(theForm, silent) {
	var theForm = $(theForm);
	var ret = true;
	var al = txt_title;
	
	var stack = [];
	// get all inputs
	elems = document.getElementsByTagName('input');
	for (var i = 0; i < elems.length; i++) {
		// check if current element is one of the submitted form
		if (elems[i].form != theForm) {
			continue;
		}
		
		switch (elems[i].type.toLowerCase()) {
			case 'submit':
			case 'button':
			case 'hidden':
				break;
			case 'checkbox':
			case 'radio':
				break;
			default:
				// checks for equal values
				if (elems[i].getAttribute('fc_mustEqual') !== null) {
					if (elems[i].value != $(elems[i].getAttribute('fc_mustEqual')).value) {
						if (err_color) {
							elems[i].style.backgroundColor = err_color;
							$(elems[i].getAttribute('fc_mustEqual')).style.backgroundColor = err_color;
						}
						al += '- ' + elems[i].getAttribute('fc_name') + ' ' + txt_notEqual.replace(/%second/, elems[i].getAttribute('fc_name')).replace(/%first/, $(elems[i].getAttribute('fc_mustEqual')).getAttribute('fc_name')) + '\n';
						ret = false;
						continue;
					}
				}
				
				// checks for required element
				if (elems[i].getAttribute('fc_required') == 'true' && elems[i].value == '') {
					if (err_color) {
						elems[i].style.backgroundColor = err_color;
					}
					al += '- ' + elems[i].getAttribute('fc_name') + ' ' + txt_required + '\n';
					ret = false;
					continue;
				}
				
				// dont check anything more if value is empty
				if (elems[i].value != '') {
					// checks for correct element type
					if (elems[i].getAttribute('fc_type') && types[elems[i].getAttribute('fc_type')]) {
						if (elems[i].value.search(types[elems[i].getAttribute('fc_type')]) == -1) {
							if (err_color) {
								elems[i].style.backgroundColor = err_color;
							}
							al += '- ' + elems[i].getAttribute('fc_name') + ' ' + txt_typ[elems[i].getAttribute('fc_type')] + '\n';
							ret = false;
							continue;
						}
					}
					
					// checks for a given pattern that matches
					if (elems[i].getAttribute('fc_type') == 'pattern' && elems[i].getAttribute('fc_pattern')) {
						if (elems[i].value.search(elems[i].getAttribute('fc_pattern')) == -1) {
							if (err_color) {
								elems[i].style.backgroundColor = err_color;
							}
							al += '- ' + elems[i].getAttribute('fc_name') + ' ' + txt_typ[elems[i].getAttribute('fc_name')] + '\n';
							ret = false;
							continue;
						}
					}
					
					// checks for min size of elements value
					if (elems[i].getAttribute('fc_minsize')) {
						if (parseInt(elems[i].getAttribute('fc_minsize')) > elems[i].value.length) {
							if (err_color) {
								elems[i].style.backgroundColor = err_color;
							}
							al += '- ' + elems[i].getAttribute('fc_name') + ' ' + txt_min.replace(/%x/, elems[i].getAttribute('fc_minsize')) + '\n';
							ret = false;
							continue;
						}
					}
					
					// checks for max size of elements value
					if (elems[i].getAttribute('fc_maxsize')) {
						if (parseInt(elems[i].getAttribute('fc_maxsize')) < elems[i].value.length) {
							if (err_color) {
								elems[i].style.backgroundColor = err_color;
							}
							al += '- ' + elems[i].getAttribute('fc_name') + ' ' + txt_max.replace(/%x/, elems[i].getAttribute('fc_maxsize')) + '\n';
							ret = false;
							continue;
						}
					}
					
					if (elems[i].getAttribute('fc_type') == 'integer') {
						// checks for maximum of elements value
						if (elems[i].getAttribute('fc_max')) {
							if (parseInt(elems[i].getAttribute('fc_max')) <= parseInt(elems[i].value, 10)) {
								if (err_color) {
									elems[i].style.backgroundColor = err_color;
								}
								al += '- ' + elems[i].getAttribute('fc_name') + ' ' + txt_numMax.replace(/%x/, elems[i].getAttribute('fc_max')) + '\n';
								ret = false;
								continue;
							}
						}
						
						// checks for minimum of elements value
						if (elems[i].getAttribute('fc_min')) {
							if (parseInt(elems[i].getAttribute('fc_min')) >= parseInt(elems[i].value, 10)) {
								if (err_color) {
									elems[i].style.backgroundColor = err_color;
								}
								al += '- ' + elems[i].getAttribute('fc_name') + ' ' + txt_numMin.replace(/%x/, elems[i].getAttribute('fc_min')) + '\n';
								ret = false;
								continue;
							}
						}
					}
				}
				
				// reset element color
				if (err_color && ok_color) {
					elems[i].style.backgroundColor = ok_color;
				}
				break;
		}
	}
	
	// we have an error. we should only alert it, when we are not in silent mode
	if (!ret && !silent) {
		if (useSilentCheck && theForm.id) {
			// we are not in silent mode, but we should be
			silentCheck = setInterval('checkForm("' + theForm.id + '", true)', 200);
		}
		
	}
	return ret;
}