function trim(str)
{
    return String(str).replace(/^\s+/,"").replace(/\s+$/,"");
}


function getQuantidadeDiasMes(mes,ano)
{
	if ((mes == 1) || (mes == 3) || (mes == 5) || (mes == 7) || (mes == 8) || (mes == 10) || (mes == 12))
		return (31);
	else
		if (mes == 2)
			if (isBissexto(ano))
				return 29;
			else
				return 28;
		else
			if (mes == 0)
				return 31;
			else
				return 30;
}

function validateDate(field)
{
	var mask = /\d{2}\/\d{2}\/\d{4}/;
	if (!mask.test(field))
	{
		return false;
	}

	var day = parseInt(field.substring(0,2),10);
	var month = parseInt(field.substring(3,5),10);
	var year = parseInt(field.substring(6,10),10);

	// verifica o dia valido para cada mes
	if ((day < 1)||(day < 1 || day > 30) && ( month == 4 || month == 6 || month == 9 || month == 11 ) || day > 31)
	{
		return false;
	}

	// verifica se o mes e valido
	if (month < 1 || month > 12 )
	{
		return false;
	}

	// verifica se e ano bissexto
	if (month == 2 && ( day < 1 || day > 29 || ( day > 28 && (parseInt((year / 4),10) != year / 4))))
	{
		return false;
	}

	return true;
}

function validateEmail(email)
{
	var mask = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	if (!mask.test(email))
	{
		return false;
	}
	return true;
}

function subDate(data1,data2)
{
	var dia1 = parseInt(data1.substr(0,2),10);
	var mes1 = parseInt(data1.substr(3,2),10);
	var ano1 = parseInt(data1.substr(6,4),10);

	var dia2 = parseInt(data2.substr(0,2),10);
	var mes2 = parseInt(data2.substr(3,2),10);
	var ano2 = parseInt(data2.substr(6,4),10);

	var diasMes = 0;
	var totalDias = 0;

	if (ano1 == ano2)
	{
		if (mes1 == mes2)
		{
			if (dia1 < dia2)
			{
				totalDias = dia2 - dia1;
			}
		}
		else
		{
			if (mes1 < mes2)
			{
				diasMes = getQuantidadeDiasMes(mes1,ano1);
				totalDias = diasMes - dia1 + dia2 + subMeses(mes1+1,mes2,ano1);
			}
		}
	}
	else
	{
		if (ano1 < ano2)
		{
			totalDias = getQuantidadeDiasMes(mes1) - dia1 + subMeses(mes1+1,12) + subAnos(ano1+1,ano2) + subMeses(0,mes2) + dia2;
		}
	}
	return totalDias;
}

function subMeses(mes1,mes2,ano)
{
	var totalDias = 0;
	for(i=mes1;i<mes2;i++)
	{
		totalDias += getQuantidadeDiasMes(i,ano);
	}
	return totalDias;
}

function subAnos(ano1,ano2)
{
	var totalDias = 0;
	var i = 0;
	for(i=ano1; i<ano2; i++)
	{
		totalDias += subMeses(0,12,i);
	}
	return totalDias;
}

function validateCNPJ(field)
{
	var cnpj = field.value;
	if (trim(field.value) == "")
	{
		return true;
	}
	var erro = new String;
	if (cnpj.length < 18)
		erro = "CNPJ Inválido!";
	if ((cnpj.charAt(2) != ".") || (cnpj.charAt(6) != ".") || (cnpj.charAt(10) != "/") || (cnpj.charAt(15) != "-"))
		if (erro.length == 0)
			erro = "CNPJ Inválido!";
	if (cnpj == '00.000.000/0000-00')
		erro = "CNPJ Inválido!";
	//substituir os caracteres que não são números
	if(document.layers && parseInt(navigator.appVersion) == 4)
	{
		var x = cnpj.substring(0,2);
		x += cnpj. substring (3,6);
		x += cnpj. substring (7,10);
		x += cnpj. substring (11,15);
		x += cnpj. substring (16,18);
		cnpj = x;
	}
	else
	{
	   cnpj = cnpj. replace (".","");
	   cnpj = cnpj. replace (".","");
	   cnpj = cnpj. replace ("-","");
	   cnpj = cnpj. replace ("/","");
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(cnpj))
		erro = "CNPJ Inválido!";
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++)
	{
	   a[i] = cnpj.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2)
		a[12] = 0
	else
		a[12] = 11-x
	b = 0;
	for (y=0; y<13; y++)
	   b += (a[y] * c[y]);
	if ((x = b % 11) < 2)
		a[13] = 0;
	else
		a[13] = 11-x;
	if ((cnpj.charAt(12) != a[12]) || (cnpj.charAt(13) != a[13]))
	   erro ="CNPJ Inválido!";
	if (erro.length > 0)
	{
	   alert(erro);
	   field.focus();
	   return false;
	}
	return true;
}

function validateCPF(field) {
	if (trim(field.value) != '')
	{
		var erro = '';
		if ((field.value == '111.111.111-11') || (field.value == '222.222.222-22') ||
			(field.value == '333.333.333-33') || (field.value == '444.444.444-44') ||
			(field.value == '555.555.555-55') || (field.value == '666.666.666-66') ||
			(field.value == '777.777.777-77') || (field.value == '888.888.888-88') ||
			(field.value == '999.999.999-99') || (field.value == '000.000.000-00'))
		{
			erro = 'CPF Inválido!';
		}
		var cpf = field.value;
		while (cpf.indexOf(".") != -1)
			cpf = cpf.replace(".","");
		while (cpf.indexOf("-") != -1)
			cpf = cpf.replace("-","");
		while (cpf.indexOf(" ") != -1)
			cpf = cpf.replace(" ","");
		var cpfCalc = cpf.substr(0,9);
		var cpfSoma = 0;
		var cpfDigit = 0;
		var digit = "";
		for (i = 0; i < 9; i++)
		{
			cpfSoma = cpfSoma + parseInt(cpfCalc.charAt(i)) * (10 - i)
		}
		cpfDigit = 11 - cpfSoma%11;
		if (cpfDigit > 9)
		{
			cpfCalc = cpfCalc + "0";
		}
		else
		{
			digit = digit + cpfDigit;
			cpfCalc = cpfCalc + digit.charAt(0);
		}
		cpfSoma = 0;
		for (i = 0; i < 10; i++)
		{
			cpfSoma = cpfSoma + parseInt(cpfCalc.charAt(i)) * (11 - i)
		}
		cpfDigit = 11 - cpfSoma%11;
		if (cpfDigit > 9)
		{
			cpfCalc = cpfCalc + "0";
		}
		else
		{
			digit = "";
			digit = digit + cpfDigit;
			cpfCalc = cpfCalc + digit.charAt(0);
		}
		if (cpf != cpfCalc)
		{
			erro = 'CPF Inválido!';
		}
		if (erro.length > 0)
		{
		   alert(erro);
		   field.focus();
		   return false;
		}
	}
	return true;
}

function getCurrentDate()
{
	var day;
	var month;
	var year;
	var currentDate = new Date();

	day = parseInt(currentDate.getDate(),10);
	month = parseInt(currentDate.getMonth()+1,10);

	if (day < 10)
	{
		day = "0"+day;
	}
	if (month < 10)
	{
        month = "0"+month;
	}

	year = currentDate.getFullYear();
	return (day+"/"+month+"/"+year);
}

function validateTime(field)
{
	var mask = /\d{2}\:\d{2}/;
	if (!mask.test(field))
	{
		return false;
	}

	var hour = parseInt(field.substring(0,2),10);
	var minute = parseInt(field.substring(3,5),10);

	// verifica se a hora está no intervalo correto
	if ((hour < 0) || (hour > 23))
	{
		return false;
	}

	// verifica se o minuto está no intervalo correto
	if ((minute < 0) || (minute > 59))
	{
		return false;
	}

	return true;
}


