function confirmLink( theLink, theQuery ) {
    var isConfirmed = confirm( theQuery );
    if ( isConfirmed ) {
      theLink.href += '&isConfirmed=1';
    }
    return isConfirmed;
}

function cssClick( e, classBase ) {
    classOver = classBase + '_over';
    classClick = classBase + '_click';
    if ( e.className == classBase || e.className == classOver ) {
        e.className = classClick;
    } else {
        e.className = classBase;
    }
}

function cssOver( e, classBase ) {
    classOver = classBase + '_over';
    classClick = classBase + '_click';
    if ( e.className != classClick ) {
        e.className = classOver;
    }
}

function cssOut( e, classBase ) {
    classOver = classBase + '_over';
    classClick = classBase + '_click';
    if ( e.className != classClick ) {
        e.className = classBase;
    }
}

function setCheckboxes( the_form, do_check ) {
    var el = document.forms[the_form].elements['node[]'];
    var el_cnt = ( typeof( el.length ) != 'undefined') ? el.length : 0;
    if ( el_cnt ) {
        for ( var i = 0; i < el_cnt; i++ ) el[i].checked = do_check;
    } else {
        el.checked = do_check;
    }
    return true;
}

function invertCheckboxes( the_form ) {
    var el = document.forms[the_form].elements['node[]'];
    var el_cnt = (typeof(el.length) != 'undefined') ? el.length : 0;
    if ( el_cnt ) {
        for ( var i = 0; i < el_cnt; i++ ) el[i].checked = !el[i].checked;
    } else {
        el.checked = !el.checked;
    }
    return true;
}


/* Despliega de tabs, del buscador */
function show_tabs( currentTab ) {
   if ( currentTab == undefined ) {
       currentTab = 0;
   }
   if ( ar == undefined ) {
       ar = new Array(0);
   }
   for ( i = 0 ; i < array_data.length ; i ++ ) { 
       if ( i != currentTab ) {
           document.getElementById( 'tab_' + ar[i] ).style.display = 'none'; 
           document.getElementById( 'btn_' + ar[i] ).className = 'inactive';
           document.getElementById( 'sp_' + ar[i] ).className = 'inactive';
       } else {
           document.getElementById( 'defaultGroupValue' ).value = i;
           document.getElementById( 'tab_' + ar[i] ).style.display = 'inline';
           document.getElementById( 'btn_' + ar[i] ).className = 'active';
           document.getElementById( 'sp_' + ar[i] ).className = 'active';
       }
   }
}


/* Funciones para mostrar un calendario */
function SACCalSetCal( lang, eId, year, month, day ) {
    SACCalDrawCal( lang, eId, year, month, day );
}

function SACCalGetTime() {
    var now = new Date();
    var hour = now.getHours();
    var minute = now.getMinutes();
    now = null;
    var ampm = "";
    if ( hour >= 12 ) {
        hour -= 12;
        ampm = "PM";
    } else {
        ampm = "AM";
    }
    hour = (hour == 0) ? 12 : hour;
        
    if ( minute < 10 ) {
        minute = "0" + minute;
    }    
    return hour + ":" + minute + " " + ampm;
}

function SACCalLeapYear( year ) {
    if ( year % 4 == 0 ) {
        return true;
    } else {
        return false;
    }
}

function SACCalGetDays( month, year ) {
    var ar = new Array(12);
    ar[0] = 31; // Enero
    ar[1] = ( SACCalLeapYear( year ) ) ? 29 : 28; // Febrero
    ar[2] = 31; // Marzo
    ar[3] = 30; // Abril
    ar[4] = 31; // Mayo
    ar[5] = 30; // Junio
    ar[6] = 31; // Julio
    ar[7] = 31; // Augosto
    ar[8] = 30; // Septiembre
    ar[9] = 31; // Octubre
    ar[10] = 30; // Noviembre
    ar[11] = 31; // Diciembre
    return ar[month]
}

function SACCalGetMonthName( month, lang ) {
    var ar = new Array(12);
    if ( lang == "en" ) {
        ar[0] = "January";
        ar[1] = "February";
        ar[2] = "March";
        ar[3] = "April";
        ar[4] = "May";
        ar[5] = "June";
        ar[6] = "July";
        ar[7] = "August";
        ar[8] = "September";
        ar[9] = "October";
        ar[10] = "November";
        ar[11] = "December";
    } else {
        ar[0] = "Enero";
        ar[1] = "Febrero";
        ar[2] = "Marzo";
        ar[3] = "Abril";
        ar[4] = "Mayo";
        ar[5] = "Junio";
        ar[6] = "Julio";
        ar[7] = "Agosto";
        ar[8] = "Septiembre";
        ar[9] = "Octubre";
        ar[10] = "Noviembre";
        ar[11] = "Diciembre";
    }
    
    return ar[month];
}

function SACCalGetDayOfWeek( day, lang ) {
    var weekDay = new Array(7);
    if ( lang == "en" ) {
        weekDay[0] = "Sun";
        weekDay[1] = "Mon";
        weekDay[2] = "Tue";
        weekDay[3] = "Wed";
        weekDay[4] = "Thu";
        weekDay[5] = "Fri";
        weekDay[6] = "Sat";
    } else {
        weekDay[0] = "Dom";
        weekDay[1] = "Lun";
        weekDay[2] = "Mar";
        weekDay[3] = "Mié";
        weekDay[4] = "Jue";
        weekDay[5] = "Vie";
        weekDay[6] = "Sáb";
    }
    return weekDay[day];
}

function SACCalDrawCal( lang, eId, year, month, day ) {

        /* Encontrar el elemento donde generar el calendario */
    var el = document.getElementById( eId );
    if ( !el ) {
        return;
    }
    while ( el.firstChild) {
        el.removeChild(el.firstChild);
    }

    var now = new Date();
    var today = now.getDate();
    
    if ( year && month && day ) {
        month -= 1;
    } else {
        if ( year && month ) {
            day = 1;
            month -= 1;
        } else {
            day = now.getDate();
            month = now.getMonth();
            year = now.getYear();
        }
    }

    var currentMonth = month == now.getMonth()
        && ( year == now.getYear() || year == now.getYear() + 1900 );

    if ( year < 1000 ) {
        year += 1900;
    }

    var pyear = year;
    var pmonth = month - 1;
    if ( month == 0 ) {
        pyear--;
        pmonth = 11;
    }
    
    var nyear = year;
    var nmonth = month + 1;
    if ( month == 11 ) {
        nyear++;
        nmonth = 0;
    }
    
    var monthName = SACCalGetMonthName( month );
    var lastDate = SACCalGetDays( month, year );
    var prevLastDate = SACCalGetDays( pmonth, pyear );

    var firstDayInstance = new Date( year, month, 1 );
    var firstDay = firstDayInstance.getDay() + 1;
    firstDayInstance = null;

    var jumpToPast = "SACCalDrawCal( '" + lang + "', '" + eId + "', " + pyear + ", " + ( pmonth + 1 ) + " );";
    var jumpToFuture = "SACCalDrawCal( '" + lang + "', '" + eId + "', " + nyear + ", " + ( nmonth + 1 ) + " );";
    
        /* Se arman los elementos del calendario */
    div = document.createElement( "div" );
    div.setAttribute( "class", "sac_calendar" );

    table = document.createElement( "table" );

    tr = document.createElement( "tr" );
    th = document.createElement( "th" );
    leftArrow = document.createElement( "a" );
    leftArrow.setAttribute( "onclick", jumpToPast );
    leftArrow.appendChild( document.createTextNode( '<-' ) );
    tr.appendChild( leftArrow );
    
    title = document.createElement( "th" );
    title.setAttribute( "colspan", "6" );
    title.appendChild( document.createTextNode( monthName + ' ' + year ) );
    tr.appendChild( title );

    rightArrow = document.createElement( "a" );
    rightArrow.setAttribute( "onclick", jumpToFuture );
    rightArrow.appendChild( document.createTextNode( '->' ) );
    tr.appendChild( rightArrow );
    
    table.appendChild( tr );

    tr = document.createElement( "tr" );
    for ( var dayNum = 0; dayNum < 7; ++dayNum ) {
        tr.appendChild( addText( "th", SACCalGetDayOfWeek( dayNum ) ) );
    }
    table.appendChild( tr );
    
    var digit = 1;
    var curCell = 1;
    var nextMonthDay = 1;
    for ( var row = 1;
          row <= Math.ceil( ( lastDate + firstDay - 1 ) / 7 );
          ++row) {
        tr = document.createElement( "tr" );
        for ( var col = 1; col <= 7; ++col ) {
            if ( digit > lastDate ) {
                tr.appendChild( addText( "td", nextMonthDay++,
                                           "next_month" ) );
                continue;
            }
            if ( curCell < firstDay ) {
                tr.appendChild( addText( "td", prevLastDate + col - 5,
                                         "prev_month" ) );  
                curCell++;
            } else {
                if ( currentMonth && digit == today ) {
                    tr.appendChild( addText( "td", digit, "now" ) );
                } else {
                    tr.appendChild( addText( "td", digit ) );
                }
                digit++;
            }
        }
        table.appendChild( tr );
    }
    div.appendChild( table );
    el.appendChild( div );
}

function addText( type, value, class ) {
    td = document.createElement( type );
    if ( class ) {
        td.setAttribute( "class", class );
    }
    text = document.createTextNode( value );
    td.appendChild( text );
    return td;
}

/*
   Estilos para
     fin de semana
     filas par e impar
     
   Arrays para
     feriados
     enlaces
     tooltips
*/
