// JavaScript Document


/* This function takes a string (str) and removes any
space characters at the beginning or the end of the string */
function trim(str)
{
	return str.replace(/^\s+|\s+$/g, '');
}

/* This function verifies that a field is not empty.
It takes the id of the field being validated (fld), and a string with 
the name of the field (fldname).

For example:
checkEmpty(name_tf, 'Name');

If the field is not empty, it returns an empty string. 
Otherwise, it returns 'fldname' plus an error message, 
followed by a newline character */
function checkEmpty(fld, fldname) {
	var error = "";
	trim(fld.value);
    if (fld.value.length == 0) {
        fld.style.background = '#FFCC33'; 
        error = fldname + " no puede estar vacio" + "\n";
    } else {
       	fld.style.background = 'White';
    }
    return error;  
}

/* This function verifies that a minimun number of checkboxes have
been checked from a selection. 
It takes the reference to the checkbox selection (fld_name), and the
minimum number of checkboxes in it that must be checked (min_checked).

For example:
validateCheckbox(this.servicio, 3);

If there are at least 'min_checked' checkboxes selected, then an
empty string is returned. Otherwise, an error message indicating
min_checked and ended in a newline character is returned. */
function validateCheckbox(fld, min_checked) {
	var error = "";
	var count = 0;
	
	for (var j=0; j < fld.length; j++) {
		if (fld[j].checked)
			count++;
	}
	if (count < min_checked)
		error = "El minimo de servicios que debe seleccionar es  " + min_checked + "\n";

	return error;
}


/* This function verifies that the content in an non-empty field is correct.
It takes the id of the field being validated (fld), a string with 
the name of the field (fldname), a regular expression with the
forbidden characters for the field (reg_exp), and a string 
(errormsg) that gives a brief message about the error.

For example:
validateString(name_tf, "Name", new RegExp("\\W"), "only accepts alphanumeric characters");

If the field does not contain any of the forbidden caracters, 
it returns an empty string. Otherwise, it returns 'fldname'
plus an error message, followed by a newline character */
function validateString(fld, fldname, reg_exp, errormsg) {
//	var reg_exp = /[\W_\d]/;
	var error = "";
	trim(fld.value);
	if (reg_exp.test(fld.value)) {
        fld.style.background = '#FFCC33';
		error = fldname + errormsg + "\n";
	}
	else {
       	fld.style.background = 'White';
	}
    return error;  
}

/* This function verifies that the content in an non-empty field 
corresponds to a valid e-mail address.
It takes the id of the field being validated (fld), and a string
with the name of the field (fldname).

For example:
validateEmail(name_tf, "Name");

If the field is a valid e-mail address, it returns an empty string. 
Otherwise, it returns 'fldname' plus an error message, followed by
a newline character */
function validateEmail (fld, fldname) {
	var error = "";
	var email = /^[^@]+@[^@\.]+\.[^@]*\w\w$/  ;
	if (!email.test(fld.value)) {
		fld.style.background = '#FFCC33';
		error = fldname + " contiene una direccion de correo electronico invalida\n";
	}
	else {
		fld.style.background = 'White';
	}
//	else {
//		var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
//		if (!email2.test(tfld))
//			error = fldname + " podria contener una direccion de correo electronico invalida";
//	}
	return error;	
}