// JavaScript Document

function seleccionaAno(ano) {
	if(ano == '') {
		document.getElementById("mes").selectedIndex = 0;
		document.getElementById("mes").disabled = true;
		document.getElementById("dia").selectedIndex = 0;
		document.getElementById("dia").disabled = true;
	} else {
		document.getElementById("mes").disabled = false;
		if(document.getElementById("mes").value != '')
			document.getElementById("dia").disabled = false;
	}
}
	
function seleccionaMes(mes) {
	if(mes == '') {
		document.getElementById("dia").selectedIndex = 0;
		document.getElementById("dia").disabled = true;
	} else {
			document.getElementById("dia").disabled = false;
	}
}



function resizeIframe(){
    var newheight;
	frame = document.getElementById('content');
	innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
	newheight = innerDoc.body.scrollHeight;
    frame.height= (newheight) + "px";
}

function resizeIframeEspecifico(iframeID){
    var newheight;
	frame = document.getElementById(iframeID);
	innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
	newheight = innerDoc.body.scrollHeight;
    frame.height= (newheight) + "px";
}


//Verifica a existencia de iframe no caso de chamada a links directos
function verificaParent(){
	var url = window.location.href;
	
	//if(parent.document.frameInterior == null){
	if(parent.document.getElementById('content') == null) {
		//Criação de FROM dinamico para tratamento de links directos
		myForm = document.createElement('form');
		myForm.method = 'POST';
		myForm.action = 'index.php';
		myForm.setAttribute('id', 'formToIndex');
		myForm.enctype = 'multipart/form-data';
		
		// Create a text field
		textField = document.createElement('input');
		textField.type = 'text';
		textField.setAttribute('value', url);
		textField.setAttribute('name', 'urlEncoded');
		textField.setAttribute('id', 'urlEncoded');
		
		myForm.appendChild(textField);
		//document.appendChild(myForm);
		document.body.appendChild(myForm);
		myForm.submit();

	} else {
		document.body.style.visibility = '';
	}
}

function abrirJanelaCentradaComScroll(url, largura, altura){
    var larguraEcra = window.screen.width;
    var alturaEcra = window.screen.height;
    var posicaoX = (larguraEcra / 2) - (largura / 2);
    var posicaoY = (alturaEcra / 2) - (altura / 2);
	window.open(url,"","left="+posicaoX+",top="+posicaoY+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width="+largura+", height="+altura);
}

function trimAll( strValue ) {

 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function validateNotEmpty( strValue ) {

   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}


//MUDANCA DE IDIOMA
function mudaIdioma(idIdioma){
	myForm = document.createElement('form');
	myForm.method = 'POST';
	myForm.action = 'index.php';
	myForm.setAttribute('id', 'myForm');
	myForm.enctype = 'multipart/form-data';
		
	// Create a text field
	textField = document.createElement('input');
	textField.setAttribute('type', 'hidden');
	textField.setAttribute('value', idIdioma);
	textField.setAttribute('name', 'idIdioma');
	textField.setAttribute('id', 'idIdioma');
		
	myForm.appendChild(textField);
	document.body.appendChild(myForm);
	myForm.submit();
}



// CAIXAS

function Caixa (idCaixa) {

	//propriedades
	this.idCaixa = idCaixa;
	this.div_block = document.getElementById(idCaixa);
	this.x_pos = parseInt(this.div_block.style.left, 10);
	this.y_pos = parseInt(this.div_block.style.top, 10);
	
	this.anim_status = false;
	
	

	//intervalo de chamada
	this.callRate = 100;
	this.divisor = 6;
	this.timer = "stop";
	
	
	//metodos
	
	this.escondeCaixa = escondeCaixa;
	this.mostraCaixa = mostraCaixa;
	this.aproximaCaixaVertical = aproximaCaixaVertical;
	this.slideCaixaVertical = slideCaixaVertical;
	this.updateProperties = updateProperties;
	this.descobreCaixa = descobreCaixa;
	this.cobreCaixa = cobreCaixa;
	this.posicionaCaixa = posicionaCaixa;
	this.out;
}

function slideCaixaVertical(destino_Y) {
		var me = this;
		if(me.anim_status == false) {
			me.anim_status = true;
			me.timer = setInterval(function() { me.aproximaCaixaVertical(destino_Y); }, me.callRate);
			//alert(timer);
		} else {
			self.setTimeout(function() {me.slideCaixaVertical(destino_Y)}, me.callRate);
		}


}

function aproximaCaixaVertical(destino_Y) {
	var me = this;
	targetY = destino_Y;//Math.max(destino_Y, -1);
	currentY = parseInt(me.div_block.style.top, 10);
	difference = currentY - targetY;
	decrement = Math.round(difference / me.divisor);

	me.div_block.style.top = (currentY - decrement)  + "px";

	if(decrement == 0) {

		window.clearInterval(me.timer);
		me.timer = "stop";
		me.anim_status = false;
		me.updateProperties();
	}
	

}

function updateProperties() {
	this.x_pos = parseInt(this.div_block.style.left, 10);
	this.y_pos = parseInt(this.div_block.style.top, 10);
	this.altura = parseInt(this.div_block.offsetHeight, 10);
	this.largura = parseInt(this.div_block.offsetWidth, 10);

}

function escondeCaixa() {
	with (this) div_block.style.display = 'none';
}

function mostraCaixa() {
	with (this) div_block.style.display = '';
}

function posicionaCaixa(topo, esquerda) {
	with (this) div_block.style.top = topo+"px";
	with (this) div_block.style.left = esquerda+"px";
	this.updateProperties();
}

function descobreCaixa(alturaClip_actual) {
	var me = this;
	me.updateProperties();
	//alert(me.timer);
	if(me.timer == "stop") {
		difference = me.altura - alturaClip_actual;
		alturaClip_actual +=Math.max((difference)/me.divisor,1);
		me.div_block.style.clip = "rect(auto auto "+ alturaClip_actual + "px auto)";
	}
	
	if(alturaClip_actual > me.altura){
		me.updateProperties();
		me.timer = "stop";
		me.anim_status = false;
	} else {
		me.anim_status = true;
		self.setTimeout(function() {me.descobreCaixa(alturaClip_actual)}, me.callRate);
	}
}

function cobreCaixa(alturaClip_actual) {
	
	var me = this;
	me.updateProperties();
	if(me.timer == "stop") {
	
	
		difference = 0 - alturaClip_actual;
		alturaClip_actual +=Math.min((difference)/me.divisor,-1);
		me.div_block.style.clip = "rect(auto auto "+ alturaClip_actual + "px auto)";

		
	}
	if(alturaClip_actual < 0){
		me.updateProperties();
		me.timer = "stop";
		me.anim_status = false;
	} else {
		me.anim_status = true;
		self.setTimeout(function() {me.cobreCaixa(alturaClip_actual)}, me.callRate);
    }
}

//paramIN:	src:string - source do movie;
//			width:int - comprimento;
//			heigth:int - altura;
//			menu:string - permite ver menu (mousse right click)
//			wmode:string - permite transparent
//			version:string - versao do flash player
//			id - id do flash
//paramOUT: void
//summary: faz o render do objecto flash
function printFlash(src, width, height, menu, wmode, version, id){
	document.write('<object id=\"'+id+'\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version + '\" width=\"' + width + '\" height=\"' + height + '\">');
	document.write('<param name=\"movie\" value=\"' + src + '\">');
	document.write('<param name=\"quality\" value=\"high\">');
	document.write('<param name=\"menu\" value=\"' + menu + '\">');
	document.write('<param name=\"wmode\" value=\"' + wmode + '\">');
	document.write('<embed id=\"'+id+'\" src=\"' + src + '\" quality=\"high\" wmode=\"' + wmode + '\" menu=\"' + menu + '" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"' + width + '\" height=\"' + height + '\"></embed>');
	document.write('</object>');
}

