/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;


/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function



/*
Funces Definidas
=================
So definidas as seguintes funcoes:

'FormataData(this, event)' que retorna o campo texto do formulario
formatado de acordo com o formato utilizado aqui no Brasil
(dd/mm/aaaa)

'VerificaData(this)' que retorna se a data no campo texto valida

'DataPorExtenso()' retorna a data atual por extenso, no formato:
dia-da-semana, dia_do_mes de mes de ano

Forma de Utilizar:
==================
<input type="Text" name="NomeDoCampo"
size="10" maxlength="10" value=""
onKeydown="FormataData(this,event)"
onBlur="VerificaData(this)">
*/

function DataPorExtenso() {
// esta funcao retorna a data do momento por extenso
// obtem os valores do dia atual
var agora = new Date();
var anoN = agora.getFullYear()
var mesN = agora.getMonth() + 1;
var diaN = agora.getDay() + 1;
var diaNr = ( (agora.getDate()<10) ? "0" : "") + agora.getDate();

// dia da semana por extenso
switch (diaN) {
case 1 : dia = "domingo";
break;
case 2 : dia = "segunda-feira";
break;
case 3 : dia = "tera-feira";
break;
case 4 : dia = "quarta-feira";
break;
case 5 : dia = "quinta-feira";
break;
case 6 : dia = "sexta-feira";
break;
case 7 : dia = "sbado";
break;
}

// mes por extenso
switch (mesN) {
case 1 :
mes = "Janeiro";
break;
case 2 :
mes = "Fevereiro";
break;
case 3 :
mes = "Maro";
break;
case 4 :
mes = "Abril";
break;
case 5 :
mes = "Maio";
break;
case 6 :
mes = "Junho";
break;
case 7 :
mes = "Julho";
break;
case 8 :
mes = "Agosto";
break;
case 9 :
mes = "Setembro";
break;
case 10 :
mes = "Outubro";
break;
case 11 :
mes = "Novembro";
break;
case 12 :
mes="Dezembro";
break;
}
return dia+", "+diaNr+" de "+mes+" de "+anoN;
}

// Formata a data em um campo do tipo input no formulario
// sintaxe, olhar acima para ver como fica
function FormataData(campo,teclapres) {
var tecla = teclapres.keyCode;
var tex = campo.value;
var vr = campo.value;
vr = vr.replace( ".", "" );
vr = vr.replace( "/", "" );
vr = vr.replace( "/", "" );
vr = vr.replace( "/", "" );
tam = vr.length + 1;

if ( tecla != 9 && tecla != 8 ){
if ( tam > 2 && tam < 5 )
campo.value = vr.substr( 0, tam - 2 ) + '/' +
vr.substr( tam - 2, tam );
if ( tam >= 5 && tam <= 10 )
campo.value = vr.substr( 0, 2 ) + '/' + vr.substr(
2, 2 ) + '/' + vr.substr( 4, 4 );
}
}

// retorna se o ano bissesto ou nao
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) {
return true;
}
}
else {
if ((intYear % 4) == 0) {
return true;
}
}
return false;
}

// verifica se a data valida
// se for nao faz nada, se nao, mostra um alerta que a data nao
// valida, apaga o campo input e coloca ele em focus denovo
function VerificaData(objNome) {
if (VerificaValidadeData(objNome) == false) {
objNome.select();
alert("O campo data invalido! Favor verificar...");
objNome.value = "";
objNome.focus();
return false;
}
else {
return true;
}
}

// trabalha com a funcao acima e retorna se a data valida
// se for retorna true, se nao, retorna false
function VerificaValidadeData(NomeObj) {
var data = NomeObj.value;

var strDay;
var strMonth;
var strYear;

var intDay;
var intMonth;
var intYear;

// retira qualquer dado estranho da data
data = data.replace( ".", "" );
data = data.replace( "/", "" );
data = data.replace( "/", "" );
tam = data.length + 1;

// se o tamanho da data for menor que 1 retorna verdadeiro (vazio)
if (data.length < 1) {
return true;
}

// pega os digitos separando dia, mes e ano
strDay = data.substr(0, 2);
strMonth = data.substr(2, 2);
strYear = data.substr(4);

// se o ano tiver somente dois digitos, converte para ano com
// quatro
if (strYear.length == 2) {
strYear = '20' + strYear;
}

// converte valores das datas para inteiros para poder fazer
// calculos sobre eles
intDay = parseInt(strDay, 10);
if (isNaN(intDay)) {
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
return false;
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
return false;
}

// verifica se o mes he menor que 1 ou he maior que 12
if (intMonth > 12 || intMonth < 1) {
return false;
}

// verifica a validade dos dias no mes (30 ou 31)
if (( intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth
== 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31
|| intDay < 1)) {
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth
== 11) && (intDay > 30 || intDay < 1)) {
return false;
}
// verifica o caso de janeiro
if (intMonth == 2) {
if (intDay < 1) {
return false;
}
if (LeapYear(intYear) == true) {
// funcao LeapYear retorna se he ano bissesto
if (intDay > 29) {
return false;
}
}
else {
if (intDay > 28) {
return false;
}
}
}
// a data esta ok, retorna true
return true;
}


