﻿function URLEncode(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[а-яА-Яa-zA-Z0-9_.]*)/;
    while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
            x += match[1].length;
        } else {
            if (clearString[x] == ' ')
                output += '+';
            else {
                var charCode = clearString.charCodeAt(x);
                var hexVal = charCode.toString(16);
                output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return output;
}

function beforePostWithParams(mark, model, modification, year, category, hdnMark, hdnModel, hdnMod, hdnYear, hdnCat) {
    if (mark.options.selectedIndex != 0) {
        if (mark.options[mark.options.selectedIndex].text == "")
            hdnMark.value = mark.options[mark.options.selectedIndex].value;
        else
            hdnMark.value = mark.options[mark.options.selectedIndex].text;
    }
    else
        hdnMark.value = "";

    if (model.options.selectedIndex != 0) {
        if (model.options[model.options.selectedIndex].text == "")
            hdnModel.value = model.options[model.options.selectedIndex].value;
        else
            hdnModel.value = model.options[model.options.selectedIndex].text;
    }
    else
        hdnModel.value = "";

    if (modification.options.selectedIndex != 0) {
        if (modification.options[modification.options.selectedIndex].text == "")
            hdnMod.value = modification.options[modification.options.selectedIndex].value;
        else
            hdnMod.value = modification.options[modification.options.selectedIndex].text;
    }
    else
        hdnMod.value = "";

    if (year.options.selectedIndex != 0) {
        if (year.options[year.options.selectedIndex].text == "")
            hdnYear.value = year.options[year.options.selectedIndex].value;
        else
            hdnYear.value = year.options[year.options.selectedIndex].text;
    }
    else
        hdnYear.value = "";

    if (category.options.selectedIndex != 0) {
        if (category.options[category.options.selectedIndex].text == "")
            hdnCat.value = category.options[category.options.selectedIndex].value;
        else
            hdnCat.value = category.options[category.options.selectedIndex].text;
    }
    else
        hdnCat.value = "";
}

function SubmitOnEnter(myfield, e) {
    var keycode;
    if (window.event)
        keycode = window.event.keyCode;
    else if (e)
        keycode = e.which;
    else
        return true;
    if (keycode == 13) {
        SearchSite();
        return false;
    }
    else
        return true;
}

 function SearchSite() {
    document.location.href = 'http://AutoKuzov.by/SearchResult.aspx?cx=004084642470310650588%3Axprul8hxvrg&cof=FORID%3A10&ie=UTF-8&q=' +
                              URLEncode(document.getElementById('search_text').value) + '&sa=Поиск';
}

function pageLoad(sender, args) {
    var cdd = $find("ModelsBehavior");
    cdd.add_populated(onPopulated);
}
