function vRad(btn,de) {
	radioOption = -1;
	for (counter=0; counter<btn.length; counter++) {
	if (btn[counter].checked) radioOption = counter;
	}
	if (radioOption == -1){
		alert('Seleccione una opcion de ' + de);
		return false;
	}
	else return true;
}

function vI(dato){
	if(dato.value.length == 0){
		return false;
	}
	return true;
}

function vIng(dato){
	if(dato.value.length == 0){
		alert('El dato "' + dato.name + '" debe ser ingresado.');
		return false;
	}
	return true;
}

function vNumIng(valor){
	if(!vIng(valor))
		return false;
	if(!vNum(valor.value)){
		alert('El dato "' + valor.name + '" debe ser un numero entero y positivo.');
		return false;
	}
	return true;
}

function vFloatIng(valor){
	if(!vIng(valor))
		return false;
	if(!vFloat(valor.value)){
		alert('El dato "' + valor.name + '" debe ser un numérico y positivo.');
		return false;
	}
	return true;
}

function vNum(valor){ 
	//Valida que el dato ingresado sea numérico.
	if(valor.length == 0) //Campo no obligatorio
		return true; 
		
	var checkOK = "0123456789"; 
	var checkStr = valor; 
	var allValid = true; 
	var decPoints = 0; 
	var allNum = ""; 
	
	for (i = 0; i < checkStr.length; i++) { 
		ch = checkStr.charAt(i); 
		for (j = 0; j < checkOK.length; j++) 
			if (ch == checkOK.charAt(j)) 
				break; 
		if (j == checkOK.length) { 
			allValid = false; 
			break; 
		} 
		allNum += ch; 
	} 
	
	if (!allValid) 
		return false; 
	
	return true;
}

function vFloat(valor){ 
	//Valida que el dato ingresado sea numérico.
	if(valor.length == 0) //Campo no obligatorio
		return true; 
		
	var checkOK = "0123456789."; 
	var checkStr = valor; 
	var allValid = true; 
	var decPoints = 0; 
	var allNum = ""; 
	
	for (i = 0; i < checkStr.length; i++) { 
		ch = checkStr.charAt(i); 
		for (j = 0; j < checkOK.length; j++) 
			if (ch == checkOK.charAt(j)) 
				break; 
		if (j == checkOK.length) { 
			allValid = false; 
			break; 
		} 
		allNum += ch; 
	} 
	
	if (!allValid) 
		return false; 
	
	return true;
}

function vFecIng(fecha){
	if(vIng(fecha) && vFec(fecha.value))
		return true;
	return false;
}

function vFec(fecha){
	
	if(fecha.length == 0 )
		return true; //Campo no obligatorio
			 
	//DIA -----------------------------------------
	if(!vNum(fecha.charAt(0)))
		return false;

	if(fecha.charAt(0)==3){
		if(fecha.charAt(1)!=0 && fecha.charAt(1)!=1){
			return false;
		}
	}
	else if(fecha.charAt(0)==0){
		if(fecha.charAt(1)==0){
			return false;
		}
	}
	else if(fecha.charAt(0)>0 && fecha.charAt(0)<3){
		if(!(vNum(fecha.charAt(1)) && fecha.charAt(1)>=0 && fecha.charAt(1)<=9)){
			return false;
		}
	}
	else{
		return false;
	}
	//MES -----------------------------------------
	if(fecha.charAt(3)==0){
		if(!(vNum(fecha.charAt(4)) && fecha.charAt(4)>0 && fecha.charAt(4)<=9)){
			return false;
		}
	}
	else if(fecha.charAt(3)==1){
		if(fecha.charAt(4)!=0 && fecha.charAt(4)!=1 && fecha.charAt(4)!=2){
			return false;
		}
	}
	else{
		return false;
	}
	//AÑO -----------------------------------------
	if(!(vNum(fecha.charAt(6)) && fecha.charAt(6)>=0 && fecha.charAt(6)<=9)){
			return false;
	}
	if(!(vNum(fecha.charAt(7)) && fecha.charAt(7)>=0 && fecha.charAt(7)<=9)){
			return false;
	}
	//SLASHES
	if(fecha.charAt(2)!="/" || fecha.charAt(5)!="/"){
		return false;
	}
		
	return true;
}

//FECHAS -----------------------------------------
function Fecha(fec){
	var fecha = fec.value;
	if(fecha.length < 8){
		if(vFec(fecha.substr(0,2)+'/'+fecha.substr(2,2)+'/'+fecha.substr(4,2))){
			return fecha.substr(0,2)+'/'+fecha.substr(2,2)+'/'+fecha.substr(4,2);
		}
		else if(vFec(fecha.substr(0,2)+'/'+fecha.substr(3,2)+'/'+fecha.substr(5,2))){
			return fecha.substr(0,2)+'/'+fecha.substr(3,2)+'/'+fecha.substr(5,2);
		}
		else if(vFec(fecha.substr(0,2)+'/'+fecha.substr(2,2)+'/'+fecha.substr(5,2))){
			return fecha.substr(0,2)+'/'+fecha.substr(2,2)+'/'+fecha.substr(5,2);
		}
	}
	else if(vFec(fecha)){
		return fecha;
	}
	alert('La fecha debe ser del format dd/mm/aa .');
	return '';
}

//MAIL -----------------------------------------
function vMail(mail){ 
//Valida que el dato ingresado sea un mail
	if(mail.length == 0)
		return true; //Campo no obligatorio
	
	if (mail.indexOf('@', 0) == -1 || mail.indexOf('.', 0) == -1 || mail.indexOf(' ',0)!= -1){ 
		alert("Direccion de e-mail invalida"); 
		return false; 
	}
	return true;
}

function vFoto(foto){ 
	//Valida que el dato ingresado sea una foto gif o jpg
	if(foto.length == 0)
		return true; //Campo no obligatorio
	if(foto.length < 5)
		return false;
	var tipo=foto.substring(foto.length-3,foto.length);
	
	if (foto.indexOf('ñ',0) != -1){
		alert("La Foto no puede contener 'ñ' ni carácteres poco comunes.");
		return false;
	}
	
	if (foto.charAt(foto.length-4)!='.' || (tipo!='gif' && tipo!='jpg' && tipo!='GIF' && tipo!='JPG')){
		alert("Foto invalida. Debe tener el formato de 'nombre.gif' o 'nombre.jpg'."); 
		return false; 
	}
	
	return true;
}
