//Verifico se ajax concluido
function ajaxDone(acao){
	var intervaloo = window.setInterval(liroliro, 100);
	function liroliro(){
		if(ajaxStatus == 200){
			clearInterval(intervaloo);
			if(typeof(acao) == "function")
				acao.call();
		}
	}
}

//Pega os checkbox marcados e os bota separados por virgula
function getCheckedCheckboxValue(checkboxObj){
	var valor = '';
	var aChk = document.getElementsByName(checkboxObj);
	for(var i=0;i<aChk.length;i++){
		if(aChk[i].checked) valor += aChk[i].value+',';
	} valor = valor.substring(0, valor.length-1);
	
	return valor;
}

//Pega o input radio marcado
function getCheckedRadioValue(radioObj){
    if(!radioObj) return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined){
		if(radioObj.checked) return radioObj.value;
		else return "";
    }
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) return radioObj[i].value;
    }
	
    return "";
}

//Verifico se adicionou/editou
function add_bt(){
	showLoader('block');
	var intervalo = window.setInterval(lerolero, 100);
	function lerolero() {
		var show = frames['aFr'].aadd;
		var blAjax = frames['aFr'].blAjax;
		var toid = frames['aFr'].toid;
		var tourl = frames['aFr'].tourl;
		var tourltime = frames['aFr'].tourltime;
		var status = frames['aFr'].status;
		var message = frames['aFr'].message;
		if(typeof message != 'undefined'){
			clearInterval(intervalo);
			showLoader('none');
			
			showNotif(status, message);
			if(typeof tourl != 'undefined' && tourl != ''){
				if(typeof blAjax != 'undefined' && blAjax != ''){
					ajax(tourl, toid, 'new');
				} else{
					window.setTimeout(function(){
						document.location = tourl;
					}, tourltime);
				}
			}
		}
	}
}

//Mostro e escondo loader (EDITAR O ID DO IFRAME SE NECESSARIO)
function showLoader(status){
	//Zero o iframe
	document.getElementById('aFr').src = '';
	
	if(status == 'block'){
		var posScroll = getScrollTop();
		var height = getHeight();
		var height2 = getHeight2('divSite');
		if(height > height2) height2 = height;
		var margemTop = posScroll + Math.ceil(height/2) - 50;
		if(margemTop < 0) margemTop = 0;
		if(margemTop > 0)document.getElementById('allLoader').style.marginTop = margemTop+"px";
		document.getElementById('bckTransparent').style.height = height2+"px";
	} document.getElementById('divLoader').style.display = status;
}

function showNotif(cName, msg){
	if(top.document.getElementById('divNotification')){
		top.document.getElementById('divNotification').style.display = 'none';
		top.$("#divNotification").fadeIn(1000);
		top.document.getElementById('divNotification').className = 'notificacao '+cName;
		top.document.getElementById('divNotification').innerHTML = msg;
		
		if(cName == 'success'){
			window.setTimeout(function(){
				top.$("#divNotification").fadeOut(1000);
			}, 5000);
		}
	}
}

//Position fixed quando a tela desce
function fixedTop(id){
	posElem = getPosicaoElemento(id).top;
	window.onscroll = function(){
		var posScroll = getScrollTop();
		if(posScroll >= posElem){
			document.getElementById(id).style.position = 'fixed';
			document.getElementById(id).style.top = '0';
			document.getElementById(id).style.zIndex = '2';
		} else{
			document.getElementById(id).style.position = '';
			document.getElementById(id).style.top = '';
			document.getElementById(id).style.zIndex = '';
		}
	}
}

function getPosicaoElemento(elemID){
	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	var offsetTop = 0;
	while(offsetTrail){
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if(navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined"){
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	
	return {left:offsetLeft, top:offsetTop};
}

function getWidth(){
	return window.innerWidth ? window.innerWidth : 
	    document.documentElement ? document.documentElement.clientWidth : 
	    document.body ? document.body.clientWidth : 
	    window.screen.width;
}

function getHeight(){
	return window.innerHeight ? window.innerHeight : /* Para non-IE */
	     document.documentElement ? document.documentElement.clientHeight : /* IE 6+ */
	     document.body ? document.body.clientHeight : /* IE 4 Compativel */
	     window.screen.height; /* Outros (Nao eh browser window size, mas screen size) */
}

function getScrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function getScrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function getHeight2(id){
	return document.getElementById(id).offsetHeight;
}

function getWidth2(id){
	return document.getElementById(id).offsetWidth;
}

//Foco da Pesquisa
function foco(t, id){
	var strPesquisa = document.getElementById(id).value;
	if(strPesquisa == t){
		document.getElementById(id).value = '';
		document.getElementById(id).style.color = '#333333';
	} else if(strPesquisa == ''){
		document.getElementById(id).value = t;
		document.getElementById(id).style.color = '#888888';
	}
}

//Mostro e escondo
function mostra(id){
	if(document.getElementById(id).style.display == 'block')
		document.getElementById(id).style.display = 'none';
	else
		document.getElementById(id).style.display = 'block';
}

function fmod(dividend, divisor){
	var multiplier = 0;
	while(divisor * multiplier < dividend) ++multiplier;
	--multiplier;
	
	return dividend - (divisor * multiplier);
}

function verificaUltimoDiaMes(anou, mese){
	var dias_fevereiro;
	if(((fmod(anou,4)==0) && (fmod(anou,100)!=0)) || (fmod(anou,400)==0))
	   dias_fevereiro = 29;
	else
	   dias_fevereiro = 28;
	switch(mese){
	   case 1: return 31;
	   case 2: return dias_fevereiro;
	   case 3: return 31;
	   case 4: return 30;
	   case 5: return 31;
	   case 6: return 30;
	   case 7: return 31;
	   case 8: return 31;
	   case 9: return 30;
	   case 10: return 31;
	   case 11: return 30;
	   case 12: return 31;
	}
}

function mktime(){
    var d = new Date(),
        r = arguments,
        i = 0,
        e = ['Hours', 'Minutes', 'Seconds', 'Month', 'Date', 'FullYear'];
 
    for (i = 0; i < e.length; i++) {
        if (typeof r[i] === 'undefined') {
            r[i] = d['get' + e[i]]();
            r[i] += (i === 3); // +1 to fix JS months.
        } else {
            r[i] = parseInt(r[i], 10);
            if (isNaN(r[i])) {
                return false;
            }
        }
    }
 
    // Map years 0-69 to 2000-2069 and years 70-100 to 1970-2000.
    r[5] += (r[5] >= 0 ? (r[5] <= 69 ? 2e3 : (r[5] <= 100 ? 1900 : 0)) : 0);
 
    // Set year, month (-1 to fix JS months), and date.
    // !This must come before the call to setHours!
    d.setFullYear(r[5], r[3] - 1, r[4]);
 
    // Set hours, minutes, and seconds.
    d.setHours(r[0], r[1], r[2]);
 
    // Divide milliseconds by 1000 to return seconds and drop decimal.
    // Add 1 second if negative or it'll be off from PHP by 1 second.
    return (d.getTime() / 1e3 >> 0) - (d.getTime() < 0);
}

function getDataNow(){
	var dataaa = new Array();
	var dataa = new Date();
	dataaa['dia'] = dataa.getDate();
	if(dataaa['dia'] <= 9) dataaa['dia'] = "0"+dataaa['dia'];
	dataaa['mes'] = dataa.getMonth()+1;
	if(dataaa['mes'] <= 9) dataaa['mes'] = "0"+dataaa['mes'];
	dataaa['ano'] = dataa.getFullYear();
	dataaa['hora'] = dataa.getHours();
	if(dataaa['hora'] <= 9) dataaa['hora'] = "0"+dataaa['hora'];
	dataaa['minutos'] = dataa.getMinutes();
	if(dataaa['minutos'] <= 9) dataaa['minutos'] = "0"+dataaa['minutos'];
	dataaa['segundos'] = dataa.getSeconds();
	if(dataaa['segundos'] <= 9) dataaa['segundos'] = "0"+dataaa['segundos'];
	
	return dataaa;
}

function getMesExtenso(mes){
	if(mes == "01" || mes == "1") return "Janeiro";
	if(mes == "02" || mes == "2") return "Fevereiro";
	if(mes == "03" || mes == "3") return "Março";
	if(mes == "04" || mes == "4") return "Abril";
	if(mes == "05" || mes == "5") return "Maio";
	if(mes == "06" || mes == "6") return "Junho";
	if(mes == "07" || mes == "7") return "Julho";
	if(mes == "08" || mes == "8") return "Agosto";
	if(mes == "09" || mes == "9") return "Setembro";
	if(mes == "10") return "Outubro";
	if(mes == "11") return "Novembro";
	if(mes == "12") return "Dezembro";
}

function float2moeda(num){
	x = 0;
	if(num<0){
	  num = Math.abs(num);
	  x = 1;
	}
	if(isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100);  
	num = Math.floor((num*100+0.5)/100).toString();
	if(cents < 10) cents = "0" + cents;
	  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		 num = num.substring(0,num.length-(4*i+3))+'.'
			   +num.substring(num.length-(4*i+3));
	ret = num + ',' + cents;
	if (x == 1) ret = ' - ' + ret;return ret;
}

function moeda2float(moeda){
	moeda = moeda.replace(".","");
	moeda = moeda.replace(",",".");
	
	return parseFloat(moeda);
}
