String.prototype.trim = function() { return this.replace(/^\s\s*/, "").replace(/\s\s*$/, ""); }
var Check = function(f) { this.form = f; }
Check.prototype.required = function(str) { return str.search(/\S/) > -1; }
Check.prototype.matches = function(str, field) { return (str == this.form.elements[field].value); }
Check.prototype.valid_email = function(str) { return str.search(/^([\w\+\-]+)(\.[\w\+\-]+)*@([a-z\d\-]+\.)+[a-z]{2,6}$/i) > -1; }
Check.prototype.set_select = function(str) { return str.search(/\S/) > -1; }
Check.prototype.set_checkbox = function(str, field) {return this.form.elements[field].checked == true; }
Check.prototype.numeric = function(str) { return ! isNaN(str); }
Check.prototype.countNumeric = function(str) { return str.length == 5; }
Check.prototype.countLetterText   = function(str) { return str.length <= 300; }
Check.prototype.countLetterInput  = function(str) { return str.length <= 100; }
Check.prototype.validate = function (rules, callback) {try {
	if (!rules.length) return true;		
	var res, errors=[];
	for (var i in rules) {
		var item = rules[i];				
		var field = this.form.elements[item.input];		
		var rule_list = item.rule.split("|");		
		var j = 0;		
		for (var r in rule_list) {
			var re = /(callback_|validate_)?(\w+)(?:\[(.+)\])?/i.exec(rule_list[r]);
			var func = re[2];
			
			if (!this[func]) {
				try { func = eval(func); } catch (e2) { }
				res = (typeof(func) == "function") ? func(field.value, re[3]) : false;
			} else {
				res = this[func](field.value, re[3]);
			}

			if (!res && item.msg[j]) {
				errors.push([item.msg[j], item.input]);
				break;
			}

			j++;
		}
	} } catch (e) { alert(e); }	
	if (errors.length) {
		// show errors
		return callback ? callback(this.form, errors) : display_alert(this.form, errors);
	}
	return true;
}

function push_validate_array(r, x) {
	if (!x)	return r;	
	var a = [];
	for (var i in r) {
		var c = true;
		for (var j in x) {
			if (r[i].input == x[j]) {
				c = false;
				break;
			}
		}		
		if (c)
			a.push(r[i]);
	}	
	return a;
}


function resetField(id, status) {
	//$("#" + id + "_message").empty();
	document.getElementById(id + "_message").innerHTML = '';
	//$("#" + id).removeClass("active");		
	if(status) document.getElementById(id).className = '';
}


function display_alert(f, errors) {		
	//$("#" + errors[0][1]).addClass("active");
	document.getElementById(errors[0][1]).className = 'active';
	for (var i in errors) 
		//$("#" + errors[i][1] + "_message").html(errors[i][0]);		
		document.getElementById(errors[i][1] + "_message").innerHTML = errors[i][0];
	return false;
}