/**
 * @author lborges
 */
function substractOffset(offsetReal, maxResults, formName)	{

	$("#"+offsetReal).val($("#"+offsetReal).val() - 1);

	$("#"+offsetReal).val($("#"+offsetReal).val() * $("#"+maxResults).val());

	var formId		= document.getElementById(formName);

	formId.submit();
}

function submitForms(formName, url)	{

	var formDocument = document.getElementById(formName);

	formDocument.action=url;

	formDocument.submit();
}

function updateAndSubmitForm(formId, pageId, pageNum){
    var fieldPage = document.getElementById(pageId);
    fieldPage.value = pageNum;
    var form = document.getElementById(formId);
    form.submit();
}

function loadOptions(url, selectId, idPropertyName, namePropertyName, selectValue){
    $.getJSON(url);
}

function loadShowcase(url, divId) {
	//alert(divId + " > " + $("#" + divId).html())
	//$("#" + divId).html("<img src='https://simg.uol.com.br/alertas/m-espera.gif' />")


$("#" + divId).html("<span style='background-color: #585AA1; color: #FFF; font: bold 11px Arial,sans-serif;'>Carregando...</span>")
	$.post(url, function(data){
		$("#"+divId).hide();
		if(data.search(/NCU002/i) != -1)	{
			window.location.href='/authFail.html'
		}else{
			//document.getElementById(divId).innerHTML	=	data;
			$("#"+divId).html(data)
			$("#"+divId).fadeIn(500);
		}
	});
}

function loadCategoryShowcase(url, divId, valueCategory) {
	
	
		$("#" + divId).html("<img src='https://simg.uol.com.br/alertas/m-espera.gif' />")
		$.post(url, function(data){
			$("#"+divId).hide();
			if(data.search(/NCU002/i) != -1)	{
				window.location.href='/authFail.html'
			}else{
				document.getElementById(divId).innerHTML	=	data;
				
				if (valueCategory != "${value}") {
					document.getElementById(valueCategory).style.display='block';
					document.getElementById(valueCategory).id='category';
				}
				
				$("#"+divId).fadeIn(500);
			}
		});
	
}

function loadWinners(url, divId, hiddenId, loadingId) {
	$("#" + divId).html("<img src='https://simg.uol.com.br/alertas/m-espera.gif' />")
	var divHtml 	= document.getElementById(divId);

	if(divHtml.style.display == 'none')	{
		$("#"+loadingId).show();

		$.post(url, function(data){
			var closeDiv 	= $("#"+hiddenId).val();
			if(data.search(/NCU002/i) != -1)	{
				window.location.href='/authFail.html'
			}else{
				divHtml.innerHTML	=	data;

				if(closeDiv != undefined) {
					$("#"+closeDiv).hide("slow");
				}
				$("#"+loadingId).hide();
				$("#"+divId).show("slow");
				$("#"+hiddenId).val(divId);
			}
		});
	} else {
		$("#"+divId).hide("slow");
		divHtml.innerHTML	=	"";
	}
}

function loadImg(numImg, divId, path, sufix)	{

	var indexNum = Math.floor(Math.random()*numImg);

	var	imgElement	=	document.createElement('img');
	imgElement.setAttribute('src',path+indexNum+sufix);

	var divImg = document.getElementById(divId);
	divImg.innerHTML = "";
	divImg.appendChild(imgElement);
}

function loadBackground(numImg, divId, path, sufix)	{

	var indexNum = Math.floor(Math.random()*numImg);

	var divImg = document.getElementById(divId);
	divImg.className = path+indexNum;
}

function submitCommentsForm(url, divId, indexValue)
{
	var urlOpen	=	url+indexValue;
	$.post(urlOpen, function(data){
		if(data.search(/sese1/i) == 1) {
			window.location.reload();
			return;
		}
		if(data.search(/NCU002/i) != -1)	{
			window.location.reload();
			return;
		}
		document.getElementById(divId).innerHTML = "";
		document.getElementById(divId).innerHTML = data;
	});
}

function pagingLeftCompetition(url, divId, divEffectImgId, divEffectTxtId, indexValue)
{
	var urlOpen	=	url+indexValue;
	$.post(urlOpen, function(data){
		if(data.search(/sese1/i) == 1) {
			window.location.reload();
			return;
		}
		if(data.search(/NCU002/i) != -1)	{
			window.location.reload();
			return;
		}
		document.getElementById(divId).innerHTML = "";
		document.getElementById(divId).innerHTML = data;

		//alert($("."+divEffectImgId).html())
		/*$("."+divEffectImgId).show(
			"slide", {
	        	direction: "left"
		    },
		    600);

		$("."+divEffectTxtId).show(
			"slide", {
	        	direction: "left"
		    },
		    600);  */
	});
}

function pagingRightCompetition(url, divId, divEffectImgId, divEffectTxtId, indexValue)
{
	var urlOpen	=	url+indexValue;
	$.post(urlOpen, function(data){
		if(data.search(/sese1/i) == 1) {
			window.location.reload();
			return;
		}
		if(data.search(/NCU002/i) != -1)	{
			window.location.reload();
			return;
		}
		document.getElementById(divId).innerHTML = "";
		document.getElementById(divId).innerHTML = data;
		$("#"+divEffectImgId).show(
			"slide", {
	        	direction: "right"
		    },
		    2000);

		$("#"+divEffectTxtId).show(
			"slide", {
	        	direction: "right"
		    },
		    2000);
	});
}

function popup(myLink, windowName) {
	if (! window.focus)
		return true;
	var href;
	if (typeof(myLink) == 'string')
		href=myLink;
	else
		href=myLink.href;
	window.open(href, windowName, 'width=725, height=420,toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no"');
	return false;
}

function loadAddress(url, cepId){

	var newUrl;
	var cep;

	cep = document.getElementById(cepId).value;

	newUrl=url+cep;

	$("#loadingAddress").fadeIn();
	hideAddress();

	$.getJSON(newUrl, function(data){


		if(data.errors)
		{
			$("#loadingAddress").fadeOut();
			alert(data.errors.join("\n"));
			$('#addressEnable').val(0);
			hideAddress();
		} else {
			$('#addressEnable').val(1);
			$('#participateDiv').fadeIn("slow");



		if(data.numberAddress != null)
		{
			var numAddress  = data.numberAddress;
			document.getElementById('numAddress').value=numAddress;
		}

		if(data.postalAreaVO != null)
		{
			var namAddress = data.postalAreaVO.name;
			document.getElementById('addressParticipant').value=namAddress;
			document.getElementById('address').value=namAddress;
			$('#addressDiv div.box-conteineir input#address').attr('disabled','disabled');
			$('#addressDiv').fadeIn("slow");
			$('#complementDiv').fadeIn("slow");
			$('#cityDiv').css('visibility','visible');
		} else {
			$('#addressDiv').fadeIn("slow");
			$('#complementDiv').fadeIn("slow");
			$('#addressDiv div.box-conteineir input').attr('disabled','');

			$('#addressDiv div.box-conteineir input#address').attr('value','');
			$('#addressDiv div.box-conteineir input#numAddress').attr('value','');
			$('#complementDiv input#desComplement').attr('value','')
			$('#cityDiv').css('visibility','visible');

			if((data.cityVO.name == '') || (data.cityVO.name == null)) {
				$('#cityDiv input#city').attr('disabled','');
			}
		}


		if(data.cityVO != null)
		{
			var namCity = data.cityVO.name;
			document.getElementById('cityParticipant').value=namCity;
			document.getElementById('city').value=namCity;
			$('#cityDiv').fadeIn("slow");

			if(data.cityVO.federationUnitVO != null)	{
				var uf = data.cityVO.federationUnitVO.abbreviation;
				document.getElementById('ufParticipant').value=uf;
				var selectUf = document.getElementById('uf');
				for(var i = 0; i <selectUf.options.length; i++){
					if(uf == selectUf.options[i].value)	{
						selectUf.options[i].selected = true;
					}
				}
				$('#ufDiv').fadeIn("slow");
			}
		}


		}
		$("#loadingAddress").fadeOut();
	});
}

function limitText(field, maxlimit) {
	if (field.value.length > maxlimit)
	{
		field.value = field.value.substring(0, maxlimit + 1);}

}

function getURI()	{
	document.getElementById('returnURL').value = window.location.href;
}

function waitAll(divId)	{
	$("#"+divId).ajaxSend(function(){
	   $("#"+divId).hide();
	});

	$("#"+divId).ajaxComplete(function(){
	   $("#"+divId).show();
	});
}

function setErrors(divId, errorMsg) {
	document.getElementById(divId).innerHTML = errorMsg+"<br>";
}

function pagingPartners(url, divId, classNameEffectImg, classNameEffectPipe, indexValue)
{
	var urlOpen	=	url+indexValue;
	$.post(urlOpen, function(data){
		if(data.search(/sese1/i) == 1) {
			window.location.reload();
		}
		if(data.search(/NCU002/i) != -1)	{
			window.location.reload();
			return;
		}
		document.getElementById(divId).innerHTML = "";
		document.getElementById(divId).innerHTML = data;
		//$("."+classNameEffectPipe).hide();
		$("."+classNameEffectImg).hide();
		//$("."+classNameEffectPipe).fadeIn(2000);
		//alert( classNameEffectImg )
		$("."+classNameEffectImg).fadeIn(600);
	});
}

function cepMask(cepTextId, cepButtonId)	{

	var cep = document.getElementById(cepTextId);

	cep.value=cep.value.replace(/D/g,"")
	cep.value=cep.value.replace(/([A-Z]*[a-z]*)/g,"")
	//cep.value=cep.value.replace(/^(\d{5})(\d{3})/,"$1-$2")

	if(cep.value.length == 8) {
		loadAddress('/loadAddress.html?cep=', 'cepAddress')
	}

	if(cep.value.length < 8) {
		hideAddress();
		$("#"+cepButtonId).fadeOut("slow");
	}
}


function hideAddress()	{
	$('#addressDiv').fadeOut("slow");
	$('#complementDiv').fadeOut("slow");
	$('#cityDiv').fadeOut("slow");
	$('#ufDiv').fadeOut("slow");
	$('#participateDiv').fadeOut("slow");
}



function confirmCPF(id,cpfValue) {

	errors = false;

	if (cpfValue == '') {
		cpfNumber = $('#numCPF').val();
	} else {
		cpfNumber = cpfValue;
	}

	if (cpfNumber != '') {

		if (verCpf(cpfNumber)) {
			errors = false;
			$('#msg-box-conteineir').css('display','none');
		}else{
			errors = true;
			//alert('O CPF INFORMADO N É VÁLIDO.')
			//document.retorno = (errors == '');
		}
	}

	if (errors == true) {

		$('#msg-box-conteineir').css('display','block');
		$('#numCPF').focus().select();

	}

}
function verCpf(cpf) {
	if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") return false;
	add = 0;
	for (i=0; i < 9; i ++)
	add += parseInt(cpf.charAt(i)) * (10 - i);
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11)
	rev = 0;
	if (rev != parseInt(cpf.charAt(9))) return false;
	add = 0;
	for (i = 0; i < 10; i ++)
	add += parseInt(cpf.charAt(i)) * (11 - i);
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11)
	rev = 0;
	if (rev != parseInt(cpf.charAt(10))) return false;
	return true;
}