/**
 * @author Ciprian
 */
var _f1 = false;
var _f2 = true;
var ops = {
			'gt': 'parseInt(x)>parseInt(y)',
			'gte' : 'parseInt(x)>=parseInt(y)',
			'lt':'parseInt(x)<parseInt(y)',
			'lte':'parseInt(x)<=parseInt(y)',
			'req':'x.toString()> " " ',
			'email':'checkMail(x)',
			'not' : 'x.toString() != (y + "").toString()'
		};
var errorClass = 'errorCssClass';//'errorCssClass';
var alertErrors = false;
var processAll = true;

var isForm = function(elem){
	return typeof elem == typeof {} && elem.tagName.toLowerCase() == 'form';
}
/**
 * Returns true for an input element inside a form.
 * @param {Object} elem
 */
var isInput = function(elem){
	try{
		return elem.value != undefined && elem.tagName.toLowerCase() == 'input' || elem.tagName.toLowerCase() == 'select'|| elem.tagName.toLowerCase() == 'textarea';
	}
	catch(e){
		return false;
	}
}

var sendValidateForm = function(formId){
	try{
		var frmEl = null;
		var valid = true;
		if(typeof formId == typeof ""){
			frmEl = getObj(formId);
		}
		else if(isForm(formId)){
			frmEl = formId;
		}
		else{
			return false;
		}
		for(var e in frmEl.elements){
			if(isInput(frmEl[e])){
				var curEl = frmEl[e];
				if(curEl.className.indexOf('test:') >=0 ){
					
					valid &= parseInput(curEl);
					if(!valid && !processAll){
						return false;
					}
				}
			}
		}
		return valid;
	}
	catch(e){
		return false;
	}
}
//alert(sendValidateForm);
var parseInput = function(obj){
	var res = false;
	var o = null;
	var c = null;
	var expr1 = '';
	var expr2 = '';
	var strClassName = obj.className;
	if(typeof strClassName == typeof " " && strClassName > ' '){
		var str = strClassName.match('test:[^ ]+').toString();
		if(typeof str == typeof " " && str > ' '){
			var e = str.split(':');
			if(e[1]!=undefined && typeof e[1] == typeof " " && e[1] > ' ' && ops[e[1]] != undefined){
				
					
					

				if(e[2] != undefined){
					if(e[2].indexOf('.') >= 0){
						var ar = e[2].split('.');
						if(ar[0] == 'this'){
							o = obj;
						}
						else{
							o = getObj(ar[0]);
						}
						ar[0] = 'o';
						expr1 += ar.join('.');
						
					}
					else {
						expr1 = e[2];
					}
				}
				else{
					expr1 = '';
				}
				if(e[3] != undefined){
					if(e[3].indexOf('.') >= 0){
						var ar = e[3].split('.');
						if(ar[0] == 'this'){
							c = obj;
						}
						else{
							c = getObj(ar[0]);
						}
						ar[0] = 'c';
						expr2 += ar.join('.');
					}
					else{
						expr2 = e[3];
					}
				}
				else{
					expr2 = '';
				}
				var op = ops[e[1]];
				if(op.indexOf('x') >= 0){
					op = op.replace('x', expr1);
				}
				
				
				if(op.indexOf('y') >= 0){
					op = op.replace('y', expr2);
				}
				
				res = eval(op);
				if(alertErrors && !res && e[e.length-1][0]=='_'){
					var m = e[e.length-1].toString();
					alert(e[e.length-1].toString().substring(1).split('_').join(" "));
				}
				
				if(!res){
//					alert(obj.name + " == " + res);
					notifyError(obj);
				}
				else{
					clearError(obj);
				}
			}
		}
	}
	return res;
}

var clearError = function(obj){
	if(isInput(obj)){
		if(errorClass > ' '){
			obj.className = trim(obj.className.toString().replace(errorClass, ''));
		}
//		errorCallbackFunction(obj,false);
	}
}

var notifyError = function(obj){
	if(isInput(obj)){
		if(errorClass > ' '){
			obj.className += ' ' + errorClass;
		}
//		errorCallbackFunction(obj,true);
	}
}

var trim = function (stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

var errors = 0;
var errorCallbackFunction = function(fieldId, valid){
	if(typeof fieldId == typeof "string"){
		fieldId = getObj(fieldId);
	}
	else{
	}
	if(!fieldId){
	}
	else{
		
		var x = getObj(fieldId.name);
		var e = x.style.display == 'none';
		x.style.display = valid ? "" : "none";
		errors += valid && e ? 1:0;
		errors = errors >= 0 ? errors : (-1) * errors;
	}
	var y = getObj('errorList');
	if(errors > 0){
		y.style.display =  "";
	}
	else{
		y.style.display =  "none";
	}
}

function checkMail(x){
	var res = ((/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/).test(x));
	return res;
}

