﻿/// <reference path="jquery.min.js" />

/*This is event for when succeeded ajax in GetAutoTypes function*/
var GetAutoTypesEvent;
var GetBrandsEvent;
var GetBrandsOrderedByNameEvent;
var GetAutoTypeByBrandEvent;
var GetTrafficAutoTypeByBrandEvent;
var GetCommercialAutoTypesEvent;
var GetUsageTypeEvent;
var GetModelYearsEvent;
var GetModelYearsWithCriteriaEvent;
var GetDistrictEvent;

function FillLoadingInfo(controlID) {
    $("#" + controlID).find('option').remove().end();
    $('#' + controlID).append($("<option></option>").
                        attr("value", "").
                        text("Yükleniyor..."));
}

/*Online Adım Kasko Markaları çeker. */
function GetAutoOnlineBrands(controlID, typeCode, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue) {
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetAutoOnlineBrands",
        method: "GET",
        dataType: "json",
        cache: false,
        data: { usageType: typeCode },
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
            if (GetBrandsEvent) {
                GetBrandsEvent();
            }
        },
        error: function (error) 
        {
        }
    });
}

/*Markaları çeker. */
function GetBrands(controlID, typeCode, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue) {
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetBrands",
        method: "GET",
        dataType: "json",
        cache: false,
        data: { usageType: typeCode },
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
            if (GetBrandsEvent) {
                GetBrandsEvent();
            }
        },
        error: function (error) {
            /*alert(error);*/
        }
    });
}

/*Markaları ada göre sıralı çeker. */
function GetBrandsOrderedByName(controlID, typeCode, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue) {
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetBrandsOrderedByName",
        method: "GET",
        dataType: "json",
        cache: false,
        data: { usageType: typeCode },
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
            if (GetBrandsOrderedByNameEvent) {
                GetBrandsOrderedByNameEvent();
            }
        },
        error: function (error) {
            /*alert(error);*/
        }
    });
}

/*Markaları sıra numarasına göre sıralı çeker. */
function GetBrandsOrderedByOrderNumber(controlID, typeCode, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue) {
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetBrandsOrderedByOrderNumber",
        method: "GET",
        dataType: "json",
        cache: false,
        data: { usageType: typeCode },
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
            if (GetBrandsOrderedByNameEvent) {
                GetBrandsOrderedByNameEvent();
            }
        },
        error: function (error) {
            /*alert(error);*/
        }
    });
} 

 /*Markaya göre modeli çeker*/
 function GetAutoTypeByBrand(controlID, pbrandCode, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue, pisOnline) {
    /* url: "/FormData/GetAutoTypeByBrand",*/
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetTypesByBrand",
        method: "GET",
        dataType: "json",
        cache: false,
        data: { brandCode: pbrandCode, isOnline: pisOnline },
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
            if (GetAutoTypeByBrandEvent) {
                GetAutoTypeByBrandEvent();
            }
        },
        error: function (error) {
           
        }
    });
}


/*Markaya göre modeli çeker*/
function GetAutoTypeByBrandForWebSale(controlID, pbrandCode, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue, pUsageType, pModel) {
    if (pUsageType == undefined) {
        pUsageType = null;
    }
    /* url: "/FormData/GetAutoTypeByBrand",*/
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetAutoOnlineBrandsForWebSale",
        method: "GET",
        dataType: "json",
        cache: false,
        data: { brandCode: pbrandCode, usageType: pUsageType, model: pModel },
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
            if (GetAutoTypeByBrandEvent) {
                GetAutoTypeByBrandEvent();
            }
        },
        error: function (error) {

        }
    });

}

 /*Trafic Markaya göre modeli çeker*/
function GetTrafficTypeByBrand(controlID, pbrandCode, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue) {
     FillLoadingInfo(controlID);
     $.ajax({
         url: "/FormData/GetTrafficTypeByBrand",
         method: "GET",
         dataType: "json",
         cache: false,
         data: { brandCode: pbrandCode },
         success: function (data) {
             FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
             if (GetTrafficAutoTypeByBrandEvent) {
                 GetTrafficAutoTypeByBrandEvent();
             }
         },
         error: function (error) {
         }
     });
 }

 /*en son bu kullanıldı*/
 function GetOnlineAutoType(controlID, usageType, brandCode, modelYear, pleaseSelectText, selectedValue) {

     if (usageType == "" || brandCode == "" || modelYear == "") {
         FillCustomDataDropDown(controlID, null, true);
     }
     else {
         FillLoadingInfo(controlID);
         $.ajax({
             url: "/FormData/GetOnlineAutoType",
             method: "GET",
             dataType: "json",
             cache: false,
             data: { usageType: usageType, brandCode: brandCode, model: modelYear },
             success: function (data) {
                 FillCustomDataDropDown(controlID, data, true, undefined, pleaseSelectText, selectedValue);

                 if (GetAutoTypesEvent) {
                     GetAutoTypesEvent();
                 }
             },
             error: function (error) {

             }
         });
     }
 }



 function GetAutoTypes(controlID, usageType, brandCode, modelYear, pleaseSelectText, selectedValue) {
     
     if (usageType == "" || brandCode == "" || modelYear == "") {
         FillCustomDataDropDown(controlID, null, true);
     }
     else {
         FillLoadingInfo(controlID);
         $.ajax({
             url: "/FormData/GetAutoTypes",
             method: "GET",
             dataType: "json",
             cache: false,
             data: { usageType: usageType, brandCode: brandCode, modelYear: modelYear },
             success: function (data) {
                 FillCustomDataDropDown(controlID, data, true, undefined, pleaseSelectText, selectedValue);

                 if (GetAutoTypesEvent) {
                     GetAutoTypesEvent();
                 }
             },
             error: function (error) {
                  
             }
         });         
     }
 }


 function GetWebSaleCommercialAutoTypes(controlID, usageType, pleaseSelectText, selectedValue) {

     if (usageType == "" || usageType == "001") {
         FillCustomDataDropDown(controlID, null, true);
         if (GetCommercialAutoTypesEvent) {
             GetCommercialAutoTypesEvent();
         }
     }
     else {
         FillLoadingInfo(controlID);
         $.ajax({
             url: "/FormData/GetWebSaleCommercialAutoTypes",
             method: "GET",
             dataType: "json",
             cache: false,
             data: { usageType: usageType },
             success: function (data) {
                 FillCustomDataDropDown(controlID, data, true, undefined, pleaseSelectText, selectedValue);
                 if (GetCommercialAutoTypesEvent) {
                     GetCommercialAutoTypesEvent();
                 }
             },
             error: function (error) {
             }
         });
     }
 }


 function GetCommercialAutoTypes(controlID, usageType, pleaseSelectText, selectedValue) {
     
     if (usageType == "" || usageType == "001") {
         FillCustomDataDropDown(controlID, null, true);
         if (GetCommercialAutoTypesEvent) {
             GetCommercialAutoTypesEvent();
         }
     }
     else {
         FillLoadingInfo(controlID);
         $.ajax({
             url: "/FormData/GetCommercialAutoTypes",
             method: "GET",
             dataType: "json",
             cache: false,
             data: { usageType: usageType },
             success: function (data) {
                 FillCustomDataDropDown(controlID, data, true, undefined, pleaseSelectText, selectedValue);
                 if (GetCommercialAutoTypesEvent) {
                     GetCommercialAutoTypesEvent();
                 }
             },
             error: function (error) {
             }
         });
     }
 }

 /*Kullanım tarzlarını listeler*/
function GetUsageType(controlID,addPleaseSelect, splitterIndex) {
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetUsageType",
        method: "GET",
        dataType: "json",
        cache: false,
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex);
            if (GetUsageTypeEvent) {
                GetUsageTypeEvent();
            }
        },
        error: function (error) {

        }
    });
}
/*Ilceleri listeler*/
function GetDistricts(controlID, cityCode, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue) {
    if (addPleaseSelect == undefined) {
        addPleaseSelect = true;
    }
    if (pleaseSelectText == undefined) {
        pleaseSelectText = "----İlçe----";
    }
    FillLoadingInfo(controlID);  
    $.ajax({
        url: "/FormData/GetDistricts",
        method: "GET",
        dataType: "json",
        data: { cityCode: cityCode },
        cache: false,
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
            if (GetDistrictEvent) {
                GetDistrictEvent();
                window.setTimeout(function () { GetDistrictEvent = null }, 500);
            }
        },
        error: function (error) {
        }
    });
}

/*İkametgah İllerini çeker. */
function GetCityOfResidence(controlID, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue) {
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetCityOfResidence",
        method: "GET",
        dataType: "json",
        cache: false,
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
        },
        error: function (error) {
            
        }
    });
}


/*Model Yıllarını çeker. */
function GetModelYears(controlID, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue) {
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetModelYears",
        method: "GET",
        dataType: "json",
        cache: false,
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
            if (GetModelYearsEvent) {
                GetModelYearsEvent();
            }
        },
        error: function (error) {

        }
    });
}

/*Model Yıllarını kritere göre çeker çeker. */
function GetModelYearsWithCriteria(controlID, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue, criteria) {
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetModelYearsWithCriteria",
        method: "GET",
        dataType: "json",
        data: { minVal: 1998 },
        cache: false,
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
            if (GetModelYearsWithCriteriaEvent) {
                GetModelYearsWithCriteriaEvent();
            }
        },
        error: function (error) {
            
        }
    });
}


/*İş Bankası ve TSK için alt kırılım listeler*/
function GetSubCorporationList(controlID, corpCode, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue) {
    if (addPleaseSelect == undefined) {
        addPleaseSelect = true;
    }
    if (pleaseSelectText == undefined) {
        pleaseSelectText = "Seçiniz";
    }
    FillLoadingInfo(controlID);
    $.ajax({
        url: "/FormData/GetSubCorporations",
        method: "GET",
        dataType: "json",
        data: { corpCode: corpCode },
        cache: false,
        success: function (data) {
            FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue);
        },
        error: function (error) {

        }
    });
}

/* Calculate Auto Price */
function GetAutoPrice(controlID, brandCode, modelYear, typeCode) {

    if (brandCode == "" || modelYear == "" || typeCode == "") {
        return;
    }
    else {
        $.ajax({
            url: "/FormData/GetAutoPrice",
            method: "GET",
            dataType: "json",
            cache: false,
            data: { brandCode: brandCode, modelYear: modelYear, typeCode: typeCode },
            success: function (data) {
                FillCustomDataTextBox(controlID, data);
            },
            error: function (error) {
                 
            }
        });
    }
}

/* Calculate Auto Price and returns formated string */
function GetAutoPriceWithFormat(controlID, brandCode, modelYear, typeCode, func) {

    if (brandCode == "" || modelYear == "" || typeCode == "") {
        return;
    }
    else {
        //FillLoadingInfo(typeCode);
        $.ajax({
            url: "/FormData/GetAutoPriceWithFormat",
            method: "GET",
            dataType: "json",
            cache: false,
            data: { brandCode: brandCode, modelYear: modelYear, typeCode: typeCode },
            success: function (data) {
                $("#" + controlID).val(data);
                if (func != undefined) {
                    func();
                }
            },
            error: function (error) {

            }
        });
    }
}



/*
controlID : verilerin basılacağı kontrol adı 
data : veri kaynağı
addPleaseSelect : Seçiniz eklenecek mi 
splitterIndex : ==== satırı eklenecek mi ? Eklenecekse index numarası, eklenmeyecek ise undefined;
*/
function FillCustomDataDropDown(controlID, data, addPleaseSelect, splitterIndex, pleaseSelectText, selectedValue) 
{
    var index = 0;
    if (pleaseSelectText == undefined) {
        pleaseSelectText = "Seçiniz";
    }
    // mevcut elemanları temizle
    $('#' + controlID).find('option').remove().end();

    if (addPleaseSelect) {
        $('#' + controlID).append($("<option></option>").
                        attr("value", "").
                        text(pleaseSelectText));
    }

    if (data == null) {
        return;
    }

    $.each(data, function (i, item) {

        if (splitterIndex != undefined && splitterIndex == index) 
        {
            $('#' + controlID).append($("<option></option>")
                        .attr("value", "")
                        .attr("disabled", "disabled")
                        .text("=========="));
        }
        $('#' + controlID).append($("<option></option>").
                        attr("value", item.CustomValueCode).
                        text(item.CustomValueName));
        index++;
    });

    if (selectedValue != undefined) {
        $('#' + controlID).val(selectedValue);
    }
}

function FillCustomDataTextBox(controlID, data) {
    if (data == null) {
        return;
    }

    $('#' + controlID).val(data.Value);
}

function SendSearchKeyword() {
    var criteria = $("#txtSearch").val();
    criteria = criteria.replace(/\s/g, "-");
    window.location.href = "/arama-sonuclari/" + criteria; 
    return false; 
}

$(function () {

    $("#txtSearch").keypress(function (e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('#imgSearch').click();
            return false;
        } else {
            return true;
        }
    });
});

function isLoggedIn() {
    var result = false;

    $.ajax({
        url: "/Account/IsLoggedIn",
        method: "POST",
        dataType: "json",
        async: false,
        cache: false,
        success: function (data) {
            result = data;
        },
        error: function (error) {
        }
    });

    return result;
}

function getLoginFullName() {
    var result = "";

    $.ajax({
        url: "/Account/GetLoginFullName",
        method: "POST",
        dataType: "json",
        async: false,
        cache: false,
        success: function (data) {
            result = data;
        },
        error: function (error) {
        }
    });

    return result;
}
