﻿
/*
Tüm genel kullanılacak combolar için ajax ile server tarafından 
yükleme işlemi yapmak için kullanılan fonksiyonlar
*/
//-----------------------------------------------------------------------------------------
//Constructor
function GeneralLists(){
    this.ListItems = new Object();
    this.keyValueArray = new Array();
    this.ListItems.ObjectIdList = new Array();
    this.ListItems.KeyValueList = new Array();
    this.ListItems.FirstItemTextList = new Array();
    this.ListItems.TypeList = new Array();
    this.ListItems.SelectedValueList = new Array();
}

//functionName(objId, objCount);
var GeneralLists_prototype_delegateFunctionName = '';

//Liste elemanı
function ListItem(){
    this.objectId;
    this.KeyValueArray = new Array();
    this.firstItemText;
    this.type;
}

//Key value array
GeneralLists.prototype.KeyValue = function(key, value){
    var KeyValues = new Object();
    KeyValues.Key = key;
    KeyValues.Value = value;
    this.keyValueArray.push(KeyValues);
}

//Elemanlar array a ekleniyor
GeneralLists.prototype.ListItemAdd = function(objectId, firstItemText, type, selectedValue){
    this.ListItems.ObjectIdList.push(objectId);
    this.ListItems.KeyValueList.push(this.keyValueArray);
    this.ListItems.FirstItemTextList.push(firstItemText);
    this.ListItems.TypeList.push(type);
    this.ListItems.SelectedValueList.push(selectedValue);
    this.keyValueArray = new Array();
};

//Liste çekiliyor.
GeneralLists.prototype.GetLists = function(){
    BizNet.Business.Ajax.CusAjax.GetGeneralList(this.ListItems, this.callback_GetList);
};

//Liste çekiliyor.
GeneralLists.prototype.GetListsSync = function(){
    return BizNet.Business.Ajax.CusAjax.GetGeneralList(this.ListItems);
};

//Listeyi checkbox lar şeklinde basıyor
GeneralLists.prototype.GetListsWithCheckBox = function(){
    BizNet.Business.Ajax.CusAjax.GetGeneralList(this.ListItems, this.callback_GetListsWithCheckBox);
};

//Listede value değeri de text ile aynı geliyor
GeneralLists.prototype.GetListsWithText = function(){
    BizNet.Business.Ajax.CusAjax.GetGeneralList(this.ListItems, this.callback_GetListText);
};

//Asenkron çağırma fonksiyonu
GeneralLists.prototype.callback_GetList = function(res)
{
    SetValues(res, true);
};

//Asenkron çağırma fonksiyonu
GeneralLists.prototype.callback_GetListsWithCheckBox = function(res)
{
    SetValuesWithCheckBox(res);
};

//Asenkron çağırma fonksiyonu
GeneralLists.prototype.callback_GetListText = function(res)
{
    SetValues(res, false);
};

//Sayfaya basılıyor
function SetValues(res, isTextValue){
    var data = res.value.data;
    var firstItemTextList = res.value.firstItemText;
    var objectIdList = res.value.objectIdList;
    var selectedValueList = res.value.selectedValueList;
    
    if(data == null)return;
    
    for(var j=0; j<data.Tables.length; j++){
        var objectId = objectIdList[j];
        var selectedValue = selectedValueList[j];
        
        var firstItemText = firstItemTextList[j];
        if(firstItemText == '')firstItemText = 'Seçiniz ...';
        
        if(document.getElementById(objectId)){
           if(GeneralLists_prototype_delegateFunctionName != '')
                eval(GeneralLists_prototype_delegateFunctionName+"('"+objectId+"',"+data.Tables[j].Rows.length.toString()+");");
                
           var objDdl = document.getElementById(objectId);
           for(var i=objDdl.length-1; i>-1;i--)objDdl.remove(i);
           
           var selectedIndex = 0;
           
           for(var i=0; i < data.Tables[j].Rows.length; i++){
               if(i==0){
                    var optnFirst = document.createElement('option');
                    optnFirst.text=firstItemText;
                    optnFirst.value='0';
                    objDdl.options.add(optnFirst);
               }
               
               var optnText = eval("data.Tables[j].Rows[i]." + data.Tables[j].Columns[1].Name);
               var optnValue = eval("data.Tables[j].Rows[i]." + data.Tables[j].Columns[0].Name);
               
               var optn = document.createElement('option');
               optn.text = optnText;
               
               //Value tarafında da text istendiği için
               if(isTextValue)optn.value = optnValue;
               else optn.value = optnText;
               
               if(isTextValue){
                   if(optnValue == selectedValue){
                        selectedIndex = i+1;
                   }
               }
               else{
                if(optnText == selectedValue){
                        selectedIndex = i+1;
                   }
               }
               
               objDdl.options.add(optn); 
            }
            
            if(objDdl.options.length > 0)
                objDdl.options[selectedIndex].selected = true;
        }
    }  
}

//Sayfaya basılıyor
function SetValuesWithCheckBox(res){
    /*<label><input type="checkbox" name="checkbox14" id="checkbox18" />alsancak</label>*/
    //txtEstateSelectedTowns
    var data = res.value.data;
    var dataType = res.value.dataType;
    var objectIdList = res.value.objectIdList;
    var html = '';
    
    for(var j=0; j<data.Tables.length; j++){
        var objectId = objectIdList[j];
        var type = dataType[j];
        
        if($(objectId)){
           for(var i=0; i < data.Tables[j].Rows.length; i++){
               var text = eval("data.Tables[j].Rows[i]." + data.Tables[j].Columns[1].Name);
               var value = eval("data.Tables[j].Rows[i]." + data.Tables[j].Columns[0].Name);
               html += '<label id="lbl'+type+'_'+value+'"><input type="checkbox" name="chk'+type+'" id="chk'+type+'_'+value+'" value="'+value+'" onclick="set'+type+'();" />'+text+'</label><input id="hdn'+type+'_'+value+'" name="hdn'+type+'" type="hidden" value="'+text+'" />';
            }
        }
    }  
    
    $(objectId).innerHTML = html;
}


/*Genel kullanımlı combolar için data çeken ajax kodları bitti*/
//-----------------------------------------------------------------------------------------

