// JavaScript Document


window.onload = function () {

	if (!document.getElementsByTagName) return true;

	ourForms = document.getElementsByTagName('form');

	// go through each form
	var numForms = ourForms.length;
	for (var i=0;i<numForms;i++) {

		// go through each form element
		var numFormElements = ourForms[i].elements.length;
		for (var j=0;j<numFormElements;j++) {

			var el = ourForms[i].elements[j];

			// ignore submit buttons
			if (el.type == "submit") continue;

			// if we got a text type input
			if (el.type == "text" || el.type == "password") {
				// only populate if we want it to
				// note: might want title attribute but no pre-population of inputs
				var ourClassName = el.className;
				if (ourClassName.match('auto-select') || ourClassName.match('auto-clear') || ourClassName.match('populate')) {
					// only populate if empty
					if (el.value == '') el.value = el.title;
				}

				// add auto select if class contains auto-select
				// note: else if below so auto-select takes precedence (assuming select is better than clear)
				if (el.className.match('auto-select')) {
					el.onfocus = function () {
						if (this.value == this.title) this.select();
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);
				}

				// add auto clear if class contains auto-clear
				else if (el.className.match('auto-clear')) {
					el.onfocus = function () {
						if (this.value == this.title) this.value = '';
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);

					el.onblur = function () {
						if (this.value == '') this.value = this.title;
					}
					if (el.captureEvents) el.captureEvents(Event.BLUR);
				}
			}

			// if we got a textarea
			if (el.type == "textarea") {
				// only populate if we want it to
				// note: might want title attribute but no pre-population of inputs
				var ourClassName = el.className;
				if (ourClassName.match('auto-select') || ourClassName.match('auto-clear') || ourClassName.match('populate')) {
					// only populate if empty
					if (el.value == '') el.value = el.title;
				}

				// add auto select if class contains auto-select
				if (el.className.match('auto-select')) {
					el.onfocus = function () {
						if (this.value == this.title) this.select();
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);
				}

				// add auto clear if class contains auto-clear
				else if (el.className.match('auto-clear')) {
					el.onfocus = function () {
						if (this.value == this.title) this.value = '';
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);

					el.onblur = function () {
						if (this.value == '') this.value = this.title;
					}
					if (el.captureEvents) el.captureEvents(Event.BLUR);
				}
			}
		}
	}
}



//ABAS

var objAba = null;

function checkAbas() {
    if (objAba){
        $(objAba).fadeOut('fast');
    }
}
 
$(document).ready(function() {
	$("#conteudoAbas > div").fadeOut('fast');
	$("#conteudoAbas > div:first").fadeIn('fast');
	$("#abas > li:first > a").addClass("active");
	
	objAba = $("#abas > li:first > a").attr("name");

	$('a.abaMenu').click(function() {
		checkAbas();
		objAba = null;

		var idDiv = $(this).attr("name");

		$(idDiv).fadeIn("fast");
		$("#abas > li").find("a").removeClass('active');
		$(idDiv + "2").find("a").addClass("active");
/*		$("#" + idMnu > li:parent).addClass("active");*/
//		$(this).find('ul').fadeIn(1000);
		objAba = idDiv;
    });
								 
	jQuery('#pgfase1').accordion({
			autoheight: false
	});
	
	jQuery('#pgfase2').accordion({
			autoheight: false
	});
});


/*$(document).ready(function() {
    var hash = window.location.hash;
    /*(!hash) ?  
        hideAllExcept('#' + $('#conteudoAbas > div:first').attr('id'))
            : hideAllExcept(window.location.hash);*/

/*    $('a.abaMenu').click(function() {
        var href = $(this).attr('title');
        hideAllExcept(href);
    });
	
	
   $("ul.sf-menu").superfish({ 
       pathClass:  'current' 
   }); 
    
	
});*/

function loadPiece(href,divName) {    
	$.get(href, function(data){ 
	  $(divName).html(data); 
	});
} 

function hideAllExcept(el) {
//    $('#conteudoAbas div').addClass('hide');
$('#conteudoAbas div').fadeOut("fast");
//    $(el).removeClass('hide');

	$(el).fadeIn("normal");
	
	$('.abaMenu').removeClass('active');
	$('a[href="' + el + '"]').addClass('active');

    //$('.btn').removeClass('active');
    //$('a[href="' + el + '"]').addClass('active');
    
}

var xmlHttp;
var retorno;

function AjaxForms(str, agURL, agRetorno)
{ 
	xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		 {
		 alert ("Browser does not support HTTP Request")
		 return
		 }
	var url=agURL;//"calendario_evento.php"
	url=url+"?"+str;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=changeForm;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	retorno = agRetorno;
}


function changeForm() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 	//document.getElementById('login').innerHTML=parseScript(xmlHttp.responseText);
	$(retorno).html(parseScript(xmlHttp.responseText));
 } 
}

function parseScript(_source) {
		var source = _source;
		var scripts = new Array();
		
		// Strip out tags
		while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
			var s = source.indexOf("<script");
			var s_e = source.indexOf(">", s);
			var e = source.indexOf("</script", s);
			var e_e = source.indexOf(">", e);
			
			// Add to scripts array
			scripts.push(source.substring(s_e+1, e));
			// Strip from source
			source = source.substring(0, s) + source.substring(e_e+1);
		}
		
		// Loop through every script collected and eval it
		for(var i=0; i<scripts.length; i++) {
			try {
				eval(scripts[i]);
			}
			catch(ex) {
				// do what you want here when a script fails
			}
		}
		
		// Return the cleaned source
		return source;
	}


function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function fcShowHide(agTarget, agAction){
	document.getElementById(agTarget).style.visibility = agAction;
	if(agAction=='hidden'){
		document.getElementById(agTarget).style.height='0px';
	}
}


function detectaBrowser()
{
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
var b = "";
if (browser=="Netscape"){
	b = "ns";
	return b;
} else if (browser=="Microsoft Internet Explorer"){
	b = "ie";
	return b;
}
}

function fcParcelas(agTotal,agDesc){
	var lista=document.getElementById("fobparcelas")
	var selected=lista.options[lista.selectedIndex].value;
	
	var pai=document.getElementById("fobfpg")
	var selpai=pai.options[pai.selectedIndex].value;
	
	var descricao="";
	var div = agTotal/selected;
	for (i=1;i<=selected;i++){
		
		if (selected==1){
			descricao = "ŕ vista: R$ "+ float2moeda(div-agDesc) +" - (desconto de R$ "+ agDesc +",00)<br>" ;
		}else{
			if (selpai=="CH"){
				if (i==1){
					var total=selected-1;
				descricao = "1+" + total.toString() + " de: R$ "+ float2moeda(div) +" <br>" ;
				}
			}else{
				descricao = descricao + i.toString() + "&ordf; parcela: R$ "+ float2moeda(div) +" - sem juros<br>" ;
			}
		}
		document.getElementById("desc").innerHTML= descricao;
		document.getElementById("parcfim").value=selected;
	}
	
}

function roundNumber (rnum) {

return Math.round(rnum*Math.pow(10,2))/Math.pow(10,2);

}
/*
2 - float2moeda

A partir de um valor float ela retorna o valor formatado com separador de milhar e vírgula nos centavos.

*/
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;

}
/*
3 - moeda2float

Pega um valor formatado com virgula e separador de milha e o transforma em float
*/
function moeda2float(moeda){

moeda = moeda.replace(".","");

moeda = moeda.replace(",",".");

return parseFloat(moeda);

}
