var reqId = 0;
var data = null;
var timeout;

function trade(form)
{
    $('#error_form').hide();
    $(form).ajaxSubmit({
        success: function(j) {
            if(j.error) {
                var html = [];
                var e = j.error
                if(typeof(e)!='string') {
                    for(var i in e.errors)
                    {
                        if(e.errors[i].length) {
                            for(var t in e.messages[i]){
                                html.push(i+':'+e.messages[i][t]+'<br>');
                            }
                        }
                    }
                } else  {
                    html.push('<li>'+j.error+'</li>');
                }
                $('#error_form').html(html.join('')).show();

            } else if(j.ok) {
                var dlg = $('#warning').jqm({
                    onHide: function(h) {
                        /* callback executed on window hide. Hide notice, overlay. */
                        setTimeout("document.location = BASE_URL+'/account/portfolio/';",100);
                    }
                });
                jQuery('div.alert_body > span', dlg).html(j.ok.message)
                dlg.jqmShow();
            }
            if(j.url)  loc(j.url);
            if(j.eval) eval(j.eval);
            return false;;
        },
        dataType: 'json'
    });
    return false;
}

function changeCompany()
{
    clearTimeout(timeout);
    timeout = setTimeout('changeCompany2()', 700);
}



function changeCompany2()
{
    var fm = $('#operationForm')
    var company = fm[0]['company'];
    $("#rightB").load(BASE_URL+'/account/getcompanyinfoajax',{
        cache:true,
        id: reqId,
        'company': company.value
    });

    $.getJSON( BASE_URL+'/account/getcompanyinfojson/',
        $('#operationForm').serialize(),
        function (obj) {
            data = obj;
            ChangeTransactionType(company.form);
        }
        );

}

function ChangeTransactionType(fm)
{
    switch (fm.type.value) {
        case 'buy':
        case 'sell':
            $.getJSON( BASE_URL+'/account/price/',
                $(fm).serialize(),
                function (obj) {
                    var price = 0;
                    var rPrice = 'N/A';
                    if(!obj.number && !obj.price) {
                        //$(fm.submit).attr('disabled',1);
                        $(fm.number).val('');
                        $(fm.price).val('');
                    } else {
                        price = obj.price
                        rPrice = '$'+roundPrice(obj.price);
                    }
                    $("#price").parent().empty().html('<span id="priceSp">'+rPrice+'<input name="price" id="price" value="'+price+'" type="hidden"></span>')
                });
            break;

        case 'limit sell':
        case 'limit buy':
            $("#price").parent().empty().html('<input name="price" size="9" id="price" value="" class="quote" onchange="changeForm(this.form)" onkeyup="changeForm(this.form)" type="text">')
            break;
    }
}


function changeForm(fm)
{
    if(typeof fm.price != 'undefined' && typeof fm.number != 'undefined')
    {
        if(typeof fm.price.value != 'undefined' && typeof fm.number.value != 'undefined')
        {
            // fm.number.value = Math.round(fm.number.value);
            fm.total.value = roundPriceForInput(fm.number.value * fm.price.value);
        }
    }

}
