﻿//order Form 复位
function reSetOrderForm()
{
     //var a = FlashIframe.setFlashContract("53");
                                            
    var comboContract= SouthOrderForm.getForm().findField('comboContract');
    comboContract.setValue('');
    
    var comboPriceType= SouthOrderForm.getForm().findField('comboPriceType');
    comboPriceType.setValue('2');
    
    var comboOrderType= SouthOrderForm.getForm().findField('comboOrderType');
    comboOrderType.setValue('0');
    
    var txtCount= SouthOrderForm.getForm().findField('txtCount');
    txtCount.setValue('');
    
    var txtPrice= SouthOrderForm.getForm().findField('txtPrice');
    txtPrice.setValue('');
    txtPrice.enable();
    var txtAvailable= SouthOrderForm.getForm().findField('txtAvailable');
    txtAvailable.setValue('');
//    txtAvailable.setText('ok');
//    txtAvailable.show();
//    txtAvailable.fieldLabel='ok';
}


function orderTrasaction()
{
    var comboContract= SouthOrderForm.getForm().findField('comboContract');
    var comboPriceType= SouthOrderForm.getForm().findField('comboPriceType');
    var comboOrderType= SouthOrderForm.getForm().findField('comboOrderType');                                                        
    var txtCount= SouthOrderForm.getForm().findField('txtCount');
    var txtPrice= SouthOrderForm.getForm().findField('txtPrice'); 
    comboContract.setValue(comboContract.getRawValue()); 
    if(ContractStoreTemp.find('value',comboContract.getRawValue(),0,false,true)==-1)
    {
        Ext.MessageBox.alert('提示', "请检查是否正确输入合约号！",function(){comboContract.focus(true,true);});
        return;
    }
    if(txtCount.getValue()=="" || !txtCount.isValid())   
    {
        Ext.MessageBox.alert('提示', "请输入正确的操作数量！",function(){txtCount.focus(true,true);});
        return;
    }
    if(txtPrice.getValue()=="" || !txtPrice.isValid())
    {
         Ext.MessageBox.alert('提示', "请输入正确的价格！",function(){txtPrice.focus(true,true);});
        return;
    }
     
    if(txtCount.getValue()<1)
    {
        Ext.MessageBox.alert('提示', "委托手数不能小于最小手数！");
        return;
    }
    
    var checkboxName= SouthOrderForm.getForm().findField('checkboxName');  
    if(checkboxName.getValue())
    {
       BuyAction();
    }else
    {
        var messageStr=comboContract.getValue()+' 买入 '+comboOrderType.getRawValue()+' '+txtCount.getValue()+'手 '+
        '价格: '+txtPrice.getValue()+','+'投机,'+'确认吗？';
          Ext.MessageBox.confirm('确认',messageStr,function(btn)
            {
            if(btn=='yes')
            {                                    
               BuyAction();
            }    			            
        });
    }
}


function BuyAction()
{
    var comboContract= SouthOrderForm.getForm().findField('comboContract');
    var comboPriceType= SouthOrderForm.getForm().findField('comboPriceType');
    var comboOrderType= SouthOrderForm.getForm().findField('comboOrderType');                                                        
    var txtCount= SouthOrderForm.getForm().findField('txtCount');
    var txtPrice= SouthOrderForm.getForm().findField('txtPrice'); 
    
      Ext.Ajax.request({
        url: 'QueryData/OrderForm.aspx',   
        timeout:300000,//30s                                         
        success : function(response) { 
                 var result=Ext.decode(response.responseText);  
                 if(result.success==true)
                 { 
                    //查用户持仓信息                    LastQueryOK=true;                    QueryUserData();
                 }
                 else{
                    if(result.msg=="-88")
                    {
                        LastQueryOK=true;
                        QueryUserData();
                    }else
                    {
                        Ext.MessageBox.alert('警告', result.msg,function(){QueryUserData();});
                    }
                 }
            },
        failure : function(response) {
                    //Ext.MessageBox.alert('警告', "Web服务器连接超时！");
                },
        params:{"InstrumentID":comboContract.value,
                "OrderPriceType":comboPriceType.value,
                "Direction":0,//买
                "LimitPrice":txtPrice.getValue(),
                "VolumeTotalOriginal":txtCount.getValue(),
                "CombOffsetFlag":comboOrderType.value}
        });

}


function SaleTrasaction()
{
    var comboContract= SouthOrderForm.getForm().findField('comboContract');
    var comboPriceType= SouthOrderForm.getForm().findField('comboPriceType');
    var comboOrderType= SouthOrderForm.getForm().findField('comboOrderType');                                                        
    var txtCount= SouthOrderForm.getForm().findField('txtCount');
    var txtPrice= SouthOrderForm.getForm().findField('txtPrice');  
    comboContract.setValue(comboContract.getRawValue()); 
    
    if(ContractStoreTemp.find('value',comboContract.getRawValue(),0,false,true)==-1)
    {
        Ext.MessageBox.alert('提示', "请检查是否正确输入合约号！",function(){comboContract.focus(true,true);});
        return;
    }
    if(txtCount.getValue()=="" || !txtCount.isValid())   
    {
        Ext.MessageBox.alert('提示', "请输入正确的操作数量！",function(){txtCount.focus(true,true);});
        return;
    }
    if(txtPrice.getValue()=="" || !txtPrice.isValid())
    {
         Ext.MessageBox.alert('提示', "请输入正确的价格！",function(){txtPrice.focus(true,true);});
        return;
    }
    if(txtCount.getValue()<1)
    {
        Ext.MessageBox.alert('提示', "委托手数不能小于最小手数！");
        return;
    }
    
     var checkboxName= SouthOrderForm.getForm().findField('checkboxName'); 
     if(checkboxName.getValue())
        { 
              SaleAction();
        }
    else
    {
        var messageStr=comboContract.getValue()+' 卖出 '+comboOrderType.getRawValue()+' '+txtCount.getValue()+'手 '+
        '价格: '+txtPrice.getValue()+','+'投机,'+'确认吗？';
          Ext.MessageBox.confirm('确认',messageStr,function(btn)
            {
                if(btn=='yes')
                { 
                    SaleAction();
                }
            });
      }
}

function SaleAction()
{
    var comboContract= SouthOrderForm.getForm().findField('comboContract');
    var comboPriceType= SouthOrderForm.getForm().findField('comboPriceType');
    var comboOrderType= SouthOrderForm.getForm().findField('comboOrderType');                                                        
    var txtCount= SouthOrderForm.getForm().findField('txtCount');
    var txtPrice= SouthOrderForm.getForm().findField('txtPrice'); 
        
     Ext.Ajax.request({
            url: 'QueryData/SaleForm.aspx',    
            timeout:300000,//五分钟
            success:function(response)
            {
                var result=Ext.decode(response.responseText);  
                         if(result.success==true)
                         {                                                                                                    
                            //Ext.MessageBox.alert('提示', result.msg);
                            //查用户持仓信息                            LastQueryOK=true;                            QueryUserData();
                         }else{
                                if(result.msg=="-88")
                                {
                                    LastQueryOK=true;
                                    QueryUserData();
                                }else
                                {
                                    Ext.MessageBox.alert('警告', result.msg,function(){QueryUserData();});
                                }
                            //QueryUserData();
                            }
            },
            failure:function()
            {
//                Ext.MessageBox.alert('错误','连接服务器超时',function(){
                QueryUserData();
//                });
            },
            params:{"InstrumentID":comboContract.value,
                "OrderPriceType":comboPriceType.value,
                "Direction":1,//卖
                "LimitPrice":txtPrice.getValue(),
                "VolumeTotalOriginal":txtCount.getValue(),
                "CombOffsetFlag":comboOrderType.value}
        });         

}

function queryBuyAbleCount()
{
     if(!CheckActionTime()) return;
    //-------------------------------------------
    var comboContract= SouthOrderForm.getForm().findField('comboContract');
    var comboPriceType= SouthOrderForm.getForm().findField('comboPriceType');
    var comboOrderType= SouthOrderForm.getForm().findField('comboOrderType');                                                        
    var txtCount= SouthOrderForm.getForm().findField('txtAvailableId');
    var txtPrice= SouthOrderForm.getForm().findField('txtPrice');      
    comboContract.setValue(comboContract.getRawValue()); 
    if(ContractStoreTemp.find('value',comboContract.getRawValue(),0,false,true)==-1)
    {
        //Ext.MessageBox.alert('提示', "请选择正确合约号！");
        //comboContract.focus(true,true);
        return;
    }
    if(comboContract.getRawValue()!=comboContract.getValue())
    {
        //comboContract.focus(true,true);
        return;
    }                              
    Ext.Ajax.request({
    url: 'QueryData/QueryBuyableCount.aspx',                                            
    success : function(response) { 
             var result=Ext.decode(response.responseText); 
             if(result.success==true)
             {                                                      
                txtCount.setValue(result.MaxVolume);                          
                 reStarRunner();
             }else{
                Ext.MessageBox.alert('警告', result.msg);
                 reStarRunner();
             }
        },
    failure : function(response) {
            reStarRunner();
            },
    params:{"InstrumentID":comboContract.getValue(),
            "OrderPriceType":comboPriceType.getValue(),
            "Direction":0,//买
            "LimitPrice":txtPrice.getValue(),
            "VolumeTotalOriginal":txtCount.getValue(),
            "OffsetFlag":comboOrderType.getValue(),
            "LimitPrice":txtPrice.getValue()}
    });
    //-------------------------------------------


}