var oURL = "";
var tURL = "";

function updateTextWidget() {
    var url    = base_url + 'text-widget.php?advanced';
    var params = [];

    if ( $("#tdate-custom").is(":checked") ) {
        var date_from = $("#tdate-from").val();
        var date_to   = $("#tdate-to").val();
        if ( ! /\d{4}-\d{1,2}-\d{1,2}/.test(date_from) ) {
            $("#tdate-from").focus();
            return;
        }
        if ( ! /\d{4}-\d{1,2}-\d{1,2}/.test(date_to) ) {
            $("#tdate-to").focus();
            return;
        }
        params.push( 'date_range='+date_from+"|"+date_to );
    }

    if ( $("input[type=checkbox][name=tdata-source]:checked").length < 4 ) {
        var data_sources = [];
        if ( $("#t30yr_fr").is(":checked") ) {
            data_sources.push( "30yr_fr" );
        }
        if ( $("#t15yr_fr").is(":checked") ) {
            data_sources.push( "15yr_fr" );
        }
        if ( $("#t5yr_ar").is(":checked") ) {
            data_sources.push( "5yr_ar" );
        }
        if ( $("#t1yr_ar").is(":checked") ) {
            data_sources.push( "1yr_ar" );
        }
        params.push( 'data=' + data_sources.join("|") );
    }

    if ( $("#tdate-custom-period").is(":checked") ) {
        var t = $("#tdate-range").val();
        if ( t && t > 0 ) {
            params.push( 'range='+t );
        }
    }

    if ( params.length > 0 ) {
        url += '&' + params.join("&");
    }

    if ( tURL != url ) {
        tURL = url;
        $("#advanced-text-widget").attr("src", url);

        var html = $("#text-preview").html();
        html = html.replace(/id="advanced-text-widget" /gi, '');
        
		html += '<br>Powered by <a href="http://www.mortgagecalculator.org/rates-widgets/mortgages/">Mortgage Rates Widget</a>';
        $("#advanced-text-code-fld").val( html );
    }
    
}

function updateWidget() {
    
    var url    = base_url + 'widget.php';
    var params = [];
    
    if ( !$("#legend").is(":checked") ) {        
        params.push('legend=off');
    }
    
    if ( !$("#labels").is(":checked") ) {
        params.push('labels=off');
    }
    
    if ( $("#date-custom").is(":checked") ) {
        var date_from = $("#date-from").val();
        var date_to   = $("#date-to").val();
        if ( ! /\d{4}-\d{1,2}-\d{1,2}/.test(date_from) ) {
            $("#date-from").focus();
            return;
        }
        if ( ! /\d{4}-\d{1,2}-\d{1,2}/.test(date_to) ) {
            $("#date-to").focus();
            return;
        }
        params.push( 'date_range='+date_from+"|"+date_to );
    }
    
    if ( $("#date-custom-period").is(":checked") ) {
        var t = $("#date-range").val();
        if ( t && t > 0 ) {
            params.push( 'range='+t );
        }
    }
    
    var width  = 0;
    var height = 0;
    if ( $("#size-custom").is(":checked") ) {
        var w = $("#widget-width").val();
        if ( w && w > 0 ) {
            width = w;
            params.push( 'width='+width );
        }
        var h = $("#widget-height").val();
        if ( h && h > 0 ) {
            height = h;
            params.push( 'height='+height );
        }
    }
    
    if ( $("input[type=checkbox][name=data-source]:checked").length < 4 ) {
        var data_sources = [];
        if ( $("#d30yr_fr").is(":checked") ) {
            data_sources.push( "30yr_fr" );
        }
        if ( $("#d15yr_fr").is(":checked") ) {
            data_sources.push( "15yr_fr" );
        }
        if ( $("#d5yr_ar").is(":checked") ) {
            data_sources.push( "5yr_ar" );
        }
        if ( $("#d1yr_ar").is(":checked") ) {
            data_sources.push( "1yr_ar" );
        }
        params.push( 'data=' + data_sources.join("|") );
    }
    
    if ( $("#compare").is(":checked") ) {
        params.push("compare_to_past");
    }
    
    if ( params.length > 0 ) {
        url += '?' + params.join("&");
    }
    
    if ( oURL != url ) {
        oURL = url;
        $("#advanced-widget").attr("src", url);
        if ( width && width > 0 ) {
            $("#advanced-widget").css("width", width + "px");
        }
        if ( height && height > 0 ) {
            $("#advanced-widget").css("height", height + "px");
        }
        var html = $("#preview").html();
        html = html.replace(/id="advanced-widget" /gi, '');
        
		html += '<br>Powered by <a href="http://www.mortgagecalculator.org/rates-widgets/mortgages/">Mortgage Rates Widget</a>';
        $("#advanced-code-fld").val( html );
    }
    
}

$(document).ready(function() {
    
    updateWidget();
    updateTextWidget();
    
    $("#legend, #labels, #date-default, #date-custom, #size-default, #size-custom, #compare, #date-custom-period").change(function() { updateWidget(); });
    $("#tdate-default, #tdate-custom, #tdate-custom-period").change(function() { updateTextWidget(); });
    
    var timeout = undefined;
    
    $("#date-from, #date-to, #widget-width, #widget-height, #date-range ").keyup(function() {
        if ( timeout != undefined ) {
            clearTimeout( timeout );
        }
        timeout = setTimeout( function() { timeout = undefined; updateWidget(); }, 900 );
    });

    var ttimeout = undefined;
    
    $("#tdate-from, #tdate-to, #tdate-range ").keyup(function() {
        if ( ttimeout != undefined ) {
            clearTimeout( ttimeout );
        }
        ttimeout = setTimeout( function() { ttimeout = undefined; updateTextWidget(); }, 900 );
    });
    
    $("input[type=checkbox][name=data-source]").change(function() {
        if ( $("input[type=checkbox][name=data-source]:checked").length == 0 ) {
            $(this).attr("checked", "checked");
        }
        else {
            updateWidget();
        }
    });

    $("input[type=checkbox][name=tdata-source]").change(function() {
        if ( $("input[type=checkbox][name=tdata-source]:checked").length == 0 ) {
            $(this).attr("checked", "checked");
        }
        else {
            updateTextWidget();
        }
    });
    
});

