/* Global que informa se a janela ja esta em uso */
var DIALOG_OPENED = false;

function createObjElement( strElementTag , strId , strName , strClass )
{
	if( strElementTag == undefined )
	{
		return( false );
	}
	var objElement = document.createElement( strElementTag.toUpperCase() );
	if( strId != undefined )
	{
		objElement.setAttribute( "id" , strId );
	}
	if( strName != undefined )
	{
		objElement.setAttribute( "name" , strName );
	}
	if( strClass != undefined )
	{
		objElement.className = strClass;
	}
	return( objElement );
}

function createElementIntoRepository( strElementTag , objRepositoryElement , strId , strName , strClass , strStyle , boolStart )
{
	if( strElementTag == undefined )
	{
		strElementTag = "DIV";
	}
	if( objRepositoryElement == undefined )
	{
		objRepositoryElement = document.body;
	}
	if( boolStart == undefined )
	{
		boolStart = false;
	}
	var objElement = document.createElement( strElementTag.toUpperCase() );
	if( strId != undefined )
	{
		objElement.setAttribute( "id" , strId );
	}
	if( strName != undefined )
	{
		objElement.setAttribute( "name" , strName );
	}
	if( strClass != undefined )
	{
		objElement.className = strClass;
	}
	if( strStyle != undefined )
	{
		objElement.style.cssText = strStyle;
	}
	if( boolStart )
	{
		if( checkIfExistsElementIntoRepositoy( objElement , objRepositoryElement ) == false )
		{
			var strOldValues = objRepositoryElement.innerHTML;
			objRepositoryElement.innerHTML = "";
			objRepositoryElement.appendChild( objElement );		
			objRepositoryElement.innerHTML += strOldValues;			
		}
	}
	else
	{
		objRepositoryElement.appendChild( objElement );
	}	
	return( objElement );
}

function checkIfExistsElementIntoRepositoy( objElement , objRepositoryElement )
{
	arrChildElementsFromRepositoy = objRepositoryElement.childNodes;
	for( var i = 0 ; i < arrChildElementsFromRepositoy.length ; ++i )
	{
		if( ( arrChildElementsFromRepositoy[ i ] ) && ( arrChildElementsFromRepositoy[ i ] == objElement ) )
		{
			return( true );
		}
	}
	return( false );
}
		
function removeElementFromRepository( objRepositoryElement , strId )
{
	if( strId == undefined )
	{
		return( false );
	}
	if( objRepositoryElement == undefined )
	{
		objRepositoryElement = document.body;
	}
	arrElement = objRepositoryElement.childNodes;
	for( var i = 0 ; i < arrElement.length ; ++i )
	{
		if( ( arrElement[ i ] ) && ( arrElement[ i ].id == strId ) )
		{
			objRepositoryElement.removeChild( document.getElementById( strId ) );
			break;
		}
	}
	return( true );
}

function returnHtmlOfObjElement( obj )
{
	var objRepositoryElement = createObjElement( "DIV" , "idHtmlReturn" );
	objRepositoryElement.appendChild( obj );
	var strResult = objRepositoryElement.innerHTML;
	removeElementFromRepository( document.body , "idHtmlReturn" );
	return( strResult );
}

function openWaitDialogIntoIframe() 
{
	if( ! DIALOG_OPENED )
	{	
		DIALOG_OPENED = true;
		document.getElementById('IframeWait').style.cssText  ="width:100%; *width:" + ( document.body.offsetWidth - 20 ) + "; height:100%; *height:" + document.body.offsetHeight + "; z-index:10000; margin:0 auto; position:absolute; border:0px; opacity:.0; *filter:alpha(opacity=0); overflow-x: hidden; overflow-y: hidden;";
		document.getElementById( 'IframeWait' ).setAttribute( "SCROLLING" , "NO");
		document.getElementById( "IframeWait" ).src="/Apoio_SITARWEB/includes/XML/stackSajax/WaitDialogIntoIframe/WaitDialogIntoIframe.htm";	
		document.getElementById('IframeWait').style.display = 'block';
		opacity( 'IframeWait' , 0 , 60 , 1000 );
	}
}

function closeWaitDialogIntoIframe( objRepositoryElement )
{
	DIALOG_OPENED = false;
	
	if( objRepositoryElement == undefined )
	{
		objRepositoryElement = document.body;
	}
	opacity( 'IframeWait' , 60 , 0 , 1000 );
	setTimeout( "document.getElementById('IframeWait').style.display = 'none'" , 1000 );
}

function alterOpacity( strId , intMillisec )
{
    if( document.getElementById( strId ).style.opacity == 0 )
     {
        opacity( strId , 0 , 100 , intMillisec);
    } 
    else 
    {
        opacity( strId , 100 , 0 , intMillisec);
	}
} 	
	
function opacity( strId , intOpacStart , intOpacEnd , intMillisec )
 {
    var intSpeed = Math.round( intMillisec / 100 );
    var intTimer = 0;
    if( intOpacStart > intOpacEnd )
     {
        for( var i = intOpacStart; i >= intOpacEnd; i-- )
         {
            setTimeout( "changeOpac( " + i + " , '" + strId + "' )" , ( intTimer * intSpeed ) );
            ++intTimer;
        }
    } 
    else if( intOpacStart < intOpacEnd )
     {
        for( var i = intOpacStart; i <= intOpacEnd; i++ )
        {
            setTimeout( "changeOpac( " + i + " , '" + strId + "' )" , ( intTimer * intSpeed ) );
            ++intTimer;
        }
    }
}

function changeOpac( intOpacity , strId ) 
{
    var object = document.getElementById( strId ).style;
    object.opacity = ( intOpacity / 100 );
    object.MozOpacity = ( intOpacity / 100 );
    object.KhtmlOpacity = ( intOpacity / 100 );
    object.filter = "alpha( opacity = " + intOpacity + " )";
} 	
	
function currentOpac( strId , intOpacEnd , intMillisec ) 
{
	var intCurrentOpac = 100;
	if( document.getElementById( strId ).style.opacity < 100 )
	 {
		intCurrentOpac = document.getElementById( strId ).style.opacity * 100;
	}
	opacity( strId , intCurrentOpac , intOpacEnd , intMillisec );
}

function mudarOperacao(operacao, imgAba){
	if ( operacao == undefined ) {
		operacao = "I";
		imgAba = 'http://sistemas.anatel.gov.br/psv/imagens/Icones/16x16/ico16_novoarquivo.gif';
	}

	if (operacao == 'L') {
		document.getElementById('divPaginacao').style.display = 'block';
		document.getElementById('CorpoCadastro').style.display = 'none';
	} else if (operacao == 'I') {
		setValueObjeto(document.getElementById('CorpoCadastro'), '');
		document.getElementById('id').value = '';
		document.getElementById('operacao').value = 'incluir';
		document.getElementById('divPaginacao').style.display = 'none';
		document.getElementById('CorpoCadastro').style.display = 'block';
		document.getElementById('fontOperacao').innerHTML = 'Incluir';
		document.getElementById('imgCadastrar').src = imgAba;
	} else if(operacao == 'A'){
		document.getElementById('operacao').value = 'confirmarAlteracao';
		document.getElementById('divPaginacao').style.display = 'none';
		document.getElementById('CorpoCadastro').style.display = 'block';
		document.getElementById('fontOperacao').innerHTML = 'Alterar';
		document.getElementById('imgCadastrar').src = imgAba;
	}
}

function alterar(form, value){
	document.getElementById(form).id.value = value;
	document.getElementById(form).operacao.value = 'alterar';
	document.getElementById(form).submit();
}

function excluirPopup( strFormName , intId )
{
	alertRedDialog( 'Tem certeza que deseja EXCLUIR este registro?' , 'excluir( "'+ strFormName +'" , '+ intId + ' )' , '' , 'Excluir registro' , undefined , true );
}

function excluir(form, value) {
	form = document.getElementById( form );
	confirmaExclusao( form , value );
}

function confirmaExclusao(form, value) {
	form.id.value = value;
	form.operacao.value = 'excluir';
	form.submit();
}

function setValueObjeto(obj, value){
	var i=0;
	if (obj != null) {
		for (i=0; i < obj.childNodes.length; i++) {
			if (obj.childNodes[i] != null && obj.childNodes[i].childNodes.length > 0) {
				setValueObjeto(obj.childNodes[i], value);
			} else {					
				if (typeof(obj.childNodes[i].type) != 'undefined' && (obj.childNodes[i].type == 'select-multiple' || obj.childNodes[i].type == 'select-one' || obj.childNodes[i].type == 'text' || obj.childNodes[i].type == 'textarea')) {
					obj.childNodes[i].value = value;
				}
				if (typeof(obj.type) != 'undefined' && (obj.type == 'select-multiple' || obj.type == 'select-one' || obj.type == 'text' || obj.type == 'textarea')) {
					obj.value = value;
				}
			}
		}
	}
}
