// Prices for all products are set in this array
// Input field names are compared against this array when calculating prices.
//
// Input fields names should be in the form:
//      category_item_fieldtype
// eg., for a KavaChart Applet (1. Basic) the quantity input field should be:
//      <input type="text" name="app_1_qty" tabindex="1" size="2" value="">
//
// Field type may be one of:
//      'qty'           quantity as input by user
//      'subt'          display only, subtotal
//      'total'         display only, total
//      'grand'         display only, grand
//
// qty      fields must have all three elements ('category', 'item', 'fieldtype')
//
// subt     fields must have all three elements (app_1_subt)
//          and must come after the qty field of the item they are totaling, 
//          and before the qty field for the next item
//
// total    fields must have 'category' and 'fieldtype' (app__total)
//          there must be only one total field per category, and it must be
//          the final field for that category
//
// grand    field must have only fieldtype (__grand)
//          there can be only one grand field, and it must be after all qty, subt
//          and total fields in the form. Field is optional and may be omitted from
//          the form

var prices = new Array();

// KavaChart Applets
prices['app'] = new Array();
prices['app']['1']              = 49.95;
prices['app']['2']              = 49.95;
prices['app']['3']              = 49.95;
prices['app']['4']              = 49.95;
prices['app']['5']              = 179.00;
prices['app']['subscr']         = 29.95;

// KavaChart ProServer Solutions
prices['pro'] = new Array();
prices['pro']['devlicense']     = 299.00;
prices['pro']['servlicense']    = 99.00;
prices['pro']['appdeploy']      = 799.00;
prices['pro']['supp30']         = 0;
prices['pro']['suppannual']     = 120.00;

// KavaChart Enterprise Soltuions
prices['ent'] = new Array();
prices['ent']['devlicense']     = 599.00;
prices['ent']['servlicense']    = 99.00;
prices['ent']['appdeploy']      = 799.00;
prices['ent']['supp30']         = 0;
prices['ent']['suppannual']     = 240.00;

// KavaChart Enterprise Source Code
prices['src'] = new Array();
prices['src']['devlicense']     = 1195.00;
prices['src']['servlicense']    = 1.00;
prices['src']['appdeploy']      = 1.00;
prices['src']['supp30']         = 0;
prices['src']['suppannual']     = 480.00;

window.onload = function()
{

    the_form = document.app;

    // register onkeyup events in 'nnn_qty' fields
    for (var i = 0; i < the_form.elements.length; i++) {
        t_field_meta = parseElName(the_form.elements[i].name);

        if (t_field_meta['type'] == 'qty') {
            the_form.elements[i].onkeyup = updateCost;
        }
    }
}

function killEvent(e)
{
    if (e.keyCode == 13 && e.returnValue) {
        e.returnValue = false;
    }

    return false;
}

function parseElName(the_name)
{
    name_bits = new Array();

    // category is the first part of the field name,
    // product the middle (null for total)
    // last segment is the purpose (eg, 'qty', 'subt', 'total')
    sep1 = the_name.indexOf('_');
    sep2 = the_name.lastIndexOf('_');

    name_bits['cat'] = the_name.substring(0, sep1);
    name_bits['prod'] = the_name.substring(sep1+1, sep2);
    name_bits['type'] = the_name.substring(sep2+1);

    return name_bits;
}

function updateCost()
{
    sub_total = 0;
    total = 0;
    grand_total = 0;

    the_form = document.app;

    for (var i = 0; i < the_form.elements.length; i++) {
        t_field_meta = parseElName(the_form.elements[i].name);

        if (t_field_meta['type'] == 'total') {
            the_form.elements[i].value = '$' + formatCurrency(total);
            total = 0;

        } else if (t_field_meta['type'] == 'subt') {
            total += sub_total;
            grand_total += sub_total;
            the_form.elements[i].value = formatCurrency(sub_total);
            sub_total = 0;

        } else if (t_field_meta['type'] == 'qty') {
            the_val = the_form.elements[i].value;
            if (isNaN(the_val)) {
                the_val = 0;
            }
            sub_total += prices[t_field_meta['cat']][t_field_meta['prod']] * the_val;
        } else if (t_field_meta['type'] == 'grand') {
            the_form.elements[i].value = '$' + formatCurrency(grand_total);
        }
    }

}

function formatCurrency(num)
{
    snum = new String(Math.round(num*100)/100 );

    if (snum.indexOf('.') == -1) {
        snum += '.';
    }

    while ((snum.length - snum.indexOf('.')) < 3) {
        snum += '0';
    }

    // khtml will give us too much precision
    // get rid of extraneous zeros
    if (snum.length - snum.indexOf('.') > 3) {
        snum = snum.substring(0,snum.indexOf('.') + 3);
    }

    // number is now in the form nnnn.nn
    // format for thousands
    // first, get the dollars, save cents for later
    num_parts = new Array();
    var num_parts = snum.split('.');

    num_digits = num_parts[0].length;
    var i = num_digits -1;
    var new_digits = new String();
    var digit_count = 1;

    for (i; i >= 0; i--) {
        // if we've built up three more digits,
        // and this isn't the first digit in the string
        if (!(digit_count % 3) && i) {
            new_digits = ', ' + num_parts[0].charAt(i) + new_digits;
        } else {
            new_digits = num_parts[0].charAt(i) + new_digits;
        }
        digit_count++;
    }

    numstring = new_digits + '.' + num_parts[1];

    return numstring;
}

var pop_window;
function openDialog(pop_url, x, y, options) {
    if (x == 0) {
        x = 500;
    }

    if (y == 0) {
        y=500;
    }

    if (options) {
        options = options + ',width='+x+',height='+y;
    } else {
        options = 'scrollbars,width='+x+',height='+y;
    }

    pop_window = open(pop_url,'',options);
    setTimeout('focusPop(pop_window)',1000);
}

function focusPop(pop_window){
	pop_window.focus();	
}
