

var validation_error_message = '';

var validation_result;
var error_message_separator = '\n';

validators = {
 'password' : /^[0-9a-zA-Z\.\-\_]*$/,
 'name'     : /^[a-zA-Z ]*$/,
 'address'  : /^[\s0-9a-zA-Z\.\-\\\/]*$/,
 'text'     : /^.*$/,
 'email'    : /^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/,
 'phone'    : /^[\d\.\s\-(),–]+$/,
 'integer'  : /^[\+\-]?\d*$/,
 'integer_p': /^[\+]?\d*$/,
 'float'    : /^[\+\-]?\d*\.?\d*$/
}

var input_error_border_color = '#FF0000';
var input_normal_border_color = null;

function add_to_error_message(str){
	if (validation_error_message.length != 0)
		validation_error_message += error_message_separator;
	validation_error_message += str;
	validation_result = false;
}

function start_validation(error_separator){
	error_message_separator = error_separator;
	validation_error_message = '';
	validation_result = true;
}

function end_validation(){
	return validation_result;
}

function illuminate_input_text(input, status){
	var color = '#000000';
	if (status == 'error')
		color = input_error_border_color;
	else if (status == 'normal')
		color = input_normal_border_color;
	input.style.borderColor = color;
}
  
function validate_input_text(input, type, display_name, allow_empty){
	if (input_normal_border_color == null)
		input_normal_border_color = input.style.borderColor;
	var text = input.value;
	var result = true;
	if (text.length == 0){
		if (!allow_empty) {
	    	add_to_error_message(display_name + ' is a required field!');
	    	illuminate_input_text(input, 'error');
	    	result = false;
	    }
	    else
	    	return true;
    }
    else {
    	var res_valid = false;
    	if (type == 'date')
			res_valid = valid_date(text);
    	else
			res_valid = validators[type].test(text);
	    if (!res_valid){
	    	add_to_error_message('"' + text + '" is not valid value for ' + display_name + '!');
	    	illuminate_input_text(input, 'error');
	    	result = false;
		}
	}
	validation_result = validation_result && result;
	if (result) {
    	illuminate_input_text(input, 'normal');
	}
	return result;
}

function valid_date(date) {
	if (date.length != 10) return false;
	separator = date.substring(2,3);
	if (separator != '.') return false;
	separator = date.substring(5,6);
	if (separator != '.') return false;
	year = date.substring(6,10);
	month = date.substring(3,5);
	day = date.substring(0,2);
	if (!validators['integer_p'].test(year)) return false;
	if (!validators['integer_p'].test(month)) return false;
	if (!validators['integer_p'].test(day)) return false;
	var m_length = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if ((year % 4) == 0) m_length[1] = 29;
	if (year < 1900 || year > 2100) return false;
	if (month < 1 || month > 12) return false;
	if (day < 1 || day > m_length[month-1]) return false;
	return Date.UTC(year, month-1, day);
}

function is_checked_CB(cb, display_name){
	var i, rezult = false;
	for (i = 0; i < cb.length; i++)
		if (cb[i].checked)
			rezult = true;
	if (!rezult) {
	    add_to_error_message(display_name + ' is a required field!');
	    validation_result = false;
		for (i = 0; i < cb.length; i++)
	    	illuminate_input_text(cb[i], 'error');
	}
	else {
		for (i = 0; i < cb.length; i++)
	    	illuminate_input_text(cb[i], 'normal');
	}
	return rezult;
}


