function formatar(src,mask,validador,somenteNumero,e)
{
	var tecla = (e.keyCode) ? e.keyCode : e.charCode
	if(tecla != 8)
	{
		string = src.value
		aux = ''
		
		if(string == '') return
		
		if(somenteNumero == 's')
		{
			string = string.replace(/\D/gi,'')
		}
		
		val = mask.replace(validador,'')
		for(i = 0; i < val.length; i++)
		{
			string = string.replace(val.charAt(i),'')
		}
		
		mask = mask.split('')
		string = string.split('')
		
		v = Array(mask.length)
		pV = 0
		pString = 0
		
		for (pMask = 0; pMask < mask.length; pMask++)
		{
			if(mask[pMask] != validador)
			{
				v[pV] = mask[pMask]
				pV++
			}
			else
			{
				v[pV] = string[pString]
				pV++
				pString++
			}
			
			if(pString > string.length)
				break
		}
	
		src.value = v.join('')
	}
}

function somenteNumero(e)
{
	var tecla = (e.keyCode) ? e.keyCode : e.charCode
	if((tecla > 47 && tecla < 58) || tecla == 13 || tecla == 0)
		return true
	else
	{
		if (tecla != 8) return false;
		else return true;
	}
}

function ConsisteEmail(valor) 
{
	var reg = /\w{1,}[@]\w{1,}[.]\w{1,}/
	return reg.test(valor);
}

function validaEmail(campo)
{   
	if (!ConsisteEmail(campo.value)  && campo.value != '')
	{
		alert("Favor Digitar o Email Corretamente.");
		campo.focus();
		return false
	}
	return true
}

function validacpf(campo)
{ 
	var i;
	s = campo.value.replace(/\D+/g, '')
//	s = cpf.value; 
	var c = s.substr(0,9); 
	var dv = s.substr(9,2); 
	var d1 = 0; 

	for (i = 0; i < 9; i++)
	{ 
		d1 += c.charAt(i)*(10-i);
	} 

	if (d1 == 0)
	{
		alert("CPF Invalido")
		campo.focus();
		return false;
	}

	d1 = 11 - (d1 % 11); 
	if (d1 > 9) d1 = 0;

	if (dv.charAt(0) != d1)
	{
		alert("CPF Invalido")
		campo.focus();
		return false;
	}

	d1 *= 2;

	for (i = 0; i < 9; i++)
	{
		d1 += c.charAt(i)*(11-i);
	}

	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;

	if (dv.charAt(1) != d1)
	{
		alert("CPF Invalido")
		campo.focus();
		return false;
	}

	return true;
} 

function validacnpj(campo) {
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais, cnpj = campo.value.replace(/\D+/g, '');
	digitos_iguais = 1;
	if (cnpj.length != 14) {
		alert('CNPJ Invalido');
		campo.focus();
		return false;
	}
	
	for (i = 0; i < cnpj.length - 1; i++)
		if (cnpj.charAt(i) != cnpj.charAt(i + 1)) {
			digitos_iguais = 0;
			break;
		}
		if (!digitos_iguais) {
			tamanho = cnpj.length - 2
			numeros = cnpj.substring(0,tamanho);
			digitos = cnpj.substring(tamanho);
			soma = 0;
			pos = tamanho - 7;
			for (i = tamanho; i >= 1; i--) {
				soma += numeros.charAt(tamanho - i) * pos--;
				if (pos < 2)
					pos = 9;
			}
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			if (resultado != digitos.charAt(0)) {
				alert('CNPJ Invalido');
				campo.focus();
				return false;
			}
	
			tamanho = tamanho + 1;
			numeros = cnpj.substring(0,tamanho);
			soma = 0;
			pos = tamanho - 7;
			for (i = tamanho; i >= 1; i--) {
				soma += numeros.charAt(tamanho - i) * pos--;
				if (pos < 2)
					pos = 9;
			}
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			if (resultado != digitos.charAt(1)) {
				alert('CNPJ Invalido');
				campo.focus();
				return false;
			} else {
				//alert('CNPJ  OK !');
				return true;
			}
		} else {
			alert('CNPJ Invalido');
			campo.focus();
			return false;
		}
} 

function validaForm(frm, excecoes, validados)
{
	printConfig(frm)
	
	Array.prototype.busca = function busca(txt)
	{
		var i
		for(i = 0; i < this.length; i++)
		{
			if(this[i] == txt)
				return true
		}
		return false
	}
	
	if(excecoes)
		excecoes = excecoes.split(',')
	else
		excecoes = new Array()
		
	if(validados)
		validados = validados.split(',')
	else
		validados = new Array
	
	for(i = 0; i < frm.length; i++)
	{
		obj = frm.elements[i]
		
		if(excecoes.length > 0)
		{
			if(!excecoes.busca(obj.name))
			{
				if(!tagName(obj))
					return false
			}
		}
		else if(validados.length > 0)
		{
			if(validados.busca(obj.name))
			{
				if(!tagName(obj))
					return false
			}
		}
		else
		{
			if(!tagName(obj))
				return false
		}
	}
	return true
}

function tagName(obj)
{
	switch(obj.tagName)
	{
		case 'SELECT':
			if(obj.value == '')
			{
				obj.focus()
				alert('Selecione o campo ' + obj.name)
				return false
			}
			return true
			break
		case 'TEXTAREA':
			if(obj.value == '')
			{
				obj.focus()
				alert('Preencha o campo ' + obj.name)
				return false
			}
			return true
			break
		case 'INPUT':
			return input(obj)
			break
	}
}

function input(obj)
{
	switch(obj.type)
	{
		case "hidden":
		case "image":
		case "button":
			return true
			break
		case "file":
			if(obj.value == '')
			{
				obj.focus()
				alert('Selecione um arquivo ' + obj.name)
				return false
			}
			return true
			break
		case "password":
			if(obj.value == '')
			{
				obj.focus()
				alert('Digite a ' + obj.name)
				return false
			}
			return true
			break
		case "text":
			if(obj.value == '')
			{
				obj.focus()
				alert('Preencha o campo ' + obj.name)
				return false
			}
			return true
			break
		case "checkbox":
			var checkbox = document.getElementsByName(obj.name)
			var i, checked = 0
			for(i = 0; checkbox.length; i++)
			{
				if(checkbox[i].checked)
					checked = 1
			}
			if(checked == 0)
			{
				obj.focus()
				alert('Selecione o campo ' + obj.name)
				return false
			}
			else
				return true
			break
		case "radio":
			var radio = document.getElementsByName(obj.name)
			var i, checked = 0
			for(i = 0; i < radio.length; i++)
			{
				if(radio[i].checked)
					checked = 1
			}
			if(checked == 0)
			{
				obj.focus()
				alert('Selecione o campo ' + obj.name)
				return false
			}
			else
				return true
			break
		default:
			return true
			break
	}
}

function printConfig(frm)
{
	if(frm.print_config)
	{
		var txtConfig = ""
		for(i = 0; i < frm.length; i++)
		{
			if(frm.elements[i].tagName == 'INPUT')
			{
				if(frm.elements[i].type != 'hidden')
				{
					if(txtConfig.match("," + frm.elements[i].name + ',') == null)
					{
						txtConfig += frm.elements[i].name + ','
					}	
				}
			}
			else
			{
				if(txtConfig.match("," + frm.elements[i].name + ',') == null)
				{
					txtConfig += frm.elements[i].name + ','
				}
			}
		}
		frm.print_config.value = txtConfig
	}
}

function abrePopup(url, largura, altura)
{
	pTop  = (window.screen.height / 2) - (altura / 2);
	pLeft = (window.screen.width / 2) - (largura / 2);
	window.open(url, 'popup', 'width=' + largura + ', height=' + altura + ', top=' + pTop + ', left=' + pLeft + ', scrollbars=yes');
}

function $(id)
{
	return document.getElementById(id)
}

function httpRequest(pag, func)
{

	if (window.XMLHttpRequest)
		xml = new XMLHttpRequest()
	else if (window.ActiveXObject)
		xml = new ActiveXObject("Microsoft.XMLHTTP")

	if (xml)
	{
		xml.onreadystatechange = function() 
		{
			if (xml.readyState == 4 && (xml.status == 200 || xml.status == 304))
			{
					eval(func)
			}
		};
		
		try
		{
			xml.open('POST', pag, true);
			xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-1');
			xml.send(null);
		}
		catch (e) {}
	}
}