/* 
 * define some needfull constants
 * @author Stanislav Degtjarjov (2007)
 * Copyright Muivo O� (ESTONIA)
 *
 */

var VALIDATION_TYPE = 0;
var VALUE_LAYER = 1;
var ERROR_LAYER = 2;
var ERROR_MESSAGE = 3;
var HIGHLITE_BG = 4;
var FORM_ID = 5;

/* USAGE NOTES
 * var validator = new FormValidator();
 * return validator.validate({0:['mail','id_value','id_value_error','Message']});
 * TODO: return validator.validate({0:['mail | index & number','id_value','id_value_error','Message']});
 * TODO: info icon with popup message
 * TODO: length type 
 */
 /*
function $(value){
    return document.getElementById(value)
}
*/
var FormValidator = function(){
    this.username = "^[a-zA-Z0-9._-]{3,14}$";//- ok
    this.sex = "^[MN]{1}$";//- ok
    this.year = "^[1-2][0-9][0-9][0-9]$";//- ok
    this.month = "^(?:[0][1-9])$|^(?:[1][0-2])$";//- ok
    this.day = "^(?:[0][1-9])$|^(?:[12][0-9])$|^(?:[3][01])$"; //- ok
    this.password = "^.{6,32}$";//- ok
    this.skype = "^[A-Za-z_.,-]{1,32}$";//- ok
    this.binary = "^[0-9]{1,32}$";//- ok
    this.icq = "^[\d]{5,12}$";//- ok
    this.mail = "^[a-zA-Z0-9-._]{1,63}@[a-zA-Z0-9-._]{2,63}[.][a-z]{2,5}$";//- ok
    this.datetime= "^[0-9]{2,4}-[0-9]{2}-[0-9]{2,4}[ ][0-9]{2}[:][0-9]{2}$";//- ok
    this.date= "^[0-9]{2,4}-[0-9]{2}-[0-9]{2,4}$";//- ok
    this.eng_alphabet = "^[a-zA-Z_-]{1,}$";
    this.numeric = "^[0-9]{1,}$";
    this.percent = "^[0-9]{2}[%]{1}$";
    this.url = "^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)*[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?$";
    this.url_strict = "^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$";
    
    this._validate = function(validation_type, value, record){
    	if(typeof(value) == 'undefined') {
    		alert('VALIDATION_FATAL_ERROR: field not found ' + record[VALUE_LAYER]);
    		return false;
    	}
    	
        var pattern;
        switch(validation_type){
        	case 'numeric':
        		return (new RegExp(this.numeric, "g").test(value));
        	case 'datetime':
        		return (new RegExp(this.datetime, "g").test(value));
        	case 'date':
        		return (new RegExp(this.date, "g").test(value));
            case 'binary':
                return (new RegExp(this.binary, "g").test(value));  
        	case 'length':
                return value.length > 0;
            case 'username':
                return (new RegExp(this.username, "g").test(value));
            case 'eng-alphabet':
                return (new RegExp(this.eng_alphabet, "g").test(value));
            case 'sex':
                return (new RegExp(this.sex, "g").test(value));
            case 'year':
                return (new RegExp(this.year, "g").test(value));
            case 'month':
                return (new RegExp(this.month, "g").test(value));
            case 'day':
                return (new RegExp(this.day, "g").test(value));
            case 'password':
                return (new RegExp(this.password, "g").test(value));
            case 'skype':
                return (new RegExp(this.skype, "g").test(value));
            case 'icq':
                return (new RegExp(this.icq, "g").test(value));
            case 'email':
            case 'mail':
                return (new RegExp(this.mail, "g").test(value));
            case 'percent':
            	return (new RegExp(this.percent, "g").test(value));
            case 'checked':
            	return $(record[VALUE_LAYER], $(record[FORM_ID])).is(':checked');
            default:
                alert('VALIDATION_FATAL_ERROR: No validation type found ' + validation_type);
        }
    }
    
    //- {0:['mail','value','value_error','Message']}
    this.reset = function(values){
    	 for(var i in values){
    		 value = values[i];
	         if(value[HIGHLITE_BG] == true){
	         	$(value[VALUE_LAYER], $(value[FORM_ID])).removeClass('validation_error');
	         } else if(typeof(value[HIGHLITE_BG]) == 'string'){
	        	 $(value[HIGHLITE_BG], $(value[FORM_ID])).removeClass('validation_error');
	         } else {
	         	$(value[ERROR_LAYER], $(value[FORM_ID])).html('');
	         }
         }
    }
    
    this.validate = function(values){
        var ret_ok = true;
        var value;
        var field_val;
        
        for(var i in values){
            value = values[i];
            if(typeof($(value[VALUE_LAYER], $(value[FORM_ID]))) == 'undefined') continue;
            
            if(typeof(value[FORM_ID]) != 'undefined'){
            	field_val = $(value[VALUE_LAYER], $(value[FORM_ID])).val();
            	if($(value[VALUE_LAYER], $(value[FORM_ID])).is("select")) field_val = $(value[VALUE_LAYER], $(value[FORM_ID])).find("option:selected").val();
            	
            	if(!this._validate(value[VALIDATION_TYPE], field_val, value)){
            		if(value[HIGHLITE_BG] == true){
                    	$(value[VALUE_LAYER], $(value[FORM_ID])).addClass('validation_error');
                    } else if(typeof(value[HIGHLITE_BG]) == 'string'){
                    	$(value[HIGHLITE_BG], $(value[FORM_ID])).addClass('validation_error');
                    } else {
                    	if(!$(value[ERROR_LAYER], $(value[FORM_ID]))) alert(value[ERROR_LAYER]);
                        $(value[ERROR_LAYER], $(value[FORM_ID])).html(value[ERROR_MESSAGE]);
                    }
                    ret_ok = false;
                }else{
                    if(value[HIGHLITE_BG] == true){
                    	$(value[VALUE_LAYER], $(value[FORM_ID])).removeClass('validation_error');
                    } else if(typeof(value[HIGHLITE_BG]) == 'string'){
	                	$(value[HIGHLITE_BG], $(value[FORM_ID])).removeClass('validation_error');
                    } else {
                    	$(value[ERROR_LAYER], $(value[FORM_ID])).html('');
                    }
                }            	
            } else {     
            	field_val = $(value[VALUE_LAYER], $(value[FORM_ID])).val();
            	if($(value[VALUE_LAYER], $(value[FORM_ID])).is("select")) field_val = $(value[VALUE_LAYER], $(value[FORM_ID])).find("option:selected").val();
            	
	            if(!this._validate(value[VALIDATION_TYPE], field_val, value)){
	                if(value[HIGHLITE_BG] == true){
	                	$(value[VALUE_LAYER], $(value[FORM_ID])).addClass('validation_error');
                    } else if(typeof(value[HIGHLITE_BG]) == 'string'){
                    	$(value[HIGHLITE_BG], $(value[FORM_ID])).addClass('validation_error');
                    } else {
	                	if(!$(value[ERROR_LAYER], $(value[FORM_ID]))) alert(value[ERROR_LAYER]);
	                    $(value[ERROR_LAYER], $(value[FORM_ID])).html(value[ERROR_MESSAGE]);
	                }
	                ret_ok = false;
	            }else{
	                if(value[HIGHLITE_BG] == true){
	                	$(value[VALUE_LAYER], $(value[FORM_ID])).removeClass('validation_error');
	                } else if(typeof(value[HIGHLITE_BG]) == 'string'){
	                	$(value[HIGHLITE_BG], $(value[FORM_ID])).removeClass('validation_error');
                    } else {
	                	$(value[ERROR_LAYER], $(value[FORM_ID])).html('');
	                }
	            }
            }
        }
        return ret_ok;
    }
}

