//validate fields form

var curentValidatorHash = {};

function setValidatorActionInput(hash){
	var c = hash["names"].length;
    for (var i = 0;i<c;i++){ 
        //alert(hash["names"][i]);
		var cur_el = document.getElementById(hash["names"][i]);
        
		try{
			var el = hash["names"][i];
 			cur_el.onblur = _checkInputErrors(el, hash);
        }
        catch(e){
        	alert(hash["names"][i] + " - not exist");
        }
    }
}

function _checkInputErrors(el, hash){
		return function (){checkInputErrors(el, hash)};
}

function checkInputErrors( element, validate_hash){
	rules = validate_hash["rules"][element]
    var cur_el = document.getElementById(element);
    if(!cur_el) return;
   
	var errors = {}
	var test = function (v){return v;}
		
	for (var i = 0; i < rules.length; i++) {
		if (rules[i] == "req") {
			if (cur_el.value == "") {
				errors[rules[i]] = "This is a required field."
			//erorrs[] = "This is a required field.";
			}
		}
		
        if (rules[i] == "int" && cur_el.value != "") {
            if (!validateIsInt(cur_el.value)){
                errors[i].error = "Please enter a valid number in this field.";
            }
        }
        
        if (rules[i] == "float" && cur_el.value!= ""){
            if (!validateIsFloat(cur_el.value)){
                errors[i].error = "Rlease enter a valid float in this feild";     
            }
        }
        
        if (rules[i] == "email" && cur_el.value != "") {
			if (!/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(cur_el.value)) {
				errors[rules[i]] = "Please enter a valid email address.";
			}
		}
		
		if (rules[i] == "words" && cur_el.value != "") {
			var test = function(v){
				return v;
			}
			if (/^[^\d]{1,}$/.test(cur_el.value)) {
			
			}
			else {
				errors[rules[i]] = "Please enter a valid value in this field.";
			}
		}
	}			
	
	b_error = false;
	for (var i = 0; i < rules.length; i++) {
	    if (errors[rules[i]]) {
			b_error = true;
		    break;
		}
	}
	if (b_error != 0){
		cur_el.style.borderColor = "red";
	} else {
		cur_el.style.borderColor = "#00B200";
	}
	
	
	return

}

function validateIsInt(val){
   if (val != "") {
       if (/^\d{1,}$/.test(val)) {
           return true;
       }
   }
   return false 
}

function validateIsFloat(val){
    if (val != "") {
        if (/^[0-9]+([\.]?[0-9]+){0,1}$/.test(val)) {
            return true;
        }
    }
}

