/* ------------ Funcao para validacao do form de cadastro de comentario ------------ */
function validaFormComentario(form) {
	// var email =form.email;
	// var confirma_email =form.confirma_email;
	// if(form.email.value == form.confirma_email.value)
	// {
	if (!validaForm(form)) {
		return (false);
	}
	if(!validaEmail(form)){
		return (false);
	}
	// }
	// else{
	// alert('A confirmação do email não está correta!');
	// return(false);
	// }
}
function imprimirDiv(urlBase, divName) {
	var WinPrint = window.open('', 'newwin',
			'toolbar=no,status=no,scrollbars=yes,width=550,height=800');
	var allDivTags = document.getElementsByTagName("div");
	var divTagContent;
	var stylesPath = urlBase + '/system/modules/br.gov.turismo.promocional/resources/css/';

	for ( var i = 0; i < allDivTags.length; i++) {
		if (allDivTags[i].className == divName) {
			divTagContent = allDivTags[i];
			break;
		}
	}

	with (WinPrint.document) {
		write('<html>');
		write('<head>' + '<link rel="stylesheet" type="text/css" href="'
				+ stylesPath + 'destinosGeral.css" />'
				+ '<link rel="stylesheet" type="text/css" href="' + stylesPath
				+ 'mtur_print.css" />' +
				// '<link rel="stylesheet" type="text/css"
				// href="'+stylesPath+'mtur_interna.css" />'+
				// '<link rel="stylesheet" type="text/css"
				// href="'+stylesPath+'mtur_viaja_mais.css" />'+
				'</head>');
		write('<body style="background:#FFF;" onload="print();">');
		write('<div class="printArea">');

		var divConteudo = divTagContent.innerHTML;
		var obj = document.getElementById("idIcones").innerHTML;
		divConteudo = divConteudo.replace(obj, "");

		write(divConteudo);
		write('</div>');
		write('</body>');
		write('</html>');
		close();
		focus();
	}
}
//ATENÇAO: ESTA FUNÇÃO NÃO FUNCIONA NO FIREFOX. NÃO UTILIZAR
function valida(form) {

	for (i = 0; i < form.length; i++) {
		var obg = form[i].obrigatorio;
		var disabled = form[i].disabled;

		if (obg == 1 && !disabled) {
			if (form[i].value == "") {
				var nome = form[i].descricao
				alert("O campo " + nome + " é obrigatório.")
				form[i].focus();
				return false
			} else if (form[i].value == "0") {
				var nome = form[i].descricao
				alert("O campo " + nome + " é obrigatório.")
				form[i].focus();
				return false
			}
		}
	}
	return true
}
//Valida os campos do formulário
function validaForm(form){
	for ( var i = 0; i < form.elements.length; i++) {
		var els = form.elements[i];
		var title = els.title;
		var descricao = "";
		var semErro = true;
		var value = els.value;
		var desabilitado =  form[i].disabled;
		if(!desabilitado){
			if(value == "" || value == "0"){
				for ( var j = 0; j < els.attributes.length; j++) {	
					var atributo = els.attributes[j];
					var nome = atributo.nodeName;
					var valor = atributo.nodeValue;
	
					//Pega a descrição
					if (nome == 'descricao'){
						descricao = valor;
					}
					//Pega o valor obrigatório
					else if(nome == 'obrigatorio' && valor == '1'){
						semErro = false;
					}
					if (descricao != "" && !semErro){
						break;
					}
				}
			}
		}
		if (descricao != "" && !semErro){
			break;
		}
	} // for
	if(!semErro){
		alert('O campo ' + descricao + ' é obrigatório.');
		els.focus();
	}
	return semErro;
}
//Valida campos de email
function validaEmail(form){
	for ( var i = 0; i < form.elements.length; i++) {
		var els = form.elements[i];
		var title = els.title;
		var descricao = "";
		var semErro = true;
		var value = els.value;
		var desabilitado =  form[i].disabled;
		if(!desabilitado){
			for ( var j = 0; j < els.attributes.length; j++) {	
				var atributo = els.attributes[j];
				var nome = atributo.nodeName;
				var valor = atributo.nodeValue;
				if(nome == 'tipoemail' && valor == '1'){
					if (value.indexOf("@") == -1 || value.indexOf(".") == -1 || value == ""
						|| !validaArroba(els) || !validaTamanhoEmail(els)) {
							alert("Insira um e-mail valido!");
							els.focus();
							return false;
					}
				}	
			}
		}
	}
		return true;

}


function checarEmail(email) {
	var valor = email.value;
	if (valor != "") {
		if (valor.indexOf("@") == -1 || valor.indexOf(".") == -1 || valor == ""
				|| !validaArroba(email) || !validaTamanhoEmail(email)) {
			alert("Insira um e-mail valido!");
			email.focus();

		}
	}
}
//Funcion que verifica se o email contem apenas 01 caractere @
function validaArroba(text) {

	var email = text.value;

	var firstIndex = email.indexOf("@");

	if (firstIndex == -1) {
		return false;
	} else {
		var textSubstring = email.substring(firstIndex + 1);

		if (textSubstring.indexOf("@") != -1) {
			return false;
		}
	}

	return true;
}

//verifica se o email esta escrito corretamente
function validaTamanhoEmail(text) {

	var email = text.value;

	var nextIndex = email.indexOf("@") + 1;
	var firstDot = email.indexOf(".");
	var emailSize = email.length;

	if (emailSize - nextIndex < 4 || firstDot == nextIndex) {
		return false;
	} else {

		var emailSubstring = email.substring(firstDot + 1);
		var curDotIndex = emailSubstring.indexOf(".");

		while (curDotIndex != -1) {

			if (curDotIndex + 1 < emailSubstring.length) {
				emailSubstring = emailSubstring.substring(curDotIndex + 1);

				curDotIndex = emailSubstring.indexOf(".");
			} else {
				emailSubstring = emailSubstring.substring(curDotIndex);
				curDotIndex = emailSubstring.indexOf(".");

				if (curDotIndex == 0) {
					curDotIndex = -1;
				}
			}
		}

		if (emailSubstring.length < 2) {
			return false;
		}
	}

	return true;
}

//envento onkeyup
function mascaraCPF(e, CPF) {
    var evt = e || window.event;
    kcode=evt.keyCode;
    if (kcode == 8) return;
    if (CPF.value.length == 3) { CPF.value = CPF.value + '.'; }
    if (CPF.value.length == 7) { CPF.value = CPF.value + '.'; }
    if (CPF.value.length == 11) { CPF.value = CPF.value + '-'; }
}

//valida o CPF digitado
function validarCPF(Objcpf){
	var valor = Objcpf.value;
	if (valor != ""){
	    var cpf = Objcpf.value;
	    exp = /\.|\-/g
	    cpf = cpf.toString().replace( exp, "" ); 
	    var digitoDigitado = eval(cpf.charAt(9)+cpf.charAt(10));
	    var soma1=0, soma2=0;
	    var vlr =11;
	    
	    for(i=0;i<9;i++){
	        soma1+=eval(cpf.charAt(i)*(vlr-1));
	        soma2+=eval(cpf.charAt(i)*vlr);
	        vlr--;
	    }    
	    soma1 = (((soma1*10)%11)==10 ? 0:((soma1*10)%11));
	    soma2=(((soma2+(2*soma1))*10)%11);
	    
	    var digitoGerado=(soma1*10)+soma2;
	    if(digitoGerado!=digitoDigitado){    
	        alert('CPF Invalido!'); 
	    	Objcpf.focus();
	    }
	}
}

function SomenteNumero(e)
{ 
	var key;

	if (window.event) 
	{
		key = event.keyCode;
	}
	else
	{ 
		key = e.which;
	}

	if(key > 47 && key < 58 || key == 8 || key == 0)
	{
		return; 
	}
	else
	{
		if(window.event)
		{
			window.event.returnValue = null; 
		}
		else 
		{
			e.preventDefault();
		}
	}
}

function abrePopup() {
	document.getElementById('siteSombra').style.height = document.body.clientHeight + 'px';
	document.getElementById('siteSombra').style.display = 'block';
	document.getElementById('sitePopup').style.display = 'block';
}

function fechaPopup(botao) {

	document.getElementById('siteSombra').style.display = '';
	document.getElementById('sitePopup').style.display = '';
}

function abrePopupIcones() {

	$('body').append('<div class="siteSombraIcones" id="siteSombraIcones"></div>');

	document.getElementById('siteSombraIcones').style.height = document.body.clientHeight + 'px';
	document.getElementById('siteSombraIcones').style.display = 'block';
	document.getElementById('sitePopupIcones').style.display = 'block';
}

function fechaPopupIcones(botao) {

	document.getElementById('siteSombraIcones').style.display = 'none';
	document.getElementById('sitePopupIcones').style.display = 'none';
	$('#siteSombraIcones').remove();
}

function maxLength(textAreaField, limit) {
	if (textAreaField.value.length > limit) {
		alert("Numero de caracteres excedeu o limite em "
				+ (textAreaField.value.length - limit) + " caracter(es)");
		textAreaField.focus();
	}
}


function doSubmit(nome){
	document.getElementById(nome).submit();
}

function AbrirLink(url){
	if (url!=null){
	window.open(url);
	}
}
