<!--
/*
* DomRegExp (DRE) Form Validator 
* Author: Fabrizio Calderan. (Ita). Version 0.9.3, Date: Feb 10, 2005
* For updates visit http://sourceforge.net/projects/dre-form
*
* This script is distribuited under GNU License
* Please don't change any comments in this file. 
*/

function show_report(report)
{
	// Comment the following two lines to hide errors into the html.
	// document.getElementById("report_form").innerHTML = report;
	// document.getElementById("report_form").style.display = "block";
	
	// Comment the following line to hide error alert
	alert(report);
}


String.prototype.toUpperCaseAt = function toUpperCaseAt(str,at){
	var re = new RegExp("^(.{"+at+"})(.)");
	return (str.match(re)) ? str.replace(re,str.match(re)[1]+str.match(re)[2].toUpperCase()) : str;
}


function drevalidate(this_form) {
var i;

	/* This <for> controls if optional and required fields corresponds to regexp or not. */
	for (i=0; i<this_form.getElementsByTagName('input').length; i++) {
        this_input = this_form.getElementsByTagName('input')[i];
        
	    /* 
        * We don't need to validate the <Submit> button or <Hidden> fields.
        * You're also encouraged to abandon the not-usable <Reset> button.
        */
        
		if ((this_input.getAttribute('type').toLowerCase() != "submit") 
            && (this_input.getAttribute('type').toLowerCase() != "hidden")) {
            
            this_name = this_input.name;
            this_value = this_input.value;
            this_length = this_input.value.length;
            
            /* 
			* Note: <.hasAttribute> method is not supported by IE, so we check the attribute
			* with <.getAttribute> method.
			*/
            
            if (this_input.getAttribute('regexp'))
                this_regexp = this_input.getAttribute('regexp');
            else
                this_regexp = "__all";
                
            if (this_input.getAttribute('required'))
                this_required = this_input.getAttribute('required');
            else
                this_required = "no";
            
            if (this_input.getAttribute('minlength'))
                this_min_length = this_input.getAttribute('minlength');
            else
                this_min_length = 0;
            
            if (this_input.getAttribute('maxlength'))
                this_max_length = this_input.getAttribute('maxlength');
            else
                this_max_length = "";
                
            if (this_input.getAttribute('altname'))
                this_altname = this_input.getAttribute('altname');
            else
                this_altname = this_name;
                
            
            // Inclusion of regexp (file regexp.js)	
            include_regexp_definition(this_min_length, this_max_length);		
                
            if (this_required.toLowerCase() == "yes")  {
                if (this_length == 0)  {
                    report_string = "Campo obbligatorio «" + this_altname +"».\t\n"
                    // i18n - Select the phrase in your language or leave the default (en-us).
                    // report_string = "Inserire un valore per il campo «" + this_altname +"».\t\t\n\n" \\ (it)
                    show_report(report_string);
                    
                    // Focus the field with an error.			
                    eval("this_form."+this_name+".focus()");
                    return false;
                }		
                else {
                    re = new RegExp(eval(eval(this_regexp)));
                    
                    if (this_value.search(re) < 0) {	
					    
                        report_string = "The field «" + this_altname +"» is not correct.\t\n";	
                        // i18n - Select the phrase in your language or leave the default (en-us).
						//report_string = "Il campo «" + this_altname +"» non è corretto.\t\t\n\n"; \\ (it)	
                        
                        setchars = (eval(this_regexp+"__validchars"));
                        if (setchars.length > 0) {
                            report_string +=  setchars + "\t\n";	
                        }
                        
                        show_report(report_string);
                        eval("this_form."+this_name+".focus()");
                        return false;
                    }					
                }
            }
                        
            if (this_required.toLowerCase() == "no" && this_length > 0)  {
                re = new RegExp(eval(eval(this_regexp)));
                
                if (this_value.search(re) < 0) {
						
                    report_string = "Optional field «" + this_altname +"» is not correct.\t\n";
                    // i18n - Select the phrase in your language or leave the default (en-us).
                    //report_string = "Il campo opzionale «" + this_altname +"» non è corretto.\t\t\n\n"; \\ (it)
						
                    setchars = (eval(this_regexp+"__validchars"));
                    if (setchars.length > 0) {
                        report_string += setchars + "\t\n";	
                    }
                        
                    show_report(report_string);
                    eval("this_form."+this_name+".focus()");
                    return false;
                }			
            }	
        }
		
	} /* end <for> */
		
		
	/* This <for> changes fields value. */
	for (i=0; i<this_form.getElementsByTagName('input').length; i++) {
		this_input = this_form.getElementsByTagName('input')[i];
		this_value = this_input.value;
		
		if (this_input.getAttribute('altname'))
			this_altname = this_input.getAttribute('altname');
		else
			this_altname = this_name;
		
			
		if (this_input.getAttribute('type').toLowerCase() != "submit") {
			
       		/*
			*  If attribute <removespaces> is set, useless spaces are deleted.
			*/
            if (this_input.getAttribute('removespaces')) {
                this_case = this_input.getAttribute('removespaces');
                
                if (this_case.toLowerCase() == "yes") {
                    /* This removes trailing spaces */
                    this_value = this_value.replace(/^\s+/, "");
                    this_value = this_value.replace(/\s+$/, "");
                    
                    /* All extra spaces between words are replaced in one space only */
                    this_value = this_value.replace(/\s+/g, " ");
                    
                    this_input.value = this_value;
                }
            }

            /*
			*  All field values change according to <case> attribute.  
			*/	
            if (this_input.getAttribute('case')) {
                this_case = this_input.getAttribute('case');
                
				switch (this_case.toLowerCase()) {			
					case "uppercase" :
						this_input.value = this_value.toUpperCase();
						break;
							
					case "lowercase": 
						this_input.value = this_value.toLowerCase();
						break;
						
					case "firstcapitalized": 
						this_value = this_value.toLowerCase();
						
						char_value = this_value.charAt(this_value.search(/\b[a-z]/));
						this_value = this_value.replace(/\b[a-z]/, char_value.toUpperCase());						
						this_input.value = this_value;
						break;
						
					case "capitalized":
						this_value = this_value.toLowerCase();
						
                        if ((pos_char = this_value.search(/^[a-z]/)) > -1) {
                            this_value = this_value.toUpperCaseAt(this_value, pos_char);
                        }                        
                        
						while ((pos_char = this_value.search(/\b[a-z]{2,}/)) > -1) {                      
                            this_value = this_value.toUpperCaseAt(this_value, pos_char);
						}					
						this_input.value = this_value;
						break;
                        
						
                    case "forcecapitalized":
						this_value = this_value.toLowerCase();                       
                        
						while ((pos_char = this_value.search(/\b[a-z]/)) > -1) {
                            this_value = this_value.toUpperCaseAt(this_value, pos_char);
						}					
						this_input.value = this_value;
						break;                        
				}
            }
        }
	
		// Start raise function
		var raise = this_input.getAttribute('raise');
		if (raise) {
            return_value = eval(raise+"('"+ this_input.value +"', '"+ this_altname +"')");
			if (!(return_value)) {
                eval("this_form."+this_name+".focus()");
				return false;
			}
		}	
	} /* end <for> */

	report_string = "Form validation completed with success.";
	// show_report(report_string);
	
	return true;

}
-->
