function skipSecondStep()
{
    $.get(BASE_URL+"/register/step2skip",
        function () {
            document.location = BASE_URL+'/register/step3/';
        });
    return false;
}

function skipThirdStep()
{
    $.get(BASE_URL+"/register/step3skip", function(){
        document.location = BASE_URL+'/register/step4/';
    });
    return false;
}

function refreshCaptcha()
{
    $('#captchaimage').attr("src",BASE_URL+"/register/captchaImage/?random="+Math.random());
}

function submitRegistration(fm)
{
    $('input+ul', fm).html('');
    $(':input',fm).css(default_css);
    jQuery.ajax({
        url: BASE_URL+"/register/checkCaptcha",
        cache: false,
        dataType: 'json',
        data:{
            code: jQuery('#captcha').val()
        },
        success: function(j) {
            if(j.req != 1) {
                $('#captcha').css(error_css);
                showError(j.message);
                refreshCaptcha()
                return false;
            }
            $('#captcha').css(ok_css);
            $(fm).ajaxSubmit({
                success: function(j) {
                    if(j.error) {
                        var e = j.error
                        if(typeof(e)!='string'){
                            var html = [];
                            for(var i in e.errors)
                            {
                                if(e.errors[i].length) {
                                    if(i=='password') {
                                        $('input[name="password_confirm"]', fm).css(error_css);
                                    } else
                                    if(i=='birthday') {
                                        $('select[name^="birthday"]', fm).css(error_css);
                                    } else {
                                        $('input[name="'+i+'"]', fm).css(error_css);
                                    }
                                    html = [];
                                    for(var t in e.messages[i]){
                                        html.push('<li>'+e.messages[i][t]+'</li>');
                                    }
                                    $('input[name="'+i+'"]+ul', fm).html(html.join(''));
                                    if(i=='birthday') {
                                        $('select[name^="birthday"]:last + ul', fm).html(html.join(''));
                                    }
                                }
                            }
                        } else  {
                            showError(j.error);
                        }
                    } else if(j.ok) {
                        var dlg = $('#warning').jqm({
                            onHide: function(h) {
                                /* callback executed on window hide. Hide notice, overlay. */
                                setTimeout("document.location = BASE_URL+'/register/step2/';",100);
                            }
                        });
                        jQuery('div.alert_body > span', dlg).html(j.ok.message)
                        dlg.jqmShow();
                    }
                },
                dataType: 'json'
            });
            return false;
        }
    });
}


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

/*

var step = {
    step : 1,
    currentStep : 0,
    setAllowStep : function(num){
        if(this.step > num)return;
        this.step = num;
    },
    selectStep:function (num) {
        if(num > this.step){
            showError('You must complete previus step');
            return false;
        }
        this.currentStep = num;
        this.refreshTabs();
        this.refreshStep();
    },
    addHtml:function(html, num) {
        html.push('<li');
        if(num == this.currentStep){
            html.push(' class="current"');
        }
        if(this.step >= num){
            html.push(' ><a ');
            html.push(' onclick="step.selectStep('+num+');return false;"');
        } else {
            html.push(' class="disabled"><a');
        }
    },
    refreshTabs:function(){
        var html = [];
        //html.push('<ul class="tabs_register ui-tabs-nav">');
        this.addHtml(html, 1);
        html.push('href="#step1"><span class="tab_one_l">Step 1</span></a></li>');

        this.addHtml(html, 2);
        html.push(' href="#step2"><span class="tab_two_l">Step 2</span></a></li>');

        this.addHtml(html, 3);
        html.push(' href="#step3"><span class="tab_three_l">Step 3</span></a></li>');

        this.addHtml(html, 4);
        html.push(' href="#step4"><span class="tab_four_l">Step 4</span></a></li>');

        $('.ui-tabs-nav').html(html.join(''));
    },
    refreshStep : function()
    {
        $('#tab-content').html('<div style="padding:30px"><img src="'+BASE_URL+'/design/images/loading.gif></div>');
        $('#tab-content').load(BASE_URL+'/register/step'+step.currentStep, null, function(){
            if(step.currentStep==1)refreshCaptcha();
            if(step.currentStep==2 || step.currentStep==3){
                autocomplete();
                updateTops();
            }
        });
    }
}
 */

function submitProfile(fm)
{
    $(fm).ajaxSubmit({
        success: function(j) {
            if(j.error) {
                showError(j.error)
            } else if(j.ok) {
                /*
                var dlg = $('#warning').jqm({
                    onHide: function(h) {
                        setTimeout("document.location = BASE_URL+'/register/step2/';",100);
                    }
                });
                jQuery('div.alert_body > span', dlg).html(j.ok.message)
                dlg.jqmShow();
                 */
                //showMessage(j.ok);
                step.selectStep(3);
            }
            if (j.url) loc(j.url);
            if(j.eval)eval(j.eval);
        },
        dataType: 'json'
    });
    return false;
}

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

function sendCode(fm){
    $(fm).ajaxSubmit({
        success: function(j) {
            if(j.ok) {
                title('Registrtion complete');
                loadPage('register/success');
            }
            if(j.error) {
                showError(j.error)
            }
            if (j.url) loc(j.url);
            if(j.eval){
                eval(j.eval);
            }
        },
        dataType: 'json'
    });
    return false;
}

function sendSms()
{
    $('button[@name=sendsms]').attr('disabled',true);
    jQuery.ajax({
        url: BASE_URL+"/register/sendSms",
        cache: false,
        dataType: 'json',
        success: function(j) {
            if(j.error) {
                $('button[@name=sendsms]').attr('disabled',false);
                showError(j.error)
            } else if(j.ok) {
                $('button[@name=sendsms]').attr('disabled',false);
                showMessage(j.ok);
            }
            if (j.url) loc(j.url);
            if(j.eval)eval(j.eval);
        }
    });
    return false;
}


function autocomplete()
{
    var data = {
        minChars: 0,
        delay: 200,
        autoFill: true,
        scrollHeight: 220,
        formatItem: function (row) {
            return "<strong>" + row[0] + "<\/strong>";
        },
        formatResult: function (row) {
            return row[0].replace(/(<.+?>)/gi, '');
        }
    };
    $("#company").autocomplete(BASE_URL+'/default/ajax/suggest/', data);
    $("#company").result(function(event, data, formatted) {
        if (data) return;
    });
}
var reqId = 0;
var data = null;



function changeCompany2()
{
    //$("#rightB").load(BASE_URL+'/portfolio/getcompanyinfoajax',{cache:true,id: reqId,'company': company.value});
    var company = $('#operationForm')[0].company
    if(company.value!='') {
        var param = {
            name : company.value
        };
        $.getJSON( BASE_URL+'/Companies/getcompanyinfojson/',
            param,
            function (obj) {
                data = obj;
                ChangeTransactionType(company.form);
            }
            );
        updateTops();
    }
}


function updateTops()
{
    var company = $('#operationForm')[0].company
    if(company.value!='') {
        $('#topsForCompany').load(BASE_URL+'/register/topsForCompany/company/'+company.value);
    }
}


function tradeStocks()
{
    var fm = $('#operationForm')
    changeCompany(fm[0]['company']);
}

function ChangeTransactionType(fm)
{
    switch (fm.type.value) {
        case 'buy':
        case 'sell':
            $.getJSON( BASE_URL+'/Companies/priceCompany/',
                $(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>')

                    $("#price").parent().empty().html(rPrice+'<input name="price" id="price" value="'+price+'" type="hidden">')
                });
            break;

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


function changeForm(fm)
{
    if(fm.price && typeof fm.price.value!='undefined' || Number(fm.price.value) && Number(fm.number.value)) {
        fm.total.value = roundPriceForInput(fm.number.value * fm.price.value);
    }
}
function registrationComplete()
{
    document.location = BASE_URL+'/register/complete/';
}

