/**
 *
 * CBOV Framework, objects for development in php
 * Copyright (C) 2005 Cassiano B.O Velloso
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * You can contact Cassiano B.O Velloso by the e-mail cbovATcbovDOTorg 
 * 
 * 01 November of 2005, Cassiano B.O Velloso
 * 
 */
var alertaErros=false;
function alerta()
{
	if(alertaErros)
	{
  		alert("Erro!\n Campo não pode ser vazio\n Ou esta com o seu formato errado.");
		alertaErros=false;
	}
}

function testDate(val)
{
 	var erDate = /([0][1-9]|[1-2][0-9]|[3][0-1])\/([0][0-9]|[1][0-2])\/[1-2][09][0-9]{2}/;
	var erNumeric= /^[0-9\/]{0,}/
	if(val.value.length)
	{
		if(erNumeric.test(val.value))
		{
			if(val.value.length >= 10 || alertaErros) //Se finalizou entao valida por inteiro
			{
				val.value = val.value.substr(0, 10);
				if(erDate.test(val.value))
					return true;
				else
				{
					alert("Data Invalida!\n Formato DD/MM/YYYY");
					alertaErros=false;
					val.value = val.value.substr(0, val.value.length-1);
					val.focus();
					return false;
				}
			}
			
			//Coloca os /
			if(val.value.length == 2||val.value.length == 5)
				val.value = val.value + "/";
		}
		else
			val.value = val.value.substr(0, val.value.length-2);
	}
	else
		return true;
}

function testHour(val)
{
 	var erHora = /([0-1][0-9]|[2][0-3])(:[0-5][0-9]){2}/;
	var erNumeric= /^[0-9:]{0,}$/
 	if(erNumeric.test(val.value))
 	{
 		if(val.value.length >= 8||alertaErros) //Se finalizou entao valida por inteiro
 		{
	 		val.value = val.value.substr(0, 8);
	 		if(erHora.test(val.value))
 				return true;
	 		else
	 		{
	 			alert("Hora Invalida!\n Formato HH:MM:SS");
				alertaErros=false;
				val.focus();
				return false;
	 		}
	 	}
	 	
	 	//Coloca os :
	 	if(val.value.length == 2||val.value.length == 5)
 			val.value = val.value + ":";
 	}
 	else
 		val.value = val.value.substr(0, val.value.length-2);
}

function notNull(val)
{
	var er = /^$/;
	if(er.test(val.value))
	{
		alerta();
		val.focus();
		return false;
	}
	else
		return true;
}

function testNumeric(val)
{
	erNumeric= /^[0-9\-.]{0,}$/;
	//Troca , por . 
	if(val.value.charCodeAt((val.value.length-1)) == 44)
		val.value = val.value.substr(0, val.value.length-1) + ".";
	if(erNumeric.test(val.value))
	{
		return true;
	}
	else
	{
		if(val.value.length > 0)
	 		val.value = val.value.substr(0, val.value.length-1);
		alerta();
		val.focus();
		return false;
	}
}


function testIp(val)
{
	
	var erIp = /(([12][0-5][0-9]){1}|[0-9]{2}|[1-9]{1})(\.([12][0-5][0-9]){1}|[0-9]{1,2}){2}\.(([12][0-5][0-9]){1}|[0-9]{2}|[1-9]{1})/;
	if(val.value.length>0)
	{	
		if(erIp.test(val.value))
			return true;
		else
		{
			alerta();
			val.focus();
			return false;
		}
	}
}

