Cufon.replace('#encabezado h2, #contenidoCentral h2,#contenidoCentral h2 a,#contenidoCentral h3,#contenidoCentral h3 a, #autoGestion h3, #autoGestion2 h3, #moduloAyuda h3, #serviciosAnswer h3, #panel legend, .boxContenido h1');
function carousel_go(val) {
    document.getElementById('go_where').value = val;
    document.getElementById('button_go').click();
}
function ContactClick() {
    if (validar() == true) {
        document.getElementById(document.getElementById('motivoserver').value).value = document.getElementById('motivo').value;
        document.getElementById(document.getElementById('DiaHora').value).value = document.getElementById('anio').value + '/' + document.getElementById('mes').value + '/' + document.getElementById('dia').value + '  ' + document.getElementById('hora').value;
        document.getElementById(document.getElementById('btn_contact').value).click();
    }
}
function validar() {
    var _return = true;
    document.getElementById('motivo-button').setAttribute('style', 'border: 0');
    document.getElementById('hora').setAttribute('style', 'border: 0');
    if (document.getElementById('motivo').selectedIndex == 0) {
        document.getElementById('motivo-button').setAttribute('style', 'border: 1px solid red');
        _return = false;
    }
    if (document.getElementById('hora_validate').value != '') {

        if (document.getElementById('hora_validate').value == 'false') {
            document.getElementById('hora').setAttribute('style', 'border: 1px solid red');
            _return = false;
        } 
    }
    return _return;
}
function contactBtnClick(form) {
    if (form == 'personales')
    { document.getElementById('motivo').selectedIndex = 6; }
    else {
        if (form == 'hogar')
        { document.getElementById('motivo').selectedIndex = 5; }
        else {
            if (form == 'auto')
            { document.getElementById('motivo').selectedIndex = 4; }
        }
    }
    $('select').selectmenu('destroy').selectmenu();
    document.getElementById('panel').style.display = '';
    document.getElementById('panelBtn').className = 'btnContacto activa';
    window.scrollTo(0, 0);
}
function goTo() {
    var i = window.location.href.toString().split('#');
    if (i.length > 1) {
        $('ul.faq').accordion('activate', parseInt(i[1].replace(/faq/, '')));
        window.location = window.location;
    }

}
function limpiaSelect(elSelect) {
    while (elSelect.hasChildNodes())
        elSelect.removeChild(elSelect.firstChild);
}
function rellenar_anio(elSelect) {
    for (var a = 2020; a >= 2011; a--)
        aniadeOpcion(elSelect, a.toString(), a.toString());
}

function rellenar_mes(elSelect) {
    for (var a = 1; a <= 12; a++)
        aniadeOpcion(elSelect, a.toString(), a.toString());
}

function esBisiesto(year) {
    return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? true : false;
}

function aniadeOpcion(elSelect, texto, valor) {
    var laOpcion = document.createElement("OPTION");
    laOpcion.appendChild(document.createTextNode(texto));
    laOpcion.setAttribute("value", valor);
    elSelect.appendChild(laOpcion);
}

function rellenar_dia(elSelect, anio, mes) {
    var ultimo_dia;
    // febrero
    if (mes == 2)
        ultimo_dia = esBisiesto(anio) ? 29 : 28;
    // acaban en 31
    else if (mes == 1 || mes == 5 || mes == 7 || mes == 8 || mes == 10 || mes == 12)
        ultimo_dia = 31;
    else
        ultimo_dia = 30;
    for (var a = 1; a <= ultimo_dia; a++) {
        aniadeOpcion(elSelect, a, a);
    }
}

function setHour_Initial() {
    var horaNow = new Date();
    var selected_in = 0;
    switch (horaNow.getHours()) {
        case 9:
            selected_in = 1;
            break;
        case 10:
            selected_in = 3;
            break;
        case 11:
            selected_in = 5;
            break;
        case 12:
            selected_in = 7;
            break;
        case 13:
            selected_in = 9;
            break;
        case 14:
            selected_in = 11;
            break;
        case 15:
            selected_in = 13;
            break;
        case 16:
            selected_in = 15;
            break;
        case 17:
            selected_in = 17;
            break;
        case 18:
            selected_in = 19;
            break;
        case 19:
            selected_in = 21;
            break;
        case 20:
            selected_in = 23;
            break;
    }
    if (horaNow.getMinutes() > 30)
    { selected_in += 1; }
    document.getElementById('hora').selectedIndex = selected_in;
    $('#hora').selectmenu('destroy').selectmenu();
}

function getSlides() {
//    var len = $('#sliderCotizador ul li').length;
//    var span = '<span></span>';
//    var $container = $('div.slides');
//    for (var i = 0; i < len; i++) {
//        $container.append('<span id="cgo_'+i+'_span"></span>');
//    }
}
$(document).ready(function () {
    setHour_Initial();

	getSlides();

    $('.boxInfo:last-child, table tbody tr:last-child').addClass('last');

    $('table tr td:first-child, table tr th:first-child, table tr:first-child').addClass('first');

    $('.btnContacto, .btnCerrar').click(function () {
        $('#panel').slideToggle('slow', function () {
            $('#encabezado .btnContacto').toggleClass('activa');
        });
        return false;
    });


    $('#btnCalcular').click(function () {
        return false;
    });

    $('.sliderCotizador2').click(function () {
        cgo_url = 'cgo_url' + document.getElementById('cgo_selected').value;
        window.location = document.getElementById(cgo_url).value;

    });

    function actualizar() {
        limpiaSelect(document.getElementById("dia"));
        rellenar_dia(document.getElementById("dia"), document.getElementById("anio").options[document.getElementById("anio").options.selectedIndex].value, document.getElementById("mes").options[document.getElementById("mes").options.selectedIndex].value);
        $('select').selectmenu('destroy').selectmenu();
    }
    $('.calendario').datepicker({
        showOn: "button",
        buttonImage: "/Data/Sites/1/skins/answer/images/calendario.png",
        buttonImageOnly: true,
        dateFormat: 'mm dd yy',
        minDate: new Date(),
        onSelect: updateSelected
    });

    function updateSelected(dates) {
        var change = false;
        var i = 0
        while (i < document.getElementById('anio').length && !change) {

            if (document.getElementById('anio').options[i].value == dates.substring(6, 10)) {
                document.getElementById('anio').selectedIndex = i;
                change = true;
            }
            i++;
        }
        document.getElementById('mes').selectedIndex = dates.substring(0, 2) - 1;
        rellenar_dia(document.getElementById('dia'), dates.substring(6, 10), dates.substring(0, 2));
        document.getElementById('dia').selectedIndex = dates.substring(3, 6) - 1;
        $('select').selectmenu('destroy').selectmenu();
    }

    $('#mes, #dia, #anio, #hora').change(function () {
        var newDate = new Date($('#anio').val(), ($('#mes').val() - 1), $('#dia').val());
        var ahora = new Date();
        ahora.setHours(0);
        ahora.setMinutes(0);
        ahora.setMilliseconds(0);
        ahora.setSeconds(0);
        if (newDate >= ahora) {
            $('.calendario').datepicker('setDate', newDate);
            if (newDate.getDate() == ahora.getDate() && newDate.getMonth() == ahora.getMonth() && newDate.getFullYear() == ahora.getFullYear()) {
                ahora = new Date();
                if (parseInt(ahora.getHours()) > parseInt($('#hora').val().substring(0, 2))) {
                    document.getElementById('hora_validate').value = 'false';
                }
                else {
                    if (parseInt(ahora.getHours()) == parseInt($('#hora').val().substring(0, 2))) {
                        if (parseInt(ahora.getMinutes()) > parseInt($('#hora').val().substring(3, 5))) {
                            document.getElementById('hora_validate').value = 'false';
                        }
                        else {
                            document.getElementById('hora_validate').value = 'true';
                        }
                    }
                    else {
                        document.getElementById('hora_validate').value = 'true';
                    }
                }
            }
            else {
                document.getElementById('hora_validate').value = 'true';
            }
        }

        else {
            document.getElementById('dia').selectedIndex = ahora.getDate() - 1;
            document.getElementById('mes').selectedIndex = ahora.getMonth();
            var change = false;
            var i = 0
            while (i < document.getElementById('anio').length && !change) {

                if (document.getElementById('anio').options[i].value == (ahora.getFullYear())) {
                    document.getElementById('anio').selectedIndex = i;
                    change = true;
                }
                i++;
            }
            $('select').selectmenu('destroy').selectmenu();
        }

    });

    $('#mes,#anio').change(function () {
        var aux = document.getElementById('dia').selectedIndex;
        actualizar();
        try {
            if (document.getElementById('dia').length > aux) {
                document.getElementById('dia').selectedIndex = aux;
                $('#dia').selectmenu('destroy').selectmenu();
            }
        }
        catch (x) { }

    });

    $('#mes, #dia, #anio').click(function () {
        window.scrollTo(0, 0);
    });



    $('a[rel="external"]').attr('target', '_blank');

    limpiaSelect(document.getElementById("anio"));
    rellenar_anio(document.getElementById("anio"));
    limpiaSelect(document.getElementById("mes"));
    rellenar_mes(document.getElementById("mes"));
    limpiaSelect(document.getElementById("dia"));
    rellenar_dia(document.getElementById("dia"), 2011, (new Date().getMonth() + 1));


    {

        document.getElementById('dia').selectedIndex = new Date().getDate() - 1;
        document.getElementById('mes').selectedIndex = new Date().getMonth();
        var change = false;
        var i = 0
        while (i < document.getElementById('anio').length && !change) {

            if (document.getElementById('anio').options[i].value == (new Date().getFullYear())) {
                document.getElementById('anio').selectedIndex = i;
                change = true;
            }
            i++;
        }
        $('select').selectmenu();
    }

    Cufon.now();
    window.onload += $('ul.faq').accordion({ autoHeight: false, active: false });
    window.onload += goTo();
});
    function openCotizador() {
        var features = "width=1024,height=720,resizable=1,alwaysRaised=0,scrollbars=yes,toolbar=no,menubar=no";
		window.open('https://clientes.answeronline.com.ar:444/RSAAnswer/Derivacion.aspx', 'Cotizador', features);
        return false;
    }
    function openGestion() {
        var features = "width=1024,height=720,resizable=1,alwaysRaised=0,scrollbars=yes,toolbar=no,menubar=no";
		window.open('https://clientes.answeronline.com.ar:444/RSAAnswerAutogestion/acceso/login.aspx', 'AutoGestion', features);
        return false;
    }
