/* Aqui vamos a ir a±adiendo todas las funciones genericas JS*/
// Seleccion de de datos en las listas

function selectionDone(itemValor,itemId) {
        this.opener.cajaValor.value=itemValor;
        this.opener.cajaId.value=itemId;
        self.close();
}


//+++++++++++++++++++++Funciones necesarias para el vetana modal en netscape (incluido el calendario)++++++++++++++++++++++++++++++++++++++++++++++++++++++

// Global for brower version branching.
//var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4));
// One object tracks the current modal dialog opened from this window.
//var dialogWin = new Object();
// Generate a modal dialog.
// Parameters:
//    url -- URL of the page/frameset to be loaded into dialog
//    width -- pixel width of the dialog window
//    height -- pixel height of the dialog window
//    returnFunc -- reference to the function (on this page)
//                  that is to act on the data returned from the dialog
//    args -- [optional] any data you need to pass to the dialog
function y2k(number)    { return (number < 1000) ? number + 1900 : number; }



function openDialog(url, name, width, height, returnFunc, objeto, args) {
        if (!dialogWin.win || (dialogWin.win && dialogWin.win.closed)) {
                // Initialize properties of the modal dialog object.
                dialogWin.returnFunc = returnFunc
                dialogWin.objeto =  objeto
                //alert(dialogWin.objeto);
                dialogWin.returnedValue = ""
                dialogWin.args = args
                dialogWin.url = url
                dialogWin.width = width
                dialogWin.height = height
                dialogWin.name = name
                //Propiedades iniciales para su uso cuando se utilice para abrir e calendario de Netscape
                var now = new Date();
                dialogWin.month =now.getMonth();
                dialogWin.year = y2k(now.getYear());
                // Keep name unique so Navigator doesn't overwrite an existing dialog.
                //dialogWin.name = (new Date()).getSeconds().toString()
                // Assemble window attributes and try to center the dialog.
                //if (Nav4) {
                        // Center on the main window.
                        dialogWin.left = window.screenX + 
                           ((window.outerWidth - dialogWin.width) / 2);
                        dialogWin.top = window.screenY + 
                           ((window.outerHeight - dialogWin.height) / 2);
                        var attr = "screenX=" + dialogWin.left + 
                           ",screenY=" + dialogWin.top + ",resizable=yes,scrollbars=yes,width=" + 
                           dialogWin.width + ",height=" + dialogWin.height;
                //}             
                // Generate the dialog and make sure it has focus.
                dialogWin.win=window.open(dialogWin.url, dialogWin.name, attr);
                dialogWin.win.focus()
        } else {
                dialogWin.win.focus()
        }
}


// Event handler to inhibit Navigator form element 
// and IE link activity when dialog window is active.

function deadend() {
        //alert("hola");
        
        if (dialogWin.win && !dialogWin.win.closed) {
                dialogWin.win.focus()
                return false
        }
}

// Grab all Navigator events that might get through to form
// elements while dialog is open. For IE, disable form elements.

function blockEvents() {
        //if (Nav4) {
                window.captureEvents(Event.CLICK);
                window.onclick = deadend;
                
        //} else {
        //disableForms()
        //}
        //window.onfocus = checkModal
}

// As dialog closes, restore the main window's original// event mechanisms.

function unblockEvents() {
        //if (Nav4) {
                window.releaseEvents(Event.CLICK);
                window.onclick = null;
                //window.onfocus = null;

        //} else {
                //enableForms()
        //}
// Invoked by onFocus event handler of EVERY frame,
// return focus to dialog window if it's open.function checkModal() {
        if (dialogWin.win && !dialogWin.win.closed) {
                dialogWin.win.focus()
        }
}       




// Function to call the calendar popup window
function Calendario(objeto){
    //El parametro objeto nos da el objeto en el que el calendario va a retornar la fecha 
    fecha = showModalDialog('/lib/calendarmodif.jsp', objeto.value,'dialogWidth=185pt;dialogHeight=153pt;maximize=no;minimize=no;border=thin;center=yes;help=no;');
    objeto.value = fecha;
}

function Calendario666(objeto) 
   {
      //alert(navigator.appVersion);
          var MS = navigator.appVersion.indexOf("MSIE");
          window.isIE4 = (MS > 0) && (parseInt(navigator.appVersion.substring(MS + 5, MS + 6)) >= 4);
          //alert(isIE4);
          
        if (!isIE4) {
          openDialog('/grupo0/plantillas/calendario.jsp','Calendario', '400', '290', returnFunc, objeto,'1')}
        else{
          fecha = showModalDialog('/grupo0/plantillas/calendarmod.jsp', objeto.value,'dialogWidth=455pt;dialogHeight=151pt;maximize=no;minimize=no;border=thin;center=yes;help=no;');
          objeto.value = fecha;
    }
   }
   
 function returnFunc(objeto){
 objeto.value = dialogWin.returnedValue;
 }
 
//Funcion para cambiar el formato de las fechas de ingles a portugues
function fechaingles(esp){
        var dia=esp.substring(2,0);
        var mes=esp.substring(5,3);
        var ano=esp.substring(10,6);
        ing = ano + "-" + mes +"-"+ dia;
return(ing);
}


// Open a new window, full screen
function NewWindowFull(url) {
	var window_width = (screen.availWidth-10);
	var window_height = (screen.availHeight-30);

	window.open(url, 'mywindow', 'resizable=yes, toolbar=no, left=0, top=0, width='+window_width+', height='+window_height);
}




// Opens a new window, with given parameters
function NewWindow(url, windowName, windowWidth, windowHeight) {
    var leftPos = (screen.availWidth - windowWidth) / 2;
    var topPos =  (screen.availHeight - windowHeight) / 2;

    MyWindow = window.open(url, windowName, 'resizable=no, scrollbars=1, toolbar=no, left='+leftPos+', top='+topPos+', width='+windowWidth+', height='+windowHeight);
    MyWindow.focus();
}



// Opens a modal window, with given parameters
function NewModalWindow(url, windowName, windowWidth, windowHeight) {
    var leftPos = (screen.availWidth - windowWidth) / 2;
    var topPos =  (screen.availHeight - windowHeight) / 2;

    MyWindow = showModalDialog(url, windowName, 'dialogWidth=450px;dialogHeight=350px;maximize=no;minimize=no;border=thin;center=yes;help=no;');
    MyWindow.focus();
}



// Function to set the focus at a given form field
function SetFocusAtElement(aId){
	var e = null;
	e = document.getElementById(aId);
	if(e == null || typeof e == "undefined" )
		setTimeout("SetFocusAtElement('"+aId+"')", 100, "javascript");
	else e.focus();
}



function Valida(frm,fn) {
	if (frm.uname.value.length == 7) {
		SetFocusAtElement('passwd');
	}
	return true;
}




function onlyDigits(e) {
var isIE = document.all?true:false;
var isNS = document.layers?true:false;

  var _ret = true;
  if (isIE) {
    //if (window.event.keyCode < 46 || window.event.keyCode > 57) {
    // Accept digits and return
    if ((window.event.keyCode < 46 || window.event.keyCode > 57) && window.event.keyCode != 13) {
      window.event.keyCode = 0;
      _ret = false;
    }
  }
  if (isNS) {
    // Accept digits and return
    if ((e.which < 46 || e.which > 57) && e.which != 13) {
      e.which = 0;
      _ret = false;
    }
  }
  return (_ret); 
}




function onlyDigitsClean(e) {
var isIE = document.all?true:false;
var isNS = document.layers?true:false;

  var _ret = true;
  if (isIE) {
    // Accept digits and return
    if ((window.event.keyCode < 48 || window.event.keyCode > 57) && window.event.keyCode != 13) {
      window.event.keyCode = 0;
      _ret = false;
    }
  }
  if (isNS) {
    // Accept digits and return
    if ((e.which < 48 || e.which > 57) && e.which != 13) {
      e.which = 0;
      _ret = false;
    }
  }
  return (_ret); 
}




function onlyDateFormat(e) {
var isIE = document.all?true:false;
var isNS = document.layers?true:false;

  var _ret = true;
  if (isIE) {
    //if (window.event.keyCode < 46 || window.event.keyCode > 57) {
    // Accept digits and return
    if ((window.event.keyCode < 48 || window.event.keyCode > 57) && window.event.keyCode != 13 && window.event.keyCode != 45) {
      window.event.keyCode = 0;
      _ret = false;
    }
  }
  if (isNS) {
    // Accept digits and return
    if ((e.which < 48 || e.which > 57) && e.which != 13 && window.event.keyCode != 45) {
      e.which = 0;
      _ret = false;
    }
  }
  return (_ret); 
}



// Display wait message
// begin
function startAnimation(){ 
    document.getElementById("MP").style.display="block"; 
    document.getElementById("MPP").innerHTML="<table width=\"100%\" height=\"100%\" align=\"center\"><tr height=\"100%\"><td width=\"100%\"><table width=\"350\" align=\"center\"  style=\"border-left: #E0E0E0 solid 1; border-right: 1 solid #808080; border-top: 1 solid #E0E0E0; border-bottom: 1 solid #808080\"><tr><td height=\"100%\" align=\"left\" bgcolor=\"#CEE0F4\" style=\"font-family: Verdana; font-size: 8pt; font-weight: bold; color: #004065;\"  >Em processamento</td></tr><tr height=\"100\"><td height=\"100\" align=\"center\" valign=\"middle\"   style=\"font-family: Verdana; font-size: 8pt; font-weight: bold; color: #000000;\"  ><img src=\"/images/progress_ff.gif\" width=\"16\" height=\"16\" align=\"absmiddle\">&nbsp;<b>Aguarde um momento, por favor... </b></td></tr></table></td></tr></table>";
    document.getElementById("PAGINA").style.visibility="hidden"; 
    return true;
}  
// end
// Display wait message



// Zoom web page
var currentzoom = 100;
function zoom(distance) {
    bodyid = document.getElementById('body');
	if(bodyid) {
	   if(distance == 0){
	   	currentzoom = 100;
	   }else{
		   if( (distance>0 && currentzoom<300) || (distance < 0 && currentzoom >60)) {
				currentzoom+=distance;
		   }
	   }
	   bodyid.className = "body" + currentzoom + "";
	}
    iframenav = window.frames['nav'];
	if(iframenav) {
	   iframenav.zoom(distance);

	}
}



// Check that emails are correctly formed
function echeck(str) {

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    var message = 'Endereço de e-mail inválido!';
    
    
    if (str.indexOf(at)==-1){
       alert(message)
       return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       alert(message)
       return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert(message)
        return false
    }

     if (str.indexOf(at,(lat+1))!=-1){
        alert(message)
        return false
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert(message)
        return false
     }

     if (str.indexOf(dot,(lat+2))==-1){
        alert(message)
        return false
     }
    
     if (str.indexOf(" ")!=-1){
        alert(message)
        return false
     }

     return true                    
}
