$(document).ready(function(){
	menu();
	
	if ($('#pop-cadastre').length != 0)
		chamaModal();
});

function chamaModal()
{
	$(window).load(function(){
		modal("pop-cadastre");
	});
}

function modal(idModal)
{
	var id = '#'+idModal;
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	$('#mask').css({ 'width':maskWidth, 'height':maskHeight });
	$('#mask').show();

	var winH = $(window).height();
	var winW = $(window).width();
	
	var idH = $(id).height();
	var idW = $(id).width();
	
	var winT = (winH - idH)/2;
	var winL =  (winW - idW)/2;

	$(id).css('top',  winT);
	$(id).css('left', winL);
	$(id).show();

	$('.window .close .input-pop-button').click(function(e){
		e.preventDefault();
		$('#mask').hide();
		$('.window').hide();
	});

	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});
	$(document).pngFix();
}



function menu()
{
	$(".header ul li.hover").click(function()
	{
		if($(this).hasClass('aberto'))
		{
			$(this).removeClass("aberto");
			$(".header ul li.hover ul").slideUp();
		}
		else
		{
			$(this).addClass("aberto");
			$(".header ul li.hover ul").slideDown();
		}
	});
}

function SelectAll(sender, gridClientID)
{
	$("input:checkbox",$("#" + gridClientID)).attr("checked", sender.checked);
}

function calculoDePosicao(vendidos, total)
{
	var total = parseInt(total);
	var vendidos = parseInt(vendidos);
	var px = 283;
	var totalAgrupe = 9;
	var pixelAgrupe = 0;
	var pixelBase = 0;
	var i = 0;
	var qtd = 0;

	porcento = (vendidos * px) / total;
	
	for(i = 0; i <= totalAgrupe; i++)
	{
		if(porcento > pixelBase)
		{
			qtd += 1;
			pixelBase += 28;
		}
	}
	
	qtd != 0 ? pixelAgrupe = (qtd * 28) + 2 : pixelAgrupe = (qtd * 28);

	ajustaPx = px - pixelAgrupe;
	
	if(qtd == 0)
	{
		pxItens = ajustaPx - 25;
	}
	else if(qtd == 10)
	{
		pxItens = ajustaPx + 10;
	}
	else
	{
		pxItens = ajustaPx - 15;
	}
	
	barraProgresso(ajustaPx, pxItens);
}

function barraProgresso(pos, posItens)
{
	var posicao = pos+"px";
	var posicaoItens = posItens+"px";
	
	$(".barraProgresso").animate({ top: posicao }, 1000, function(){
		$(".itensVendidos").css({"top" : posItens});
		$(".itensVendidos").fadeIn(1000);
	});
}



	function ValidarCPF(sender)
      {
		  var cpf = sender.value;
		  var numeros, digitos, soma, i, resultado, digitos_iguais;
		  digitos_iguais = 1;
		  if (cpf.length < 11)
				return false;
		  for (i = 0; i < cpf.length - 1; i++)
				if (cpf.charAt(i) != cpf.charAt(i + 1))
					  {
					  digitos_iguais = 0;
					  break;
					  }
		  if (!digitos_iguais)
				{
				numeros = cpf.substring(0,9);
				digitos = cpf.substring(9);
				soma = 0;
				for (i = 10; i > 1; i--)
					  soma += numeros.charAt(10 - i) * i;
				resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
				if (resultado != digitos.charAt(0))
					  return false;
				numeros = cpf.substring(0,10);
				soma = 0;
				for (i = 11; i > 1; i--)
					  soma += numeros.charAt(11 - i) * i;
				resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
				if (resultado != digitos.charAt(1))
					  return false;
				return true;
				}
		  else
				return false;
      }



function oldValidarCPF(objCPF){
		var cpf = objCPF.value;
		
        exp = /\.|\-/g
        cpf = cpf.toString().replace( exp, "" ); 
        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 || (cpf == '11111111111' || cpf == '22222222222' ||cpf == '33333333333' ||cpf == '44444444444' ||cpf == '55555555555' ||cpf == '66666666666' ||cpf == '77777777777' ||cpf == '88888888888' ||cpf == '99999999999'))
        {
                alert('O CPF digitado e invalido!');
                objCPF.value = '___.___.___-__';
                objCPF.focus();
                return false;
        }
        return true;
}


function VerificarEmail(sender) 
{
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	
	return filter.test(sender.value);
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function ValidarData(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf("/")
	var pos2=dtStr.indexOf("/",pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1)
		return false

	if (strMonth.length<1 || month<1 || month>12)
		return false

	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
		return false
	if (strYear.length != 4 || year==0 || year<1900 || year>2010)
		return false

	if (dtStr.indexOf("/",pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, "/"))==false)
		return false

return true
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
