function checkDate(field) {
    var checkstr = "0123456789";
    var DateField = field;
    var Datevalue = "";
    var DateTemp = "";
    var seperator = "/";
    var day;
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;
    err = 0;
    DateValue = DateField.value;
    
    /* Deletando todos os caracteres exceto o 0..9 */
    for (i = 0; i < DateValue.length; i++) {
        if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
            DateTemp = DateTemp + DateValue.substr(i,1);
        }
    }
    DateValue = DateTemp;

    /* Exectutando a data para 8 digitos - string*/
    /* if entrada do ano com 2-digitos / exemplo 20xx */
    if (DateValue.length == 6) {
        DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2);
    }
    if (DateValue.length != 8) {
        err = 19;
    }
    /* Se o ano for errado = 0000 */
    year = DateValue.substr(4,4);
    if (year == 0) {
        err = 20;
    }
    /* Validando o mês*/
    month = DateValue.substr(2,2);
    if ((month < 1) || (month > 12)) {
        err = 21;
    }
    /* Validando o dia*/
    day = DateValue.substr(0,2);
    if (day < 1) {
        err = 22;
    }
    /* Validando ano Bissexto / fevereiro / dia */
    if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
        leap = 1;
    }
    if ((month == 2) && (leap == 1) && (day > 29)) {
        err = 23;
    }
    if ((month == 2) && (leap != 1) && (day > 28)) {
        err = 24;
    }
    /* Validando o mês */
    if ((day > 31) && ((month == "01") || (month == "03") || 
        (month == "05") || (month == "07") || (month == "08") || 
        (month == "10") || (month == "12"))) {
        err = 25;
    }
    if ((day > 30) && ((month == "04") || (month == "06") || 
        (month == "09") || (month == "11"))) {
        err = 26;
    }
    /* if 00 houvendo entrada, sem erros */
    if ((day == 0) && (month == 0) && (year == 00)) {
        err = 0; day = ""; month = ""; year = ""; seperator = "";
    }
    /* if sem erros, escrevo a data completa no Input-Field (e.x. 13/12/2001) */
    if (err == 0) {
        DateField.value = day + seperator + month + seperator + year;
    }
    else { /* Mensagem de erro if err != 0 */
        alert("Atenção, a data está incorreta!");
        DateField.select();
        DateField.focus();
    }
}

function confirmaExclusao(mainForm) {
    return confirm('Confirma a exclusão dos itens selecionados ?');
}

function doConfirmaExclusao(mainForm) {
    if (confirmaExclusao(mainForm)) {
        if (mainForm != null)
            mainForm.submit();
        else {
            var aForm = window.document.forms[0];
            aForm.submit();
        }        
    }
}

function enviarForm() {
    var aForm = document.forms[0];
    aForm.submit();
}

function formatNumberUS(numeroToReplace) {
    var sReplace = "";

    sReplace = numeroToReplace.replace(".", "");
    sReplace = sReplace.replace(",", ".");

    return sReplace;
}

function hideLayer(layerName) {
    var visibleVar = "null";

    layerStyleRef = "layer.style.";
    layerRef = "document.getElementById";
    styleSwitch = ".style";
    visibleVar = "visible";

    eval(layerRef+'("'+layerName+'")'+styleSwitch+'.visibility="hidden"');
}

function irPara(urlName) {
    window.location.href = urlName;
}

function maskDate(field) {
    var checkstr = "0123456789";
    var DateField = field;
    var Datevalue = "";
    var DateTemp = "";
    var seperator = "/";
    var day;
    var month;
    var year;
    var leap = 0;
    var err = 0;
    var i;
    err = 0;
    DateValue = DateField.value;
    
    /* Delete all chars except 0..9 */
    for (i = 0; i < DateValue.length; i++) {
        if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
            DateTemp = DateTemp + DateValue.substr(i,1);
        }
    }
    DateValue = DateTemp;
    /* Always change date to 8 digits - string*/
    /* if year is entered as 2-digit / always assume 20xx */
    if (DateValue.length == 6) {
        DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); 
    }
    if (DateValue.length != 8) {
        err = 19;
    }
    /* year is wrong if year = 0000 */
    year = DateValue.substr(4,4);

    //if (year == 0 || year > 9999 || year <= 0000) {
    if (year == 0 || year > 9999) {
        err = 20;
    }
    
    /* Validation of month*/
    month = DateValue.substr(2,2);
    if ((month < 1) || (month > 12)) {
        err = 21;
    }

    /* Validation of day*/
    day = DateValue.substr(0,2);
    if (day < 1) {
        err = 22;
    }

    /* Validation leap-year / february / day */
    if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
        leap = 1;
    }
    if ((month == 2) && (leap == 1) && (day > 29)) {
        err = 23;
    }
    if ((month == 2) && (leap != 1) && (day > 28)) {
        err = 24;
    }
    /* Validation of other months */
    if ((day > 31) && ((month == "01") || (month == "03") || 
        (month == "05") || (month == "07") || (month == "08") || 
        (month == "10") || (month == "12"))) {
        err = 25;
    }
    if ((day > 30) && ((month == "04") || (month == "06") || 
        (month == "09") || (month == "11"))) {
        err = 26;
    }

    /* if 00 ist entered, no error, deleting the entry */
    if ((day == 0) && (month == 0) && (year == 00)) {
        err = 0; day = ""; month = ""; year = ""; seperator = "";
    }

    /* if no error, write the completed date to Input-Field 
        (e.g. 13.12.2001) */
    if (err == 0) {
        DateField.value = day + seperator + month + seperator + year;
    
        return true;
    }
    else {    /* Error-message if err != 0 */    
        alert("Data inválida.");
        DateField.select();
        return false;
    }
}

function maskMoeda(campo, e) {
    var SeparadorDecimal = ","
    var SeparadorMilesimo = "."
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;

    if (whichCode == 13) 
        return true;
    key = String.fromCharCode(whichCode); // Valor para o código da Chave

    if (strCheck.indexOf(key) == -1) 
        return true; // Chave inválida
    len = campo.value.length;

    for(i = 0; i < len; i++) {
        if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != SeparadorDecimal)) 
	       break;
    }
    
    aux = '';
    for(; i < len; i++) {
        if (strCheck.indexOf(campo.value.charAt(i)) != -1)
            aux += campo.value.charAt(i);
    }
    
    aux += key;
    len = aux.length;

    if (len == 0) campo.value = '';
    if (len == 1) campo.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) campo.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        campo.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--) {
            campo.value += aux2.charAt(i);
        }
        campo.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function showLayer(layerName) {
    var visibleVar = "null";

    layerStyleRef="layer.style.";
    layerRef="document.getElementById";
    styleSwitch=".style";
    visibleVar="visible";

    eval(layerRef+'("'+layerName+'")'+styleSwitch+'.visibility="'+visibleVar+'"');
}


function stripe(name) {
    var tables = document.getElementsByName(name);
    
    for(var x=0; x != tables.length; x++) {
        var table = tables[x];
        if (! table) { return; }
    
        var tbodies = table.getElementsByTagName("tbody");
        
        for (var h = 0; h < tbodies.length; h++) {
            var even = true;
            var trs = tbodies[h].getElementsByTagName("tr");
          
            for (var i = 0; i < trs.length; i++) {
                trs[i].onmouseover = function() {
                  this.className += " ruled"; 
                  return false
                }
                
                trs[i].onmouseout = function() {
                    this.className = this.className.replace("ruled", ""); 
                    return false
                }
            
                if(even)
                    trs[i].className += " even";
            
                even = !even;
            }
        }
    }
}

function voltar() {
   var aWindow = window;
   aWindow.history.back(-1);
}

function toURL(urlName) {
    if (urlName != "")
        location.href = urlName;
}

function DoPrinting(){
	if (!window.print){
		alert("Use o Netscape  ou Internet Explorer \n nas versões 4.0 ou superior!")
		return
	}
	window.print()
}
