﻿// Colors the quote Change value based on whether it's positive or negative; 
// also checks the symbol type and selectively inserts a '$' in front of the quote Change value
function changequotecolor(quote_change, qs)
{
    var v_color = '#008000';
    var result = quote_change.match(/[\-\.\d]+/g);
    var parsedresult = parseFloat(result[0]);
    var extracted_result = parsedresult.toString().substr(1);
    var extracted_result_float = parseFloat(extracted_result);

//    alert(result + '|' + parsedresult + '|' + extracted_result + '|' + extracted_result_float);
//  
    if (parsedresult < 0)
    {
        v_color = 'red';
                
        if (checksymboltype(qs).toString() == 'CF')
        {
            document.write('<span style="color:'+v_color+'">'+'-$'+roundNumber(extracted_result_float, 3)+'</span>');
        }
        else
        {
            document.write('<span style="color:'+v_color+'">'+'-'+roundNumber(extracted_result_float, 3)+'</span>');
        }
    }
    else 
    {
        v_color = '#008000';
                
        if (checksymboltype(qs).toString() == 'CF')
        {
            document.write('<span style="color:'+v_color+'">'+'+$'+roundNumber(parsedresult, 3)+'</span>');
        }
        else
        {
            document.write('<span style="color:'+v_color+'">'+'+'+roundNumber(parsedresult, 3)+'</span>');
        }
    }
}

// Checks the type of symbol; extracts value from between two <span></span> tags
// then checks for a '$' or '/', denoting a Commodity/Future
function checksymboltype(quote_symbol)
{ 
    var regex = /<div([^>]*)>(.*)<\/div>/;
    var result = quote_symbol.match(regex);
    var symbolcode = result[2].substr(0, 1);

//    alert(quote_symbol);
////    alert(symbolcode);
//  
    if (symbolcode == "/" || symbolcode == "$")
    {
        // Symbol is a Commodity or Future
        return 'CF';
    }
    // Symbol is NOT a Commodity or Future
    else {return 'nCF';}
}

// Rounds accurately
function roundNumber(num, dec) {
	var result = Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec);
	return result;
}

// Extracts a numeric value from between html tags
// Rounds Last traded and Change values to 3 decimals
function roundcurrency(valuetoround) 
{
    var result = valuetoround.match(/[\-\.\d]+/g);

    document.write('<span>'+roundNumber(result, 3)+'</span>');
}

// Extracts a numeric value from between html tags
function extractfromdivs(valuetoextract) 
{
    var result = valuetoextract.match(/[\-\.\d]+/g);
    var parsedresult = parseFloat(result[0]);
    var extracted_result = parsedresult.toString().substr(1);
    var extracted_result_float = parseFloat(extracted_result);

    document.write('<span>' + result + '</span>');
}