// FORM VALIDATOR
// v 1.2
// @ Ovidiu Cosma
// 

var fields = new Array();

function s_f_i_v(form, a){
	var submit = true;
	for (i = 0;i < a.length ;i++ )
	{
		name   = a[i].name;
		msg    = a[i].msg;
		str    = form[name].value;
		reg    = a[i].reg;
		if (reg){
			submit = _reg(reg, form, msg);
			if (!submit){
				break;
			}
		}
	}
	if (submit) {
		form.submit();
		//alert("OK");
	}	
}

function _inner(id, val){
	
	document.getElementById(id).innerHTML = val;

}

function _reg(reg, form, mg){
	
	submit     = true;
	type       = reg.match(/^\w{3,}/);
	var first  = reg.match(/\d{1,}/g);	
	var eq     = reg.split(/eq=/);
	eq         = eq[1]	
	//alert(eq[1]);
	
	if(first != null){
		a      = first[0] != undefined ? first[0] : '';
		b      = first[1] != undefined ? first[1] : '';
	}else{
		var a = "";
		var b = "";
	}
	
	var reg = false;
	
	if (type == 'int'){
		
		reg = '^\\d{' + a + ',' + b + '}$';
		if (a && !b){
			msg = 'Campul ' + name.toUpperCase() + ' trebuie sa contina un numar de minim ' + a + ' caractere!';
		}
		if (!a && b){
			msg = 'Campul ' + name.toUpperCase() + ' trebuie sa contina un numar maxim ' + b + ' caractere!';
		}
		if (a && b){
			msg = 'Campul ' + name.toUpperCase() + ' trebuie sa contina un numar de la ' + a + ' la ' + b + ' caractere!';
		}
	}
	else if (type == 'str' || type == 'user'){

		//reg = '\.{' + a + '?,' + b + '?}$';
		if (a && !b){
			msg = 'Campul ' + name.toUpperCase() + ' trebuie sa contina minim ' + a + ' caractere!';
		}
		if (!a && b){
			msg = 'Campul ' + name.toUpperCase() + ' trebuie sa contina maxim ' + b + ' caractere!';
		}
		if (a && b){
			msg = 'Campul ' + name.toUpperCase() + ' trebuie sa contina minim ' + a + ' si maxim ' + b + ' caractere!';
		}
	}
	else if ( type == 'email'){

		reg = '^.{1,}@[a-zA-Z0-9]{1,}\\.[a-z.]{2,8}$';
		msg = 'Campul ' + name.toUpperCase() + ' trebuie sa contina o adresa valabila de e-mail!';
	}
	
	if (!eq){
		msg = mg ? mg : msg;
		reg = new RegExp(reg);
		
		if (!str.match(reg) && type != 'str' && type != 'user') {
			//_inner(name, "Nu e OK!");
			alert(msg);
			form[name].focus();
			submit = false;
		}
	
		if (type == 'str' || type == 'user'){
			if (a && str.length < a){
				alert(msg);
				form[name].focus();
				submit = false;
			}
		
			if (b && str.length > b){
				alert(msg);
				form[name].focus();
				submit = false;
			}
		}
	}	

	if (eq){
		if (form[eq].value != form[name].value){
			alert(msg);
			form[name].focus();
			submit = false;
		}
	}

	//_inner(name, "Validated OK!");
	//alert("OK - " + str.match(reg));
		

	return submit;

}

function validate_select(frm, name, msg){

	var x=document.getElementById("nota")
	myindex = x.selectedIndex;
	if (myindex == '0') {
		alert(msg);
		return false;
	}else{
		return true;
	}
}

var checkflag = "false";
function check_all(field) {
	if (checkflag == "false") {
		for (i = 0; i < field.length; i++) {
			field[i].checked = true;
		}
		checkflag = "true";
		return "Uncheck All"; 
	}
	else {
		for (i = 0; i < field.length; i++) {
			field[i].checked = false; 
		}
		checkflag = "false";
		return "Check All"; 
	}
}