﻿//Ver 1.0, MY_Prod
/* ---------------------------------------------------------------------- *\
  BİRDENBİRE # Ali BİNGÖL 10/10/2010#
\* ---------------------------------------------------------------------- */
var MY_Prod=new function(){
    var _objectsProd
    this.Prod=function(objectId){                
        this.Prod.Get=function() {            
            if(!_objectsProd) return             
            for(var i=0;i<_objectsProd.length;i++) {
                if(_objectsProd[i].objectId==objectId) return _objectsProd[i];
            }
            return
        }        
        //initialize
        this.Prod.Init=function(FolderDepth,DoPostBack,IsProdTypeVisible,QI_prodType,IsUseBlackList) {
            if(!_objectsProd) _objectsProd=new Array();
            var _new=new Prod_Init(objectId,FolderDepth,DoPostBack,IsProdTypeVisible,QI_prodType,IsUseBlackList)
            _objectsProd[_objectsProd.length]=_new
            return _new
        }
        //objects
        this.Prod_objects=_objectsProd;
        return this.Prod ;
    }
    ///Initialize
    function Prod_Init(objectId,FolderDepth,DoPostBack,IsProdTypeVisible,QI_prodType,IsUseBlackList){
        this.objectId=objectId;        
        this.FolderDepth=FolderDepth;
        this.IsTypeVisible=IsProdTypeVisible;
        this.DoPostBack=DoPostBack;
        var cnstFOUND_COLOR= '#EEF9EC';
        var QI_prodType=QI_prodType
        this.IsUseBlackList=IsUseBlackList
        var xMe=this
        var lastSelectedSource=-1
        var cmbTYPE_JQ=$('#'+this.objectId+'_cmbProdType');               
        var txtSRCH_JQ=$('#'+this.objectId+'_TXT');
        var hidKEY_JQ=$('#'+this.objectId+'_hidProdKey');
        var aREF_JQ=$('#'+this.objectId+'_arefList');
        var cmdPOSTBCK_JQ=$('#'+this.objectId+'_cmdPostBack');
        //
        this.OnSelected=function(ProdKey)  {
            //overridable
        }
        this.OnProdTypeChanged=function(ProdType)  {
            //overridable
        }                
        this.ProdKey=function()  {
            return hidKEY_JQ.val()
        }        
        this.ProdType=function()  {
            return MY.cNumeric(cmbTYPE_JQ.val());
        }
        this.IsFlightSelected=function()  {
            return (xMe.ProdType()==10 || xMe.ProdType()==7)
        }        
        this.ProdUnKey=function()  {            
            if (xMe.ProdKey()=='') return 0
            return MY.cNumeric(xMe.ProdKey().split('_')[2])//same as srProd...
        }
        this.ProdSource=function()  {            
            if (xMe.ProdKey()=='') return 0
            return MY.cNumeric(xMe.ProdKey().split('_')[1])//same as srProd...
        }        
        this.focus=function ()    {
            return txtSRCH_JQ.focus();
        }        
        this.SetProdType=function (type)    {
            TYPE_Set(type)
        }
        //yani bir alttakinin name'siz seti(overload)
        this.SetItemOnlyKey=function (key)    {
            xMe.SetItem(key,'')
            FindProdName()
        }                
        this.SetItem=function (key,name)    {
            //gelen key'in prodtype'i aktiften farklı ise
            var lcType=MY.cNumeric(key.split('_')[0])
            if(lcType!=xMe.ProdType()) {
                TYPE_Set(lcType)
            }
            //set key and name
            SetProd(key,false)
            txtSRCH_JQ.val(name)
        }
        //load'da prodkey set edilmisse
        if(xMe.ProdKey()!='') FindProdName()
        // fancy
        aREF_JQ.fancybox({
		    'width': '75%','height': '75%','autoScale': false,'type':'iframe'
        });       
        init_AREF();      
        //jq subs
        Set_AutoComplete()
        //jq result 
        txtSRCH_JQ.result(function(event, data, formatted) {                
            var row=data[0].split('$')//id$name
            SetProd(row[0],false)
        });
        //Object events
        cmbTYPE_JQ.change(function() {
            TYPE_Change()
        });     
        txtSRCH_JQ.keyup(function() {
            ResetProd();
        }); 
           
        //subs
        function TYPE_Set(xVal){
            cmbTYPE_JQ.val(xVal)
            TYPE_Change()        
        }
        function TYPE_Change(){
            txtSRCH_JQ.val('');
            Set_AutoComplete();//her change'de autocomplete set edilmeli(yeni prodtype)            
            //flt veya fltdom ise
            if(xMe.IsFlightSelected()) $('#'+xMe.objectId +'_trProdKey').hide()
                else $('#'+xMe.objectId +'_trProdKey').show()
            ResetProd();
            init_AREF();      
            xMe.OnProdTypeChanged(xMe.ProdType() );      
        }
        function init_AREF(){            
            if(xMe.ProdType()!=0) {
                aREF_JQ.show();
                aREF_JQ.attr('href',xMe.FolderDepth +'UC_ProdsList.aspx?'+QI_prodType+'='+xMe.ProdType()+'&excbl='+(xMe.IsUseBlackList?'0':'1')+'&clientid='+xMe.objectId);            
            } else aREF_JQ.hide()
        }
        
        function SetProd(xVal,fromLoad){                        
            hidKEY_JQ.val(xVal);
            txtSRCH_JQ.css('backgroundColor',cnstFOUND_COLOR);
            //eski source farklı ise postback yapabiliriz,fromLoad'da olmayacak
            if(xMe.DoPostBack && !fromLoad) {
                if(lastSelectedSource!=xMe.ProdSource()) cmdPOSTBCK_JQ.trigger('click');
            }
            if(xMe.ProdSource()!=0) lastSelectedSource=xMe.ProdSource()
            xMe.OnSelected(xVal);
        }
        
        function ResetProd(){
            hidKEY_JQ.val('');
            txtSRCH_JQ.css('backgroundColor','');
        }
        //her type change'da tekrar set edilmeli
        function Set_AutoComplete(){
            var ac_ExtraParams="'prodtype':'"+xMe.ProdType()+"','IsUseBlackList':'"+xMe.IsUseBlackList+"'"
            txtSRCH_JQ.autocomplete(xMe.FolderDepth +"Service_UcProd.asmx/SearchProductItems", {
	            minChars: 0,
	            width: txtSRCH_JQ.width()+100,matchCase:true,
                dataType: 'json',httpMethod: 'POST',contentType: 'application/json; charset=utf-8',		
	            autoFill: false, ExtraParams : ac_ExtraParams,
	            formatItem: function(row, i, max) {
		            var rowx=row[0].split('$');
		            return rowx[1];
	            },
	            formatMatch: function(row, i, max) {
		            return row[0].split('$')[1]//name
	            },
	            formatResult: function(row) {
		            return row[0].split('$')[1]//name
	            }
            });        
        }
        //Find Name, call from load
        function FindProdName(){
            $.ajax({
                type: "POST",async:true, contentType: "application/json; charset=utf-8",dataType: "json",
                url: xMe.FolderDepth +"Service_UcProd.asmx/SearchProductItemName",
                data: "{'argProdKey':'" + xMe.ProdKey()  + "'}",
                success:     function (data)  {
                    if(!data.d.IsSuccess) {alert(data.d.Val);return false;}
                    txtSRCH_JQ.val(data.d.Val);
                    SetProd(xMe.ProdKey(),true)//bunu çağırınca postback'lerde loopa giriyor. from load demeliyiz
                    return true
                },
                error: function(data) {alert(data.responseText);return false}                
            });
        }
    }
    
}
