
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 ( array_data == undefined ) {
        array_data = new Array(0);
    }
    for ( i = 0 ; i < array_data.length ; i ++ ) { 
        if ( i != currentTab ) {
            document.getElementById( 'tab_' + array_data[i] ).style.display = 'none'; 
            document.getElementById( 'btn_' + array_data[i] ).className = 'inactive';
            document.getElementById( 'sp_' + array_data[i] ).className = 'inactive';
        } else {
            document.getElementById( 'defaultGroupValue' ).value = i;
            document.getElementById( 'tab_' + array_data[i] ).style.display = 'inline';
            document.getElementById( 'btn_' + array_data[i] ).className = 'active';
            document.getElementById( 'sp_' + array_data[i] ).className = 'active';
        }
    }
}


/* Funciones para mostrar un calendario */
var calendarLinks = new Array();

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, nId ) {
    
        /* Encontrar el elemento donde generar el calendario */
    var el = document.getElementById( eId );    
    if ( !el ) {
        return;
    }
    while ( el.firstChild) {
        el.removeChild(el.firstChild);
    }

    if ( document.all ) {
        var classAttr = 'className';
    } else {
        var classAttr = 'class';
    }

    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, lang );
    var lastDate = SACCalGetDays( month, year, lang );
    var prevLastDate = SACCalGetDays( pmonth, pyear, lang );

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

    var jumpToPrevMonth = "SACCalDrawCal( '" + lang + "', '" + eId + "', " + pyear + ", " + ( pmonth + 1 ) + ", 1, " + nId + " );";
    var jumpToNextMonth = "SACCalDrawCal( '" + lang + "', '" + eId + "', " + nyear + ", " + ( nmonth + 1 ) + ", 1, " + nId + " );";
    var jumpToPrevYear = "SACCalDrawCal( '" + lang + "', '" + eId + "', " + ( pyear - 1 ) + ", " + pmonth + ", 1, " + nId + " );";
    var jumpToNextYear = "SACCalDrawCal( '" + lang + "', '" + eId + "', " + ( nyear + 1 ) + ", " + nmonth + ", 1, " + nId + " );";
    
        /* Se arman los elementos del calendario */
    mainDiv = document.createElement( "div" );
    mainDiv.setAttribute( classAttr, "sac_calendar" );

    var table = document.createElement( "div" );
    table.setAttribute( classAttr, 'table' );

    var tr = document.createElement( "div" );
    tr.setAttribute( classAttr, 'title' );
    var th = document.createElement( "span" );
    th.setAttribute( classAttr, "prev_month" );
    var a = document.createElement( "a" );
    a.setAttribute( "onclick", jumpToPrevMonth );
    a.setAttribute( "href", 'javascript:' + jumpToPrevMonth );
    a.appendChild( document.createTextNode( '< ' ) );
    th.appendChild( a );
    tr.appendChild( th );
    var th = document.createElement( "span" );
    th.setAttribute( classAttr, "title" );
    th.appendChild( document.createTextNode( monthName ) );
    tr.appendChild( th );
    var th = document.createElement( "span" );
    th.setAttribute( classAttr, "next_month" );
    var a = document.createElement( "a" );
    a.setAttribute( "onclick", jumpToNextMonth );
    a.setAttribute( "href", 'javascript:' + jumpToNextMonth );
    a.setAttribute( classAttr, 'next_month' );
    a.appendChild( document.createTextNode( ' >' ) );
    th.appendChild( a );
    tr.appendChild( th );

    var th = document.createElement( "span" );
    th.setAttribute( classAttr, "prev_year" );
    var a = document.createElement( "a" );
    a.setAttribute( "onclick", jumpToPrevYear );
    a.setAttribute( "href", 'javascript:' + jumpToPrevYear );
    a.appendChild( document.createTextNode( '< ' ) );
    th.appendChild( a );
    tr.appendChild( th );
    var th = document.createElement( "span" );
    th.setAttribute( classAttr, "title" );
    th.appendChild( document.createTextNode( year ) );
    tr.appendChild( th );
    var a = document.createElement( "a" );
    var th = document.createElement( "span" );
    th.setAttribute( classAttr, "next_year" );
    a.setAttribute( "onclick", jumpToNextYear );
    a.setAttribute( "href", 'javascript:' + jumpToNextYear );
    a.setAttribute( classAttr, 'next_year' );
    a.appendChild( document.createTextNode( ' >' ) );
    th.appendChild( a );
    tr.appendChild( th );

    table.appendChild( tr );
    
    var tr = document.createElement( "div" );
    tr.setAttribute( classAttr, 'header' );
    for ( var dayNum = 0; dayNum < 7; ++dayNum ) {
        tr.appendChild( addText( "span", SACCalGetDayOfWeek( dayNum, lang ) ) );
    }
    table.appendChild( tr );
    var digit = 1;
    var curCell = 1;
    var nextMonthDay = 1;
    var extraCssClass;
    var typeOfEventsUsed = Array();
    var currElement = 0;
    for ( var row = 1;
          row <= Math.ceil( ( lastDate + firstDay - 1 ) / 7 );
          ++row) {
        var tr = document.createElement( "div" );
        tr.setAttribute( classAttr, 'row' );
        for ( var col = 1; col <= 7; ++col ) {
            if ( calendarLinks[nId]
                 && calendarLinks[nId][year]
                 && calendarLinks[nId][year][month + 1]
                 && calendarLinks[nId][year][month + 1][digit] ) {
                var anchor = document.createElement( "a" );
                var importText = calendarLinks[nId][year][month + 1][digit];
                var typeEvent = parseInt( importText.substring( 0, 4 ), 10 );
                var completeText = importText.substring( 5 );
                var comma = completeText.search( ',' );
                var url = completeText.substring( 0, comma );
                var title = completeText.substring( comma + 1 );
                anchor.setAttribute( "href", url
                                     + ( url.indexOf( '?' ) < 0 ? '?' : '&' )
                                     + 'dateCalendar=' + year + '-' + ( month + 1 ) + '-' + digit );
                extraCssClass = '';
                if ( typeEvent > 0
                     && calendar_events
                     && calendar_events[typeEvent] ) {
                    extraCssClass = ' ' + calendar_events[typeEvent];
                    found = false;
                    for ( ti = 0; ti < typeOfEventsUsed.length; ti++ ) {
                        if ( calendar_events[typeEvent]
                             == typeOfEventsUsed[ti] ) {
                            found = true;
                            break;
                        }
                    }
                    if ( !found ) {
                        typeOfEventsUsed[currElement++]
                            = calendar_events[typeEvent];
                    }
                }
                anchor.appendChild( document.createTextNode( digit ) );
                anchor.setAttribute( "title", title );
                td = document.createElement( 'span' );
                td.setAttribute( "title", title );
                if ( currentMonth && digit == today ) {
                    td.setAttribute( classAttr,
                                     'now activity' + extraCssClass );
                } else {
                    td.setAttribute( classAttr,
                                     'activity' + extraCssClass );
                }
                td.appendChild( anchor );
                textLink = td;
            } else {
                if ( currentMonth && digit == today ) {
                    textLink = addText( "span", digit, 'now' );
                } else {
                    textLink = addText( "span", digit );
                }
            }
            
            if ( digit > lastDate ) {
                tr.appendChild( addText( "span", nextMonthDay++, "next_month" ) );
                continue;
            }
            if ( curCell < firstDay ) {
                tr.appendChild( addText( "span", prevLastDate + col - 5, "prev_month" ) );  
                curCell++;
            } else {
                tr.appendChild( textLink );
                digit++;
            }
        }
        table.appendChild( tr );
    }
    mainDiv.appendChild( table );
    el.appendChild( mainDiv );

    var table = document.createElement( "div" );
    table.setAttribute( classAttr, 'table' );
    for ( i = 0; i < typeOfEventsUsed.length; i++ ) {
        var tr = document.createElement( "div" );
        tr.setAttribute( classAttr, 'row' );
        
        var td = document.createElement( "span" );
        td.setAttribute( classAttr, 'activity ' + typeOfEventsUsed[i] );
        var anchor = document.createElement( "a" );
        anchor.appendChild( document.createTextNode( ' ... ' ) );
        td.appendChild( anchor );
        tr.appendChild( td );
        
        var td = document.createElement( "b" );
        td.setAttribute( classAttr, '' );
        td.appendChild( document.createTextNode( typeOfEventsUsed[i] ) );
        tr.appendChild( td );

        table.appendChild( tr );
    }
    el.appendChild( table );
}

function addText( type, value, ownclass ) {
    var td = document.createElement( type );
    if ( document.all ) {
        var classAttr = 'className';
    } else {
        var classAttr = 'class';
    }
    if ( ownclass ) {
        td.setAttribute( classAttr, ownclass );
    }
    text = document.createTextNode( value );
    td.appendChild( text );
    return td;
}

function _setStyle( element, declaration ) {
    if ( declaration.charAt( declaration.length - 1 ) == ';' ) {
        declaration = declaration.slice( 0, -1 );
    }
    var k, v;
    var splitted = declaration.split( ';' );
    for ( var i = 0, len = splitted.length; i < len; i++ ) {
        k = rzCC( splitted[i].split( ':' )[0] );
        v = splitted[i].split( ':' )[1];
        eval( "element.style." + k + "='" + v + "'" );
    }
}

function rzCC(s){
    for( var exp=/-([a-z])/; 
         exp.test(s); 
         s=s.replace( exp, RegExp.$1.toUpperCase() ) );
    return s;
}
