var proceed = 2;  

function commonCheck    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
	proceed=2;
	var seguir = proceed;
	with (valfield)
	{
		//alert("*"+value+"*");
		if (value==null||value=="") {
			if (required==true) {
				seguir = 0;
			}
			else {
				proceed = 1;
				seguir = 1;
			}
		}
	}
	return seguir;
}

function checkName (strng) {
	var error = "0";
	if (strng.length > 500) {
      error = "La longitud máxima es de 500 caracteres.";
    }
	return error;
}

function checkPassword (strng) {
    var illegalChars = /[\W_]/; // allow only letters and numbers
	var error = "0";
	if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    }
	/*else if (!((strng.search(/[a-z]+/) > -1) && (strng.search(/[A-Z]+/) > -1) && (strng.search(/[0-9]+/) > -1))) {
	  error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
	}*/
	return error;
}

function checkEmail(strng) {
	var emailFilter=/^.+@.+\..{2,3,4,6}$/;
	emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
	var error = "0";
	if (!(emailFilter.test(strng))) { 
		   error = "Por favor introduzca una direccion de Email válida.\n";
	}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
	   error = "La dirección de Email contiene caracteres no válidos.\n";
	
	}
	return error;
}

function checkPhone (strng) {
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
	var error = "0";
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	   error = "El número de teléfono contiene caracteres no válidos.\n";
	}
	return error;
}

function validateOnSubmit(formulario) {
	var cont = 0;
	
	var error = new Array(33);
	for (y=0;y<33;y++) {
		error[y]="0";
	}
	
	for (x = 0 ; x < campo.length ; x++) 
	{
		
		field=document.getElementById(campo[x]);
		//alert(field.name + " " + field.value);
		var stat = commonCheck (field, campo[x], obligado[x]);
		if (stat != proceed) {
			error[x]="campo requerido.";
		}
		if (stat == proceed && stat==2) {
			var filtro;
			//alert("analizamos "+campo[x]);
			switch (tipo[x]) {
				case 1://Alphanumerico
					filtro = /[\W_]/  ;
					error[x] = checkName(field.value);
					break;
				case 2://Email
					filtro = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
					error[x] = checkEmail(field.value);
					break;
				case 3://Alphanumerico comparado (contrasena)
					filtro = /[\W_]/;
					error[x] = checkPassword(field.value);
					break;
				case 4://Numerico
					filtro = /^\+?[0-9 ()-]+[0-9]$/  ;
					error[x] = checkPhone(field.value);
					break;
			}
		}
		
	}
	
	enc = 0;
	for (x = 0 ; x < error.length ; x++) 
	{
		if (error[x]!="0" && enc ==0) 
		{
			alert(campo[x]+": "+error[x]);
			enc=1;		
		}
	}
	
	if (enc==0) {
		//alert("SALGO 1");
		return true;
		//document.getElementById(formulario).submit();
	}
	else
	{
		//alert("SALGO 2");
		return false;	
	}
}

