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)
	{
		var col = document.getElementById("colaborador");
		//alert("*"+value+"*");
		if (value==null||value=="" && (col != null)) 
		{
			//alert(name+"\n"+col.checked );
			if (required==true || ( name=="Telefono" && col.checked) || ( name=="Direccion" && col.checked) ) 
			{
				seguir = 0;
			}
			else 
			{
				proceed = 1;
				seguir = 1;
			}
		}
	}
	return seguir;
}

function checkName (strng) {
	var error = "0";
	if (strng.length > 500) {
      error = "Max size is 500 chars";
    }
	return error;
}

function checkPassword (strng) {
    var illegalChars = /[\W_]/; // allow only letters and numbers
	var error = "0";
	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 = "Please enter a valid email address.\n";
	}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (strng.match(illegalChars)) {
	   error = "The email address contains illegal characters.\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 = "The number contains illegal characters.";
	}
	return error;
}

function validateOnSubmit(formulario) {
	var cont = 0;
	var error = new Array(10);

	for (y=0;y<10;y++) {
		error[y]="0";
	}
	
	for (var x = 0 ; x < campo.length; x++) 
	{
	
		
		var field=document.getElementById(campo[x]);
		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 (contraseña)
					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 (var x = 0 ; x < campo.length; x++) 
	{
		if (error[x]!="0" && enc ==0) {
			alert(campo[x]+": "+error[x]);
			enc=1;		
		}
	}
	if (enc==0) {
		document.getElementById(formulario).submit();
	}
}


function decimal(e)
{
	var keynum;
	var keychar;
	var numcheck;
	var comacheck;
	var pointcheck;
	if(window.event) 
	{
		keynum = e.keyCode;
	}
		else if(e.which) // Netscape/Firefox/Opera
			{
				keynum = e.which;
			}
	keychar = String.fromCharCode(keynum);
	
	numcheck = /\d/;
	pointcheck = /\./;
	comacheck = /\,/;
	//alert(keynum+":"+keychar+"_"+numcheck.test(keychar)+"_"+pointcheck.test(keychar)+"_"+comacheck.test(keychar));
	return (numcheck.test(keychar) || pointcheck.test(keychar) || comacheck.test(keychar) || keynum==8);
}

function vfecha(e)
{
	var keynum;
	var keychar;
	var numcheck;
	var comacheck;
	var pointcheck;
	if(window.event) 
	{
		keynum = e.keyCode;
	}
		else if(e.which) // Netscape/Firefox/Opera
			{
				keynum = e.which;
			}
	keychar = String.fromCharCode(keynum);
	
	numcheck = /\d/;
	barracheck = /\//;
	//alert(keynum+":"+keychar+"_"+numcheck.test(keychar)+"_"+pointcheck.test(keychar)+"_"+comacheck.test(keychar));
	return (numcheck.test(keychar) || barracheck.test(keychar) || keynum==8);
}
function cadena(e)
{
	var keynum;
	var keychar;
	var numcheck;
	if(window.event) 
	{
		keynum = e.keyCode;
	}
		else if(e.which) // Netscape/Firefox/Opera
			{
				keynum = e.which;
			}
	keychar = String.fromCharCode(keynum);
	
	//alert(keynum+":"+keychar);
	numcheck = /\W_/;
	return !numcheck.test(keychar);
}
