/*
 * Javascript auxiliars functions
 * 
 */

function popUp( url, name ) {
   WREF = window.open( url, name,
         'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=600,height=300' );
   if( !WREF.opener ) {
      WREF.opener = this.window;
   }
}

function multiSelect( action, selType ) {
   var selcl = document.getElementById( 'clid' );
   var selclIndex = selcl.selectedIndex;
   clid = selcl.options[ selclIndex ].value;

   if( selType == 'profile' ) {

      var selpf = document.getElementById( 'profile' );
      var selpfIndex = selpf.selectedIndex;
      pfid = selpf.options[ selpfIndex ].value;

      url = "/wth-bugplan/" + action + "?clid=" + clid + "&rsid=" + pfid;

   } else if( selType == 'days' ) {

      var seldays = document.getElementById( 'days' );
      var seldaysIndex = seldays.selectedIndex;
      days = seldays.options[ seldaysIndex ].value;

      url = "/wth-bugplan/" + action + "?clid=" + clid + "&d=" + days;
   }

   window.location.href = url;
}

function go( page, id ) {
   var url = "/wth-bugplan/" + page + "?id=" + id;
   window.location.href = url;
}

function update( page, bugid, clid, resourceid ) {
   var url = "/wth-bugplan/" + page + "?bugid=" + bugid + "&clid=" + clid + "&resourceid=" + resourceid;
   window.location.href = url;
}

function filter( page, cl, filter ) {
   var url = "/wth-bugplan/" + page + "?id=" + cl + "&f=" + filter;
   window.location.href = url;
}

function filterReport( page, clid, pfid, filter ) {
   var url = "/wth-bugplan/" + page + "?clid=" + clid + "&rsid=" + pfid + "&f=" + filter;
   window.location.href = url;
}

function reload( id, bug_anchor ) {
   window.opener.refresh( id, bug_anchor );
   window.close();
}

function toggle( obj ) {
   var el = document.getElementById( obj );
   if( el.style.display != 'none' ) {
      el.style.display = 'none';
   } else {
      el.style.display = '';
   }
}

// get index of element selected in select object
function selectedIndex( selId ) {
   var selObj = document.getElementById( selId );
   var selIndex = selObj.selectedIndex;
   var txtValueObj;

   txtValueObj = selObj.options[ selIndex ].value;

   return txtValueObj;
}

function showSelected( obj ) {
   var selObj = document.getElementById( obj );
   var txtIndexObj = document.getElementById( 'txtIndex' );
   var txtValueObj = document.getElementById( 'txtValue' );
   var txtTextObj = document.getElementById( 'txtText' );

   var selIndex = selObj.selectedIndex;
   txtIndexObj.value = selIndex;
   txtValueObj.value = selObj.options[ selIndex ].value;
   txtTextObj.value = selObj.options[ selIndex ].text;
}

function testeCss() {

   var rua = document.getElementById( 'endereco.logradouro' ).value;
   var complemento = document.getElementById( 'endreco.complemento' ).value;
   var bairro = document.getElementById( 'endereco.bairro' ).value;
   var cidade = document.getElementById( 'endereco.cidade' ).value;
   var estado = document.getElementById( 'endereco.estado' ).value;

   if( rua == "" ) {
      document.getElementById( 'endereco.logradouro' ).className = "textoCampoFormulario";
   }
   if( complemento == "" ) {
      document.getElementById( 'endereco.complemento' ).className = "textoCampoFormulario";
   }
   if( bairro == "" ) {
      document.getElementById( 'endereco.bairro' ).className = "textoCampoFormulario";
   }
   if( cidade == "" ) {
      document.getElementById( 'endereco.cidade' ).className = "textoCampoFormulario";
   }
   if( estado == "" ) {
      document.getElementById( 'endereco.estado' ).className = "textoCampoFormulario";
   }

}

function testCep() {
   var cep = document.getElementById( 'cep' );
   // sleep2(1000, carregando);

   showdiv( 'carregando' );
   populaEndereco();

   var rua = document.getElementById( 'rua' ).value;
   var complemento = document.getElementById( 'complemento' ).value;
   var bairro = document.getElementById( 'bairro' ).value;
   var cidade = document.getElementById( 'cidade' ).value;
   var estado = document.getElementById( 'estado' ).value;

   if( rua == "" ) {
      document.getElementById( 'rua' ).className = "textoCampoFormulario";
   }
   if( complemento == "" ) {
      document.getElementById( 'complemento' ).className = "textoCampoFormulario";
   }
   if( bairro == "" ) {
      document.getElementById( 'bairro' ).className = "textoCampoFormulario";
   }
   if( cidade == "" ) {
      document.getElementById( 'cidade' ).className = "textoCampoFormulario";
   }
   if( estado == "" ) {
      document.getElementById( 'estado' ).className = "textoCampoFormulario";
   }

   hidediv( 'carregando' );
}

function populaEndereco() {
   var end = "Rua Hamilton Picanço";
   document.getElementById( 'rua' ).value = end;
}

function hidediv( obj ) {
   if( document.getElementById ) { // DOM3 = IE5, NS6
      document.getElementById( obj ).style.visibility = 'hidden';
   } else {
      if( document.layers ) { // Netscape 4
         document.hideshow.visibility = 'hidden';
      } else { // IE 4
         document.all.hideshow.style.visibility = 'hidden';
      }
   }
}

function showdiv( obj ) {
   if( document.getElementById ) { // DOM3 = IE5, NS6
      document.getElementById( obj ).style.visibility = 'visible';
   } else {
      if( document.layers ) { // Netscape 4
         document.hideshow.visibility = 'visible';
      } else { // IE 4
         document.all.hideshow.style.visibility = 'visible';
      }
   }
}

function oi( nome ) {
   alert( "oi " + nome );
}

function sleep2( milliseconds, div ) {
   var start = new Date().getTime();
   showdiv( div );
   for( var i = 0; i < 1e7; i++ ) {
      if( ( new Date().getTime() - start ) > milliseconds ) {
         break;
      }
   }
}

function sleep( milliseconds ) {
   // alert(milliseconds);
   var start = new Date().getTime();
   for( var i = 0; i < 1e7; i++ ) {
      if( ( new Date().getTime() - start ) > milliseconds ) {
         break;
      }
   }
}

/*
 * Altera visibilidade do elemento Esconde ou Mostra o elemento
 */
function MM_showHideLayers() { // v9.0
   var i, p, v, obj, args = MM_showHideLayers.arguments;
   for( i = 0; i < ( args.length - 2 ); i += 3 )
      with( document )
         if( getElementById && ( ( obj = getElementById( args[ i ] ) ) != null ) ) {
            v = args[ i + 2 ];
            if( obj.style ) {
               obj = obj.style;
               v = ( v == 'show' )? 'visible': ( v == 'hide' )? 'hidden': v;
            }
            obj.visibility = v;
         }
}

function confirmaEnviarEmail( location, id ) {
   if( confirm( 'Confirma o re-envio das informações o email?' ) ) {
      window.location = location + id;
   }
}

function gotoLocation( location, id ) {
   window.location = location + id;
}

function maxLength( elem, maxlength ) {
   var value = elem.value;
   if( value.length > maxlength ) {
      alert('Tamanho máximo excedido.');
      return false;
   }
   return true;
}
