/*
 * $Id: getrate.js 665 2009-02-26 01:17:40Z jason $
 *
 * Fetches rate info from the database for display when making a call.
 */


var _rate_cache = [ ];

/**
 * Retrieve the rate for a call from src to dst.
 * See PHP get_rate().
 */
function request_rate( src, dst ) {
    if( _rate_cache[src] && _rate_cache[src][dst] )
    {
        update_rate( _rate_cache[src][dst] );
        return;
    }

    var xmlrpc = _make_xhr( );
    if( xmlrpc ) {
        xmlrpc.open('GET', 'query/rate/' + escape(src) + '/' + escape(dst) + '/' );
        xmlrpc.onreadystatechange = function () {
            request_rate_cb( xmlrpc, src, dst );
        };
        xmlrpc.send(null);
    }
}


/**
 * Callback for request_rate.
 */
function request_rate_cb( xmlrpc, src, dst ) {
    if( xmlrpc.readyState == 4 && xmlrpc.status == 200 )
    {
        var rate = eval( "("+xmlrpc.responseText+")" );
        if( !_rate_cache[src] )
            _rate_cache[src] = [ ];
        _rate_cache[src][dst] = rate;
        update_rate( rate );
    }
}


/**
 * Updates the rate display.
 */
function update_rate( rate ) {
    var ratefield = document.getElementById('currrate');
    ratefield.innerHTML = //rstChild.nodeValue =
        (rate.rate_amount == -1) ? 'You may not make calls from this number.'
            : (rate.rate_amount == -2) ? 'Destination number is invalid.'
                : (rate.rate_amount == '') ? ''
                    : (rate.rate_amount == 0) ? '<span class="rate">Free!</span>'
                        : '<span class="rate">' + rate.rate_text + '</span>';
    
    if( rate == '' ) {
        ratefield.style.display = 'none';
    }
    else {  
        ratefield.style.display = 'inline';
    }  
    
    if( rate.rate_amount > 0 ) {
        $('#src_rate_desc').html( '<img src="http://cdn.dlcworldwide.com/images/1.0/flags/'+rate.src_cc+'.png" alt="'+rate.src_cc+'"/>&nbsp;'+rate.src_desc );
        $('#dst_rate_desc').html( '<img src="http://cdn.dlcworldwide.com/images/1.0/flags/'+rate.dst_cc+'.png" alt="'+rate.dst_cc+'"/>&nbsp;'+rate.dst_desc );
    }

    /*if(document.getElementById('shcurtext')) {
        display_rate();
    }*/
}



// update el's corresponding country code text field
function fillCCbox( selectid ) {
    var select = document.getElementById( selectid );
    var ccbox = document.getElementById( selectid + 'txt' );
    ccbox.value = '+' + select.options[select.selectedIndex].value;
    //var split_cnt_code =  select.options[select.selectedIndex].value.split('|');
    //ccbox.value = '+' + split_cnt_code[1];
}


function cc2cnt( ccEl, cntEl )
{
    // strip out non-digits
    var cc = ccEl.value.replace( /[^\d]+/, "" );
    ccEl.value = "+" + cc;  // format display

    var found = false;
    for( i=0; i<cntEl.length; ++i )
    {
        if( cntEl.options[i].value == cc )
        {
            cntEl.selectedIndex = i;
            found = true;
            break;
        }
    }

    if( !found ) cntEl.selectedIndex = 0; // assumes 0 is "select" message
}

